// JavaScript Document
var imgCount = 0;
var totalImage = 0;
var itens = null;
var timeOver = 300;
var timeMove = 600;
var slideLength = 10;
var barWidth = 704;
var currentPos = 0;
var moved = true;
var t;
var timer_is_on = false;
var timer = 0;
var slide = [];
var widthW = 0;
var heightW = 0;
var overNav = false;
var images = [];

$(document).keyup(function(e){
	        
    switch (e.keyCode){                                                                                                                     
        case 39:                                                                                                                            
        moveNext();                                                                                                                          
        break;                                                                                                                              
        case 37:                                                                                                                            
        movePrev();
		break;   
		case 38:
		//cima
		moveUp(currentPos)
		break;   
		case 40:
		//baixo
		moveDown(currentPos)
		break;
    }
	
});

$(document).ready(function()
{	
	$(document).pngFix();
	window.onresize = resize;

	$('.navbar').hover(
		function()
		{
			overNav=true;
			timer=0;
		},
		function()
		{
			overNav=false;
			timer=0;
		}
	);
	
	$('.navprev').hover(
		function()
		{
			timer=0;
			$(this).children('.text').stop(true, true).animate({"color": "#fff"}, timeOver);
			$(this).children('.seta').stop(true, true).animate({"marginRight": 15}, {duration: timeOver, easing:"easeInOutQuart"});
		},
		function()
		{
			$(this).children('.text').stop(true, true).animate({"color": "#666"}, timeOver);
			$(this).children('.seta').stop(true, true).animate({"marginRight": 10}, {duration: timeOver, easing:"easeInOutQuart"});
		}
	);
	$('.navprev').click(
		function()
		{
			timer=0;
			movePrev();
		}
	);
	
	$('.navnext').hover(
		function()
		{
			timer=0;
			$(this).children('.text').stop(true, true).animate({"color": "#fff"}, timeOver);
			$(this).children('.seta').stop(true, true).animate({"marginLeft": 15}, {duration: timeOver, easing:"easeInOutQuart"});
		},
		function()
		{
			$(this).children('.text').stop(true, true).animate({"color": "#666"}, timeOver);
			$(this).children('.seta').stop(true, true).animate({"marginLeft": 10}, {duration: timeOver, easing:"easeInOutQuart"});
		}
	);
	$('.navnext').click(
		function()
		{
			timer=0;
			moveNext();
		}
	);
	
	$('.navUp').hover(
		function()
		{
			$(this).children('img').stop(true, true).animate({"top": 10}, {duration: timeOver, easing:"easeInOutQuart"});
		},
		function()
		{
			$(this).children('img').stop(true, true).animate({"top": 12}, {duration: timeOver, easing:"easeInOutQuart"});
		}
	);
	$('.navUp').click(
		function()
		{
			var id = String($(this).parent('.slide').attr("ID")).replace("slide_", "");
			id = Math.floor(id);
			moveUp(id);
		}
	);
	
	$('.navDn').hover(
		function()
		{
			$(this).children('img').stop(true, true).animate({"top": 18}, {duration: timeOver, easing:"easeInOutQuart"});
		},
		function()
		{
			$(this).children('img').stop(true, true).animate({"top": 16}, {duration: timeOver, easing:"easeInOutQuart"});
		}
	);
	$('.navDn').click(
		function()
		{
			var id = String($(this).parent('.slide').attr("ID")).replace("slide_", "");
			id = Math.floor(id);
			moveDown(id);
		}
	);
	
	$('.slideimg').click(
		function()
		{
			var id = $(this).attr("alt");
			var idd = $(this).attr("ID");
			if(slide[id][0]==false)
			{
				zoomInPhoto(id, idd, 400);
			}else
			{
				zoomOutPhoto(id, idd, 400);
			}
		}
	);
	$( ".scrollDrag" ).draggable({ 
		containment:'parent', 
			   axis:'x',
			   drag: function(event, ui) 
			   {
				   	var pos = ui.position.left / barWidth;
				  	widthW = getWindowWidth();
					widthW = (widthW<1000) ? 1000 : widthW;
					$('.contanerSlides').stop(true, true).animate({"left": - (pos * ((slideLength-1) * widthW))}, 0);
			   },
			   stop: function(event, ui) 
			   {
				   var pos = ui.position.left / barWidth;
				   pos = Math.round(pos * (slideLength-1));
				   moveContainer(pos, 800);
			   }
	});
	$('.containerGeral').mousemove(function(event) {
		if(overNav==false)
		{
			$('.navbar').stop(true, true).fadeIn();
			$('.assinatura').stop(true, true).fadeIn();
			timer=0;
			moved=true;
			setArrow(currentPos, 500);
		}else
		{
			timer=0;
			moved=false;
		}
		$(".slidecontent iframe").css({"pointer-events": "auto"});
	});
	
	/*var $iframe = $('.movieframe');
	$iframe.load(function()
	{
	  	var $body = $iframe.contents().find(document);
	 	$body.mousemove(function(event) {
			alert("xx");
			if(moved==false)
			{
				$('.navbar').stop(true, true).fadeIn();
				moved=true;
				timer=0;
			}
		});
	});*/
	
	openLoader(500);
	for (var i = 0 ; i < totalImage ; i++)
	{
		var imgs = loadImg(images[i], i, imgLoaded);
	}
	
	posicionaContainer();
	timedCount();
});

