var header_height = 30;
var min_panel_width=32;
var min_panel_height = image_height;
var container_x=0;
var container_y=0;
var new_panel_height=400;
var panel_out=false;

var HpNeO = {

	init: function() {
		BrowserDetect.init(); //hpneo : 04-05-2010
		refresh_elements(); //hpneo : 17-05-2010
		
		/* Thumbs */ 
		if(document.getElementById("browse")) {
			$(".thumbnails").bind("mouseover", function(event){ 
				$(this).css("border-color","#555555").css("background-color","#000000");
			});
			$(".thumbnails").bind("mouseout", function(event){ 
				$(this).css("border-color","#333333").css("background-color","#151515");
			});
		}
		
		/* Deal with image displays */
		if(!document.getElementById("image")) {
			return;
		}
		
		/*if(document.getElementById("image_notes").innerHTML.trim()=="") {
			document.getElementById("image_notes").style.display="none";
		}*/
		
		if (document.location.href.indexOf("thanks_comment") > -1) {
        document.getElementById('merci').style.display = "block";
        toggle_panel()
		}
		
		HpNeO.minContainerWidth = 680;
		HpNeO.containerWidth = HpNeO.minContainerWidth;
		HpNeO.setContainerWidth();
		
		/* Exifs */
		$("#exif").hide();
		$("#image").add("#exif").bind("mouseover", function(){
			$("#exif").stop().fadeTo("slow", 0.85);
		});
		$("#image").add("#exif").bind("mouseout", function(){
			$("#exif").stop(false, true).fadeOut("slow");
		});
	},
	
	setContainerWidth: function() {
		var pictureWidth = $("#image").width();
		HpNeO.containerWidth = (pictureWidth < HpNeO.minContainerWidth) ? HpNeO.minContainerWidth : pictureWidth;
		$("#container").width(HpNeO.containerWidth);
	}
}

function magicLineHpn() {
	var $el, leftPos, newWidth,
		$mainNav = $("#menu");
	
	$mainNav.append("<li id='magic-line'></li>");
	
	var $magicLine = $("#magic-line");
	
	$magicLine
		.width($(".menu_text").width()-10)
		.height($mainNav.height()-8)
		.css("left", $(".menu_text a").position().left+5)
		.data("origLeft", $(".menu_text a").position().left+5)
		.data("origWidth", $magicLine.width())
		.data("origColor", $(".menu_text a").attr("rel"));
	
	$("#menu li").find("a").hover(function() {
		$el = $(this);
		leftPos = $el.position().left+5;
		newWidth = $el.parent().width()-10;
		$magicLine.stop().animate({
			left: leftPos,
			width: newWidth,
			backgroundColor: $el.attr("rel")
		})
	},
	
	function() {
		$magicLine.stop().animate({
			left: $magicLine.data("origLeft"),
			width: $magicLine.data("origWidth"),
			backgroundColor: $magicLine.data("origColor")
		});
	});
};

function refresh_elements() {
	if(BrowserDetect.browser!='Explorer') {
		magicLineHpn();
	}
	if(BrowserDetect.browser=='Mozilla') {
		document.getElementById('txtarea_chrome').style.resize="none";
	}
	set_panel_height();
	setContainerPosition();
	setExifPosition();
}

function setContainerPosition() {  // hpneo : 04-05-2010 : modif 15-05-2010
	container_x=Math.round(((document.body.clientWidth-min_panel_width)/2)-(image_width/2)+min_panel_width);
	if(container_x<min_panel_width){
		container_x=min_panel_width;
	}
	$("#container").css("left", container_x);
	
	container_y = Math.round(header_height+(new_panel_height/2)-(image_height/2));
	if(container_y < header_height) {
		container_y = header_height;
	}
	$("#container").css("top", container_y);
}

function setExifPosition() {
		$("#exif").css("left", container_x);
		$("#exif").css("top", container_y);
}

