// JavaScript Document
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}
function find_pos(obj) {
	var curleft = obj.offsetLeft || 0;
    var curtop = obj.offsetTop || 0;
    while (obj = obj.offsetParent) {
        curleft += obj.offsetLeft
        curtop += obj.offsetTop
    }
    return {x:curleft,y:curtop};
}
function displayEtiquette(pRef){
	var div = $('cont_legend');
	if(pRef){
		var source = $('media_legend_' + pRef);
		div.innerHTML = source.innerHTML ;
		div.style.display = 'block' ;
		var pos = find_pos($('item_' + pRef)) ;
		div.style.top = pos.y + 'px';
		div.style.left = pos.x + 'px' ;
	}
	else{
		div.style.display = 'none' ;
		div.innerHTML = '' ;
	}
}
function displayLegende(pRef){
	var div = $('cont_details');
	if(pRef){
		var source = $('item_' + pRef);
		div.innerHTML = source.innerHTML ;
		div.style.display = 'block' ;
		var pos = find_pos($('partners_item_' + pRef)) ;
		div.style.top = pos.y + 'px';
		div.style.left = pos.x + 'px' ;
	}
	else{
		div.style.display = 'none' ;
		div.innerHTML = '' ;
	}
}
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} 
	else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth) windowWidth = document.documentElement.clientWidth; 
		else windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	if(yScroll < windowHeight) pageHeight = windowHeight;// for small pages with total height less then height of the viewport
	else pageHeight = yScroll;
	if(xScroll < windowWidth) pageWidth = xScroll;// for small pages with total width less then width of the viewport		
	else pageWidth = windowWidth;
	var isIE = (navigator.appName=='Microsoft Internet Explorer'?true:false);
	return [pageWidth,pageHeight,isIE];
}
function displayVideo(pBoolean, pRef, pRow, pZone){
	if(pBoolean){
		scroll(0,0);
		var dimensions = getPageSize();
		$('video_display_bg').style.display = "block";
		$('video_display_bg').style.width = dimensions[0] + 'px' ;
		$('video_display_bg').style.height = dimensions[1] + 'px' ;
		$('video_display_bg').onclick = function (){ displayVideo(false);}
		if(dimensions[2]) $('video_display_bg').style.filter = "alpha(opacity=80)";
		else $('video_display_bg').style.opacity = "0.8";
		var http = ajax_createRequestObject ();
		var vars = 'id_event=' + pRef + '&id_row=' + pRow + '&zone=' + pZone ;
		http.open("GET", 'codes/ajax/ajax.video.php?' + vars);
		http.onreadystatechange = function() {
			if(http.readyState == 4) {
				var json = eval('('+ http.responseText +')');
				$('video_display').innerHTML = '<div id="video_display_content"></div><div id="video_display_close"><a href="javascript:displayVideo(false);"><img src="/js/lightbox/images/closelabel.gif" border="0" /></a></div>' ;
				$('video_display').style.width = new Number(json.w) + 20 + 'px' ;
				$('video_display').style.height = new Number(json.h) + 65 + 'px' ;
				$("video_display").style.marginLeft = 0 - (new Number(json.w) + 20) /2 + 'px';
				$("video_display").style.marginTop = 0 - (new Number(json.h) + 200)/2 + 'px';
				$('video_display_content').innerHTML = json.video_code ;
				$('video_display_content').style.width = new Number(json.w) + 'px' ;
				$('video_display_content').style.height = (new Number(json.h)) + 'px' ;
				//alert($('video_display').innerHTML);
			}
		}
		http.send(null);
		$('video_display').style.display = "block";
		window.onresize = function (){
			var dimensions = getPageSize();	
			$('video_display_bg').style.width = dimensions[0] + 'px' ;
			$('video_display_bg').style.height = dimensions[1] + 'px' ;
		}
	}
	else {
		window.onresize = null ;
		$('video_display_bg').style.display = "none";
		$('video_display').innerHTML = '' ;
		$('video_display').style.display = "none";
	}
}

function ajax_createRequestObject() {
	var xhr = null;
	if(window.XMLHttpRequest) { //firefox
		xhr = new XMLHttpRequest();
	} else if(window.ActiveXObject) { //ie
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				alert("Your browser is not compatible with extended Javascript functionnalities. Please update.");
			}
		}
	} else { // unsupported
		alert("Your browser is not compatible with extended Javascript functionnalities. Please update.");
		xhr = false;
	}
	return (xhr);
}