// Poll
var requestObject = null;
try 
{
	requestObject = new XMLHttpRequest();
} 
catch (trymicrosoft) 
{
	try 
	{
    	requestObject = new ActiveXObject("Msxml2.XMLHTTP");
  	}
	catch (othermicrosoft) 
	{
    	try 
		{
      		requestObject = new ActiveXObject("Microsoft.XMLHTTP");
    	} 
		catch (failed) 
		{
      		requestObject = null;
		}
  	}
}

function pollReturned()
{
	var reqTxt = requestObject.responseText;
	if(requestObject.readyState == 4 || requestObject.readyState == "complete")
	{
		var alertEnd = reqTxt.indexOf("~");
		var alertTxt = reqTxt.substr(0,alertEnd);
		var pollText = reqTxt.substr(alertEnd + 1);
		document.getElementById("pollUL").innerHTML = pollText;
		alert(alertTxt);
	}
}

function pollVote(vote,question)
{
	var url = "assets/inc/vote.php";
	var data = "vote=" + vote + "&q=" + question;
	postRequest(url,data,pollReturned);
}

function postRequest(url, data, returnFunction)
{
	if(requestObject.readyState != 0)
	{
		requestObject.abort();
	}
	else 
	{		
		requestObject.open("POST", url, true);
		requestObject.onreadystatechange = returnFunction;
		requestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		requestObject.send(data);
	}
}

// Slider
$(function () {
	$('#slider').anythingSlider({
		width               : 636,      // Override the default CSS width
		height              : 200,      // Override the default CSS height
		resizeContents      : true,      // If true, solitary images/objects in the panel will expand to fit the viewport
		
		// Navigation
		startPanel          : 1,         // This sets the initial panel
		hashTags            : true,      // Should links change the hashtag in the URL?
		buildArrows         : false,      // If true, builds the forwards and backwards buttons
		buildNavigation     : true,      // If true, buildsa list of anchor links to link to each panel
		navigationFormatter : null,      // Details at the top of the file on this use (advanced use)
		forwardText         : "&raquo;", // Link text used to move the slider forward (hidden by CSS, replaced with arrow image)
		backText            : "&laquo;", // Link text used to move the slider back (hidden by CSS, replace with arrow image)
		
		// Slideshow options
		autoPlay            : true,      // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
		startStopped        : false,     // If autoPlay is on, this can force it to start stopped
		pauseOnHover        : true,      // If true & the slideshow is active, the slideshow will pause on hover
		resumeOnVideoEnd    : true,      // If true & the slideshow is active & a youtube video is playing, it will pause the autoplay until the video has completed
		stopAtEnd           : false,     // If true & the slideshow is active, the slideshow will stop on the last page
		playRtl             : false,     // If true, the slideshow will move right-to-left
		startText           : "Start",   // Start button text
		stopText            : "Stop",    // Stop button text
		delay               : 4000,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
		animationTime       : 600,       // How long the slideshow transition takes (in milliseconds)
		easing              : "elastic"    // Anything other than "linear" or "swing" requires the easing plugin
	});
});

// Uniform
$(document).ready(function() {
	$("select, input:checkbox, input:radio, input:file").uniform();
})(jQuery);

// Shadowbox
Shadowbox.init({
    handleOversize: "drag",
    modal: true
});

// Styleswitch (kelvinluck.com)
(function($) {
	$(document).ready(function() {
		$("#switcher a").click(function() {
			switchStylestyle(this.getAttribute("rel"));
			return false;
		});
		var c = readCookie("style");
		if(c) {
			switchStylestyle(c);
		} else {
			var styleName = "main";
		}
	});
	function switchStylestyle(styleName) {
		$("link[@rel*=style][title]").each(function(i) {
			this.disabled = true;
			if(this.getAttribute("title") == styleName) this.disabled = false;
		});
		createCookie("style", styleName, 365);
	}
})(jQuery);

// Cookie Functions (quirksmode.org)
function createCookie(name,value,days) {
	if(days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for(var i=0; i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==" ") c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name, "", -1);
}

// Smooth Scroll
$(function(){
	$("a[href*=#]").click(function() {
		if(location.pathname.replace(/^\//,"") == this.pathname.replace(/^\//,"") && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $("[name=" + this.hash.slice(1) +"]");
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$("html,body").animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
});

// Toggles
$(document).ready(function(){
	$(".toggle-video").click(function () {
		$(".video").slideToggle();
	});
	$(".toggle-item-help").click(function () {
		$(".item-help").slideToggle();
	});
	$(".toggle-whitelist").click(function () {
		$(".whitelist").slideToggle();
	});
});