function setImg(id, url)
{
	images[Math.floor(id)] = url;
	totalImage++;
}
function setSlide(nr)
{
	slide[nr] = [false, 0, 0];
}

function setSlideLength(nr)
{
	slideLength = Math.floor(nr);
}

function setContentSlideLength(nr, length)
{
	slide[Math.floor(nr)][1] = length;
}

function timedCount()
{	
	if(timer>=200)
	{
		$(".slidecontent iframe").css({"pointer-events": "none"});
		$('.navbar').stop(true, true).fadeOut();
		$('.navUp').stop(true, true).fadeOut();
		$('.navDn').stop(true, true).fadeOut();
		$('.assinatura').stop(true, true).fadeOut();
		moved=false;
		timer=0;
	}
	if(moved==true)
	{
		timer++;
	}
	if(timer_is_on==true)
	{
		moveZoom(currentPos);	
	}
	t = setTimeout("timedCount()", 1);
}

function moveUp(id)
{
	var pos = slide[id][2] - 1;
	pos = (pos<0) ? 0 : pos;
	moveContent(id, pos, 800, true);
}

function moveDown(id)
{
	var pos = slide[id][2] + 1;
	pos = (pos>slide[id][1]-1) ? slide[id][1]-1 : pos;
	moveContent(id, pos, 800, true);
}

function moveContent(id, pos, time, zoom)
{
	$('#slideContainer_' + id).stop(true, true).animate({"top": - (pos * heightW)}, {duration: time, easing:"easeInOutQuart"});
	slide[id][2] = pos;
	setArrow(id, 300);
}

function setArrow(id, time)
{
	if(moved==true||overNav==true)
	{
		if(slide[id][2]>0)
		{
			$('#slide_' + id).children('.navUp').stop(true, true).fadeIn(time);
		}else
		{
			$('#slide_' + id).children('.navUp').stop(true, true).fadeOut(time);
		}
		
		if(slide[id][2]<slide[id][1]-1)
		{
			$('#slide_' + id).children('.navDn').stop(true, true).fadeIn(time);
		}else
		{
			$('#slide_' + id).children('.navDn').stop(true, true).fadeOut(time);	
		}
	}
}

function animationComplete()
{
	if(zoom==true)
	{
		for (var i=0; i<slideLength; i++)
		{
			for (var j=0; j<slide[i][1]; j++)
			{
				if(slide[i][0] == true)
				{
					zoomOutPhoto(i, j, 0);
				}
			}
		}
	}
}

function movePrev()
{
	var pos = currentPos - 1;
	pos = (pos<0) ? 0 : pos;
	moveContainer(pos, 800, true);
}

function moveNext()
{
	var pos = currentPos + 1;
	pos = (pos>slideLength-1) ? slideLength-1 : pos;
	moveContainer(pos, 800, true);
}

function moveContainer(pos, time, zoom)
{
	timer_is_on = false;
	setArrow(pos, 0);
	var cp = currentPos
	$('.contanerSlides').stop(true, true).animate({"left": - (pos * widthW)}, {duration: time, easing:"easeInOutQuart", complete:function(){
		if($('#slide_' + cp).children('.slideContainer').children('.subSlide').attr("alt")=="video")
		{
			var src = $('#slide_' + cp + ' .slidecontent iframe').attr("src");
			$('#slide_' + cp + ' .slidecontent iframe').attr("src","");
			$('#slide_' + cp + ' .slidecontent iframe').attr("src",src);
		}	
		for (var i=0; i<slideLength; i++)
		{
			for (var j=0; j<slide[i][1]; j++)
			{
				zoomOutPhoto(i, j, 0);
			}
		}
	}});
	var posX = String($( ".scrollDrag" ).css("left")).replace("px", "");
	posX = (pos/(slideLength-1)) * barWidth;
	$('.scrollDrag').stop(true, true).animate({"left": posX}, {duration: time, easing:"easeInOutQuart"});
	currentPos = pos;
}

