/***************************************************************
 * 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);
                      }
                    }
                  );
                  
  

}

function getUserInfo(uid, field, id){
	
	var url = "/mods/members/getUserData.php?uid="+uid+"&field="+field;

	new Ajax.Request(url, {
                      asynchronous:true,
                      evalScripts:true,
                      onComplete: function(transport)  {
                        //make the page toggle now
						            $(id).update(transport.responseText);
                        
                       // $('userstatus').update(text);
                      }
                    }
                  );
  
}

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

}
