/***************************************************************
 * Javascript functions and classes common to all engine pubs.
 * located: /mods/common/scripts/ 
 *
 * compiled by: John Ward 1.9.08
 * 
 * written by: John Ward
 *             Ron Phillips
 *
 * *****************************************************************/      


/************************** AJAX functions ****************************/

function ajaxUpdater(id,url) {  
  displayAnimation(id);
  
  new Ajax.Updater(id,url,{
            asynchronous:true,
            evalScripts:true});  
  
}

function displayAnimation(id) {
  $(id).update('<img src="/mods/common/scripts/wait_big.gif">');
}

/* END AJAX */ 



/*******************************  POLLS *********************************
 *                       
 *  Replaces a standard named div 'poll_container' with each respective sites
 *  polls.results.tpl.html and polls.questions.tpl.html depending on the state
 *  of the voting cookie.  
 *
 */  

function pollVote(qid, pollform){
  
  var ser = Form.serialize($(pollform))+"&qid="+qid;
  
  var url = "/mods/polls/quickUpdate.php?"+ser;
  
  new Ajax.Request(url, {
                      asynchronous:true,
                      evalScripts:true,
                      onComplete: function(transport)  {
                        //make the page toggle now
                        checkPollState(qid);
                       // var text = transport.responseText;
                       // $('userstatus').update(text);
                      }
                    }
                  );
                  
  

} 

function pollMultiVote(qid, pollform, container){
  
  var ser = Form.serialize($(pollform))+"&qid="+qid;
  
  var url = "/mods/polls/quickUpdate.php?"+ser;
  
  new Ajax.Request(url, {
                      asynchronous:true,
                      evalScripts:true,
                      onComplete: function(transport)  {
                        //make the page toggle now
                        checkMultiPollState(qid, container);
                       // var text = transport.responseText;
                       // $('userstatus').update(text);
                      }
                    }
                  );
                  
  

}

var cnUser = new function(){
	this.id=0;
	this.username=0;
	this.email=0;
	this.status=0;
	this.create_date=0;
};
function getUserInfo(uid, field, id){
	var url = "/mods/members/getUserData.php?uid="+uid;
	if(window.XMLHttpRequest)
		a=new XMLHttpRequest();
	else if(window.ActiveXObject)
		a=new ActiveXObject("Microsoft.XMLHTTP");
	a.open("get",url,true);
	a.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	a.setRequestHeader("Content-length", 0);
	a.setRequestHeader("Connection", "close");
	a.onreadystatechange=function(){
		if(a.readyState==4){
			eval(a.responseText);
			$("members").innerHTML = cnUser.username;
		}
	};
	a.send();
}
function checkPollState(pollid) {

  if(Cookie.get(pollid) == 'voted') {
    var filename = "polls.results.tpl.html";
  }
  else {
    var filename = "polls.questions.tpl.html";
    
  }
  
  ajaxUpdater('poll_container', "/mods/common/scripts/ajaxEngine.php?filename="+filename+"&qid="+pollid);
  
}
function uname() {
	ajaxUpdater('uname_container',"/mods/common/scripts/uname.php");
}
function checkMultiPollState(pollid, container) {

  if(Cookie.get(pollid) == 'voted') {
    var filename = "polls.results.tpl.html";
  }
  else {
    var filename = "polls.questions.multi.tpl.html";
    
  }
    
  ajaxUpdater(container, "/mods/common/scripts/ajaxEngine.php?filename="+filename+"&qid="+pollid);
  
}

function showPollResults(pollid){
  var filename = "polls.results.tpl.html";
  ajaxUpdater('poll_container', "/mods/common/scripts/ajaxEngine.php?filename="+filename+"&qid="+pollid);
}

/* END POLLS */



/************************* Obits *******************************************/

function chnageObitDate(input) {
  window.location = "/Obituaries/"+$F(input);
}

/* End obits */



/************************** Story Tools **************************************
 *
 * CODED BY: Ron PHillips
 *
 *compiled by: John ward 11/13/2007.  Compiled all the JS functions need for the 
 *In-Story tool-kits since it was common across all sites.
 */
 