function moveZoom(id)
{
	var subid = slide[id][2];
	var yy = mouseY / heightW ;
	var ht = Math.floor($('#slide_' + id).children('#slideContainer_' + id).children('#subSlide_' + subid).children('.slidecontent').css("height").replace("px", ""));
	var py = yy * (ht - heightW);
	
	var xx = mouseX / widthW ;
	var wt = Math.floor($('#slide_' + id).children('#slideContainer_' + id).children('#subSlide_' + subid).children('.slidecontent').css("width").replace("px", ""));
	var px = xx * (wt - widthW);
	
	$('#slide_' + id).children('#slideContainer_' + id).children('#subSlide_' + subid).children('.slidecontent').css({"top": -py, "left": -px });
}

function zoomInPhoto(id, subid, time)
{
	var size = String($('#slide_' + id).children('#slideContainer_' + id).children('#subSlide_' + subid).children('.slidecontent').attr("name")).split(";");
	var w = size[0];
	var h = size[1];
	var ww = 0;
	var hh = 0;
	var xx = 0;
	var yy = 0;
		
	var prop = widthW/heightW;
	var propimg = w/h;
	if(prop<=1)
	{
		hh = heightW;
		ww = heightW * propimg;
	}else{
		ww = widthW;
		hh = widthW / propimg;
	}
	xx = (widthW - ww) / 2;
	yy = (heightW - hh) / 2;
	
	$('#slide_' + id).children('#slideContainer_' + id).children('#subSlide_' + subid).children('.slidecontent').stop(true, true).animate({"top": yy, "left": xx, "height": hh, "width": ww}, {duration: time, easing:"easeInOutQuint"});
	
	slide[id][0] = true;
	timer_is_on = true;
}

function zoomOutPhoto(id, subid, time)
{
	var size = String($('#slide_' + id).children('#slideContainer_' + id).children('#subSlide_' + subid).children('.slidecontent').attr("name")).split(";");
	var w = size[0];
	var h = size[1];
	var prop = widthW/heightW;
	var propimg = w/h;
	var ww = 0;
	var hh = 0;
	var xx = 0;
	var yy = 0;
	if(prop>=propimg)
	{
		hh = heightW;
		ww = heightW * propimg;
	}else{
		ww = widthW;
		hh = widthW / propimg;
	}
	xx = (widthW - ww) / 2;
	yy = (heightW - hh) / 2;
	$('#slide_' + id).children('#slideContainer_' + id).children('#subSlide_' + subid).children('.slidecontent').stop(true, true).animate({"top": yy, "left": xx, "height": hh, "width": ww}, {duration: time, easing:"easeInOutQuint"});
	
	slide[id][0] = false;
	timer_is_on = false;
}

function openLoader(time)
{	
	$('.loaderBarD').animate({"width": 0}, 0);
	$('.loaderSite').fadeIn(time);
}

function closeLoader(time)
{
	$('.containerGeral').fadeIn(time);
	$('.loaderSite').fadeOut(time);	
}

function imgLoaded(e)
{	
	var img = (BrowserDetect.browser!="Explorer") ? this : e;
	imgCount++;
	var pct = imgCount / totalImage * 100;
	$('.loaderBarD').animate({"width": pct + "%"}, 500);
	if(imgCount>=totalImage)
	{
		window.setTimeout(function() 
		{
			closeLoader(800);
		}, totalImage * 500);
	}
}

function posicionaContainer()
{		
	widthW = getWindowWidth();
	widthW = (widthW<1000) ? 1000 : widthW;	
	heightW = getWindowHeight();
	heightW = (heightW<600) ? 600 : heightW;
	$('.containerGeral').css({"width": widthW, "height": heightW});
	$('.backgroundSite').css({"width": widthW, "height": heightW});
	$('.navUpD').css({"left": (widthW - 60) / 2});
	$('.navbar').css({"left": (widthW - 900) / 2});
	
	for (var i=0; i<slideLength; i++)
	{
		$('#slide_' + i).css({"left": widthW * i, "height": heightW, "width": widthW});
		for (var j=0; j<slide[i][1]; j++)
		{
			$('#slide_' + i).children('#slideContainer_' + i).children('#subSlide_' + j).css({"top": heightW * j, "height": heightW, "width": widthW});
			if(slide[i][0] == false)
			{
				zoomOutPhoto(i, j, 0);
			}else
			{
				zoomInPhoto(i, j, 0);
			}
		}
	}
	
	moveContainer(currentPos, 0, false);
}

function resize()
{	
	posicionaContainer();
}
