// JavaScript Document

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';
	document.getElementById('main').style.visibility='visible';
	opacity('faderlayer',100,0,500);
}

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;
}

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 (height <= maxheight ) {
		$(cdiv).tween('height', [maxheight, endheight+10]);	
		if(divmlt.length>0) document.getElementById(divmlt).innerHTML='show less';
	} else {
		$(cdiv).tween('height', [endheight+10, maxheight]);	
		if(divmlt.length>0) document.getElementById(divmlt).innerHTML='show more';
	}
}