// JavaScript Document
//

// folder
var dir = "/slideshow/";

// the speed while hovering the controls
var fast = 1;

// the normal speed
var normal = 1;

// the speed multiplicator
// as this script is quite unefficient, for faster movement we need to do more than 1px move
var multiplicator = 0.8;

var pic, numImgs, arrLeft, i, totalWidth, n, myInterval;
var width;

var whereto = "flexiScrollLeft()";


$(document).ready(function() {

	// Homepage Slideshow
	function slideshowHome(xmlFile) {
		
		$.ajax({
			url: dir + xmlFile,					
			type: "GET",	
			data: "",		
			dataType: "xml",
			async: false,
			success: function(xml) {
				
				var i = 1;
				
				var ext = ".jpg";
				
				var multiplicator = parseFloat($(xml).find('speed').text());
				var speed = parseFloat($(xml).find('speed').text());
				var pause = parseFloat($(xml).find('pause').text());
				var path = $(xml).find('path').text();
				var total = parseFloat($(xml).find('total').text());
				var finalPause = $(xml).find('finalPause').text();
				var caption = $(xml).find('caption').text();
				var url = $(xml).find('url').text();
				
				
				var slides = Array();
				
				$('#slideshow-div').append('<div class="slide-container"><img src="' + dir + path + 1 + ext + '"/></div>');
				
				for(i=2; i<=total; i++) {
								
					$(".slide-container").append('<img src="' + dir + path + i + ext + '"/>');
					
				}
								
				$('#slideshow-div').append('<div class="slide-caption-img"><a href="' + url + '"><img src="' + dir + caption + '"/></a></div>');
										
				$(".slide-container").carouFredSel({
					width: 480,
					height: 319,
					items: {
						visible: 1,
						width: 480,
						height: 319
					},
					scroll: {
						items: 1,
						easing: "linear",
						duration: speed,
						pauseOnHover: true
					},
					auto: pause
				});
				
			}
			
		});
	
	}

	// Node Slideshow 
	function slideshowNode(xmlFile) {
		
		$.ajax({
			url: dir + xmlFile,					
			type: "GET",	
			data: "",		
			dataType: "xml",
			async: false,
			success: function(xml) {
				
				
				
				var speed = parseFloat($(xml).find('speed').text())
				var pause = parseFloat($(xml).find('pause').text());
				var path = $(xml).find('path').text();
				var finalPause = $(xml).find('finalPause').text();
				
				var ext = ".jpg";
				
				var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
				
				if(is_chrome) {
					multiplicator = speed / 2;
				} else {
					multiplicator = speed;
				}
				
				//alert(is_chrome + " - " + speed);
								
				// Insert elements from XML file into DOM
				
				$(xml).find('item').each(function(index){					
									
					$(".rs-scroller").append('<a href="' + $(this).find('link').text() + '" title="' + $(xml).find('top:eq(' + index + ')').text() + '" name="' + $(xml).find('bottom:eq(' + index + ')').text() + '"><img src="' + dir + path + (index + 1) + ext + '" /></a>');
														
				});	
				
				
				var imgWidth = $(".rs-scroller").children("a").children("img").width();
				var totalImgs = $(".rs-scroller").children("a").length;
				
				var totalWidth = parseFloat(imgWidth * totalImgs);
				
				var scrollSpeed = parseFloat((speed * totalImgs) + speed);	
				
				
				// CONTROLS
				
				// Create controls
				$('.rs-slideshow').append('<div class="slide-controls"><div class="prevBtn"></div><div class="controlBtns pauseBtn"></div><div class="nextBtn"></div></div>');
				
				
				// Play / Pause controls 
				$(".controlBtns").toggle(function() {
					
					$(this).removeClass("pauseBtn").addClass("playBtn");
					clearInterval(myInterval);
					
				}, function() {
					
					$(this).removeClass("playBtn").addClass("pauseBtn");
					clearInterval(myInterval);
					myInterval = setInterval(whereto, fast);
					
				});
				
				// Images hover
				$(".rs-scroller").hover(function() {
					
					if ($(".slide-controls").children(".playBtn").length != 1) {
						clearInterval(myInterval);
					}
					
				}, function() {
					
					if ($(".slide-controls").children(".playBtn").length != 1) {
						$(this).removeClass("playBtn").addClass("pauseBtn");
						clearInterval(myInterval);
						myInterval = setInterval(whereto, fast);
					}
					
				});
				
				// Prev control
				$(".prevBtn").click(function() {
					
					if ($(".slide-controls").children(".playBtn").length != 1) {
						clearInterval(myInterval);
						whereto = "flexiScrollLeft()";
						myInterval = setInterval(whereto, fast);
					}
					
				});
				
				// Next control
				$(".nextBtn").click(function() {
					
					if ($(".slide-controls").children(".playBtn").length != 1) {
						clearInterval(myInterval);
						whereto = "flexiScrollRight()";
						myInterval = setInterval(whereto, fast);
					}
					
				});

				width = $(".rs-scroller").width();
				pic = $(".rs-scroller").children("a").children("img");
				numImgs = pic.length;
				arrLeft = new Array(numImgs);
				
				for (i=0;i<numImgs;i++){
				
					totalWidth=0;
					for(n=0;n<i;n++){
						totalWidth += $(pic[n]).width();
					}
				
					arrLeft[i] = totalWidth;
					$(pic[i]).css("left", totalWidth);
				}
				
				myInterval = setInterval("flexiScrollLeft()", normal);
				$(pic).show();
	
			   
			   
			    // CAPTIONS 
				
				// Set first caption when page laods
				$('.rs-slideshow').append('<div class="slide-caption"><div class="captionTop">' + $(xml).find('top:first').text() + '</div><div class="captionBottom">' + $(xml).find('bottom:first').text() + '</div></div>');
			   
							
		 
			}
			
		});
	
	}	
	
	
	// Check which slideshow to call
	if($(".rs-slideshow.home").length > 0) {
		slideshowHome("mod01.xml");
	} 
	
	if($(".rs-slideshow.node.by_market").length > 0) {
		slideshowNode("mod02b.xml");
	} 	
	
	if($(".rs-slideshow.node.by_product").length > 0) {
		slideshowNode("mod02.xml");
	} 
	
	
});



