jQuery.fn.outerHTML = function(s){
	return (s) ? this.before(s).remove() : jQuery("<div>").append(this.eq(0).clone()).html();
}

// ******************************
// PROJECT LIST
// ******************************
$(document).ready(function(){

	//SET MENU
	try
	{
		$(".button" + (($(".wrapper_section > .container:first").attr("id")).split("_"))[1]).addClass("selected");
	}
	catch(e)
	{}


	var PaneSize		= 16;
	var PaneReviewTime	= 2 * (24*60*60*1000); //2 * (24*60*60*1000); //day count * 24*60*60*1000


	//GET HIGH PRIORITY PROJECT LIST
	var MainProjectList = GetProjectList($(".container-priority-1"), 0, PaneSize);
	$(".container-priority-1").remove();


	//GET SECTION PRIORITY PROJECTS
	var sPr = $(".container-priority-2");
	if(sPr.length>0)
	{
		var Now 		= new Date();
		var FirstViewItem	= (sPr.eq(0).attr("id")).split("_");
		var LastViewItem 	= $.cookie("OFCN_" + FirstViewItem[1]);

		if(LastViewItem==null)
		{
			LastViewItem	= FirstViewItem[2]+"_"+FirstViewItem[3]+"_"+Now.valueOf();
			$.cookie("OFCN_" + FirstViewItem[1], LastViewItem, {expires:365, path:"/"});
		}
		var TimeSpan = Now.valueOf() - parseInt((LastViewItem.split("_"))[2]);

		if( (MainProjectList.length + sPr.length) <= PaneSize )
		{
			//SHOW ALL SECTION PRIORITY PROJECTS
			var SectionProjectList = GetProjectList(sPr, 0, sPr.length);
		}
		else if(TimeSpan<PaneReviewTime)
		{
			//SHOW LAST CONFIGURATION
			var SectionProjectList = GetProjectList(sPr, $("#container_" + FirstViewItem[1] + "_" + (LastViewItem.split("_"))[0]+"_"+(LastViewItem.split("_"))[1]).index(".container-priority-2"), PaneSize-MainProjectList.length);
		}
		else
		{
			//RESET COOKIE
			var oIndex = $("#container_" + FirstViewItem[1] + "_" + (LastViewItem.split("_"))[0]+"_"+(LastViewItem.split("_"))[1]).index(".container-priority-2") + 1;
			if(oIndex==sPr.length)
			{
				oIndex = 0;
			}
			var Now2 	= new Date();
			$.cookie("OFCN_" + FirstViewItem[1], ((sPr.eq(oIndex).attr("id") ).replace("container_" + FirstViewItem[1] + "_", "")) + "_" + Now2.valueOf(), {expires:365, path:"/"});

			//SHOW NEXT ONE
			var SectionProjectList = GetProjectList(sPr, oIndex, PaneSize-MainProjectList.length);
		}
		$(".container-priority-2").remove();
	}

	var oParent = $("#wrapper_index");

	if(MainProjectList!=null && MainProjectList.length>0)
	{
		SetProjectList(MainProjectList.sort(function(a,b){
			return (isNaN(a.order) || isNaN(b.order)) ? a < b : a.order < b.order;

		}), oParent, false);
	}
	if(SectionProjectList!=null && SectionProjectList.length>0)
	{
		SetProjectList(SectionProjectList, oParent, true);
	}

	//SET PROJECT COMBO
	if($("#project-combo > .project-combo-list > .project-combo-item").length>0)
	{
		$("#project-combo > .project-combo-header").bind("click", function(){
			$("#project-combo > .project-combo-list").css("display", $("#project-combo > .project-combo-list").css("display")!="none" ? "none" : "block");
		});
		$("#project-combo").show();
	}
});


function SetProjectList(VisibleProjectList, oParent, randomShow)
{
	while(VisibleProjectList.length>0)
	{
		var rndItem	= randomShow ? Math.floor(Math.random()*VisibleProjectList.length) : 0;
		var oItem 	= VisibleProjectList.splice(rndItem, 1);
		var oPr 	= $(oItem[0].html);

		oPr.find("a").css({"background" : oItem[0].background}).attr("order", oItem[0].order);
		oPr.find("div").bind("click", function(){
			document.location = $(this).parent().find("a").attr("href");
		});
		oPr.bind("mouseenter", function(){
			var oThis = $(this);
			oThis.find("div").stop().animate({top:0, left:0}, 400);
		}).bind("mouseleave", function(){
			var oThis = $(this);
			oThis.find("div").stop().animate({top:-207, left:-207}, 200);
		})
		oParent.append(oPr);
		oPr.show(); //600
	}
}

