var projectVimeo;
var projectTemplate;
var projectVideos;
var projectAudios;
var projectSelectedItem;
var projectSelectedAudio;
function initProject(colors,prev,next,files){
	changeColors(colors);
	prelaoderVisibility(false);
	$(".title1 span").css({color:COLOR_1});
	$(".title2 span").css({color:COLOR_1});
	projectCredits();
	projectNavControl(prev,next);
	projectCreateFileList(files);
	projectControlCols();
	projectScreenResolution();
	$("#pageContent").stop(true).css({visibility:"visible",opacity:0}).delay(0).animate({opacity:1},300,function(){  
		ahrefControlLinks($("#projectCredits"));
		ahrefControlLinks($("#projectTextCol1"));
		ahrefControlLinks($("#projectTextCol2"));
	});
}
function projectStillFadeIn(){
	//alert("projectStillFadeIn");
	$("#videoStill").css({display:"block",opacity:0}).animate({opacity:1},200);
}
function projectCredits(){
	$("#projectCredits span").css({color:COLOR_1});
	$("#projectCredits span").css({color:COLOR_1});
	ahrefApplyColorAndRollovers($("#projectCredits"),COLOR_1,COLOR_MENU);
}
function projectNavControl(prev,next){
	/*
	var visibility = ($.address.pathNames()[0] == "curated") ? "visible" : "hidden";
	$("#navRight").css({visibility:visibility});
	$("#navLeft").css({visibility:visibility});
	//asigno eventos para pasar de un project a otro con navegación lineal
	navRightClickEvent = projectChangeProject;
	navRightClickParam = next;
	navLeftClickEvent = projectChangeProject;
	navLeftClickParam = prev;
	*/
}
function projectChangeProject(url){
	$.address.path(url);
}
function projectCreateFileList(str){
	if(str != ""){
		projectVideos = new Array();
		projectAudios = new Array();
		//src,ext,name,vimeoid;
		var files = str.split(";");
		var file;
		for(var i=0;i<files.length;i++){
			file = files[i].split(",");
			if(file[1].toLowerCase() == "mp3"){
				projectAudios.push({src:file[0],name:file[2]});
			}else{
				projectVideos.push({still:file[0],name:file[2],videoId:file[3]});
			}
		}
		projectSelectedItem = null;
		$("#projectMediaList").css({color:COLOR_1});
		$("#projectMediaList span").css({color:COLOR_1,cursor:"pointer"});
		$("#projectMediaList span").mouseover(function(){
			if($(this).attr("id") != projectSelectedItem){
				$(this).stop(true).animate({color:COLOR_2},150);
			}
		});
		$("#projectMediaList span").mouseout(function(){
			if($(this).attr("id") != projectSelectedItem){
				$(this).stop(true).animate({color:COLOR_1},150);
			}
		});
		$("#projectMediaLisTitle").unbind("mouseover");
		$("#projectMediaLisTitle").unbind("mouseout");
		$("#projectMediaLisTitle").css({cursor:"auto"});
		$("#videoStill").css({cursor:"pointer"});
		$("#videoStill").mouseover(function(){
			$("#videoPlayIcon").stop(true).animate({opacity:0.5});
		});
		$("#videoStill").mouseout(function(){
			$("#videoPlayIcon").stop(true).animate({opacity:1});
		});
		$("#videoStill").click(function(){
			projectAddVideoPlayer();
		});
		if(projectAudios.length > 0){
			projectCreateAudioList();	
		}
		if(projectVideos.length > 0){
			$("#playMediaItem0").mouseover();
			projectChangeVideo(0);
		}else if(projectAudios.length > 0){
			projectOpenAudioList();
		}
	}
}
function projectChangeVideo(index,target){
	if($(target).css("cursor") == "pointer" || target == undefined || detectMobile()){
		projectVimeo = projectVideos[index].videoId;
		var html = '<img src="'+projectVideos[index].still+'" onload="projectStillFadeIn()"/>';
		html += '<img src="./assets/videoPlayIcon.png" id="videoPlayIcon" />';
		$("#videoStill").html(html);
		$("#videoPlayer").html('');
		if(projectSelectedItem != null){
			var current = "#"+projectSelectedItem;
			projectSelectedItem = null;
			$(current).css({cursor:"pointer"});
			$(current).mouseout();
		}
		projectSelectedItem = "playMediaItem"+index;
		$("#playMediaItem"+index).css({cursor:"auto"});
		//
		$("#videoStill").css({display:"none"});
		$("#audioPlayer").stop(true).css({display:"none"});
		$("#audioList").css({display:"none"});
		$("#jquery_jplayer_1").jPlayer("stop");
		/*
		$("#videoPlayer").stop(true).css({display:"block",opacity:0}).animate({opacity:1},200);
		$("#videoStill").stop(true).css({display:"block",opacity:0}).animate({opacity:1},200);
		$("#audioPlayer").stop(true).css({display:"none"});
		$("#audioList").stop(true).css({display:"none"});
		*/
	}
}
function projectAddVideoPlayer(){
	$("#videoStill").html('');
	//alert("projectVimeo="+projectVimeo);
	var video = projectVimeo;
	video = video.split("http://vimeo.com/").join("");
	video = video.split("http://www.vimeo.com/").join("");
	$("#videoPlayer").html('<iframe id="vimeoplayer" src="http://player.vimeo.com/video/'+video+'?title=0&autoplay=1&amp;byline=0&amp;portrait=0&amp;color=ffffff" width="640" height="360" frameborder="0"></iframe>');
}
function projectCreateAudioList(){
	var top = $("#videoPlayer").position().top+86;
	var left = $("#videoPlayer").position().left;
	$("#audioPlayer").css({top:top,left:left,backgroundColor:COLOR_1});
	//audio player events
	$("div.jp-seek-bar").css({backgroundColor:COLOR_1});
	$("div.jp-play-bar").css({backgroundColor:COLOR_2});
	$("div.jp-audio div.jp-type-single a.jp-play").css({backgroundColor:COLOR_1});
	$("div.jp-audio div.jp-type-single a.jp-pause").css({backgroundColor:COLOR_1});
	$("div.jp-audio div.jp-type-single a.jp-play").mouseover(function(){
		$(this).stop(true).animate({backgroundColor:COLOR_2},300);
	});
	$("div.jp-audio div.jp-type-single a.jp-play").mouseout(function(){
		$(this).stop(true).animate({backgroundColor:COLOR_1},300);
	});
	$("div.jp-audio div.jp-type-single a.jp-pause").mouseover(function(){
		$(this).stop(true).animate({backgroundColor:COLOR_2},300);
	});
	$("div.jp-audio div.jp-type-single a.jp-pause").mouseout(function(){
		$(this).stop(true).animate({backgroundColor:COLOR_1},300);
	});
	$("#jquery_jplayer_1").jPlayer({
		ready: function () {
			
		},
		ended: function (event) {
			//$(this).jPlayer("play");
		},
		swfPath: "js",
		supplied: "mp3, m4a, oga"
	});
	//
	top += 80;
	$("#audioList").css({top:top,left:left,color:COLOR_1});
	var html = '';
	var num;
	for(var i=0;i<projectAudios.length;i++){
		num = (i<9) ? '0'+(i+1) : (i+1);
		html += '<div id="playAudioItem'+i+'" onclick="projectOpenAudio('+i+')">'+num+' '+projectAudios[i].name+'</div>';
	}
	$("#audioList").html(html);	
	projectSelectedAudio = null;
	$("#audioList div").css({cursor:"pointer"});
	$("#audioList div").mouseover(function(){
		if($(this).attr("id") != projectSelectedAudio){
			$(this).stop(true).animate({color:COLOR_2},150);
		}
	});
	$("#audioList div").mouseout(function(){
		if($(this).attr("id") != projectSelectedAudio){
			$(this).stop(true).animate({color:COLOR_1},150);
		}
	});
}
function projectOpenAudioList(index,target){
	if($(target).css("cursor") == "pointer" || target == undefined){
		//alert("projectOpenAudioList="+index);
		if(projectSelectedItem != null){
			var current = "#"+projectSelectedItem;
			projectSelectedItem = null;
			$(current).css({cursor:"pointer"});
			$(current).mouseout();
		}
		projectSelectedItem = "playMediaItem"+index;
		$("#playMediaItem"+index).css({cursor:"auto"});
		//
		$("#videoStill").html('');
		$("#videoPlayer").html('');
		$("#videoStill").css({display:"none"});
		$("#audioPlayer").stop(true).css({display:"none"});
		$("#audioList").css({display:"block"});
		//
		$("#playAudioItem0").mouseover();
		projectOpenAudio(0);
		setTimeout(function(){$("#jquery_jplayer_1").jPlayer("pause")},10);
	}
}
function projectOpenAudio(index){
	if(projectSelectedAudio != null){
		var current = "#"+projectSelectedAudio;
		projectSelectedAudio = null;
		$(current).css({cursor:"pointer"}).mouseout();
	}
	projectSelectedAudio = "playAudioItem"+index;
	$("#"+projectSelectedAudio).css({cursor:"auto"});
	$("#audioPlayer").stop(true).css({display:"block",opacity:0}).delay(100).animate({opacity:1},300);
	//$("#jquery_jplayer_1").jPlayer("setMedia",{mp3:projectAudios[index].src});
	var mp3 = projectAudios[index].src;
	var oga = projectAudios[index].src.split(".mp3").join(".ogg");
	var m4a = projectAudios[index].src.split(".mp3").join(".m4a");
	$("#jquery_jplayer_1").jPlayer("setMedia",{mp3:mp3,oga:oga,m4a:m4a}).jPlayer("play");
}
function addProjectShare(){
	var html = '<div id="projectShare"><span id="projectShareTxt">Share </span><span id="projectEmailButton">Email</span> / '+getSocialNetworkLinks()+'</div>';
	var align = ($("#projectTextCol2").html() == "") ? "right" : "left";
	$("#projectTextCol2").append((align == "right") ? html : "<br/><br/>"+html);
	$("#projectShare").css({color:COLOR_1,textAlign:align});
	$("#projectShareTxt").css({color:COLOR_2});
	$("#projectEmailButton").css({cursor:"pointer"});
	$("#projectEmailButton").mouseover(function(){
		$(this).stop(true).animate({color:COLOR_2},150);
	});
	$("#projectEmailButton").mouseout(function(){
		$(this).stop(true).animate({color:COLOR_1},150);
	});
	$("#projectEmailButton").click(function(){
		addEmailForm();
	});
}
function projectControlCols(){
	$("#projectTextCol1").css({color:COLOR_1});
	$("#projectTextCol2").css({color:COLOR_1});
	txtCleanBR($("#projectTextCol1"));
	txtCleanBR($("#projectTextCol2"));
	projectTemplate = 1;
	var bottom = Number(String($("#projectTextCol1").css("bottom")).split("px").join(""));
	//if($("#projectTextCol1").css("bottom") == "50px"){
	if(bottom <= 50){
		projectTemplate = ($("#projectTextCol1").position().left < 100) ? 3 : 2;
		var top;
		if($("#projectTextCol1").height() > $("#projectTextCol2").height()){
			top = $("#projectTextCol1").position().top;
		}else{
			top = $("#projectTextCol2").position().top;
		}
		$("#projectTextCol1").css({bottom:"auto",top:top});
		$("#projectTextCol2").css({bottom:"auto",top:top});
		if($("#projectTextCol2").html() == ""){
			$("#projectTextCol2").css({top:(top+$("#projectTextCol1").height()-15)});
		}
	}
	addProjectShare();
	ahrefApplyColorAndRollovers($("#projectTextCol1"),COLOR_1,COLOR_2);
	ahrefApplyColorAndRollovers($("#projectTextCol2"),COLOR_1,COLOR_2);
}
function projectScreenResolution(){
	if(!detectMobile()){
		var resolution = screen.width;
		if(resolution < 1380){
			var margin = 10;
			var offsetX = 1380-resolution-margin;
			var width = resolution;
			var objects;
			var target;
			var left;
			$("#pageContent").css({width:width});
			if(projectTemplate == 1 || projectTemplate == 2){
				objects = ["projectImg","projectTextCol1","projectTextCol2","projectMediaList","videoStill","videoPlayer","audioPlayer"];	
				for(var i=0;i<objects.length;i++){
					target = "#"+objects[i];
					//left = $(target).position().left-offsetX;
					left = Number($(target).css("left").split("px").join(""))-offsetX;
					$(target).css({left:left});
				}
			}
			if(projectTemplate == 3){
				var imgWidth = $("#projectImg").width()-offsetX;
				$("#projectImg").css({width:imgWidth});
				$("#projectImg img").css({marginLeft:-offsetX})
			}
		}
	}
}
