// JavaScript Document
function confirmAction($message) {
	return confirm($message);
}

function m() {
	strURL = "m"
	strURL = strURL + "a"
	strURL = strURL + "i"
	strURL = strURL + "l"
	strURL = strURL + "to:"
	strURL = strURL + "info"
	strURL = strURL + "@"
	strURL = strURL + "vitagion"
	strURL = strURL + ".nl"
	strURL = strURL + "?subject=info"
	top.location.href=strURL
}

var timer;
window.addEvent('domready', function(){
	if (document.getElementById("hss")) {
		
		window.addEvent('resize', function(){
			$clear(timer);
			timer = reposHssHomePage.delay(20);
		});
		
		reposHssHomePage();	
		document.getElementById("hss").style.visibility="visible";
		document.getElementById("streamer").style.visibility="visible";
		$('substreamer').set('tween', {duration: '20000'});
		$('substreamer').tween('opacity',['0'], ['100'])
	} else if (document.getElementById("st_sq")) {
		$('st_sq').set('tween', {duration: '1000'});
		$clear(timer);
		timer = tweenSquareDefaultPage.periodical(3000);
	}
});

function tweenSquareDefaultPage() {
	if ($('st_sq').getStyle('background-color') =='#82B3CC') $('st_sq').tween('background-color', ['#82B3CC'], ['#D0E2ED']);	
	else $('st_sq').tween('background-color',['#D0E2ED'], ['#82B3CC']);	
}

function reposHssHomePage() {
	windowHeight=document.documentElement.clientHeight;
	BGimageHeight=document.getElementById("background-img").height;
	heightHSS=(windowHeight)*0.57;
	if (heightHSS<280) heightHSS=280;
	streamerH=(heightHSS*0.75)-300;
	if (streamerH<0) streamerH=0;
	if (BGimageHeight<heightHSS+210+35) BGimageHeight=heightHSS+210+35;
	if ( (BGimageHeight<windowHeight) || (windowHeight>heightHSS+210+35)) BGimageHeight=windowHeight;
	document.getElementById("background-img").style.height=BGimageHeight+"px";
	document.getElementById("streamer").style.top=streamerH+"px";
	document.getElementById("hss").style.top=heightHSS-130+"px";
	document.getElementById("footer").style.top=BGimageHeight+"px";
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 		
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
	if (opacity==0) document.getElementById(id).style.display='none';
}

function fademain() {
	window.addEvent('domready', function() {
//			 executefademain();
	});
}

function executefademain() {
	faderLayer = document.getElementById('faderlayer');
	faderLayer.style.height=getHeightDiv(document.getElementById('main'))+'px';
	if (document.getElementById("vpreview")) document.getElementById("vpreview").style.top="0px";													 
	if (document.getElementById("spreview")) document.getElementById("spreview").style.top="0px";													 
	document.getElementById('main').style.visibility='visible';
	var id = setTimeout('opacity(\'faderlayer\',100,0,500)', 150);	
}

function getHeightDiv(obj) {
	if (document.all) {
		h = obj.offsetHeight;
	}
	else {
		h = obj.offsetHeight;
	}
	return h;
}
 
var maxi;

function fadeentry(i) {
	maxi=i;
	document.getElementById('faderlayer'+i).style.height=getHeightDiv(document.getElementById('searchresultEntry'+i))+'px';
	if (i==0) window.addEvent('domready', function() {
		executefadeentry(0);
	});
}
 
function executefadeentry(i) {
	var id = setTimeout('fadeentryitem('+i+')', 250);
}

function fadeentryitem(i) {
	if (i!=-1) opacity('faderlayer'+i,100,0,1250);
	if ((i)<maxi) executefadeentry(i+1);
}
 
function showsubmenu() {
	height=document.getElementById('submenu').offsetHeight;
	$('submenuwrapper').tween('height', [0, height]);	
}

//Maintains the last div that was set to play including player, url & title
var lastplayed=null;
var lastplayer=null;
var lasturl=null;
var lasttitle=null;

