	
function count() {
	if( $("#header-inner").width() >= 480 ) {
		
		for (i in slideshows) {
			slideshows[i].count();
		}
	
	}
}

var slideshows;


$(document).ready(function() {


	function Slideshow( element ) {

		var slideshowDiv = element;
		var currentSlide = 0;
		var container = element.find(".slide_container");
		var numberOfSlides = container.children().length;
		var duration;
		var fadeTime;
		if( element.hasClass("campaign") ) {
			duration = 700;	
			fadeTime = 750;
		}
		else {
			duration = 1700;
			fadeTime = 500;
		}
		

		var counter = 0;
		
		// controls:
		
		slideshowDiv.find(".slideshow_slide").each( function() {	
											 
		
			var buttonText = $("<span/>");
			buttonText.html("#");
			var button = $("<li/>");
			button.append( buttonText );
			

	
			slideshowDiv.find(".slideshow_controls").append( button );
				
		
			button.click( function() {
				$(this).parent().find("li").removeClass("active");
				$(this).addClass("active");
				changeTo( $(this).index() );
			});
			
		});



		if( numberOfSlides > 1 ) {
			slideshowDiv.find(".slideshow_controls").css("display","block");
		}
		
		var changeTo = function( slideIndex ) {
			container.find(".slideshow_slide").each( function() {
				if( $(this).index() == slideIndex ) {
					$(this).fadeIn(fadeTime, function() {
						$(this).css("filter","");							  
					});
				}
				else {
					$(this).fadeOut(fadeTime);	
				}
			});
			currentSlide = slideIndex;
			
			counter = 0;
		};
		slideshowDiv.find(".slideshow_controls li:first").click();

		return {
			count: function() {

				counter++;
				if( counter >= duration ) {
					var newIndex = (currentSlide+1)%numberOfSlides;
					slideshowDiv.find(".slideshow_controls li:nth-child("+(newIndex+1)+")").click();
					counter = 0;
				}

			}		
		};
	}
	
	
	slideshows = new Array();
	
	$(".slideshow").each( function() {
		slideshows.push( new Slideshow( $(this) ) );
	});
	
	
	interval = window.setInterval("count()",1);

});




function checkCampaignHeight() {
	

	if( $("#header-inner").width() < 480 ) {
		//if mobile:
		$(".slideshow.campaign").css("display","block");
		
		var slideshowWidth = $(".slideshow.campaign").width();
		var slideshowHeight = slideshowWidth/2.16;
		
		

		$(".slideshow.campaign object").height( slideshowHeight );
		$(".slideshow.campaign .flash_container").height( slideshowHeight );
		$(".slideshow.campaign .slide_text").css("top",slideshowHeight);
		$(".slideshow.campaign .slide_text").css("width","100%");
		
		var slideTextMaxHeight = 0;
		$(".slideshow.campaign .slideshow_slide").css("display","block");
		$(".slideshow.campaign .slide_text").each( function() {
			if( $(this).height() > slideTextMaxHeight ) {
				slideTextMaxHeight = $(this).height();
			}
		});
		$(".slideshow.campaign .slideshow_slide").removeAttr("style");
		
		$(".slideshow.campaign").height( slideshowHeight+slideTextMaxHeight );
		$(".slideshow.campaign .slideshow_controls li:first").click();
	}
	else {
		$(".slideshow.campaign object").removeAttr("style");
		$(".slideshow.campaign object").css("visibility","visible");
		$(".slideshow.campaign .flash_container").removeAttr("style");
		$(".slideshow.campaign .slide_text").removeAttr("style");
		
		$(".slideshow.campaign").removeAttr("style");
	}

}

function setSlideshowHeights() {
	
	if( $("#header-inner").width() >= 480 ) {

		$(".slideshow.small").each( function() {
			var maxHeight = 0;
			$(this).find(".slideshow_slide").each( function() {
				if( $(this).height() > maxHeight ) {
					maxHeight = $(this).height();
				}
			});
			$(this).height( maxHeight );
		});
		
		
		$(".slideshow.mini").each( function() {
			var maxHeight = 0;
			$(this).find(".slideshow_slide").each( function() {
				if( $(this).height() > maxHeight ) {
					maxHeight = $(this).height();
				}								
			});
			$(this).height( maxHeight );
		});
			
	}
	else {
		$(".slideshow.small .slideshow_slide, .slideshow.mini .slideshow_slide").removeAttr("style");
		$(".slideshow.small, .slideshow.mini").removeAttr("style");
	}

	
}
