var jsTVNZpoll = window.jsTVNZpoll || {};

// the Initialize function, which runs on load, all sub-functions are called in here.
jsTVNZpoll.initialize = function(id) {

	// Horrendous hack to get links to reference parent frame
	//$("a").attr("target","_parent");

	$("#divID" + id).parent().find(".viewResults").click(function(){
		jsTVNZpoll.Common().showPoll(id, $("#poll" + id).css('display') == "none");
		return false; // make sure target no followed
	});
	jsTVNZpoll.Common().showPoll(id, true);

	// changes the submit on the form to do an ajax submit instead. e.g when you call $(<insert form id>).submit(); it will use ajax.
	$("#pollForm" + id).ajaxSubmit(id);
}


// TVNZ Common functions, that can be re-called at any point.
jsTVNZpoll.Common = function() {

	var showPoll = function(id, enable) {
		var pollCookie = jsTVNZpoll.Common().readCookie("pollForm" + id);
		if (pollCookie == "voted") {
			// Already voted - disable the link back to 'poll'
			$("#divID" + id + "#results" + id).css({display:"none"});
			$("#divID" + id).parent().find(" .viewResults").css({display:"none"});
		}
		if (enable && pollCookie != "voted") {
			// hasn't already voted so can see poll
			$("#divID" + id + " .viewResults").text("view results");
			$("#divID" + id).parent().find(".viewResults").text("view results");
			$("#divID" + id + " #results" + id).css({display: "none"});
			$("#divID" + id + " #poll" + id).css({display: "block"});
		} else {
			// Has already voted/just wants to see results
			$("#divID" + id + ".viewResults").text("view poll");
			$("#divID" + id).parent().find(".viewResults").text("view poll");
			$("#divID" + id + " #poll" + id).css({display: "none"});
			$("#divID" + id + " #results" + id).css({display: "block"});
		}
	}

	var createCookie = function(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=/";
	}

	var readCookie = function(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;
	}

	var eraseCookie = function(name) {
		jsTVNZpoll.Common().createCookie(name,"",-1);
	}
;
	return {
		createCookie : createCookie,
		readCookie : readCookie,
		eraseCookie : eraseCookie,
		showPoll : showPoll
	}
}

$.fn.ajaxSubmit = function(id) {
	/* Change a form's submission type to ajax */
	$("#pollForm" + id + " input[type=radio]").click(function() {
		$("#pollForm" + id + " input[name=submit]").removeClass("disabled");
	});
	$("#pollForm" + id).submit(function() {
		if ($('input:radio', this).is(':checked')) {
			var params = {};
			$(this)
				.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
				.filter(":enabled")
				.each(function() { params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; });
			$("body").addClass("curWait");
			// adds cookie before it submits
			jsTVNZpoll.Common().createCookie("pollForm" + id,"voted",365);
			$.post(this.getAttribute("action") + "?call=ajax", params, function(xml){
				jsTVNZpoll.Common().showPoll(id, false);
			});
			return false;
		} else {
			return false;
		}
	});
	return this;
}


