var doEnterFrame = 0;
var tEnterFrame;
var mouseX = 0;
var realPreviewWidth = 0;
var easing = .8;
var previewOn = 0;
var infoPanelOpen = 0;

function initPreview()
{
	initKeyEvents();
	initNavigationMenu();
	initInfoPanel();
}

function initInfoPanel()
{
	$("#openCloseInfo").click(function(event){
		if(infoPanelOpen == 0)
			openInfoPanel();
		else
			closeInfoPanel();
	});
}

function openInfoPanel()
{
	infoPanelOpen = 1;
	$("#infoPanelDescriptionOut").stop();
	$("#infoPanelDescriptionOut").animate({
    	width: "170px",
    	height: "250px"
    }, 1000, 'easeOutExpo');
    
    $("#infoPanel").stop();
	$("#infoPanel").animate({
    	bottom: "282px"
    }, 1000, 'easeOutExpo');
}

function closeInfoPanel()
{
	infoPanelOpen = 0;
	$("#infoPanelDescriptionOut").stop();
	$("#infoPanelDescriptionOut").animate({
    	width: "0px",
    	height: "0px"
    }, 1000, 'easeOutExpo');
    
    $("#infoPanel").stop();
	$("#infoPanel").animate({
    	bottom: "31px"
    }, 400, 'easeOutExpo');
}

function initNavigationMenu()
{
	$("#arrowLeft").click(function(event){
		movePrev();
	});
	
	$("#arrowRight").click(function(event){
		moveNext();
	});
	
	$("#viewAllButton").click(function(event)
	{
		prevPath = $.address.path();
		actualId = ($.address.path().slice(($.address.path().lastIndexOf(("/")) + 1)));
		actualId++;
		var newPath = $.address.path().slice(1, $.address.path().lastIndexOf(("/")));
		$.address.value(newPath);
	});
}

function showNavigationMenu()
{
	$("#navigationMenu").fadeIn();
}


function hideNavigationMenu()
{
	$("#navigationMenu").fadeOut();
}

function showPreview()
{
	previewOn = 1;
	initClickBackgroundLayer();
	$("#blockPreview").fadeIn(500, addPreviewEvents);	
}

function addPreviewEvents()
{
	$('#containerPreviewOut #containerPreviewIn #listPreview li').mouseover(function() {
		if($(this).find(".titleGallery"))
			showTitle($(this).find(".titleGallery"));
 	 	showSinglePreview($(this), 500);
	});
	
	$('#containerPreviewOut #containerPreviewIn #listPreview li').mouseout(function() {
		if($(this).find(".titleGallery"))
			hideTitle($(this).find(".titleGallery"));
 	 	hideSinglePreview($(this), 500);
	});
	
	initEnterFrame();
	initCatchMousePosition();
	findRealPreviewWidth();
	initClickBackgroundLayer();
}

function showTitle(element)
{
	$(element).stop();
		$(element).animate({
    	top: "140px"
    }, 200, 'easeOutExpo');
}

function hideTitle(element)
{
	$(element).stop();
		$(element).animate({
    	top: "170px"
    }, 200, 'easeOutExpo');
}

function initClickBackgroundLayer()
{
	$("#backgroundPreviewLayer").click(function(event)
	{
		$("#backgroundPreviewLayer").unbind("click");
		$.address.value(prevPath);
	});
}

function hidePreview(msg)
{
	previewOn = 0;
	$("#blockPreview").fadeOut(500, function(event)
	{
		if(msg != "")
			$("#listPreview").html(msg);
		removeEventsPreview();
	}
	);	
}

function removeEventsPreview()
{
	removeCathMousePosition();
	removeEnterFrame();
	$('#containerPreviewOut #containerPreviewIn #listPreview li').unbind("mouseover");
	$('#containerPreviewOut #containerPreviewIn #listPreview li').unbind("mouseout");
	$('#containerPreviewOut #containerPreviewIn #listPreview li').unbind("click");	
}

function findRealPreviewWidth()
{
	realPreviewWidth = 0;
	for(var a = 0; a < $("#containerPreviewOut #containerPreviewIn #listPreview li").children().length; a++)
	{
		var element = $("#containerPreviewOut #containerPreviewIn #listPreview li:eq("+a+")");
		realPreviewWidth += parseFloat($(element).find("img").width());
		
		$(element).find(".titleGallery").css("width",parseFloat($(element).find("img").width()) + "px");
	}
}

function initCatchMousePosition()
{
	$(document).mousemove(function(e){
		mouseX = e.pageX;
   }); 
}

function removeCathMousePosition()
{
	$(document).unbind('mousemove');
}


function enterFrame()
{
	moveListPreview();
	if(doEnterFrame == 1)	
		tEnterFrame = setTimeout("enterFrame()", 33);
}

function moveListPreview()
{
	findRealPreviewWidth();
	var nextPos = parseFloat(map(mouseX, 0, $(window).width(), 0, (($(window).width() - realPreviewWidth))));
	var actualPos = parseFloat($("#listPreview").css("left"));
	var newPos = actualPos;
	newPos += (nextPos - actualPos) * easing;
	if(realPreviewWidth <= $(window).width())
		nextPos = 0;
	//var newPos += ()) - parseFloat($("#listPreview").css("left"))) * easing;
	$("#listPreview").css("left", nextPos + "px");	
}

function initEnterFrame()
{
	doEnterFrame = 1;
	enterFrame();
}

function removeEnterFrame()
{
	doEnterFrame = 0;
}

function showSinglePreview(element, time)
{
	$(element).stop();
	$(element).animate({
    opacity:1
    }, time, 'linear');
}

function hideSinglePreview(element, time)
{
	$(element).stop();
	$(element).animate({
    opacity:.2
    }, time, 'linear');
}

function initKeyEvents()
{
	$(document).keydown(function(event){onKeyDownHandler(event.keyCode);});
}

function onKeyDownHandler(keyCode)
{
	if(doEnterFrame == 1) return;
	if(keyCode == "37")
		movePrev()
	else if(keyCode == "39")
		moveNext();
}

function moveNext()
{
	if(isHome == 1) 
		return;
	var actualId = -1;
	var newPath  = "";
	actualId = ($.address.path().slice(($.address.path().lastIndexOf(("/")) + 1)));
	newPath = $.address.path().slice(1, $.address.path().lastIndexOf(("/")));
	actualId++;
	if(actualId > $("#listPreview").children().length)
		return;
	$.address.value(newPath + "/" +actualId);
}

function movePrev()
{
	if(isHome == 1) 
		return;
	var actualId = -1;
	var newPath  = "";
	actualId = ($.address.path().slice(($.address.path().lastIndexOf(("/")) + 1)));
	newPath = $.address.path().slice(1, $.address.path().lastIndexOf(("/")));
	actualId--;
	if(actualId < 1)
		return;
	$.address.value(newPath + "/" +actualId);
}
