/**
 * @author camilo
 */

var IDLE_TIMER_DELAY = 65;
var IDLE_SHOW_TIMER_DELAY = 5;
var lastActivityTimer = new Date();
var idleAnimationIsOn = false;
var idleAnimationTimerId;

// paging preview timeout ids
var pagingPreviewTimeoutId;
var pagingPreviewShowTimeoutId;

// is it the the startpage?
var isStart = false;

var idleAnimations = [
	{swf: 'wp-content/themes/bronson/media/swf/idle/temp.swf'}
];

jQuery(document).ready(bronsonInit);

function bronsonInit()
{
	jQuery(".siteversion").bind("click", function() { location.href = '/changelog'; })
	//jQuery(".siteversion").toolTiper();
	if (isStart) 
	{
		//	jQuery(document.body).bind("mousemove", resetAnimationTimer);
		//idleAnimationTimerId = setTimeout(idleAnimationTimer, 1000);
	}

	//jQuery(".post[rel!='']").bind("mouseenter.effect", showEffect);
	
	jQuery(".siteversion").bind("mouseenter", function() { jQuery("#changeLog").animate({top: 0}) });
	jQuery(".siteversion").bind("mouseleave", function() { jQuery("#changeLog").animate({top: jQuery("#changeLog").height() * -1 - 50}) });
	
	// start the logo animations
	logoAnims();
	
	// menus
	setupMenus();
	
	setTimeout(function(){$("#firstTextContainerDecorator1").fadeOut(2000);}, 500);
	setTimeout(function(){$("#firstTextContainerDecorator2").fadeOut(2000);}, 1000);
	setTimeout(function(){$("#firstTextContainerDecorator3").fadeOut(2000);}, 1500);
	setTimeout(function(){$("#firstTextContainerDecorator4").fadeOut(2000);}, 2000);
	
	//setupProjectAnims();
	
}

function logoAnims()
{

	swfobject.embedSWF
	(
		templatePath + "/media/swf/logo/blommor.swf",
		"flashLogoContainer",
		"260",
		"130",
		"9.0.0",
		"meda/swf/expressInstall.swf",
		{},
		{scale: "default", wmode: "transparent", align: "l", salign: "br"}
	 );
	
	setTimeout(
		function()
		{
			jQuery("#logo").fadeOut();
		}, 1000
	);
}



function showEffect(event)
{
	jQuery(".postEffect").remove();
	
	var post = jQuery(event.currentTarget);
	var postData = post.attr("rel").split(";");
	var swf = postData[0];
	var url = postData[1];
	
	console.log(swf);
	console.log(url);
	var overLayer = jQuery("<div id='postEffect' class='postEffect'></div>");

	jQuery("#postEffectContainer").append(overLayer);
	
	var elementOffset = post.offset();
	
	var document = jQuery(document);
	var totalWidth = document.width();
	var totalHeight = document.height();
	
	var elementX = elementOffset.left;
	var elementY = elementOffset.top;
	
	jQuery("#postEffectContainer").css("width", totalWidth + "px").css("height", totalHeight + "px");

	swfobject.embedSWF
	(
		swf,
		"postEffect",
		"100%",
		"100%",
		"9.0.0",
		"meda/swf/expressInstall.swf",
		{top: elementY, left: elementX, width: post.width(), height: post.height(), url: url},
		{scale: "noscale", wmode: "transparent", align: "l", salign: "tl"}
	);

	/*
	overLayer.bind("mouseleave.effect", hideEffect);
	*/
	jQuery("#postEffectContainer").show();
}

function hideEffect()
{
	console.log("hide");
	jQuery(".postEffect").remove();
	jQuery("#postEffectContainer").hide();
}



