var projectScenesHeight;
var projectStillsHeight;
var projectBoardsHeight;
var projectVideoListInterval = null;
var projectVideosData;
var projectListButtonSelected;
var projectStillsData;
var projectScenesData;
var projectBoardsData;
var projectStillsTarget;
var projectBg;
var projectStillsIndex;
var projectStillsTimeout = null;
var projectKeys = false;
var projectInPlaylist = false;
function initProjectWork(palette,stillsH,scenesH,boardsH,videosDat,stillsStr,scenesStr,boardsStr,pbg){
	//
	var pn = $.address.pathNames();
	var url = "/"+pn[0]+"/"+pn[1];
	if(pn.length == 3){
		var menuItem = $("#menu").children("#"+pn[0].toLowerCase());
		menuItem.attr("href",url);
		menuItem.attr("rel","address:"+url);
	}
	//
	prelaoderVisibility(false);
	changeColorPalette(palette);
	headerlineVisibility(true);
	projectScenesHeight = scenesH;
	projectStillsHeight = stillsH;
	projectBoardsHeight = boardsH;
	projectVideosData = videosDat.split(",");
	projectListButtonSelected = null;
	projectStillsData = stillsStr.split(",");
	projectScenesData = scenesStr.split(",");
	projectBoardsData = boardsStr.split(",");
	projectBg = pbg;
	projectInPlaylist = slugInPlaylist($("#fav_icon").attr("url"));
	/*
	$(".project_img_group #p_lines").tintIMG(currentPalette.lines[0]);
	$(".project_img_group #collapse_button #circle").tintIMG(currentPalette.icons[0]);
	$(".project_img_group #collapse_button #minus").tintIMG(currentPalette.icons[1]);
	$(".project_img_group #collapse_button #plus").tintIMG(currentPalette.icons[1]);
	*/
	$(".project_display").css({backgroundColor:currentPalette.rectangleAndProjectName[0]});
	$(".project_display #txt1").css({color:currentPalette.rectangleAndProjectName[1]});
	$(".project_display #txt2").css({color:currentPalette.rectangleAndProjectName[2]});
	$(".project_display #txt3").css({color:currentPalette.rectangleAndProjectName[3]});
	$(".p_display_icons").mouseover(function(){
		var id = $(this).attr("id");
		var txt;
		switch (id){
			case "arr_left":
			txt = "Previous";
			break;
			case "arr_right":
			txt = "Next";
			break;
			case "close":
			txt = "Close";
			break;
		}
		$(this).showTooltip(txt,-5,-5);
		$(this).fadeTintIMG(currentPalette.rectangleAndProjectName[2],5);
	});
	$(".p_display_icons").mouseout(function(){
		hideTooltip();
		$(this).fadeTintIMG(currentPalette.rectangleAndProjectName[1],5);
	});
	$(".p_display_icons").click(function(){
		hideTooltip();
		var id = $(this).attr("id");
		if(id == "close"){
			projectClose();
		}else{
			//son las flechas next/prev
			projectChangeNextPrev($(this).attr("url"),id == "arr_left" ? -1 : 1);	
		}
	});
	if(($("#arr_left").attr("url") == "null" && playlist.length < 2 && pn[0] == "playlist") || (pn[0] != "playlist" && $("#arr_left").attr("url") == $("#arr_right").attr("url"))){
		$("#arr_left").css({display:"none"});
		$("#arr_right").css({display:"none"});
	}
	//text
	$(".project_description").css({color:currentPalette.text[0]});
	$(".project_description a").css({color:currentPalette.highlight[0]});
	$(".project_description a").mouseover(function(){
		$(this).stop(true).animate({color:currentPalette.text[0]},110);
	});
	$(".project_description a").mouseout(function(){
		$(this).stop(true).animate({color:currentPalette.highlight[0]},110);
	});
	$(".p_description_icons").mouseover(function(){
		var id = $(this).attr("id");
		var color = projectInPlaylist && id == "fav_icon" ? currentPalette.icons[0] : currentPalette.icons[1];
		var txt;
		switch (id){
			case "fav_icon":
			txt = projectInPlaylist && id == "fav_icon" ? "Remove From Playlist" : "Playlist"
			break;
			case "sh_icon":
			txt = "Share";
			break;
			case "facebook_icon": 
			txt = "Facebook";
			break;
			case "twitter_icon": 
			txt = "Twitter";
			break;
			case "zip_icon": 
			txt = "Download";
			break;
		}
		$(this).data("txt",txt);
		$(this).showTooltip(txt,-5,-5);
		$(this).fadeTintIMG(color,5);
	});
	$(".p_description_icons").mouseout(function(){
		hideTooltip();
		var id = $(this).attr("id");
		var color = (projectInPlaylist && id == "fav_icon") ? currentPalette.icons[1] : currentPalette.icons[0];
		$(this).fadeTintIMG(color,5);
	});
	$(".p_description_icons").click(function(){
		var id = $(this).attr("id");
		switch (id){
			case "fav_icon":
				var url = $(this).attr("url");
				target = $(this).attr("id");
				addToPlaylist(url,target);
				projectInPlaylist = projectInPlaylist ? false : true;
				var txt = $(this).data("txt");
				var newtxt = (txt == "Playlist" || txt == "Removed") ? "Added" : "Removed";
				$(this).data("txt",newtxt);
				$(this).showTooltip(newtxt,-5,-5);
			break;
			case "sh_icon":
				var target = $("#content");
				var url = $("#project_content").attr("url_mail");
				var name = $("#txt2").html(); 
				var image = $("#project_main img").attr("src"); 
				var c1 = currentPalette.rectangleAndProjectName[2];
				var c2 = currentPalette.rectangleAndProjectName[1];
				var c3 = currentPalette.rectangleAndProjectName[0];
				target.addShareForm(726,96,478,315,c1,c2,c3,url,name,image,false,false);
				hideTooltip();
			break;
			case "facebook_icon": 
				var url = $("#project_content").attr("url_mail");
				$(this).shareSocialNetwork(url);
			break;
			case "twitter_icon": 
				var url = $("#project_content").attr("url_mail");
				$(this).shareSocialNetwork(url);
			break;
			case "zip_icon": 
				window.location = $(this).attr("zip");
			break;
		}
	});
	if($("#copy_icon").length > 0){
		$("#copy_icon").setClipboardData(($.address.baseURL()+"/#"+$.address.path()));
		$("#copy_icon").mouseover(function(){
			$(this).showTooltip("Copy Link",-10,-15);
			$(this).children("img").fadeTintIMG(currentPalette.icons[1],5);
		});
		$("#copy_icon").mouseout(function(){
			hideTooltip();
			$(this).children("img").fadeTintIMG(currentPalette.icons[0],5);
		});
		$("#copy_icon").mousedown(function(){
			$(this).showTooltip("Copied",-10,-15);
		});
	}
	//main
	if(videosDat == ""){
		//$("#project_main #img_main").css({display:"none"});
		$("#project_main #project_vimeo").html("").css({cursor:"auto"});
	}else{
		if(projectVideosData.length > 1){
			$("#project_main").mouseover(function(){
				if(projectVideoListInterval == null){
					MOUSE_X = $(this).offset().left;
					MOUSE_Y = $(this).offset().top;
					$("#project_video_list_bg").stop(true).css({display:"block",height:0,opacity:0.5}).animate({height:$("#project_video_list").outerHeight()},150,function(){
						$("#project_video_list").stop(true).css({visibility:"visible",opacity:0}).delay(25).animate({opacity:1},75);
					});
					projectVideoListInterval = setInterval(function(){
						if(!$("#project_main").hitTestPoint(MOUSE_X,MOUSE_Y)){
							$("#project_video_list").stop(true).css({visibility:"hidden"});
							$("#project_video_list_bg").stop(true).animate({opacity:0},120,function(){
								$(this).css({display:"none"});	
							});
							projectRemoveVideoListInterval();
						}
					},300);
				}
			});
		}
		$("#project_main #play_icon").click(function(){
			projectLoadVideo(projectVideosData[0],$("#project_video_list #button0"));
		});
	}
	//video list
	$("#project_video_list_bg").css({height:$("#project_video_list").outerHeight(),display:"none",backgroundColor:"#000000",opacity:0.5});
	$("#project_video_list span").css({cursor:"pointer",color:currentPalette.rectangleAndProjectName[2]});
	$("#project_video_list span").mouseover(function(){
		if(!$(this).data("selected")){
			$(this).stop(true).animate({color:currentPalette.rectangleAndProjectName[1]},100);
		}
	});
	$("#project_video_list span").mouseout(function(){
		if(!$(this).data("selected")){
			$(this).stop(true).animate({color:currentPalette.rectangleAndProjectName[2]},100);
		}
	});
	$("#project_video_list span").click(function(){
		if(!$(this).data("selected")){
			projectLoadVideo($(this).attr("video_id"),$(this));
		}
	});
	$("#project_video_list").css({visibility:"hidden"});
	//collapse buttons
	$(".project_img_group #txt").css({color:currentPalette.text[0]});
	$(".project_img_group #collapse_button #plus").css({visibility:"hidden"});
	$(".project_img_group #collapse_button").mouseover(function(){
		$(this).children("#circle").fadeTintIMG(currentPalette.icons[1],5);
		$(this).children("#plus").fadeTintIMG(currentPalette.icons[0],5);
		$(this).children("#minus").fadeTintIMG(currentPalette.icons[0],5);
	});
	$(".project_img_group #collapse_button").mouseout(function(){
		$(this).children("#circle").fadeTintIMG(currentPalette.icons[0],5);
		$(this).children("#plus").fadeTintIMG(currentPalette.icons[1],5);
		$(this).children("#minus").fadeTintIMG(currentPalette.icons[1],5);
	});
	$(".project_img_group #collapse_button").click(function(){
		var id = $(this).parent().attr("id");
		var height = $(this).parent().height();
		var maxHeight;
		switch(id){
			case "project_stills":
			  maxHeight = projectStillsHeight;
			  break;
			case "project_scenes":
			  maxHeight = projectScenesHeight;
			  break;
			case "project_boards":
			  maxHeight = projectBoardsHeight;
			  break;
			default:
			  maxHeight = projectStillsHeight;
		}
		var h = (height > 62) ? 62 : maxHeight;
		$(this).parent().stop(true).animate({height:h},300);
		$(this).children("#plus").css({visibility:(h == 62 ? "visible" : "hidden")});
		$(this).children("#minus").css({visibility:(h > 62 ? "visible" : "hidden")});
	});
	//stills and behind the scenes
	$(".box_still div").css({opacity:0,backgroundColor:"#000000"});
	$(".box_still #still_icon").css({opacity:0});
	$(".box_still").mouseover(function(){
		$(this).children("div").stop(true).animate({opacity:0.5},100);
		$(this).children("#still_icon").stop(true).animate({opacity:1},100);
	});
	$(".box_still").mouseout(function(){
		$(this).children("div").stop(true).animate({opacity:0},100);
		$(this).children("#still_icon").stop(true).animate({opacity:0},100);
	});
	$(".box_still").click(function(){
		var num = Number($(this).attr("num"));
		var type = $(this).attr("type");
		switch (type){
			case "stills":
			projectStillsTarget = projectStillsData;
			break;
			case "scenes":
			projectStillsTarget = projectScenesData;
			break;
			case "boards": 
			projectStillsTarget = projectBoardsData;
			break;
			default: projectStillsTarget = projectStillsData;
		}
		projectShowStills(num);
	});
	if(stillsH == 0){
		$("#project_stills").css({display:"none"});
	}
	if(scenesH == 0){
		$("#project_scenes").css({display:"none"});
	}
	if(boardsH == 0){
		$("#project_boards").css({display:"none"});
	}
	if(projectBg == ""){
		removeBg();
		bgLoadCompleteArgs = null;
		bgLoadComplete = null;
		bgHolder.css({display:"none"});	
	}else{
		prelaoderVisibility(true);
		bgLoadCompleteArgs = [false];
		bgLoadComplete = prelaoderVisibility;
		changeBg(projectBg);
		bgHolder.css({display:"block"});
	}
}
function projectOnLoadIcons(target){
	var id = $(target).attr("id");
	if(id == "p_lines"){
		$(target).tintIMG(currentPalette.lines[0]);
	}
	if(id == "circle"){
		$(target).tintIMG(currentPalette.icons[0]);
	}
	if(id == "plus"){
		$(target).tintIMG(currentPalette.icons[1]);
	}
	if(id == "minus"){
		$(target).tintIMG(currentPalette.icons[1]);
	}
	if(id == "still_icon"){
		$(target).tintIMG(currentPalette.icons[0]);
	}
	if(id == "sh_icon" || id == "facebook_icon" || id == "twitter_icon" || id == "copy_icon_img"){
		$(target).tintIMG(currentPalette.icons[0]);
	}
	if(id == "zip_icon"){
		if(!detectMobile()){
			$(target).css({marginLeft:29});
		}
		$(target).tintIMG(currentPalette.icons[0]);
	}
	if(id == "fav_icon"){
		$(target).tintIMG(slugInPlaylist($(target).attr("url")) ? currentPalette.icons[1] : currentPalette.icons[0]);
	}
	if(id == "close" || id == "arr_left" || id == "arr_right"){
		$(target).tintIMG(currentPalette.rectangleAndProjectName[1]);
	}
}
function projectFadeIn(){
	$("#project_content").css({opacity:0,visibility:"visible"}).animate({opacity:1},100);
}
function projectOnLoadIcons2(target){
	$(target).tintIMG(currentPalette.rectangleAndProjectName[1]);
}
function projectOnLoadStills(target){
	$(target).parent().css({visibility:"visible"}).animate({opacity:1},100);
	$(target).removeAttr("onload");
}
function projectClose(){
	var pn = $.address.pathNames();
	var url = "/"+pn[0]+"/"+pn[1];
	if(pn.length == 2){
		url = "/"+pn[0];
	}
	$.address.path(url);
}
function stopProject(){
	projectStopStillsMode();
	$("#project_content").stop(true);
	projectRemoveVideoListInterval();	
}
function projectRemoveVideoListInterval(){
	if(projectVideoListInterval != null){
		clearInterval(projectVideoListInterval);
		projectVideoListInterval = null;
	}
}
function projectLoadVideo(id,button){
	//$("#project_main #img_main").remove();
	var video = id;
	video = video.split("http://vimeo.com/").join("");
	video = video.split("http://www.vimeo.com/").join("");
	var 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="720" height="405" frameborder="0"></iframe>';
	$("#project_vimeo").stop(true).css({opacity:0,display:"none"}).html(html).delay(1100).animate({opacity:1},10,function(){
		$(this).css({display:"block"});	
	});
	if(projectListButtonSelected != null){
		projectListButtonSelected.data("selected",false);
		projectListButtonSelected.mouseout();
		projectListButtonSelected.css({cursor:"pointer"});
	}
	button.mouseover();
	button.data("selected",true);
	button.css({cursor:"auto"});
	projectListButtonSelected = button;
}
function projectChangeNextPrev(url,num){
	var path = url		
	if($.address.pathNames()[0] == "playlist"){
		var pn = $.address.pathNames();
		var tmp;
		var prev;
		var next;
		for(var i=0;i<playlist.length;i++){
			tmp = playlist[i].split("/");
			if(pn[1] == tmp[1]){
				next = i+1;
				prev = i-1;
				if(next == playlist.length){
					next = 0;
				}
				if(prev < 0){
					prev = playlist.length-1;
				}
				break;
			}
		}
		path = "/playlist/"+playlist[(num > 0 ? next : prev)].split("/")[1];
	}
	$.address.path(path);
}
function projectShowStills(num){
	//alert("Stills="+projectStillsData);
	//alert("Scenes="+projectScenesData);
	projectStillsIndex = num;
	var html = '';
	html += '<img src="assets/close_p.png" id="close" onload="projectOnLoadIcons2()"/>';
	if($.address.pathNames().length < 3){
		html += '<span id="txt2">'+$(".project_display #txt2").html()+'</span><br/>';	
		html += '<span id="txt1">'+$(".project_display #txt1").html()+'</span><br/>';
	}else{
		html += '<span id="txt1">'+$(".project_display #txt1").html()+'</span><br/>';
		html += '<span id="txt2">'+$(".project_display #txt2").html()+'</span><br/>';
	}
	html += '<span id="txt3">'+$(".project_display #txt3").html()+'</span><br/>';
	for(var i=0;i<projectStillsTarget.length;i++){
		html += '<img src="assets/circle_1.png" class="circle_button" id="circle_button'+i+'" onload="projectOnLoadCirclesFullscreen(this)"/>';
	}
	$("#project_stills_display").stop(true).css({display:"block",opacity:0}).html(html);
	$("#project_stills_display #txt1").css({color:currentPalette.rectangleAndProjectName[1]});
	$("#project_stills_display #txt2").css({color:currentPalette.rectangleAndProjectName[2]});
	$("#project_stills_display #txt3").css({color:currentPalette.rectangleAndProjectName[3]});
	$("#project_stills_display #close").mouseover(function(){
		$(this).showTooltip("Close",-5,-5);
		$(this).fadeTintIMG(currentPalette.rectangleAndProjectName[2],5);
	});
	$("#project_stills_display #close").mouseout(function(){
		hideTooltip();
		$(this).fadeTintIMG(currentPalette.rectangleAndProjectName[1],5);
	});
	$("#project_stills_display #close").click(function(){
		projectHideStills();
	});
	$("#project_stills_display").stop(true).delay(150).animate({opacity:1},150);
	$("#project_stills_display_bg").stop(true).delay(150).css({backgroundColor:currentPalette.rectangleAndProjectName[0],display:"block",opacity:0}).animate({opacity:0.5},150);
	$("#container").stop(true).animate({opacity:0},100,function(){
		$(this).css({display:"none"});
		prelaoderVisibility(true);
		bgLoadComplete = projectStillsAddTimeout;
		changeBg(projectStillsTarget[num],"width");
		projectKeys = true;
		bgHolder.css({display:"block"});
	});
	//alert(projectStillsData[num]);
}
function projectOnLoadCirclesFullscreen(target){
	var color = currentPalette.rectangleAndProjectName[1];
	if($(target).attr("id") == "circle_button"+projectStillsIndex){
		color = currentPalette.rectangleAndProjectName[2];
		$(target).data("selected",true);
		$(target).css({cursor:"auto"});
	}else{
		$(target).data("selected",false);
		$(target).css({cursor:"pointer"});	
	}
	$(target).tintIMG(color);
	$(target).removeAttr("onload");
	$(target).mouseover(function(){
		if(!$(this).data("selected")){
			$(this).fadeTintIMG(currentPalette.rectangleAndProjectName[2],5);
		}
	});
	$(target).mouseout(function(){
		if(!$(this).data("selected")){
			$(this).fadeTintIMG(currentPalette.rectangleAndProjectName[1],5);
		}
	});
	$(target).click(function(){
		if(!$(this).data("selected")){
			var num = Number($(this).attr("id").split("circle_button").join(""));
			projectShowStillsFromButton(num);
		}
	});
}
function projectLoadNextStill(){
	projectStillsIndex ++;
	if(projectStillsIndex == projectStillsTarget.length){
		projectStillsIndex = 0;
	}
	projectShowStillsFromButton(projectStillsIndex);
}
function projectLoadPrevStill(){
	projectStillsIndex --;
	if(projectStillsIndex < 0){
		projectStillsIndex = projectStillsTarget.length-1;
	}
	projectShowStillsFromButton(projectStillsIndex);
}
function projectShowStillsFromButton(num){
	projectStillsIndex = num;
	var color;
	$("#project_stills_display .circle_button").each(function(){
		color = currentPalette.rectangleAndProjectName[1];
		if($(this).attr("id") == "circle_button"+num){
			color = currentPalette.rectangleAndProjectName[2];
			$(this).data("selected",true);
			$(this).css({cursor:"auto"});
			$(this).fadeTintIMG(color,3);
		}else{
			$(this).data("selected",false);
			$(this).css({cursor:"pointer"});
			$(this).fadeTintIMG(color,3);	
		}
	});
	prelaoderVisibility(true);
	bgLoadComplete = projectStillsAddTimeout;
	changeBg(projectStillsTarget[num],"width");
	bgHolder.css({display:"block"});
	bgHolder.unbind("click");
	bgHolder.css({cursor:"auto"});
}
function projectHideStills(){
	hideTooltip();
	projectKeys = false;
	projectRemoveStillsTimeout();
	$("#project_stills_display_bg").stop(true).css({display:"none",opacity:0});
	$("#project_stills_display").stop(true).css({display:"none",opacity:0});
	$("#container").stop(true).css({display:"block"}).delay(150).animate({opacity:1},150);
	$("body").stop(true).animate({opacity:1,backgroundColor:currentPalette.background[0]},150);
	if(projectBg == ""){
		removeBg();
		bgHolder.css({display:"none"});
		prelaoderVisibility(false);
		bgLoadCompleteArgs = null;
		bgLoadComplete = null;	
	}else{
		prelaoderVisibility(true);
		bgLoadCompleteArgs = [false];
		bgLoadComplete = prelaoderVisibility;
		changeBg(projectBg);
		bgHolder.css({display:"block"});
	}
	bgHolder.unbind("click");
	bgHolder.css({cursor:"auto"});
}
function projectStopStillsMode(){
	projectKeys = false;
	bgHolder.unbind("click");
	bgHolder.css({cursor:"auto"});
	if($("#container").css("display") == "none"){
		$("#project_stills_display_bg").stop(true).css({display:"none",opacity:0});
		$("#project_stills_display").stop(true).css({display:"none",opacity:0});
		$("#container").stop(true).css({display:"block",opacity:1});
		$("body").stop(true).css({opacity:1,backgroundColor:currentPalette.background[0]});
		//
		//$("#container").stop(true).css({visibility:"visible"}).animate({opacity:1},100);
		//$("#header").stop(true).css({visibility:"visible"}).animate({opacity:1},100);
	}
	projectRemoveStillsTimeout();
}
function projectStillsAddTimeout(){
	$("body").stop(true).animate({opacity:1,backgroundColor:"#000000"},150);
	bgHolder.unbind("click");
	bgHolder.css({cursor:"pointer"});
	bgHolder.click(function(){
		projectLoadNextStill();
	});
	projectRemoveStillsTimeout();
	prelaoderVisibility(false);
	projectStillsTimeout = setTimeout("projectLoadNextStill()",5000);
}
function projectRemoveStillsTimeout(){
	if(projectStillsTimeout != null){
		clearTimeout(projectStillsTimeout);
		projectStillsTimeout = null;
	}
}

