var projectList = new Array();
var projectDetailes = new Array();
var activeProject = 0;
var wzFmt = "";
var wzFmtActive = "";
var isChinaInland = false;

function loadWebzineFormat()
{
	var loader = dhtmlxAjax.getSync("/wzFmt.html");
	wzFmt = loader.xmlDoc.responseText;
	
	loader = dhtmlxAjax.getSync("/wzFmtActive.html");
	wzFmtActive = loader.xmlDoc.responseText;
}

function loadProjectList()
{
	var loader = dhtmlxAjax.getSync("/data/projectList.cfg");
	var list = loader.xmlDoc.responseText.split("\n");//loader.doXPath("/projects/project");

	for(var i=0; i<list.length; i++)
	{
		if(list[i] != "")
			projectList.push(list[i]);
	}

	var txt = "";

	for(var i=0; i<projectList.length; i++)
	{
		txt += "<li id=\"pl_"+i+"\"";

		if(activeProject == i)
			txt += " class=\"active\">";
		else
			txt += ">";

		var dt = getProjectDetail(i);
		txt += "<span onclick=\"loadProject('"+i+"');\">"+dt.title+"</span></li>\n";
//		txt += "<span onclick=\"loadProject('"+i+"');\">"+projectList[i]+"</span></li>\n";
	}
	$("projectList").innerHTML = txt;
}

function getProjectDetail(idx)
{
	var fname = "/data/project/"+projectList[idx]+"/info.cfg";
	var loader = dhtmlxAjax.getSync(fname);

	if(loader)
	{
		var obj = new Object();
		var txt =loader.xmlDoc.responseText;
		var lines = txt.split("\n");
		
		obj.title			= lines[0];
		obj.subTitle		= lines[1];
		obj.airDate			= lines[2];
		obj.comment			= lines[3];
		obj.creative		= lines[4];
		obj.projectName		= lines[5];
		obj.airage			= lines[6];
		obj.shootLocation	= lines[7];
		obj.format			= lines[8];
		obj.client			= lines[9];
		obj.agency			= lines[10];
		obj.production		= lines[11];
		obj.director		= lines[12];
		obj.DOP				= lines[13];
		obj.telecine		= lines[14];
		obj.imageCnt		= lines[15];
		obj.video			= lines[16];
		obj.youku			= lines[17];
		return obj;
	}
	else
	{
		return "";
	}
	/*
	var fname = "/data/project/"+projectList[idx]+"/info.json";
	var loader = jQuery.ajax({	url:fname, dataType:"json", async:false});

	var txt = loader.response;
	txt = txt.replace(/\\\"/g, "&quot");
	var obj = jQuery.parseJSON(txt.replace(/</g, "&lt;").replace(/>/g, "&gt;"));
	

	if(obj == null || typeof(obj) === "undefined")
		return "";
	else
		return obj;
*/
}

function bulidWebZine()
{
	var txt = "";
	var txt2 = "";
	for(var i=0; i<projectList.length; i++)
	{
		projectDetailes.push(getProjectDetail(i));

		var tmp = wzFmt;
		var sFirst = "";

		if(i==activeProject)
			tmp = wzFmtActive;

		sFirst = i>0 ? "" : " top";
		tmp = tmp.replace(/{first}/g, sFirst);
		tmp = tmp.replace(/{idx}/g, i);
		tmp = tmp.replace(/{name}/g, projectList[i]);
		tmp = tmp.replace(/{wvTitle}/g, projectDetailes[i].title);
		tmp = tmp.replace(/{wvSubTitle}/g, projectDetailes[i].subTitle);
//		tmp = tmp.replace(/{wvAirDate}/g, projectDetailes[i].airDate);
		tmp = tmp.replace(/{comment}/g, projectDetailes[i].comment);
		tmp = tmp.replace(/{wvClient}/g, projectDetailes[i].client);
		tmp = tmp.replace(/{wvCreavite}/g, projectDetailes[i].creative);
		tmp = tmp.replace(/{wvBrief}/g, projectDetailes[i].brief);
		tmp = tmp.replace(/{production}/g, projectDetailes[i].production);
		tmp = tmp.replace(/{director}/g, projectDetailes[i].director);

		txt += "\n"+tmp;

//		if(i < projectList.length-1)
			txt += "<hr class='dash'/>";
	}
	$("webzinView").innerHTML = txt;
}

