//------------------------------------
//	HOMEPAGE.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.3.2
//				global.js
//				cufon.js
//				gazz.js
//------------------------------------

//CUFON
Cufon.replace('#twitter p.loading');
Cufon.replace('#twitter_update_list li');
Cufon.replace('#news dd a', {
	hover: true
});

// screen preview

/*
 * Url preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
 var leftmarginToTillAnchorDivEnd = 1130; // margin left value from screen left to end of the anchorlink main div
this.screenshotPreview = function(){	
	/* CONFIG */
		xOffset = 10;
		yOffset = 30;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";
		var divValue = (leftmarginToTillAnchorDivEnd * 0.60); // to calcilate the 70% margin left value ,means after 3 tabs frm left side 
		if(divValue > e.pageX){
			yOffset = 30;
		} else {
			yOffset = -500; // display pop up image to left side the box 
		}
		if(400 > e.clientY){
			xOffset = 10;
		} else {
			xOffset = 400; // display pop up image to top position 
		}
		var c = (this.t != "") ? "" + this.t : "";
		var c = (this.t != "") ? "" + this.t : "";
		ext = this.rel.substr(this.rel.lastIndexOf('.')+1);
		
		wd = this.getAttribute('relwidth');
		hg = this.getAttribute('relheight');
		
		if(ext!='swf')
		{
			$("body").append("<p id='screenshot'>"+ c +"<img src='"+ this.rel +"' alt='url preview' /></p>");
		}
		else
		{		
			$("body").append("<p id='screenshot'>"+ c +"<embed src='"+ this.rel +"' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='"+ wd +"px' height='"+ hg +"px'> </embed></p>");
		}
		$("#screenshot")
			.css("top",(e.pageY + xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	screenshotPreview();
});







//CUFON
Cufon.replace('#twitter p.loading');
Cufon.replace('#twitter_update_list li');

//BEGIN jQuery
$(function(){

	//TWITTER
	
	totalT = 0;
	
	$("#twitter").getTwitter({
		userName: "2adproinkzoo",
		numTweets: 7,
		loaded: function(){
			
			totalT = $('#twitter_update_list li').size();
			
			$('#twitter_update_list li:first').clone().appendTo('#twitter_update_list');
			
			//Apply cufon
			Cufon.refresh('#twitter_update_list li');
			
			//Do the animating!
			$('#twitter p.loading').animate({opacity:0},400,'easeInOutExpo',function(){
				$(this).remove();
				$('#twitter_update_list').animate({top:0},400,'easeInOutExpo',function(){
					tweetCycle();
				});
			});
		}
	});
	
	currentY = 0;
	currentT = 1;
	
	var tweetTimer = {};
	
	$('#twitter').hover(function(){
		$.clearTimer(tweetTimer);
	},function(){
		tweetCycle();
	});
	
	function tweetCycle(){
		tweetTimer = $.timer(8000,function(){
			$.clearTimer(tweetTimer);
			if(currentT==totalT+1){
				$('#twitter_update_list').css({top:0});
				currentY = 0;
				currentT = 1;
			}
			currentY = currentY - 38;
			$('#twitter_update_list').animate({top:currentY},400,'easeInOutExpo');
			currentT = currentT + 1;
			tweetCycle();
		});
	}
	
	
	//PROMO SLIDER

	totalPromo = $('#promo_slider div').size();
	xPos = 0;
	currentPromo = 1;
	moose = true;
	autoMoose = true;
	promoDelay = 7000;
	easeIn = 'easeInExpo';   
	easeOut = 'easeOutElastic'; 
	inSpeed = 400;
	outSpeed = 1000;
	
	$('#promo_nav .next a').click(function(){
		if(moose==true && totalPromo>currentPromo){
			xPos = xPos - 775;
			currentPromo = currentPromo + 1;
			slideTo(xPos, 'right');
		}else if(moose==true){			
			currentPromo = 1;
			infiniteLoop('right');
		}
		return false;
	});
	
	$('#promo_nav .prev a').click(function(){
		if(moose==true && currentPromo>1){
			xPos = xPos + 775;
			currentPromo = currentPromo - 1;
			slideTo(xPos, 'left');
		}else if(moose==true){
			currentPromo = totalPromo;
			infiniteLoop('left');
		}
		return false;
	});
	
	function infiniteLoop(dir){
		
		moose = false;
		
		totalX = 775 - (775 * totalPromo);
		
		if(dir == 'left'){
			$('#promo_slider').prepend('<div class="promo_content">'+$('#promo_slider div.promo_content:last').html()+'</div>').css({left:-775}).animate({left:387},inSpeed,easeIn,function(){
				$(this).animate({left:0},outSpeed,easeOut,function(){
					$('#promo_slider').css({left:totalX});
					$('#promo_slider div.promo_content:first').remove();
					moose = true;
				})
			});
			xPos = totalX;
		}else if(dir == 'right'){
			$('#promo_slider').append('<div class="promo_content">'+$('#promo_slider div.promo_content:first').html()+'</div>').animate({left:totalX-387},inSpeed,easeIn,function(){
				$(this).animate({left:totalX-775},outSpeed,easeOut,function(){
					$('#promo_slider').css({left:0});
					$('#promo_slider div.promo_content:last').remove();
					moose = true;
				});
			});
			xPos = 0;
		}
	}
	
	$('#promo_panel').hover(function(){
		$.clearTimer(promoTimer);
	},function(){
		autoMagical();
	});
	
	var promoTimer = {};
	
	function autoMagical(){
		promoTimer = $.timer(promoDelay,function(){
			$.clearTimer(promoTimer);
			$('#promo_nav .next a').click();
			autoMagical();
		});
	}
	
	autoMagical();
	
	function slideTo(newX, dir){
		moose = false;
		if(dir == 'left'){
			halfX = newX - 387;
		}else{
			halfX = newX + 387;
		}
		$('#promo_slider').animate({left:halfX},inSpeed,easeIn,function(){
			$(this).animate({left:newX},outSpeed,easeOut,function(){


				moose = true;
			});
		})
	}
});

























function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',6000);
	
}

function rotate() {	
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};

$(document).ready(function() {		
	//Load the slideshow
	theRotator();
});