function flexiScrollLeft(){ 
	for (i=0;i<numImgs;i++){
		arrLeft[i] -= multiplicator;

		if (arrLeft[i] <= -($(pic[i]).width())){
			totalWidth = 0;
			for (n=0;n<numImgs;n++){
				if (n!=i){
					totalWidth += $(pic[n]).width();
				}
			}

			arrLeft[i] =  totalWidth + (arrLeft[i] + $(pic[i]).width());
			
		}
		
		$(pic[i]).css("left", arrLeft[i]);
		
		
		var halfWidth =  parseInt(width / 2);
	
		if( parseInt(((arrLeft[i] - halfWidth) / numImgs)) == parseInt(((arrLeft[i] - halfWidth)  * i)) ) {
				
			$('.captionTop').html($(".rs-scroller").children("a:eq(" + i + ")").attr("title"));
			$('.captionBottom').html($(".rs-scroller").children("a:eq(" + i + ")").attr("name"));
		
		}
		
		
	}
}

function flexiScrollRight(){
	for (i=numImgs-1;i>-1;i--){
		arrLeft[i] += multiplicator;		

		if (arrLeft[i] >= width){
			totalWidth = 0;
			for (n=0;n<numImgs;n++){
					totalWidth += $(pic[n]).width();
			}
			arrLeft[i] =  arrLeft[i] - totalWidth;
		}
		
		$(pic[i]).css("left",arrLeft[i]);
		
		var halfWidth =  parseInt(width / 2);
	
		if( parseInt(((arrLeft[i + 1] - halfWidth) / numImgs)) == parseInt(((arrLeft[i + 1] - halfWidth)  * (i + 1))) ) {
				
			$('.captionTop').html($(".rs-scroller").children("a:eq(" + i + ")").attr("title"));
			$('.captionBottom').html($(".rs-scroller").children("a:eq(" + i + ")").attr("name"));
		
		}
		
	}
}		

// End of script