function emailStory(){
  
	if(document.getElementById('emailbox').style.display == 'none')
		document.getElementById('emailbox').style.display = 'block';
	else if(document.getElementById('emailbox').style.display == 'block')
		document.getElementById('emailbox').style.display = 'none';

  document.getElementById('emailform').action = "?build=yes";
    	
	document.getElementById('title').value = document.getElementById('storytitle').innerHTML;
	document.getElementById('story').value = document.getElementById('storybody').innerHTML;
	document.getElementById('intro').value = document.getElementById('storyintro').innerHTML;
	document.getElementById('pubdate').value = document.getElementById('storydate').innerHTML;
	document.getElementById('author').value = document.getElementById('authors').innerHTML;
	
}

function emailObit(id) {
  var tmp = $('emailbox').innerHTML;
  
  var obit = 'email_'+id;
  $(obit).update(tmp).show();
   

  return false;

}

function changeFontSize(el,inc)
{
  var p = document.getElementById(el);
  var min = .5;
  var max = 1.5;
 
  if(p.style.fontSize) {
     var size = parseFloat(p.style.fontSize.replace("em", ""));
  } else {
     var size = 1;
  }
  
  var newsize = size+inc;
  
  if(newsize >= min && newsize <= max){
  
	 p.style.fontSize = newsize + 'em';

  }
  
  if(newsize < min){
	 document.getElementById('textminus').style.visibility = "hidden";
  } else {
	 document.getElementById('textminus').style.visibility = "visible";
  }
	 
	 
  if(newsize > max){
	 document.getElementById('textplus').style.visibility = "hidden";
  } else {
	 document.getElementById('textplus').style.visibility = "visible";	
  }
	   
 
}
/*************************** END STORY TOOLS *************************/






/************************* JS FUNCTION TO parse GET PARAMS ***************/
function parseQueryString(name){

  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];

}

/************************************************************************/


/*********************** JS highlight image function **********************/
function flashON(image){
    image = $(image);
    
    new Effect.Opacity(image, {
      from: 1.0,
      to: .65,
      duration: .25,
      queue: 'front'
    });
    
    new Effect.Opacity(image, {
      from: .65,
      to: 1.0,
      duration: .25,
      queue: 'end'
    });

}

/*Process class allows the dynamic management of page onload and onunload attributes*/
/*Added 05.31.11 by lgshort*/
var Process = new function(){
	this.loadArr = Array();
	this.unloadArr = Array();
	this.load = function(){
		for(var i=0;i<this.loadArr.length;i++)
			eval(this.loadArr[i]);
	};
	this.unload = function(){
		for(var i=0;i<this.unloadArr.length;i++)
			eval(this.unloadArr[i]);
	};
	this.addLoad = function(tmp){
		this.loadArr[this.loadArr.length] = tmp;
	};
	this.addUnload = function(tmp){
		this.unloadArr[this.unloadArr.length] = tmp;
	};
};
window.onload = function(){Process.load();};
window.unload = function(){Process.unload();};
/***********************************************************************************/

/*Roadblock class allows story pages to roadblock readers who are not logged in*/
/*Added 05.31.11 by lgshort*/
var Roadblock = new function(){
	this.userStatus = "guest";
	this.hide = Array();
	this.show = Array();
	this.blockStatus = "n";
	this.run = function(s){
		this.userStatus = s;
		this.blockStatus = $("nonfree").value;
		this.processPage();
	};
	this.processPage = function(){
		if(getURLVar("paywall")!="true")
			return;
		if(IE){
			var div = document.getElementsByTagName("div");
			for(var i=0;i<div.length;i++){
				if(div[i].name=="roadblock_hide")
					this.hide[this.hide.length] = div[i];
				else if(div[i].name=="roadblock_show")
					this.show[this.show.length] = div[i];
			}
		}else{
			this.hide = document.getElementsByName("roadblock_hide");
			this.show = document.getElementsByName("roadblock_show");
		}
		if(this.blockStatus=="y" && this.userStatus!="member"){
			for(i=0;i<this.show.length;i++)
				if(this.show[i].innerHTML.replace(/^\s+|\s+$/g,"")!="")
					this.show[i].style.display = "block";
			for(i=0;i<this.hide.length;i++)
				this.hide[i].style.display = "none";
		}
	};
};