function GetProjectList(oProjects, Start, Count)
{
	var ProjectsToShow	= new Array();
	var ProjectsDisplayed	= 0;

	oProjects.each(function(ItemIndex){
		if(ItemIndex>=Start && ProjectsDisplayed<Count)
		{
			ProjectsDisplayed++;
			var oClip = $(this);
			ProjectsToShow.push({
						html			: oClip.outerHTML(), 
						background		: "#FFFFFF url('/getthumbnail.aspx?w=207&h=207&q=100&id_file=" + ((oClip.attr("id")).split("_"))[2] + "') no-repeat top center",
						order			: ((oClip.attr("id")).split("_"))[5] | 1000
			});
		}
	});
	if(oProjects.length-ProjectsDisplayed>0)
	{
		var cnt = 0;
		while(ProjectsDisplayed<Count)
		{
				ProjectsDisplayed++;
				var oClip = oProjects.eq(cnt);
				ProjectsToShow.push({
							html		: oClip.outerHTML(), 
							background	: "#FFFFFF url('/getthumbnail.aspx?w=207&h=207&q=100&id_file=" + ((oClip.attr("id")).split("_"))[2] + "') no-repeat top center",
							order			: ((oClip.attr("id")).split("_"))[5] | 1000
				});
				cnt++;
		}
	}

	return ProjectsToShow;
}


// ******************************
// PROJECT DETAIL
// ******************************
$(document).ready(function(){
	try
	{
		$(".button" + $("#wrapper_karta").attr("className")).addClass("selected");
	}
	catch(e)
	{}

	$(".container_pinup > .pinup_obdelnik > *").each(function(ItemIndex){
		var oEl = $(this);
		oEl.css({"cursor":"pointer"}).bind("click", function(){
			$("#preview-video-clip").html('<div id="preview-video"></div>');

			if(oEl.parent().hasClass("pinup_video"))
			{
				//SHOW VIDEO CLIP
				oEl = oEl.parent().find("img");
				var VideoID	= oEl.parent().find("div").attr("id");
				var VideoType	= (isNaN(parseInt(VideoID)) || (parseInt(VideoID)+"").length!=VideoID.length) ? "jwplayer" : "vimeo";
				IniVideoPlayer(oEl, VideoID, VideoType);
				$("#preview-image").hide();
				$("#preview-video-clip").show();

				var oDescription = ParseProjectNameDetail(oEl.attr("alt"));
				$("#comment_detail").html(oDescription);
			}
			else
			{
				$("#preview-video-clip").hide();
				$("#preview-image").html("<img src=\"/GetThumbNail.aspx?w=640&h=36000&q=100&id_file=" + oEl.attr("id") + "\" alt=\"\" border=\"0\" />").show();
				var oDescription = ParseProjectNameDetail(oEl.parent().find("img").attr("alt"));
				$("#comment_detail").html(oDescription);

			}
		});
	});
	$(".container_pinup img:first").click();

	if($(".container_pinup > .pinup_obdelnik").length>1)
	{
		$(".container_pinup > .pinup_video > div").bind("mouseover", function(){
			$(this).addClass("hover");
		}).bind("mouseout", function(){
			$(this).removeClass("hover");
		});
	}
	else
	{
		$(".container_pinup > .pinup_obdelnik > *").css({"cursor":"default"}).unbind("click");
	}
});

function ParseProjectNameDetail(oTx)
{
	var oName = oTx.replace(".jpg","");
	if(oName.length<6)
	{
		return "";
	}
	else if(oName.indexOf(" - ")==3)
	{
		return " / " + oName.substring(6);
	}
	else
	{
		return " / " + oName;
	}
}

function IniVideoPlayer(PosterImage, VideoID, VideoType)
{
	var player	= {
				jwplayer:	"/Client.Media/player.swf", 
				vimeo:		"http://vimeo.com/moogaloop.swf"
	};

	var flashvars 	= {
				/* COMMON PROPS */
				width: 			640, //PosterImage.width(),
				height: 		360, //PosterImage.height(),
				
				/* JWPLAYER PROPS */
				file: 			escape(VideoID),
				shuffle:		"false",
				showstop:		"true",
				autostart:		"false",
			   	image: 			escape(PosterImage.attr("src")),
				backcolor:		"#000",
				frontcolor:		"#eeeeee",
				lightcolor:		"#ffffff",
				skin:			escape("/Client.Media/overlay.swf"),
				controlbar:		"over",

				/* VIMEO PROPS */
				clip_id:		escape(VideoID),
				title:			false,
				byline:			false,
				portrait:		false,
				js_api:			true
	};

	var params 	= {
				allowscriptaccess:	"always",
				allowfullscreen:	"true",
				wmode:			"opaque"
	};

	var attributes 	= {
				id:			"OFCN_video"
	};

	swfobject.embedSWF(player[VideoType], "preview-video", flashvars.width,flashvars.height,'9.0.0', "", flashvars, params, attributes);
}

