
function toggleImg(id,state)
{
	if (state)
		dojo.byId(id).src = "/elements/menu/" + id + "_o.png"
	else
		dojo.byId(id).src = "/elements/menu/" + id + ".png"
}

function resizeContent()
{
	var sidebar2 = dojo.byId("sidebar2")
	if (sidebar2 == null)
		sidebar2 = dojo.byId("sidebar2_180")
	if (sidebar2 != null)
	{
   		var sidebar2Height = sidebar2.offsetHeight;
   		
		if(sidebar2Height+37 > dojo.byId("content").offsetHeight)
			dojo.style("content", "height", sidebar2Height);
	
   			
	}
}

function checkFormField(field)
{
	var box = dojo.byId(field);
	if (box.value.length<3)
	{
		box.style.border = '1px solid red';
		return false;
	}
	box.style.border = '1px solid #E4DDCF';
	return true;
}

function getPageSize()
{
	var viewportwidth;
	var viewportheight;
	 
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
	     viewportwidth = window.innerWidth,
	     viewportheight = window.innerHeight
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
	      viewportwidth = document.documentElement.clientWidth-20,
	      viewportheight = document.documentElement.clientHeight-20
	}
	
	// older versions of IE
	else
	{
	      viewportwidth = document.getElementsByTagName('body')[0].clientWidth-20,
	      viewportheight = document.getElementsByTagName('body')[0].clientHeight-20
	}
	
	return new Array(viewportwidth, viewportheight);	
}

function showThumb(title, filepath, filename, imgWidth, imgHeight)
{
	var imgLink = dojo.byId("imglink");
	var imgDisp = dojo.byId("imgdisp");

	imgLink.href = "javascript:showImg(\"" + title + "\", \"" + filepath + filename + "\", " + imgWidth + ", " + imgHeight + ")";
	imgDisp.src = filepath + "thumbs/" + filename;
	imgDisp.alt = title;
	
	resizeContent();
}

function addStatEntry(filepath) {
	
	var xhrArgs = {
	    url: "stat.html",
	    postData: "filepath=" + filepath,
	    handleAs: "text",
	    load: function(data) {},
	    error: function(error) {}
	}
	dojo.xhrPost(xhrArgs);
}
function showOther(filepath) {
	window.open(filepath, '_blank');
	addStatEntry(filepath);
}
function showImg(title, filepath, imgWidth, imgHeight)
{
	var winSize = getPageSize();
	
	image = new Image();
	image.src = "http://www.fiveco.ch/" + filepath;
	
	if (imgWidth > (0.85 * winSize[0]))
	{
		imgHeight = Math.round(imgHeight * 0.9 * winSize[0] / imgWidth);
		imgWidth = Math.round(0.9 * winSize[0]);
	}
	if (imgHeight > (0.9 * winSize[1]))
	{
		imgWidth = Math.round(imgWidth * 0.9 * winSize[1] / imgHeight);
		imgHeight = Math.round(0.9 * winSize[1]);
	}
	
	//alert(imgWidth + " / " + imgHeight);
		
	//displayDiag.set("content", "<img src='" + filepath + "' style='width: " + imgWidth + "; height: " + imgHeight + "' alt='" + title + "' />");
	displayDiag.set("content", "<img src='" + filepath + "' width='" + imgWidth + "' height='" + imgHeight + "' alt='" + title + "' />");
	displayDiag.set("title", title);
	displayDiag.show();
	addStatEntry(filepath);
}
function showPDF(title, filepath)
{
	var winSize = getPageSize();
	displayDiag.set("title", title);
	var target = dojo.create("div", {style:{width:Math.round(0.9 * winSize[0]), height:Math.round(0.9 * winSize[1]), overflow: 'auto'}});
	displayDiag.set("content", target);
	var myPDF = new PDFObject({ url: filepath }).embed(target);
	displayDiag.show();
	addStatEntry(filepath);
}

function getVideoTag(filepath, formats)
{
	// formats bits :
	// 0x01 : mp4
	// 0x02 : webm
	// 0x04 : ogg
	// 0x80 : flv
	
	var content = "";
	
	if((formats & 0x7F) > 0)
	{
		content += "<video width='480' height='320' controls='controls' autoplay='true'>";
		if((formats & 0x01) > 0)
			content += "<source src='/" + filepath + ".mp4' type='video/mp4' />";
		if((formats & 0x02) > 0)
			content += "<source src='/" + filepath + ".webm' type='video/webm' />";
		if((formats & 0x04) > 0)
			content += "<source src='/" + filepath + ".ogv' type='video/ogg' />";
	}
	if((formats & 0x80) > 0)
	{
		content += "<object type='application/x-shockwave-flash' data='/" + "player_flv_maxi.swf' width='480' height='320'>";
		content += "<param name='movie' value='/" + "player_flv_maxi.swf' />";
		content += "<param name='allowFullScreen' value='true' />";
		content += "<param name='FlashVars' value='flv=/" + filepath + ".flv&amp;width=480&amp;height=320&amp;autoplay=1&amp;autoload=1&amp;margin=0&amp;showstop=1&amp;showvolume=1&amp;loadingcolor=746a61&amp;buttonovercolor=746a61&amp;sliderovercolor=746a61&amp;showiconplay=1&amp;showmouse=autohide' />";
	}

	content += "Your browser is too old to display this video ! Sorry.";
	
	if((formats & 0x80) > 0)
	{
		content += "</object>";
	}
	if((formats & 0x7F) > 0)
		content += "</video>";
	
	return content;
}
function showVideo(title, filepath, formats)
{	
	displayDiag.set("title", title);
	displayDiag.set("content", getVideoTag(filepath, formats));
	displayDiag.show();
	addStatEntry(filepath);
}

function openCloseLearnMore()
{	
	if (dojo.style("learnMoreDisplay", "display") == "none")
	{
        dojo.style("learnMoreDisplay", "display", "none");
        var wipeArgs = {
            node: "learnMoreDisplay",
            duration: 1000,
            easing: dojo.fx.easing.expoOut,
            onAnimate: function(){
            	dojo.style("content", "height", "auto");
resizeContent();            }
        };
        dojo.fx.wipeIn(wipeArgs).play();
		dojo.style("learnMoreOption", "background", "url(elements/moins.png) no-repeat left center");
	} else {
        dojo.style("learnMoreDisplay", "height", "");
        dojo.style("learnMoreDisplay", "display", "block");
        var wipeArgs = {
            node: "learnMoreDisplay",
            duration: 1000,
            easing: dojo.fx.easing.expoOut,
            onAnimate: function(){
resizeContent();            }
        };
        dojo.fx.wipeOut(wipeArgs).play();
		dojo.style("learnMoreOption", "background", "url(elements/plus.png) no-repeat left center");
	}	
}

function openCloseRef()
{	
	if (dojo.style("refDisplay", "display") == "none")
	{
       // dojo.style("refDisplay", "display", "none");
        var wipeArgs = {
            node: "refDisplay",
            duration: 1000,
            easing: dojo.fx.easing.expoOut,
            onAnimate: function(){
            	dojo.style("content", "height", "auto");
resizeContent();            }
        };
        dojo.fx.wipeIn(wipeArgs).play();
	} else {
        dojo.style("refDisplay", "height", "");
        dojo.style("refDisplay", "display", "block");
        var wipeArgs = {
            node: "refDisplay",
            duration: 1000,
            easing: dojo.fx.easing.expoOut,
            onAnimate: function(){
resizeContent();            }
        };
        dojo.fx.wipeOut(wipeArgs).play();
	}	
}
