
/* Simple AJAX Code-Kit (SACK) v1.6.1 */
/* ©2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence,
   see documentation or authors website for more details */

function sack(file) {
	this.xmlhttp = null;

	this.resetData = function() {
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}



// Variables
var polls_ajax_url = "/wp-content/themes/bat/polls.php";
var polls_text_wait = "Your last request is still being processed. Please wait a while ...";
var polls_text_valid = "Please choose a valid poll answer.";
var polls_text_multiple = "Maximum number of choices allowed:";
var polls = new sack(polls_ajax_url);
var poll_id = 0;
var poll_answer_id = "";
var poll_fadein_opacity = 0;
var poll_fadeout_opacity = 100;
var poll_show_loading = 1;
var poll_show_fading = 1;
var is_ie = (document.all && document.getElementById);
var is_moz = (!document.all && document.getElementById);
var is_opera = (navigator.userAgent.indexOf("Opera") > -1);
var is_being_voted = false;

// Function: When User Vote For Poll
function poll_vote(current_poll_id) {
	if(!is_being_voted) {
		is_being_voted = true;
		poll_id = current_poll_id;
		poll_form = document.getElementById('polls_form_' + poll_id);
		poll_answer = eval("poll_form.poll_" + poll_id);
		poll_answer_id = "";
		if(document.getElementById('poll_multiple_ans_' + poll_id)) {
			poll_multiple_ans = parseInt(document.getElementById('poll_multiple_ans_' + poll_id).value);
		} else {
			poll_multiple_ans = 0;
		}
		poll_multiple_ans_count = 0;
		if(poll_answer.length != null) {
			for(i = 0; i < poll_answer.length; i++) {
				if (poll_answer[i].checked) {					
					if(poll_multiple_ans > 0) {
						poll_answer_id = poll_answer[i].value + "," + poll_answer_id;
						poll_multiple_ans_count++;
					} else {
						poll_answer_id = parseInt(poll_answer[i].value);
					}
				}
			}
		} else {
			poll_answer_id = poll_answer.value;
		}
		if(poll_multiple_ans > 0) {
			if(poll_multiple_ans_count > 0 && poll_multiple_ans_count <= poll_multiple_ans) {
				poll_answer_id = poll_answer_id.substring(0, (poll_answer_id.length-1));
				poll_loading_text();
				poll_process();
			} else if(poll_multiple_ans_count == 0) {
				is_being_voted = false;
				alert(polls_text_valid);
			} else {
				is_being_voted = false;
				alert(polls_text_multiple + " " + poll_multiple_ans + ".");
			}
		} else {
			if(poll_answer_id > 0) {
				poll_loading_text();
				poll_process();
			} else {
				is_being_voted = false;
				alert(polls_text_valid);
			}
		}
	} else {
		alert(polls_text_wait);
	}
}

// Function: When User View Poll's Result
function poll_result(current_poll_id) {
	if(!is_being_voted) {
		is_being_voted = true;
		poll_id = current_poll_id;
		poll_loading_text();
		poll_process_result();
	} else {
		alert(polls_text_wait);
	}
}

// Function: When User View Poll's Voting Booth
function poll_booth(current_poll_id) {
	if(!is_being_voted) {
		is_being_voted = true;
		poll_id = current_poll_id;
		poll_loading_text();
		poll_process_booth();
	} else {
		alert(polls_text_wait);
	}
}

// Function: Poll Fade In Text
function poll_fadein_text() {
	if(poll_fadein_opacity == 90) {
		poll_unloading_text();
	}
	if(poll_fadein_opacity < 100) {
		poll_fadein_opacity += 10;
		/*
		if(is_opera) {
			poll_fadein_opacity = 100;
			poll_unloading_text();
		} else if(is_ie) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').filters.alpha.opacity = poll_fadein_opacity;
			} else {
				poll_fadein_opacity = 100;
				poll_unloading_text();
			}
		} else	 if(is_moz) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.MozOpacity = (poll_fadein_opacity/100);
			} else {
				poll_fadein_opacity = 100;
				poll_unloading_text();
			}
		}
		*/
		$('#polls-' + poll_id + '-ans').fadeTo( 1, (poll_fadein_opacity/100) );
		
		setTimeout("poll_fadein_text()", 100); 
	} else {
		poll_fadein_opacity = 100;
		is_being_voted = false;
	}
}

// Function: Poll Loading Text
function poll_loading_text() {
	if(poll_show_loading) {
		document.getElementById('polls-' + poll_id + '-loading').style.display = 'block';
	}
}

// Function: Poll Finish Loading Text
function poll_unloading_text() {
	if(poll_show_loading) {
		document.getElementById('polls-' + poll_id + '-loading').style.display = 'none';
	}
}

// Function: Process The Poll
function poll_process() {
	if(poll_fadeout_opacity > 0) {
		poll_fadeout_opacity -= 10;
		/*
		if(is_opera) {
			poll_fadeout_opacity = 0;
		} else if(is_ie) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').filters.alpha.opacity = poll_fadeout_opacity;
			} else {
				poll_fadeout_opacity = 0;
			}
		} else if(is_moz) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.MozOpacity = (poll_fadeout_opacity/100);
			} else {
				poll_fadeout_opacity = 0;
			}
		}
		*/
		$('#polls-' + poll_id + '-ans').fadeTo( 1, (poll_fadeout_opacity/100) );
		
		setTimeout("poll_process()", 100); 
	} else {
		poll_fadeout_opacity = 0;
		polls.reset();
		polls.setVar("vote", true);
		polls.setVar("poll_id", poll_id);
		polls.setVar("poll_" + poll_id, poll_answer_id);
		polls.method = 'POST';
		polls.element = 'polls-' + poll_id + '-ans';
		polls.onCompletion = poll_fadein_text;
		polls.runAJAX();
		poll_fadein_opacity = 0;
		poll_fadeout_opacity = 100;
	}
}

// Function: Process Poll's Result
function poll_process_result() {
	if(poll_fadeout_opacity > 0) {
		poll_fadeout_opacity -= 10;
		/*
		if(is_opera) {
			poll_fadeout_opacity = 0;
		} else if(is_ie) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').filters.alpha.opacity = poll_fadeout_opacity;
			} else {
				poll_fadeout_opacity = 0;
			}
		} else if(is_moz) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.MozOpacity = (poll_fadeout_opacity/100);
			} else {
				poll_fadeout_opacity = 0;
			}
		}
		*/
		$('#polls-' + poll_id + '-ans').fadeTo( 1, (poll_fadeout_opacity/100) );
		
		setTimeout("poll_process_result()", 100); 
	} else {
		poll_fadeout_opacity = 0;
		polls.reset();
		polls.setVar("pollresult", poll_id);
		polls.method = 'GET';
		polls.element = 'polls-' + poll_id + '-ans';
		polls.onCompletion = poll_fadein_text;
		polls.runAJAX();
		poll_fadein_opacity = 0;
		poll_fadeout_opacity = 100;
	}
}

// Function: Process Poll's Voting Booth
function poll_process_booth() {
	if(poll_fadeout_opacity > 0) {
		poll_fadeout_opacity -= 10;
		/*
		if(is_opera) {
			poll_fadeout_opacity = 0;
		} else if(is_ie) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').filters.alpha.opacity = poll_fadeout_opacity;
			} else {
				poll_fadeout_opacity = 0;
			}
		} else if(is_moz) {
			if(poll_show_fading) {
				document.getElementById('polls-' + poll_id + '-ans').style.MozOpacity = (poll_fadeout_opacity/100);
			} else {
				poll_fadeout_opacity = 0;
			}
		}
		*/
		$('#polls-' + poll_id + '-ans').fadeTo( 1, (poll_fadeout_opacity/100) );
		
		setTimeout("poll_process_booth()", 100); 
	} else {
		poll_fadeout_opacity = 0;
		polls.reset();
		polls.setVar("pollbooth", poll_id);
		polls.method = 'GET';
		polls.element = 'polls-' + poll_id + '-ans';
		polls.onCompletion = poll_fadein_text;
		polls.runAJAX();
		poll_fadein_opacity = 0;
		poll_fadeout_opacity = 100;
	}
}


var quotcoll_requrl, quotcoll_nextquote, quotcoll_loading, quotcoll_errortext;

function quotescollection_init(requrl, nextquote, loading, errortext)
{
	quotcoll_requrl = requrl;
	quotcoll_nextquote = nextquote;
	quotcoll_loading = loading;
	quotcoll_errortext = errortext;
}



function quotescollection_refresh(instance, exclude, show_author, show_source, filter_tags, char_limit)
{
	jQuery("#quotescollection_nextquote-"+instance).html(quotcoll_loading);
	jQuery.ajax({
		type: "POST",
		url: quotcoll_requrl,
		data: "refresh="+instance+"&exclude="+exclude+"&show_author="+show_author+"&show_source="+show_source+"&char_limit="+char_limit+"&tags="+filter_tags,
		success: function(response) {
			jQuery("#quotescollection_randomquote-"+instance).hide();
			jQuery("#quotescollection_randomquote-"+instance).html( response );
			jQuery("#quotescollection_randomquote-"+instance).fadeIn("slow");	
		},
		error: function(xhr, textStatus, errorThrown) {
			alert(textStatus+' '+xhr.status+': '+errorThrown);
			jQuery("#quotescollection_nextquote-"+instance).html('<a class=\"quotescollection_refresh\" style=\"cursor:pointer\" onclick=\"quotescollection_refresh('+instance+', '+exclude+', '+show_author+', '+show_source+', \''+filter_tags+'\', '+char_limit+')\">'+quotcoll_nextquote+' &raquo;</a>');
		}	
	});
}




