/***** JS Error Handling *******

function reportJSError(errorMessage,url,lineNumber){
	//var img=new Image();
	//url2 = url.replace('/\//g', '_');
	//img.src="/public/aerrorCatching/js/"+escape(lineNumber)+"/"+escape(errorMessage)+"/"+escape(url2);
	//alert("JS Error! admin has been alerted!!!!!!"+"/public/aerrorCatching/js/"+escape(lineNumber)+"/"+escape(errorMessage)+"/"+escape(url2));
	return true;
}

window.onerror=reportJSError;
*/

/***** COOKIES ********/
function _cookiesAreEnabled() {
  SetCookie( "foo", "bar" );
  if ( GetCookie( "foo" ) ) {
    DeleteCookie( "foo" );
    return true;
  } else {
    return false;
  }
}

//navigator.cookiesAreEnabled = _cookiesAreEnabled;

/*function _getHVCCAuthID() {
  return GetCookie( "HVCCAuthID" );
}
document.getHVCCAuthID = _getHVCCAuthID;
*/

function GetCookie( name ) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while ( i < clen ) {
    var j = i + alen;
    if ( document.cookie.substring(i, j) == arg ) return getCookieVal(j);
    i = document.cookie.indexOf( " ", i ) + 1;
    if ( i == 0 ) break;
  }
  return null;
}