function loadProject(idx)
{
	var sFirst;

	if(activeProject>0)	sFirst = "";
	$("pl_"+activeProject).className = "";
	sFirst = activeProject>0 ? "" : " top";
	$("wzContents_"+activeProject).className = "normal"+sFirst;

	activeProject = idx;
	sFirst = activeProject>0 ? "" : " top";

	$("pl_"+activeProject).className = "active";
	$("wzContents_"+activeProject).className = "webzinActive"+sFirst;

	var loader = dhtmlxAjax.getSync("/viewArea.html");
	var txt = loader.xmlDoc.responseText;
	var movieFile = isChinaInland ? projectDetailes[idx].youku : projectDetailes[idx].video;

	txt = txt.replace(/{projectName}/g, projectDetailes[idx].title);
	txt = txt.replace(/{subTitle}/g, projectDetailes[idx].subTitle);
	txt = txt.replace(/{airage}/g, projectDetailes[idx].airage);
	txt = txt.replace(/{shootLocation}/g, projectDetailes[idx].shootLocation);
	txt = txt.replace(/{format}/g, projectDetailes[idx].format);
	txt = txt.replace(/{client}/g, projectDetailes[idx].client);
	txt = txt.replace(/{agency}/g, projectDetailes[idx].agency);
	txt = txt.replace(/{production}/g, projectDetailes[idx].production);
	txt = txt.replace(/{director}/g, projectDetailes[idx].director);
	txt = txt.replace(/{DOP}/g, projectDetailes[idx].DOP);
	txt = txt.replace(/{telecine}/g, projectDetailes[idx].telecine);

	txt = txt.replace("<span id=\"projectName\"></span><br/>", "");
	txt = txt.replace("<span id=\"airage\"></span><br/>", "");
	txt = txt.replace("<span id=\"shootLocatgion\"></span><br/>", "");
	txt = txt.replace("<span id=\"format\"></span><br/>", "");
	
	txt = txt.replace("<span id=\"client\"></span><br/>", "");
	txt = txt.replace("<span id=\"agency\"></span><br/>", "");
	txt = txt.replace("<span id=\"production\"></span><br/>", "");
	txt = txt.replace("<span id=\"director\"></span><br/>", "");
	txt = txt.replace("<span id=\"DOP\"></span><br/>", "");
	txt = txt.replace("<span id=\"telecine\"></span><br/>", "");

//	txt = txt.replace(/{movieFile}/g, movieFile);

	$("viewArea").innerHTML = txt;

	var txt = "";

	
	var loader = dhtmlxAjax.getSync("./loadImageList.php?dir="+projectList[activeProject]);
	var txt =loader.xmlDoc.responseText;
	var list =txt.split("\n");

	var txt = "";

//	for(var i=0; i<projectDetailes[idx].imageCnt && i<9; i++)
	for(var i=0; i<list.length; i++)
	{
		if(list[i] == "")
			continue;
		var hidden = "style='visibility:hidden'";
		var tmp = "<a href='"+list[i]+"' class='highslide' onclick=\"return hs.expand(this)\" {hidden}><img class=\"locRight grayed\" src=\"/loadIcon.php?file="+list[i]+"&w=70&h=25\";\"/></a>";

		if(i<9)
			hidden = "";
			
		tmp = tmp.replace(/{hidden}/g, hidden);
		tmp = tmp.replace(/{idx}/g, pad(i, 3));
		tmp = tmp.replace(/{pdIdx}/g, idx);
		tmp = tmp.replace(/{i}/g, i);
		tmp = tmp.replace(/{project}/g, projectList[idx]);

		txt += tmp;
	}
	if(txt != "")
		$("imgPhotos").style.visibility = "visible";
	else
		$("imgPhotos").style.visibility = "hidden";

	$("ozcam-gallery").innerHTML = "<br/>"+txt;

	changeVideoFile(movieFile);

	scroll(0,0);
}

