var Original_window_width = $(window).width();
var resizing = false;
var auto_rotate_carousel = null;
var autoscrolling = null;
var currentPage, jQueryitems, singleWidth, sideWidth;
var slowfade = 1100;
var carouselLoaded = false;

jQuery.fn.infiniteCarousel = function(){
	function repeat(str, num){
		return new Array(num + 1).join(str);
	}
	return this.each(function(){
		sideWidth = (jQuery(window).width() - 1020) / 2;
		sideWidth = sideWidth < 1 ? 0 : sideWidth;
		//jQuery('#photos').width(jQuery(window).width());
		var jQuerywrapper = jQuery('> div', this).css('overflow', 'hidden');
		var jQueryslider = jQuerywrapper.find('> ul');
		jQueryitems = jQueryslider.find('> li');
		var jQuerysingle = jQueryitems.filter(':first');
		singleWidth = jQuerysingle.outerWidth();
		var visible = 1;
		//if (!resizing) 
		currentPage = 1;
		var pages = Math.ceil(jQueryitems.length / visible);
			
		// 1. Pad so that 'visible' number will always be seen, otherwise create empty items
		if ((jQueryitems.length % visible) != 0){
			 jQueryslider.append(repeat('<li class="empty" />', visible - (jQueryitems.length % visible)));
			jQueryitems = jQueryslider.find('> li');
		}
		
		// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
		if (!resizing){
			jQueryitems.filter(':first').before(jQueryitems.slice(-visible).clone().addClass('cloned'));
			jQueryitems.filter(':last').after(jQueryitems.slice(0, 2).clone().addClass('cloned'));
			jQueryitems = jQueryslider.find('> li'); // reselect
			
			// 3. Set the left position to the first 'real' item
			jQuerywrapper.scrollLeft(530);
			jQuery('li img').stop().fadeIn();
			jQuery(jQueryitems[currentPage]).addClass('selected');
		}
		jQuery(jQueryitems[currentPage]).width(1020);
		jQuery(jQueryitems[currentPage + 1]).addClass('selectednext');
		jQuery(jQueryitems[currentPage + 1]).width(sideWidth);
		jQuery(jQueryitems[currentPage - 1]).addClass('selectedprev');
		jQuery(jQueryitems[currentPage - 1]).width(sideWidth);
		
		// 4. paging function
		function gotoPage(page, speed){
			var dir = page < currentPage ? -1 : 1,
				n = Math.abs(currentPage - page),
				left = singleWidth * dir * visible * n;
			if (page == 0) {
				jQuerywrapper.scrollLeft(singleWidth * visible * pages - 490);
				page = pages;
			} else if (page > pages) {
				jQuerywrapper.scrollLeft(980);
				page = 1;
			}
			currentPage = page;

			jQuery('div#photos').append('<div id="wrapper" class="tempCarousel" style="height: 468px; width: 100%; overflow: hidden; position:absolute; top: 0px; left: 0px; z-index: 49;"><ul><li class="selprev" style="display:inline-block; width: '+sideWidth+'px; text-align:right; overflow:hidden;">'+jQuery('li.selectedprev').html()+'</li><li style="display:inline-block; width: 1020px; overflow:hidden;">'+jQuery('li.selected').html()+'</li><li style="display:inline-block; width: '+sideWidth+'px; overflow:hidden;">'+jQuery('li.selectednext').html()+'</li></ul></div>');

			jQuery('li.selected').css({
				width: null
			});
			jQuery('li.selected').removeClass('selected');
			jQuery('li.selectednext').css({
				width: null
			});
			jQuery('li.selectednext').removeClass('selectednext');
			jQuery('li.selectedprev').css({
				width: null
			});

			jQuery('li.selectedprev').removeClass('selectedprev');
			jQuery(jQueryitems[currentPage]).addClass('selected');
			jQuery(jQueryitems[currentPage]).width(1020);
			jQuery(jQueryitems[currentPage + 1]).addClass('selectednext');
			jQuery(jQueryitems[currentPage + 1]).width(sideWidth);
			jQuery(jQueryitems[currentPage - 1]).addClass('selectedprev');
			jQuery(jQueryitems[currentPage - 1]).width(sideWidth);
			jQuery('div.tempCarousel').stop().fadeOut(speed,function(){jQuery(this).remove();});
			return false;
		}
		if (!resizing) jQuerywrapper.after('<a class="arrow back"><span id="leftarrow"></span></a><a class="arrow forward"><span id="rightarrow"></span></a>');
		jQuery('a.back').width(sideWidth);
		jQuery('a.forward').width(sideWidth+1); // +1 to fix resolution leakage

		// 5. Bind to the forward and back buttons
		if (!resizing){
			jQuery('a.back', this).click(function () {
				clearInterval(auto_rotate_carousel);
				auto_rotate_carousel = setInterval(function(){if (autoscrolling) gotoPage(currentPage + 1, slowfade);}, 12000);
				return gotoPage(currentPage - 1, 'fast');
			});
			jQuery('a.forward', this).click(function () {
				clearInterval(auto_rotate_carousel);
				auto_rotate_carousel = setInterval(function(){if (autoscrolling) gotoPage(currentPage + 1, slowfade);}, 12000);
				return gotoPage(currentPage + 1, 'fast');
			});
			// create a public interface to move to a specific page
			jQuery(this).bind('goto', function (event, page) {
				gotoPage(page, slowfade);
			});
			var autoscrolling = true;
			jQuery('.infiniteCarousel').mouseover(function () {
				autoscrolling = false;
			}).mouseout(function () {
				autoscrolling = true;
			});
		}
		clearInterval(auto_rotate_carousel);
		auto_rotate_carousel = setInterval(function(){if (autoscrolling) gotoPage(currentPage + 1, slowfade);}, 5000);
	});
};
var fullyloaded = false;
jQuery(document).ready(function(){
	jQuery(window).load(function(){
		alldone();
		setInterval(function(){
			if (carouselLoaded != true){
				jQuery('.infiniteCarousel').infiniteCarousel();
				carouselLoaded = true;
			}
		}, 10000);
	});
	jQuery(".infiniteCarousel div ul li:nth-child(1) img").load(function(){
		alldone();
	});
});
function alldone(){
	if (fullyloaded) return;
	fullyloaded = true;
	if (carouselLoaded != true){
		jQuery('.infiniteCarousel').infiniteCarousel();
		carouselLoaded = true;
	}
	jQuery(window).resize(function(){
		var New_window_width = $(window).width();
		if (Original_window_width != New_window_width){
			resizing = true;
			
			sideWidth = (jQuery(window).width() - 1020) / 2;
			sideWidth = sideWidth < 1 ? 0 : sideWidth;
			jQuery(jQueryitems[currentPage + 1]).width(sideWidth);
			jQuery(jQueryitems[currentPage - 1]).width(sideWidth);			
			jQuery('a.back').width(sideWidth);
			jQuery('a.forward').width(sideWidth+1); // +1 to fix resolution leakage
			
			resizing = false;
			Original_window_width = New_window_width;
			jQuery("ul#menu ul.ekflexmenu_submenu_items li a.ekflexmenu_button_selected:first").hover();
		}
	});
	$('div.infiniteCarousel a.arrow').hover(function(){
			$(this).find('span').stop().animate({opacity: .8},200);
		},function(){
			$(this).find('span').stop().animate({opacity: 0},200);
		});
	//$('#leftarrow, #rightarrow').click(function(){$(this).stop().animate({opacity: .8},200);})
}