function togglePlayStop(player,div,command,url,title) {
	if (command=='play') {		
		player = document.getElementById(player);
		player.sendEvent('LOAD',url);
		player.sendEvent('PLAY');
		content='<a href="javascript:togglePlayStop('+player.id+',\''+div+'\',\'stop\',\''+url+'\',\''+title+'\');">stop</a>: '+title;
		if (lastplayed!=null) { 
			document.getElementById(lastplayed).innerHTML='<a href="javascript:togglePlayStop(\''+lastplayer.id+'\',\''+lastplayed+'\',\'play\',\''+lasturl+'\',\''+lasttitle+'\');">play</a>: '+lasttitle;
			if (lastplayer.id!=player.id) lastplayer.sendEvent('STOP');		
		}
		lastplayed=div;
		lastplayer=player;
		lasturl=url;
		lasttitle=title;
	} else {
		lastplayer.sendEvent('STOP');		
		content='<a href="javascript:togglePlayStop(\''+lastplayer.id+'\',\''+lastplayed+'\',\'play\',\''+lasturl+'\',\''+lasttitle+'\');">play</a>: '+lasttitle;	
		lastplayed=null;
	}	
	document.getElementById(div).innerHTML=content;
}

var playerX = null;
function playerReady(thePlayer) {
	playerX = document.getElementById(thePlayer.id);
	addListeners(playerX);
}

function addListeners(playerX) {
	if (playerX) { 
		playerX.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners(playerX)",100);
	}
}

var lastplayerX = null;
function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	if (lastplayerX) {
		if (lastplayerX!=obj.id) {
			playerX = document.getElementById(lastplayerX);
			playerX.sendEvent('STOP');		
		}
	}
	lastplayerX=obj.id;
}

function URLDecode(text) {
   // Replace + with ' ', Replace %xx with equivalent character
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = text;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	}
   return plaintext;
};

function menuAction(div,url) {
	window.location=url;	
	height=document.getElementById(div).offsetHeight;
	document.getElementById('wrapper_'+div).offsetHeight=heigth;
}

// Begin AJAX functions

function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
//        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-type", "text/html");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { }
}
 
function processData(responseText, responseStatus) {
  if (responseStatus==200) {
    alert(responseText);
  } else {
    alert(responseStatus);
  }
}

//entryTemplaterAction
function entryTemplaterAjaxAction(page_id,entry_id,to_return,cdiv,div,divmlt,maxheight) {	
	var url="PagesHandler.php?ajax=true&page_id="+page_id+"&EntriesFormHandler.php&action=view";
	url=url+"&entry_id="+entry_id;
	url=url+"&to_return="+to_return;
	url=url+"&sid="+Math.random();
	var myRequest = new ajaxObject(url, processData);  
	myRequest.callback = function(responseText) {
		document.getElementById(div).innerHTML=responseText;
		window.addEvent('domready', function() {
			toggleMoreLess(cdiv,div,maxheight);	
		});
	}
	myRequest.update();	
}

//entryTemplaterAction
function pageTemplaterAjaxAction(page_id,entry_id,to_return,cdiv,div,divmlt,maxheight) {	
	var url="PagesHandler.php?ajax=true&page_id="+page_id+"&EntriesFormHandler.php&action=view";
	url=url+"&entry_id="+entry_id;
	url=url+"&to_return="+to_return;
	url=url+"&sid="+Math.random();
	var myRequest = new ajaxObject(url, processData);  
	myRequest.callback = function(responseText) {
		document.getElementById(div).innerHTML=responseText;
		window.addEvent('domready', function() {
			toggleMoreLess(cdiv,div,maxheight);	
		});
	}
	myRequest.update();	
}

function toggleMoreLess(cdiv,div,maxheight,divmlt) {
	height=document.getElementById(cdiv).offsetHeight;
	endheight=document.getElementById(div).offsetHeight;
	if (maxheight=='auto') maxheight=0;
	if (height <= maxheight ) {
		$(cdiv).tween('height', [maxheight, endheight+0]);	
		if(divmlt.length>0) document.getElementById(divmlt).innerHTML='show less';
	} else {
		$(cdiv).tween('height', [endheight+0, maxheight]);	
		if(divmlt.length>0) document.getElementById(divmlt).innerHTML='show more';
	}
}