function DeleteCookie( name, path, domain ) {
  if ( GetCookie( name ) ) {
    document.cookie = name + "=" +
    ( ( path ) ? "; path=" + path : "" ) +
    ( ( domain ) ? "; domain=" + domain : "" ) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function SetCookie( name, value, expires, path, domain, secure ) {
  document.cookie = name + "=" + escape (value) +
  ( ( expires ) ? "; expires=" + expires.toGMTString() : "" ) +
  ( ( path ) ? "; path=" + path : "" ) +
  ( ( domain ) ? "; domain=" + domain : "" ) +
  ( ( secure ) ? "; secure" : "" );
}

/**
 *   Helper function for GetCookie()
 */
function getCookieVal( offset ) {
  var endstr = document.cookie.indexOf ( ";", offset );
  if ( endstr == -1 ) endstr = document.cookie.length;
  return unescape( document.cookie.substring( offset, endstr ) );
}



  function doJavaScriptTest() {


    window.alert( "JavaScript is enabled!" );


  }


  function doCookieTest() {


    var msg;


    /*if ( !navigator.cookiesAreEnabled() ) {
      msg = "Cookies are not enabled on this browser! ";
      msg += "Please enable cookies in your browser in order to use weetic.com. You will not be able to access systems requiring authentication if your cookies are disabled.";
      window.alert( msg );
    }*/

  }






/* 
 * Global Vars:
 *
*/
var iAmIE6andImHidden=false;
var ActiveTimer=0;
var UnlockTimer=0;
var canIshowMeetingRoom=false;
var roomStatus=false;

//------------------------- BEGIN: Content Layers ----------------------

var contentLayers=new Array();
var activeContentLayer="";
var contentLayerDefaults=new Array();

// Add Content Layers into list array
function addContentLayer(id) {
	contentLayers.push(id);
	el=document.getElementById(id);
	contentLayerDefaults[id]=el.innerHTML; 
}

// Hide the All Content Layers
function hideContentLayers() {
	for (x in contentLayers) {
		el=document.getElementById(contentLayers[x]);
		el.style.left = "-1000px";
		el.style.top = "-1000px";
	}
}
// Clear Content for all Content Layer
function clearContentLayers() {
	for (x in contentLayers) {
		el=document.getElementById(contentLayers[x]);
		el.innerHTML='',
		el.style.left = "-100px";
	}
}

function moveContentLayer(elementId, x, y) {
	toshow=document.getElementById(elementId);
	toshow.style.top = y;
	toshow.style.left = x;
}

function reloadContentLayer(elementId) { 
	if (elementId=="meetingroom") return;
	if (contentLayerDefaults && contentLayerDefaults[elementId]) {
		setContent(elementId, contentLayerDefaults[elementId]);
	}
}
function showContentLayer(elementId) { 
	if (ActiveTimer!=0) { clearTimeout(ActiveTimer); ActiveTimer=0;}
	if (activeContentLayer=='admincontrolledroom') {
		setContent('admincontrolledroom', '');
	}
	if (activeContentLayer==elementId) {
		reloadContentLayer(elementId);
	}
	hideContentLayers();
	moveContentLayer(elementId,"59px", "76px");
	activeContentLayer=elementId;
}

//------------------------- END: Content Layers ----------------------
//------------------------- Begin: Controls ----------------------

var activecontrols=new Array();
var topposition=82;

// to add a control button
function addControl(id,h) {
	el=document.getElementById(id);
	el.style.left = "-100px";
	el.style.top = topposition + "px";
	if(!h){
		topposition+=63;
	}
	else {
		topposition+=h;
	}
	activecontrols.push(id);
}

// Hide the active controls 
function hideControls() {
	for (x in activecontrols) {
		el=document.getElementById(activecontrols[x]);
		el.style.left = "-100px";
	}
}

// Show the active controls 
function showControls() {
	for (x in activecontrols) {
		el=document.getElementById(activecontrols[x]);
		el.style.left = "5px";
	}
}

// Hide and destroy controls
function destroyControls() {

	hideControls();
	topposition=82;
	activecontrols = new Array();
}

//------------------------- END: Controls ----------------------

//------------------------- Begin: HeaderBanner ----------------------
// Header is only for the ROOMS controller
function hideHeaderBanner() {
	el=document.getElementById("header");
	el.style.top = "12px";
	el.style.left = "-1000px";
}

function setHeaderBanner(html) {
	el=document.getElementById("header");
	el.innerHTML=html;
	el.style.top = "12px";
	el.style.left = "59px";
}

function showHeaderBanner() {
	el=document.getElementById("header");
	el.style.top = "12px";
	el.style.left = "59px";
}
//------------------------- END: HeaderBanner ----------------------

function showWaitingMessage() {
	el=document.getElementById('iwaitmessage');
	el.style.top = "200px";
	el.style.left = "300px";
}

//------------------------- Begin: Decobars ----------------------
// NOT USED ?
function showDecobars() {
	el=document.getElementById('deco_top');
	el.style.top = "0px";
	el.style.left = "59px";
	//el=document.getElementById('deco_bottom');
	//el.style.top = "801px";
	//el.style.left = "59px";
}
function hideDecobars() {
	el=document.getElementById('deco_top');
	el.style.left = "-1000px";
	el=document.getElementById('deco_bottom');
	el.style.left = "-1000px";
}

//------------------------- END: Decobars ----------------------

function setBackground(imgsrc) {

	if(server_environment=='prod'){
		imgsrc="http://backgrounds.weetiz.com"+imgsrc;
	}
	document.body.background = imgsrc;
}

function setContent(id, html) {
	el=document.getElementById(id);
	if(el){
		el.innerHTML=html;
	}
}

//------------------------- Begin: Digital Samba Specifics -----------

function showDSLayerIE6() { 
	
	toshow=document.getElementById("meetingroom");

	$("#meetingroom").css("top","59px");
	$("#meetingroom").css("left","76px");
	$("#meetingroom").css("width","978px");
	
	$("#imeet").css("top","59px");
	$("#imeet").css("left","76px");
	$("#imeet").css("width","978px");
	
	iAmIE6andImHidden=false;
}

function hideDSLayerIE6() { 

	$("#meetingroom").css("top","34px");
	$("#meetingroom").css("left","0px");
	$("#meetingroom").css("width","6px");
	
	$("#imeet").css("top","59px");
	$("#imeet").css("left","76px");
	$("#imeet").css("width","6px");

	iAmIE6andImHidden=true;
}

function showRecordingIE6() { 
	toshow=document.getElementById("presentationHolder");
	toshow.style.top = "0px";
	toshow.style.left ="70px";
	toshow.style.width = "970px";
	toshow.style.height = "725px";
	
	toshow=document.getElementById("dsrec");
	toshow.style.top = "0px";
	toshow.style.left ="70px";
	toshow.style.width = "970px";
	toshow.style.height = "725px";
	
	iAmIE6andImHidden=false;
}

function hideRecordingIE6() { 
	toshow=document.getElementById("presentationHolder");
	toshow.style.top = "0px";
	toshow.style.left ="70px";
	toshow.style.width = "10px";
	toshow.style.height = "725px";
	
	toshow=document.getElementById("dsrec");
	toshow.style.top = "59px";
	toshow.style.left ="70px";
	toshow.style.width = "10px";
	toshow.style.height = "725px";
	
	iAmIE6andImHidden=true;
}


function loadDS(dsurl) {
	el=document.getElementById('imeet');
	el.src=dsurl;
}


//------------------------- END: Digital Samba Specifics -----------

//------------------------- Begin: Divisions ----------------------
var divisionList=new Array();
function hideDivision(elementId) {
	toshow=document.getElementById(elementId);
	toshow.style.top = "-1000px";
	toshow.style.left = "-1000px";
	
}

// Hide All Divisions
function hideAllDivisions() {
	for (x in divisionList) {
		el=document.getElementById(divisionList[x]);
		el.style.left = "-1000px";
		el.style.top = "-1000px";
	}
	divisionList = new Array();
}

function showDivision(elementId, xLeft, yTop) {
	hideAllDivisions();
	toshow=document.getElementById(elementId);
	toshow.style.left = xLeft;
	toshow.style.top = yTop;
	divisionList.push(elementId);
}

function delayedShowDivision(elementid, timeout,xLeft, yTop) {
        ActiveTimer=setTimeout("showDivision('"+elementid+"', '"+xLeft+"', '"+yTop+"')", timeout);
}
//------------------------- End: Divisions ----------------------
//------------------------- Begin: Global Funcs ----------------------

function getPushContentFromUrl(url) {
	ret='<iframe id="ipushContent" name="ipushContent" width="970" height="725" FRAMEBORDER=0 SCROLLING=no '
	ret+='src="'+url+'"></iframe>';
	return ret;
}

function getWaitingRoom(id,sts) {
	ret='<iframe id="iwaitingroomiframe" name="iwaitingroomiframe" width="100%" height="950px" ';
	ret+='FRAMEBORDER=0 SCROLLING=no src="';
	ret+='/rooms/waiting/index/'+id+'/'+sts;
	ret+='"></iframe>';
	return ret;
}


function unlockMeetingRoom(timeout) {
	canIshowMeetingRoom=true;
}

function unlockTeaser(timeout) {
	canIshowMeetingRoom=false;
        UnlockTimer=setTimeout("unlockMeetingRoom()", timeout*1000);
}

function delayedshowAuditorium(timeout) {
        ActiveTimer=setTimeout("showAuditorium()", timeout);
}

function clearAll() {

	if (ActiveTimer!=0) { clearTimeout(ActiveTimer); ActiveTimer=0;}
	hideDecobars();
	hideHeaderBanner();
	destroyControls();
	setBackground('');
	clearContentLayers();
}

function hideAll() {

	if (ActiveTimer!=0) { clearTimeout(ActiveTimer); ActiveTimer=0;}
	hideDecobars();
	hideHeaderBanner();
	hideControls();
	hideContentLayers();
}

function createMediaPlayer(placeholder,videourl,w,h,options){


	//Already Exists? 
	var placeholderExists=document.getElementById("wtzmp_"+placeholder);
	if(placeholderExists!=null){
		//lets use the current one to play file
		wtzmp.sendEvent('LOAD',videourl);
		return;
	}

	var flashVer="8";
	
	var params = {
	  height: h,
	  width: w,
	  allowfullscreen: "true",
	  allowscriptaccess: "always"
	};
	
	var attributes = {
	  id:  "wtzmp_"+placeholder,
	  name:"wtzmp_"+placeholder
	};
	
	
	var flashvars = {
		height: h,
	  	width: w,
		file: videourl,
		shownavigation: "true",
		autostart: "false",
		bufferlength: "2",
		frontcolor: "ffffff",
		lightcolor: "182e58",
		skin : "/static/swf/mediaplayer/stylish.swf",
		controlbar: "over",
		abouttext: "Weetiz.com",
		aboutlink: "http://www.weetiz.com",
		type: "video"
	};
	
	if(options.allowfullscreen)
		params.allowfullscreen = options.allowfullscreen;
	if(options.shownavigation)
		flashvars.shownavigation = options.shownavigation;

	if(options.autostart)
		flashvars.autostart = options.autostart;
	if(options.bufferlength)
		flashvars.bufferlength = options.bufferlength;	
	if(options.overstretch)
		params.overstretch = options.overstretch;
	if (options.captions)
		flashvars.captions = options.captions;
	if (options.showdownload)
		flashvars.showdownload = options.showdownload;
	if (options.controlbar)
		flashvars.controlbar = options.controlbar;
	if (options.tracecall)
		flashvars.tracecall = options.tracecall;
	
	swfobject.embedSWF("/static/swf/mediaplayer/player.swf", placeholder,w,h,flashVer,"", flashvars, params, attributes);
}

var wtzmp=null;

function playerReady(thePlayer) {
	wtzmp = window.document[thePlayer.id];
}


function createTextBalloon(x,y,text){	
	var content =
	'<div id="bubble_tooltip"><div class="bubble_top"><span></span></div> <div class="bubble_middle"><span id="bubble_tooltip_content">'+text+'</span></div> <div class="bubble_bottom"></div></div>';
	
	document.writeln(content);
	
	var obj = document.getElementById('bubble_tooltip');
	//var obj2 = document.getElementById('bubble_tooltip_content');
	//obj2.innerHTML = text;
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	
	///////////if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 

	obj.style.left = x + 'px';
	obj.style.top = y - obj.offsetHeight -1 + st + 'px';
	
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			  var value = arr[item];

			  if(typeof(value) == 'object') { //If it is an array,
				   dumped_text += level_padding + "'" + item + "' ...\n";
				   dumped_text += dump(value,level+1);
			  } else {
				   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			  }
		 }
	} else { //Stings/Chars/Numbers etc.
		 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

//------------------------- End: Global Funcs ----------------------








// Open Full Room
function showFlash() {

	destroyControls();
	hideDecobars();

}

//This funciton decides if we send you to the waiting room or to the guestroom
function roomAssistant(roomid, userid) {

	createWaitingRoom(roomid, userid);

}

function showWaitingRoom(id, gostatus) {

	////setBackground('/static/themes/'+theme+'/wait_loading.jpg'); DONT SHOW THE LOADING BG...
	$("body").css("background-image","url('/static/images/loading_bar.gif')");
	$("body").css("background-position","center 308px");
	$("body").css("background-repeat","no-repeat");

 	hideAll();
 
 	var waitingRoomContent;
 	waitingRoomContent =  getWaitingRoom(id, gostatus);
 	
 	//alert("setting content..."+waitingRoomContent);
 	
	setContent('iwaitingroom',waitingRoomContent);
	
	//alert("moving content...");
	moveContentLayer('iwaitingroom',"0px", "0px");
}

function roomMonitor(msg) {
	if (msg.status=="open") {
		if (roomStatus=="closed") {
			loadDS(dsLoadUrl);
			parent.showLoadingRoomBalloon();
		}
		else{
			showMeetingRoom();
		}
	} else if (msg.status=="closed") {
		setContent("imeet","");
		showWaitingRoom(msg.id, "after");
	} else {
		if (roomStatus=="closed") {
			loadDS(dsLoadUrl);
		}
		showWaitingRoom(msg.id, msg.status);
	}
	
	roomStatus=msg.status;
}

function showMeetingRoom() {

	hideAll();
	showControls();
	
	//setBackground('/static/themes/'+theme+'/in_background.jpg');
	$("body").css("background-image","url('/static/images/room/bg.gif')");
	$("body").css("background-position","0 0");
	$("body").css("background-repeat","repeat-x");
	
	showDecobars();
	showHeaderBanner();
	showControls();
	
	if(iAmIE6andImHidden) showDSLayerIE6();
	
	showContentLayer('meetingroom');
	
	
}


function showAuditorium() {

	if (iAmIE6andImHidden) showRecordingIE6();
	else showDivision('presentationHolder','70px','0px');

}

function confirmExit(id) {
        var answer = confirm("Souhaitez-vous vraiment quitter la conference?");
        if (answer){
			showWaitingRoom(id,'after');
        }
}



function building(destination) {

	var currDomain = document.domain;

	if (destination=="hall") {
		swffile="/swfs/loader_weetic_12.swf?entradaConferencia=directa";
	} else if (destination=="elevator") {
		swffile="/swfs/loader_weetic_12.swf?entradaConferencia=elevator";
	} else if (destination=="pressroom") {
		swffile="/swfs/loader_weetic_12.swf?entradaConferencia=elevator";
	} else {
		swffile="/swfs/loader_weetic_12.swf";
		//swffile="/swfs/loader_weetic_12.swf?dummy=1";
	}
	
	//swffile="/swfs/loader_weetic_11.swf?entradaConferencia=directa";
	//TESTING
	//swffile="/swfs/loader_weetic_12.swf?cargaHostess=no";
	//Bypass the hostess loading if testing
	//if(currDomain == "localhost"){
	//	swffile += '&cargaHostess=no';
	//	alert(swffile);
	//}
	
	var flashcode = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="weetic" align="middle">';
	flashcode += '<param name="allowScriptAccess" value="sameDomain" />';
	flashcode += '<param name="movie" value="'+swffile+'" />';
	flashcode += '<param name="quality" value="high" />';
	flashcode += '<param name="bgcolor" value="#ffffff" />';
	flashcode += '<embed src="'+swffile+'" quality="high" bgcolor="#ffffff" width="100%" height="100%" name="weetic_1" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	flashcode += '</object>';

	setContent('flashBuilding', flashcode);
}

function elevatorClicked(floor){

	//8.Corporate
	if (floor=="btn8") {
		window.location = "/corporate";
	}
	//7.Press Room
	if (floor=="btn7") {
		window.location = "/pressroom";
	}
	//6.Space Clients
	if (floor=="btn6") {
		window.location = "/admin";
	}
	//5.Auditorium
	if (floor=="btn5") {
		window.location = "/auditorium";
	}
	//4.Expositions
	if (floor=="btn4") {
		window.location = "/events";
	}
	//3.conferences
	if (floor=="btn3") {
		window.location = "/rooms";
	}
	//2.Salles de reunion
	if (floor=="btn2") {
		window.location = "/rooms";	
	}
	//1.Accueil - Hall
	if (floor=="btn1") {
		window.location = "/go/hall";
	}
}

function elevator_corporate() { 
	window.location = "/corporate";
}

function elevator_contact() { 
	window.location = "/corporate/content/view/13/34/";
}


/*1 Button: INFOS*/
function elevator_adminWTC() { 
	window.location = "/corporate";
}

/*2nd Button PressRoom*/
function elevator_pressroom() {
	window.location = "/pressroom";
}

/*3rd Button Space Clients*/
function elevator_partners(){
	window.location = "/admin";
}

/*4th Button Auditorium*/
function elevator_auditorium(){
	window.location = "/auditorium";
}

/*5th Button expositions*/
function elevator_expositions(){
	window.location = "/events";
}

//------------------ AMS Functions -----------------
function showAlert(token) {
	alert(token.content);
}
function goRoom(token) {
	reloadContentLayer(token.tab);
	showContentLayer(token.tab);
}
// Show in the admincontrolledtab the content pushed by the admin
function pushContent(token) {
	setContent('admincontrolledroom', token.content);
	showContentLayer('admincontrolledroom');
}
// Show in the admincontrolledtab the content pushed by the admin
function pushURL(token) {
	setContent('admincontrolledroom', getPushContentFromUrl(token.content));
	showContentLayer('admincontrolledroom');
}

function amsForceLogout(token) {
	showWaitingRoom(token.id,'after');
}

//---------WGC Related ----------------
function WGCSetBackground(bgurl,fireOnLoad){


//	if($.browser.msie && /6.0/.test(navigator.userAgent)){

		$(document).ready(function(){
			$("#WGC_content").css("background-image","url("+bgurl+")");
				eval(fireOnLoad);
		});

	/*}
	else
	{
	
		var img = new Image();
		img.src = bgurl;
		
		$(img).load(function () { 
			$("#WGC_content").css("background-image","url("+bgurl+")");
			eval(fireOnLoad);
		});
	}
	*/
}