function idleAnimationTimer()
{

	var now = new Date();
	var currentTimeLeft = Math.round((IDLE_TIMER_DELAY * 1000 - (now.getTime() - lastActivityTimer.getTime())) / 1000);
	
	var iatObject = jQuery("#idleAnimationTimer");	
	
	if (currentTimeLeft < 0)
	{
		currentTimeLeft = -1;
	}
	else if (currentTimeLeft > IDLE_TIMER_DELAY - (IDLE_SHOW_TIMER_DELAY - 1))
	{
		currentTimeLeft = IDLE_TIMER_DELAY - (IDLE_SHOW_TIMER_DELAY - 1);
		iatObject.hide();
	}
	else if (currentTimeLeft == IDLE_TIMER_DELAY - IDLE_SHOW_TIMER_DELAY) 
	{
		iatObject.html(currentTimeLeft).fadeIn();	
	}
	else if (currentTimeLeft > IDLE_TIMER_DELAY - IDLE_SHOW_TIMER_DELAY) 
	{
		iatObject.hide();
	}
	
	
	
	clearTimeout(idleAnimationTimerId);
	if (currentTimeLeft == -1) 
	{
		iatObject.html(0).fadeOut();
		if (!idleAnimationIsOn) showIdleAnimation();
	}
	else
	{
		iatObject.html(currentTimeLeft);
		setTimeout(idleAnimationTimer, 1000);
	}

}

function resetAnimationTimer()
{
	lastActivityTimer = new Date();
	if (idleAnimationIsOn) 
	{
		hideIdleAnimation();
	}
}

function showIdleAnimation()
{
	debug.write("showIdleAnimation");
	if (!idleAnimationIsOn) 
	{
		idleAnimationIsOn = true;
		
		swfobject.embedSWF
	 (
	 idleAnimations[Math.round(Math.random() * (idleAnimations.length - 1))].swf,
	 "idleContainer",
	 "450",
	 "400",
	 "9.0.0",
	 "meda/swf/expressInstall.swf",
	 {},
	 {scale: "default", wmode: "transparent", align: "l", salign: "br"}
	 );
		jQuery("#idleContainer").show();
		jQuery("#idleContainer").css("height", jQuery(document.body).height() + "px");
		jQuery("#idleContainer").css("background-color", "red");//show();
	}
}


function hideIdleAnimation()
{
	idleAnimationIsOn = false;
	debug.write("hideIdleAnimation");
	lastActivityTimer = new Date();
	jQuery("#idleContainer").empty().hide();
	clearTimeout(idleAnimationTimerId);
	setTimeout(idleAnimationTimer, 1000);
}

function setupMenus()
{
	
	$("#topMenu.mainMenu .menuItemContainer").bind("mouseenter",
		function(event)
		{
			var itemDom = $(event.currentTarget);
			itemDom.stop().animate({top: -21}, 300);
		}
	)
	
	$("#topMenu.mainMenu .menuItemContainer").bind("mouseleave",
		function(event)
		{
			setTimeout(
				function()
				{
					var itemDom = $(event.currentTarget);
					itemDom.stop().animate({top: 0}, 200);
				}, 300
			);
		}
	)
}

/**
 * So ugly but no time to make beutyful
 */
var slideIndex = 0;
var pauseSlide = false;
var slideAnimTimeoutRef = -1;
function setupProjectAnims()
{
	setTimeout(slideAnimation, 5000);
	
	var getSlides = $("#firstTextContainer .slide");
	var max = getSlides.length;
	for (var i = 0; i < max; i++)
	{
		$(getSlides[i]).css("top", -500 * i).show();;
	}
	
	 $("#firstTextContainer").bind("mouseenter", function() {clearTimeout(slideAnimTimeoutRef)});
	 $("#firstTextContainer").bind("mouseleave", function() {slideAnimTimeoutRef = setTimeout(slideAnimation, 2000);});
	 
}

function slideAnimation()
{
	var getSlides = $("#firstTextContainer .slide");
	console.log(getSlides);
	var activeSlide = $(getSlides[slideIndex]);
	
	var nextSlideIndex = slideIndex + 1 > getSlides.length - 1 ? 0 : slideIndex + 1;
	var nextSlide = $(getSlides[nextSlideIndex]);
	
	activeSlide.animate({top: activeSlide.height() * -1}, 1000);
	nextSlide.css("top", nextSlide.height() * -1).animate({top: 0}, 1000);
	
	console.warn(nextSlide);
	
	slideIndex = nextSlideIndex;
	clearTimeout(slideAnimTimeoutRef);
	slideAnimTimeoutRef = setTimeout(slideAnimation, 5000);
}

// create a proxy for functions so that they run in the desired scope
CallbackProxy = function(scope, functionRef)
{
	return function(response)
	{
		functionRef.apply(scope, [response]);
	}
}