function toggle_panel() {
	if(panel_out==false) {
		$("#panel").animate({"left": "+=400px"}, "slow");
		// $("#container").add("#exif").animate({"left": "+=200px"}, "slow"); // Ajout le 02-12-2010 mais pas de solutions
		$("#click_bar_arrow").toggleClass("active");
		panel_out=true;
	} else {
		$("#panel").animate({"left": "-=400px"}, "slow");
		// $("#container").add("#exif").animate({"left": "-=200px"}, "slow"); // Ajout le 02-12-2010 mais pas de solutions
		$("#click_bar_arrow").toggleClass("active");
		panel_out=false;
	}
}

var BrowserDetect = {
	init: function() {
		this.browser=this.searchString(this.dataBrowser)||"An unknown browser";
		this.version=this.searchVersion(navigator.userAgent)||this.searchVersion(navigator.appVersion)||"an unknown version";
		this.OS=this.searchString(this.dataOS)||"an unknown OS"
	},
	
	searchString: function(data) {
		for(var i=0; i<data.length; i++) {
			var dataString=data[i].string;
			var dataProp=data[i].prop;
			this.versionSearchString=data[i].versionSearch||data[i].identity;
			if(dataString) {
				if(dataString.indexOf(data[i].subString)!=-1)return data[i].identity
			} else if(dataProp)return data[i].identity
		}
	},
	
	searchVersion: function(dataString) {
		var index=dataString.indexOf(this.versionSearchString);
		if(index==-1)return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1))
	},
	
	dataBrowser:[
		{string:navigator.userAgent,subString:"OmniWeb",versionSearch:"OmniWeb/",identity:"OmniWeb"},
		{string:navigator.vendor,subString:"Apple",identity:"Safari"},
		{prop:window.opera,identity:"Opera"},
		{string:navigator.vendor,subString:"iCab",identity:"iCab"},
		{string:navigator.vendor,subString:"KDE",identity:"Konqueror"},
		{string:navigator.userAgent,subString:"Firefox",identity:"Firefox"},
		{string:navigator.vendor,subString:"Camino",identity:"Camino"},
		{string:navigator.userAgent,subString:"Netscape",identity:"Netscape"},
		{string:navigator.userAgent,subString:"MSIE",identity:"Explorer",versionSearch:"MSIE"},
		{string:navigator.userAgent,subString:"Gecko",identity:"Mozilla",versionSearch:"rv"},
		{string:navigator.userAgent,subString:"Mozilla",identity:"Netscape",versionSearch:"Mozilla"}
	],
	dataOS:[
		{string:navigator.platform,subString:"Win",identity:"Windows"},
		{string:navigator.platform,subString:"Mac",identity:"Mac"},
		{string:navigator.platform,subString:"Linux",identity:"Linux"}
	]
};

function set_panel_height() {
	if(document.documentElement.clientHeight-header_height<min_panel_height) {
		new_panel_height=min_panel_height
	} else {
		new_panel_height=document.documentElement.clientHeight-header_height
	}
	if(window.opera) {
		if(document.body.clientHeight-header_height<min_panel_height) {
			new_panel_height=min_panel_height
		} else {
				new_panel_height=document.body.clientHeight-header_height
		}
	} else if(navigator.vendor) {
		if(navigator.vendor.substring(0,5)=="Apple") {
			if(window.innerHeight-header_height<min_panel_height) {
				new_panel_height=min_panel_height
			} else {
				new_panel_height=window.innerHeight-header_height
			}
		}
	}
	
	document.getElementById('panel').style.height=document.getElementById('panel_scroll').style.height=document.getElementById('panel_shad').style.height=document.getElementById('panel_click_bar').style.height=new_panel_height+"px";
}

function initImage() {
	var imageId = 'image';
	var image = document.getElementById(imageId);
	if(!image) {
		return;
	}
	var opacity = 0;
	setOpacity(image, opacity);
	fadeIn(imageId,0);
}

function fadeIn(objId,opacity) {
	if (document.getElementById) {
		var obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 5;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
		}
	}
}

function setOpacity(obj, opacity) {
	var opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function body_fade(target_color) {
	$("#body").animate({"backgroundColor": target_color}, "slow");
}

window.onload = function() {initImage()}
window.onresize = refresh_elements; // hpneo : 04-05-2010
$(document).ready(function(){
	HpNeO.init();
});
