var IE = (navigator.appVersion.indexOf("MSIE ")==-1) ? false : true;

function acg_lbcf_pop(artistName) {
	lbcf_showLoading();
	url = location.href;
	new lbcf_loadXmlHttp(acg_lbcf_url + "?acg_lbcf_artist=" + artistName + "&url=" + url, "acg_lbcf_div");
}

function acg_lbcf_submit(el) {
	var url = "", sel = "?";
	var i, pname, param;
	if (el == "unsubscribe") {
		url = "?action=unsubscribe";
	}
	else {
		for (i = 0; i < el.elements.length; i++) {
			if (el.elements[i].type == "checkbox" ) {
				pname = (el.elements[i].checked) ? el.elements[i].name : ""
				param = (el.elements[i].checked) ? "checked" : "";
			} else if (el.elements[i].type == "radio") {
				pname = (el.elements[i].checked) ? el.elements[i].name : ""
				param = (el.elements[i].checked) ? el.elements[i].value : "";
			} else {
				pname = el.elements[i].name;
				param = el.elements[i].value;
			}
			url += (pname) ? sel + pname + "=" + encodeURIComponent(param) : "";
			sel = "&";
		}
	}
	new lbcf_loadXmlHttp(acg_lbcf_url+url, "acg_lbcf_div");
	return false;
}

// **** CORE AJAX FUNCTION ****

function lbcf_loadXmlHttp(url, id) {
	var f = this;
	var xmlTimeout;
	f.xmlHttp = null;
	/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
	/*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
	try {f.ie = window.ActiveXObject}catch(e){f.ie = false;}
	@end @*/
	if (window.XMLHttpRequest && !f.ie || /^http/.test(window.location.href)) {
		f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
	}
	else if (/(object)|(function)/.test(typeof createRequest)) {
		f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
	} else {
		f.xmlHttp = null;
	// Internet Explorer 5 to 6, includes IE 7+ when local //
	/*@cc_on @*/
	/*@if(@_jscript_version >= 5)
	 try{f.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
	 catch (e){try{f.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){f.xmlHttp=null;}}
	 @end @*/
	}
	if (f.xmlHttp != null) {
		if (id!="") {
			f.el = document.getElementById(id);
		} else {
			f.el = id;
		}

		// Function to append a ranlbcf_Dom number parameter in the url to prevent caching
		var ses=Math.floor(Math.random()*100000000001)
		url = (url.indexOf('?')>0) ? url + "&sesid=" + ses : url+"?sesid=" + ses;
		f.xmlHttp.open("GET", url, true);
		f.xmlHttp.onreadystatechange = function(){
			f.stateChanged();
		};
		f.xmlHttp.send(null);
		// Call timeout function to display message after 10 seconds if response not received
		f.xmlTimeout=setTimeout(function() {f.timeOut();},10000);

	}
	else {
		alert('This browser does not support AJAX!'); // substitute your desired request object unsupported code here
		this.el.innerHTML = "<p style='color: red; font-weight: bold;'>This browser does not support AJAX.  This page will not function properly.</p>";
	}
}

// *** STATE CHANGE HANDLER ***
lbcf_loadXmlHttp.prototype.stateChanged=function () {
	if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href))) {
		// this.el.innerHTML = this.xmlHttp.responseText;
		clearTimeout(this.xmlTimeout);
		var str = this.xmlHttp.responseText;
		if (str.indexOf("|")>0 && str.indexOf("|")<3) {
			var resptext = str.split("|");
			this.el.innerHTML = "<h3>" + resptext[1] + "</h3>";
			setTimeout("lbcf_clearWait()", 2000);
		} else {
			this.el.innerHTML = str;
			if (window.datepicker) {
				window.datepicker.attach();
			}
		}
	}
}

// *** TIMEOUT HANDLER ***
lbcf_loadXmlHttp.prototype.timeOut=function(){
   this.xmlHttp.abort();
   var strMsg = "Oops! The server could not be reached.  Try again, or check your network connection.";
   alert(strMsg);
   lbcf_clearWait(this.el.id, strMsg);
}

