/*
 * MonkCMS stuff
 */

function gotoSermon2() {
  var x=document.getElementById("sermonLists");
  var go=x.options[x.selectedIndex].value;

  window.location=go;
}

function gotoArticle() {
  var x=document.getElementById("articleLists");
  var go=x.options[x.selectedIndex].value;

  window.location=go;
}

function gotoContent(x) {
  var go=x.options[x.selectedIndex].value;

  window.location=go;
}

function wimpyPopPlayer(theFile,id,stuff) {
  window.open(theFile,id,stuff);
}

/*
 * Wrote this wrapper to stay compatible / eT Oct 2008
 */
function waspPopup (filename, width, height){
  window.open('http://my.ekklesia360.com/Clients/waspPopup.php?theFile='+filename+'&w='+width+'&h='+height,'_mediaplayer','width='+width+',height='+height);

}

function doJSEvents() {
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("mcmsSearch")) {
      links[i].onclick = function() {
        document.getElementById('searchForm').submit();
        return false;
      };
    }
    if (links[i].className.match("mcmsSearch2")) {
      links[i].onclick = function() {
        document.getElementById('searchForm2').submit();
        return false;
      };
    }
    if (links[i].className.match("thickbox")) {
      links[i].onmouseover = function() {
        window.status='';
		return true;
      };
    }
 }
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i < inputs.length; i++) {
    if (inputs[i].className.match("clearClick")) {
      inputs[i].onfocus = function() {
        if (document.getElementById('search_term')) document.getElementById('search_term').value='';
        if (document.getElementById('search_term2')) document.getElementById('search_term2').value='';
        if (document.getElementById('newsletter_text')) document.getElementById('newsletter_text').value='';
        return false;
      };
    }
  }
}

//created separate function so with ajax filtering this can be initiated to add click handler to new links
function doMediaEvents(){
	var links = document.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) { 
   		if (links[i].className.match("mcms_audioplayer") || links[i].className.match("mcms_videoplayer")) {
	      var player = new Object();
		    player[i] = new MonkMedia.popup(links[i]);
		    MonkMedia.popup.prototype.doOnClick = function(event,element){
		         this.clickHandler();
             event.preventDefault ? event.preventDefault() : event.returnValue = false; //IE<9 uses event.returnValue 
		    }
		}
	}   
}

function init(){ 
	if (!document.getElementsByTagName){
	  return false;
	}
	   doJSEvents();
	   doMediaEvents();
}

window.onload=init;

function changeFieldValue(fieldName,fieldValue) {
  document.getElementById(fieldName).value=fieldValue;
}

function monkIsValidEmail(emailAddress) {
  atPos=emailAddress.indexOf("@");
  dotPos = emailAddress.indexOf(".", atPos);
  spacePos = emailAddress.indexOf(" ");
  if ((emailAddress.length > 0) && (atPos > 0) && (dotPos > atPos) && (spacePos == -1)) {
    return true;
  }
  alert("Please enter a valid email address.");
  return false;
} 
 
/*
   Monk Media Popup v.1 - a script to open window with videoplayer
   (c) 2010 Adam Randlett : monkdevelopment.com
*/  
//associates the audio or video anchor on click event 
//with a new instance of MonkMedia Popup object
function associateObjWithEvent(obj, methodName){
    return (function(e){
        e = e||window.event;
        return obj[methodName](e, this);
    });
}
//MonkMedia.popup object
var MonkMedia = MonkMedia || {};
MonkMedia.popup = MonkMedia.popup || function(obj){
   this.globalwidth='';
   this.globalheight='';
   this.options = {};
   this.element = obj;
   this.eurl = this.element.href;
   this.offsetHeight = 25; //acounts for window heading height
  
   this.gup = function(name){
		  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		  var regexS = "[\\?&]"+name+"=([^&#]*)",
		  	  regex = new RegExp( regexS ),
		  	  results = regex.exec( this.eurl );
		  if( results === null ){
		    return "";
		  }else{
		    return results[1];
		  }
	};
	
	this.options = {
		width:this.gup('width'),
		height:this.gup('height'),
		playlist:this.gup('playlist'),
		template:this.gup('template') //"/_player/videoplayer.php"  
	};
	
    //assigns on click event with anchor element
	this.element.onclick = associateObjWithEvent(this, "doOnClick");   

	this.clickHandler = function(){
		if(this.options.playlist !== "false"){
			this.pwidth = 240;
			this.varwidth = Number(this.options.width) + this.pwidth;
			this.globalwidth = String(this.varwidth);  
		}else{
			this.globalwidth = this.options.width;
		} 
			
		this.globalheight = Number(this.options.height) + this.offsetHeight;
		this.indx = this.eurl.indexOf("?");
		this.attr = this.eurl.substring(this.indx);
		this.url = this.options.template +this.attr+"&target=MediaPlayer"; 
		
		window.open(this.url,"MediaPlayer","menubar=no,resizable=no,width="+this.globalwidth+",height="+this.globalheight+",scrollbars=no,status=no"); 
	};
};