/*
function changeVideoFile(movieFile)
{
	movieFile += "&hl=en";

	var txt = "<object style=\"height: :WIDTH:px; width: :HEIGHT:px\"><param name=\"movie\" value=\":MOVIE:\"><param name=\"allowFullScreen\" value=\"true\"><param name=\"allowScriptAccess\" value=\"always\"><embed src=\":MOVIE:\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowScriptAccess=\"always\" width=\":WIDTH:\" height=\":HEIGHT:\"></object>";

	txt = txt.replace(":WIDTH:/g", "570");
	txt = txt.replace(":HEIGHT:/g", "360");
	txt = txt.replace(":MOVIE:", movieFile);
	$("movie").innerHTML = txt;
*/
function changeVideoFile(movieFile)
{
	var w=615;
	var h=360;

	if(isChinaInland)
	{
		$("movie").innerHTML  = movieFile;//"<embed src='http://player.youku.com/player.php/Type/Folder/Fid/6558170/Ob/1/Pt/0/sid/XMjkyMzQyNzI4/v.swf' quality='high' width='"+w+"' height='"+h+"' align='middle' allowScriptAccess='always' allowFullScreen='true' mode='transparent' type='application/x-shockwave-flash'></embed>";
	}
	else
	{

		movieFile = "http://www.youtube.com/v/"+movieFile+"?version=3&amp;hl=en&amp;fs=1&amp;autoplay=1&rel=0";
		$("movie").innerHTML = "<object style=\"height: "+h+"px; width: "+w+"px\"><param name=\"movie\" value=\""+movieFile+"\"><param name=\"wmode\" value=\"transparent\"><param name=\"allowScriptAccess\" value=\"always\"><embed src=\""+movieFile+"\" type=\"application/x-shockwave-flash\" allowScriptAccess=\"always\" width=\""+w+"\" height=\""+h+"\" wmode=\"transparent\"></object>";
	}
}


function changeVideoFile2(movieFile)
{
	alert($("movie").innerHTML);
}

function initIcon() {
	
	jQuery("<li><img src=\"/images/off_facebook.jpg\" border=\"0\" style=\"background:url(/images/on_facebook.jpg);\" id=\"facebook\" onclick=\"goURL(this.id)\"/></li>").appendTo("#iconmenu");
	jQuery("<li><img src=\"/images/off_youtube.jpg\" border=\"0\" style=\"background:url(/images/on_youtube.jpg);\" id=\"youtube\" onclick=\"goURL(this.id)\"/></li>").appendTo("#iconmenu");
	jQuery("<li><img src=\"/images/off_newsletter.jpg\" border=\"0\" style=\"background:url(/images/on_newsletter.jpg);\" id=\"newsletter\" onclick=\"goURL(this.id)\"/></li>").appendTo("#iconmenu");
	jQuery("<li><img src=\"/images/off_intranet.jpg\" border=\"0\" style=\"background:url(/images/on_intranet.jpg);\" id=\"intra\" onclick=\"goURL(this.id)\"/></li>").appendTo("#iconmenu");
}

jQuery.fn.crossfade = function () {  
	return this.each(function () {     // cache the copy of jQuery(this) - the start image    
		var $$ = jQuery(this);        // get the target from the backgroundImage + regexp    
		var target = $$.css('backgroundImage').replace(/^url|[()]/g, '');        // nice long chain: wrap img element in span
		target = target.replace(/\"/g,"");
		$$.wrap('<div style="position: relative;"></div>')      // change selector to parent - i.e. newly created span      
		//$$.wrap("<li></li>")
		.parent()      // prepend a new image inside the span      
		.prepend('<img>')      // change the selector to the newly created image      
		.find(':first-child')      // set the image to the target      
		.attr('src', target);        // position the original image    
		$$.css({      'position' : 'absolute',       
					  'left' : 0,       // this.offsetTop aligns the image correctly inside the span      
					  'top' : this.offsetTop    
		});        
// note: the above CSS change requires different handling for Opera and Safari,    
// see the full plugin for this.        

// similar effect as single image technique, except using .animate     
// which will handle the fading up from the right opacity for us    

		$$.hover(function () {      
			$$.stop().animate({          
					opacity: 0      
			}, 500);    }, 
			function () {      
				$$.stop().animate({          
						opacity: 1      
				}, 1000);    });  
	});
};

// Not only when the DOM is ready, but when the images have finished loading,
// important, but subtle difference to $(document).ready();

jQuery(window).bind('load', function () {  // run the cross fade plugin against selector  
	jQuery('#facebook').crossfade();
	jQuery('#youtube').crossfade();
	jQuery('#newsletter').crossfade();
	jQuery('#intra').crossfade();
	//$('img.a1fade').crossfade();
	//$('img.ncomfade').crossfade();
});

function goURL(id) {
	var frm = document.getElementById('frm_iconmenu');
	frm.target = "_blank";
	switch(id) {
	case 'facebook' :
		frm.action = "http://www.facebook.com/ozcampro";
		
		break;
	case 'youtube' :
		frm.action = "http://www.youtube.com/user/ozcamproduction";
		break;
	case 'newsletter' :
		frm.action = "mailto:info@ozcam.net.au";
		frm.target = "";
		break;
	case 'intra' :
		frm.action = "http://bd.ozcam.net.au";
		break;
	}
//	window.open(frm.action);
	frm.submit();
}
