// JavaScript Document

$(document).ready(function() {
	
	//play the video
	playTheVideo();
	
	var titles 					= [];
	var captions 				= [];
	var urls 					= [];
	var images 					= [];
	
	var imageWidth = 190;
	var animateValue = 190;
	var fullWidth;
	
	var lastLeftValue;
	var animateValue = 190;
	var currentImageNo = 0;
	var tempNo = 1;
	var mouseOver= false;
	var animationStopped= false;
	
	$.ajax({
		url: "/flash/logo.xml",
		dataType: "xml",
		success: parseXml,
		error:function (xhr, ajaxOptions, thrownError){
                    //alert(xhr.status);
                    //alert(thrownError);
                }
	});
	
	function parseXml(xml)
	{
		
		if(!(xml instanceof jQuery))
		{
			xml = $(xml);
		}
		
	
		noOfImages = xml.find('logo').length;
		lastLeftValue = ((noOfImages)*(imageWidth-1));
		fullWidth = (noOfImages)*(imageWidth)
		
		
		xml.find('logo').each(function(){
			titles.push($(this).find('Name').text());
			captions.push($(this).find('Industry').text());
			urls.push($(this).find('Href').text());
			images.push($(this).find('Image').text());
		});

		for(var i = 0; i <noOfImages ; i++){
			
			var imageChilds ="";
			
			var toolTip = "<div class='logoToolTip'><div class='logoTextSet'><h4>"+ titles[i] +"</h4><p>"+ captions[i] +"</p></div><div class='logoMoreButton'><a href='"+ urls[i] +"'><img src='/Images/plus.gif' border='0' /></a></div></div>";
			
			var imageSet = "<a href='"+ urls[i] +"'><img src='"+ images[i] +"' border='0' title='"+ titles[i] +"'/></a>";
			
			imageChilds = "<li>"+ imageSet + toolTip +"</li>";
			
			$('#logoSet').append(imageChilds);

		}
		
		$('#logoSet').css({width:(fullWidth*2)});
		
		var cloneSet = $('#logoSet').children().clone()
		$('#logoSet').append($('#logoSet').children().clone());
		
		//$('#logoSet').children().children().animate({opacity:0.5});
		$('.logoToolTip').animate({opacity:0},10);
		
		moveLeft();
		function moveLeft(){
			
			if(mouseOver == false){
				animationStopped = false;
				$('#logoSet').animate({
					left: ($('#logoSet').position().left - 47.5)
				},1000, 'linear' , function(){
					animationStopped = true;
					if($('#logoSet').position().left <= - fullWidth){
						var currentLeft = fullWidth - Math.abs($('#logoSet').position().left);
						$('#logoSet').css({left:currentLeft});
						//$('#logoSet').stop(true, true);
						//console.log($('#logoSet').position().left);	
					}
					
					moveLeft();
				});
			}
			//console.log(animationStopped);
			
		}
		
		$('#logoSet').hover(function(){
			mouseOver = true;
		},function(){
			mouseOver = false;
			if(animationStopped == true){
				moveLeft();
			}
			
		});
		
		$('#logoSet').children().hover(function(){
			$(this).children('.logoToolTip').animate({
				opacity:1,
				top:-00
			},{ queue:false, duration:500 });
			
		},function(){
			$(this).children('.logoToolTip').animate({
				opacity:0,
				top:-30
			},{ queue:false, duration:500 });	
		});
		
		
	}
	
	function playTheVideo(){
		//IndexFlash
		$('.playTheFlash').click(function(){
			var embdFlashText = "<object height='600' align='middle' width='960' viewastext='' id='MyFlashId' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'><param value='sameDomain' name='allowScriptAccess'><param value='/flash/intro3.swf' name='movie'><param value='high' name='quality'><param value='transparent' name='wmode'><param value='false' name='menu'><embed height='600' width='960' pluginspage='http://www.macromedia.com/go/getflashplayer' menu='false' wmode='transparent' type='application/x-shockwave-flash' allowscriptaccess='sameDomain' quality='high' src='/flash/intro3.swf' name='MyFlashId'></object>";
			
			$('#IndexFlash').html('');
			$('#IndexFlash').append(embdFlashText);
		});
	}

});
