// Global variabels
// ----------------
var ABS_URL = "http://www.seagullsfly.com/en/";
//var ABS_URL = "http://192.168.0.187/seagulls/";
var deviceAgent;
var agentID;
var isSliding=false;
var arrowClicks=0;
var totalThumbs=0;
var spacex=9;
var percent=0;
var xscroll;
var project_index=0;
var project_name="";
var listImagens;
var lastShortHome;

// Functions to handle scroller events on desktop browsers
// -------------------------------------------------------
function handleSliderChange(e, ui)
{
	percent = ui.value;
	var maxScroll = $("#thumbs-scroll").attr("scrollWidth") - $("#thumbs-scroll").width();
	$("#thumbs-scroll").animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);
	isSliding=false;
}

function handleSliderSlide(e, ui)
{
	percent = ui.value;
	var maxScroll = $("#thumbs-scroll").attr("scrollWidth") - $("#thumbs-scroll").width();
	$("#thumbs-scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
	isSliding=true;
}

function setCarretEvents()
{
	var carret = $("a[class*='ui-slider-handle']");
	var over=false;
	spacex = (267/(totalThumbs))*.5;
	
	carret.append('<img id="carret" src="'+ABS_URL+'images/slider.gif" width="29" height="11" />');
	
	//carret.mouseover(function() { if(!over) { $('#carret').animate({width: 25, height: 9, top: 5, left:20 }, 200, function(){over=true;}); } });
	//carret.mouseout(function() { if(over && !isSliding) { $('#carret').animate({width: 12, height: 4, top: 8, left:percent>0?30:20 }, 200, function(){over=false;}); } });
}

function findScrollPos() {
	checkMobileDev();
	if(agentID) return;
	
	var links = ['motion-effect','special-effects','print-and-outdoor','concept-art','web','people'];
	var local = window.location.toString();
	for (var i = links.length - 1; i >= 0; i--){
		ind = local.indexOf(links[i]);
		if(ind>0) {
			i=-1;
		}
	};
	sliceurl = local.slice(ind);
	project = sliceurl.split('/')[1];
	node = $("div > a[href*='"+project+"']");
	if(node.attr('id')) {
		node_index = parseInt(node.attr('id').split('_')[1]);
		arrowClicks = node_index - 1;
	}
	tx = arrowClicks*spacex;
	if(tx>100) {
		tx = 100;
		arrowClicks--;
	}
	if(tx<0) tx=0;
	adujstTx(tx);
}

function scrollPrev()
{
	if(agentID) {
		arrowClicks--;
		tx = -267*arrowClicks;
		if(tx>0) {
			arrowClicks++;
			return;
		}
		xscroll.scrollTo(tx,0);
		return;
	}
	
	arrowClicks--;
	tx = arrowClicks*spacex;
	
	if(tx<0) {
		arrowClicks++;	
		return;
	}
	adujstTx(tx);
}

function scrollNext()
{
	if(agentID) {
		arrowClicks++;
		tx = -267*arrowClicks;
		if(Math.abs(tx)>$("#thumbs-holder").width()-(267*3)) {
			arrowClicks--;
			return;
		}
		xscroll.scrollTo(tx,0);
		return;
	}
	
	arrowClicks++;
	tx = arrowClicks*spacex;
	
	//console.log(arrowClicks);
	
	if(tx>100) {
		tx = 100;
		arrowClicks--;
	}
	adujstTx(tx);
	//marginLeft
}

function adujstTx(p) {
	var carret = $("a[class*='ui-slider-handle']");
	percent = p;
	carret.animate({left: p+'%' }, 1000);
	var maxScroll = $("#thumbs-scroll").attr("scrollWidth") - $("#thumbs-scroll").width();
	$("#thumbs-scroll").animate({scrollLeft: p * (maxScroll / 100) }, 1000);
	$('#carret').animate({left:percent>0?30:20 }, 200);
}

function checkMenu() {
	var el = $('.menu-selected');
	if(el) { el.attr('style',"background:url("+ABS_URL+"images/menu_principal_"+el.attr('id')+"_over.png) center right no-repeat;"); }
}

function checkListMenu() {
	var elt = $('.t-selected');
	if(elt.attr('id')) {
		var ii = elt.attr('id').substr(-1);
		elt.attr('style',"background:url("+ABS_URL+"images/tab_0"+ii+"_over.gif) center right no-repeat;"); 
	}
}

// Functions to handle scroller events on mobile browsers
// ------------------------------------------------------
function setMobileEvents() {
	xscroll = new iScroll('thumbs-holder', {vScrollBar: false, hScrollBar: true, scrollbarClass:'custom-scrollbar' });
}

function processVideoDiv(data){
	checkMobileDev();
	if(!agentID) {
		$(".video_portfolio").html('<div id="video-player"><div id="flash_content"></div></div>');
		var flvars = {};
		flvars.url =  data.url + ".f4v";
		flvars.poster =  data.poster;
		swfobject.embedSWF(data.fp, "flash_content", "765px", "433px", (swfobject.getQueryParamValue("detectflash")=="false" ? "0" : "10.0.0"), "expressInstall.swf", flvars, {scale:"noscale", menu:"false", allowScriptAccess:"always", allowFullScreen:"true"}, {id:"flash_content"});
	}
	else {
		var h5vars = {};
		h5vars.url =  data.url + ".mp4";
		h5vars.poster =  data.poster;
		//video_html = '<div id="video-player"><div id="video-h5"><video id="video" poster="'+h5vars.poster+'" controls="controls" autoplay="autoplay" width=765 height=433><source src="'+h5vars.url+'" type="video/mp4" /></video></div></div>';
		video_html = '<div id="video-player"><div id="video-h5"><video id="video" poster="'+h5vars.poster+'" src="'+h5vars.url+'" controls="controls" autoplay="autoplay" width=765 height=433></video></div></div>';
		$(".video_portfolio").html(video_html);
	}
}

function loadZoomPicture(tgt) {
	window.open(tgt);
}

function getEL(id) {
	return document.getElementById(id);
}

function checkMobileDev() {
	deviceAgent = navigator.userAgent.toLowerCase();
	agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
}

function overHome(tgt) {
	var id = tgt.id.split('_')[1];
	$("#shortcut_home_"+id).animate({opacity: 0.7}, 200);
}

function outHome(tgt) {
	var id = tgt.id.split('_')[1];
	$("#shortcut_home_"+id).animate({opacity: 0}, 200);
}

function overThumb(tgt) {
	var id = tgt.id.split('_')[1];
	$("#project_interna_"+id).animate({opacity: 0.55}, 200);
	$("#project_details_"+id).animate({opacity: 1}, 200);
	
}

function outThumb(tgt) {
	var id = tgt.id.split('_')[1];
	$("#project_interna_"+id).animate({opacity: 0}, 200);
	$("#project_details_"+id).animate({opacity: 0}, 200);
}

function overPeople(tgt) {
	var id = tgt.id.split('_')[1];
	$("#thb_"+id).animate({width:'115%', height:'115%', top:'-20', left:'-20'}, 200);
	$("#nome_"+id).addClass('h5nome');
}

function outPeople(tgt) {
	var id = tgt.id.split('_')[1];
	$("#thb_"+id).animate({width:'100%', height:'100%', top:'0', left:'0'}, 200);
	$("#nome_"+id).removeClass('h5nome');
}

// Dispatcher for document ready
// -----------------------------

$(document).ready(function(){
	var imgs = $('img[class=item-thumb]');
	var fw=0;
	
	checkMobileDev();
	
	totalThumbs = imgs.length;
	for(i=0;i<imgs.length;i++) {
		fw+=(imgs[i].width+2);
	}
	
	$("#thumbs-holder").width(fw);
	if(imgs.length>3) {
		if(agentID) { 
			setMobileEvents(); 
		}
		else { 
			$("#content-slider").slider({
			  animate: true,
			  change: handleSliderChange,
			  slide: handleSliderSlide
			});
			setCarretEvents();
		}
	}
	else {
		$('#navbar').empty();
	}
	checkMenu();
	findScrollPos();
	//$('#p_0').click();
});

