/*!
 * Copyright (c) 2011 Matthew Rose, Avalanche Tech
 * http://www.matthewrose.com.au
 */

	$(document).ready(function(){ 
		var promoImagesURLs = new Array();
		var promoImagesLinks = new Array();
		
		var count = 0;
		var inMotion = false;
		var currentPosition = 0;
		var calculatedImagesToLoad = 0;
		var timerDirection = "forward";
		var timer;		
		
		/* assign swipe gestures */
		var swipeOptions =
		{
			swipe:swipe,
			threshold:0
		}
		
		initTimer = function()
		{
			timer = window.setInterval(nextPromo, 5000);
		}
		
		nextPromo = function() 
		{ 
			if (currentPosition == 0){
				timerDirection = "forward"
			}
			else if (currentPosition == promoImagesURLs.length - 1){
				timerDirection = "backwards";
			}
			
			if (timerDirection == "forward"){
				tweenRight();
			}
			else
			{
				tweenLeft();
			}
		}
		
		initGestures = function()
		{
			for (var s = 0 ; s < promoImagesURLs.length; s++){
				$(document.getElementById('image'+s)).swipe( swipeOptions );
			}
		}

		function swipe(event, direction)
		{
			//alert("You swiped " + direction );
			if (direction == "left"){
				tweenRight();
			}
			else if (direction == "right"){
				tweenLeft();
			}
		}
		/* end swipe gestures */
		
		//pass the xml
		$.ajax({
			type: "GET",
			url: "promo/avalanchepromos.xml",
			dataType: "xml",
		
			success: function(xml) {
				 //console.log("passed");
				$(xml).find('item').each(function(){
					promoImagesURLs[count] = $(this).find('image').text();
					promoImagesLinks[count] = $(this).find('url').text();
					count++;
				});
				loadImages();
			},
	            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
            }
			
		});


		//run forloop and laod the images
		loadImages = function() {
			//calculate how many images need to load before rendering
			calculatedImagesToLoad = promoImagesURLs.length; //main image + breadcrumbs + controller
			
			var injectionString;
			var injectionStringBc;
			for (var a = 0 ; a < promoImagesURLs.length; a++){
				if (a == 0){
					injectionString = "<a href='"+promoImagesLinks[a]+"'><img name='image"+a+"' src='" + promoImagesURLs[a] +"' width='960' height='324' border='0'/></a>";
				}
				else
				{
					injectionString = injectionString+"<a href='"+promoImagesLinks[a]+"'><img name='image"+a+"' src='" + promoImagesURLs[a] +"' width='960' height='324' border='0'/></a>";
					
				}
			}
			
			for (var b = promoImagesURLs.length ; b > 0; b--){
				if (b == promoImagesURLs.length ){
					injectionStringBc = "<img onclick='tweenPos("+b+");' name='breadcrumb"+b+"' src='promo/breadcrumb_up.png' width='17' height='17' border='0'/>";
				}
				else if (b == 1){
					injectionStringBc = injectionStringBc+"<img onclick='tweenPos("+b+");' name='breadcrumb"+b+"' src='promo/breadcrumb_down.png' width='17' height='17' border='0'/>";
				}
				else 
				{
					injectionStringBc = injectionStringBc+"<img onclick='tweenPos("+b+");' name='breadcrumb"+b+"' src='promo/breadcrumb_up.png' width='17' height='17' border='0'/>";
				}
			}
				
			//console.log(injectionString);
			//inject HTML
			$(document.getElementById('promoItems')).css({ 'width' : (promoImagesURLs.length * 960) });
			document.getElementById('promoItems').innerHTML = injectionString;
			
			$(document.getElementById('breadcrumbs')).css( 'cursor', 'pointer' );
			document.getElementById('breadcrumbs').innerHTML = injectionStringBc;
			
			initGestures();
			initTimer();
		}
		
		resetBreadcrumbs = function() {
			var injectionStringBc;
			
			for (var b = promoImagesURLs.length ; b > 0; b--){
				if (b == promoImagesURLs.length ){
					if (currentPosition == promoImagesURLs.length - 1){
						injectionStringBc = "<img onclick='tweenPos("+b+");' name='breadcrumb"+b+"' src='promo/breadcrumb_down.png' width='17' height='17' border='0'/>";
					}
					else
					{
						injectionStringBc = "<img onclick='tweenPos("+b+");' name='breadcrumb"+b+"' src='promo/breadcrumb_up.png' width='17' height='17' border='0'/>";
					}
				}
				else if (b == currentPosition + 1){
					injectionStringBc = injectionStringBc+"<img onclick='tweenPos("+b+");' name='breadcrumb"+b+"' src='promo/breadcrumb_down.png' width='17' height='17' border='0'/>";
				}
				else 
				{
					injectionStringBc = injectionStringBc+"<img onclick='tweenPos("+b+");' name='breadcrumb"+b+"' src='promo/breadcrumb_up.png' width='17' height='17' border='0'/>";
				}
			}
			
			document.getElementById('breadcrumbs').innerHTML = injectionStringBc;	
		}
		
		tweenPos = function(position) {
			if (!inMotion){
				if (currentPosition < position){
				var tweenDifference = currentPosition - (position - 1);
					var t1 = new Tween(document.getElementById('promoItems').style,'left',Tween.regularEaseOut, (currentPosition * -960), (currentPosition - tweenDifference) * -960,1,'px');
					t1.onMotionStarted = function(){
						inMotion = true; 
					}; 
					t1.onMotionFinished = function(){
						inMotion = false;
						currentPosition = position - 1;
						resetBreadcrumbs();
					};
					t1.start();
				}
				else if (currentPosition > position){
					var tweenDifference = (position - 1) - currentPosition;
					var t1 = new Tween(document.getElementById('promoItems').style,'left',Tween.regularEaseOut, (currentPosition * -960), (currentPosition + tweenDifference) * -960,1,'px');
					t1.onMotionStarted = function(){
						inMotion = true; 
					}; 
					t1.onMotionFinished = function(){
						inMotion = false;
						currentPosition = position - 1;
						resetBreadcrumbs();
					};
					t1.start();
				}
			}
		}
		

		tweenRight = function() {
			if (!inMotion){
				if (currentPosition != promoImagesURLs.length - 1){
						var t1 = new Tween(document.getElementById('promoItems').style,'left',Tween.regularEaseOut, (currentPosition * -960), (currentPosition + 1) * -960,1,'px');
						t1.onMotionStarted = function(){
							inMotion = true; 
						}; 
						t1.onMotionFinished = function(){
							inMotion = false;
							currentPosition++;
							resetBreadcrumbs();
						};
						t1.start();
				} 
			}
		}
		
		tweenLeft = function() {
			if (!inMotion){
				if (currentPosition > 0){
						var t2 = new Tween(document.getElementById('promoItems').style,'left',Tween.regularEaseOut, (currentPosition * -960), (currentPosition - 1) * -960,1,'px'); //?
						t2.onMotionStarted = function(){
							inMotion = true;
						}; 
						t2.onMotionFinished = function(){
							inMotion = false
							currentPosition--;
							resetBreadcrumbs();
						};
						t2.start();
				}
			}
		}
	});