/**
* @author Cale
*/

var moveint;
var refreshid;

if (document.images) {
  pic1= new Image(30,30);
  pic1.src="images/loading.gif";
}

var lbcf_showLoading = function() {
	var ls = lbcf_Dom.get("acg_lbcf_blur");
	var top = "200";
	if(ls == null) {
		var ls = document.createElement("div");
		ls.id = "acg_lbcf_blur";
		var body = lbcf_Dom.get("body");
		lbcf_Dom.add(ls, body);
	}
	var ld = lbcf_Dom.get("acg_lbcf_div");
	if (ld==null) {
		var ld = document.createElement("div");
		ld.id = "recipediv";
		var body = lbcf_Dom.get("body");
		lbcf_Dom.add(ld, body);
	}
	var height = lbcf_getSize();
	var top = lbcf_getScrollXY();
	ls.top = top + "px";
	ls.height = height + "px";
	ld.top = height + "px";
	lbcf_changeOpac(0, "acg_lbcf_blur");
	lbcf_changeOpac(0, "acg_lbcf_div");
	ls.style.display="block";
	ld.innerHTML='<p class=\"loading\">Loading Form...</p>';
	ld.style.display="block";
	lbcf_fade("acg_lbcf_blur", 0, 60, 200);
	lbcf_fade("acg_lbcf_div", 0, 100, 200);
	moveint = setInterval("lbcf_moveBlur()", 10);
}

function lbcf_clearWait() {
	lbcf_fade("acg_lbcf_div", 100, 0, 200);
	lbcf_fade("acg_lbcf_blur", 60, 0, 200);
	setTimeout("lbcf_hideLoading()", 700);
}

var lbcf_hideLoading = function() {
	var ls = lbcf_Dom.get("acg_lbcf_blur");
	var ld = lbcf_Dom.get("acg_lbcf_div");
	if (ls!= null) {
		ls.style.display = "none";
	}
	if (ld) {
		ld.style.display = "none";
	}
}

//change the opacity for different browsers
function lbcf_changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	if (IE) {
		object.filter = "alpha(opacity=" + opacity + ")";
		return;
	}
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
}

function lbcf_fade(id, opacStart, opacEnd, millisec) {
    //speed for each frame
//    var speed = Math.round(millisec / 100);
    var speed = 0.3;
	var timer = 0;
	if (IE) {
		speed = Math.round(speed/30);
	}
    //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("lbcf_changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("lbcf_changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

function lbcf_getSize(x) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if (x) {
  	return [myWidth];
  } else {
  	return [myHeight];
  }
}

function lbcf_getScrollXY(x) {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //lbcf_Dom compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  if (x) {
  	return [ scrOfX ];
  } else {
	return [ scrOfY ];
  }
}

function lbcf_moveBlur() {
	var el = lbcf_Dom.get("acg_lbcf_blur");
	if (el!=null) {
		var height = lbcf_getSize();
		var top = lbcf_getScrollXY();
		el.style.top = top+"px";
		el.style.height = height+"px";
		el = lbcf_Dom.get("acg_lbcf_div");
		if (el!=null) {
			top=parseInt(top);
			top+=(parseInt(height)/2)-150;
			el.style.top = top+"px";
		}
	}
}


 var lbcf_Dom = {
    get: function(el) {
      if (typeof el === 'string') {
        return document.getElementById(el);
      } else {
        return el;
      }
    },
    add: function(el, dest) {
      var el = this.get(el);
      var dest = this.get(dest);
      dest.appendChild(el);
    },
    remove: function(el) {
      var el = this.get(el);
      el.parentNode.removeChild(el);
    },
	exists: function(el) {
		if (typeof el =='string') {
			if (document.getElementById(el) !=null) {
				return true;
			} else {
				return false;
			}
		} else {
			if (el != null) {
				return true;
			} else {
				return false;
			}
		}
	}

  };


