function goto_URL(object)
{
    window.location.href = object.options[object.selectedIndex].value;
}

function get_mp_height(selector)
{
	var to_check=['padding-top','padding-bottom','margin-top','margin-bottom'];
	var height=0;
	for(var i=0;i<to_check.length;i++){
		height+=parseInt($(selector).css(to_check[i]));
	}
	return height;
}

function newsletter_subscribe(e)
{
	e.preventDefault();
	var name = $('form#newsletter_subscribe input[name=name]').val();
	if (name === undefined) {
		name='';
	}
	var email = $('form#newsletter_subscribe input[name=email]').val();
	if (email === undefined) {
		email = '';
	}
	if (name === '' || email === '') {
		GATEWAY_MESSENGER.alertbox('Please fill in all the fields');
	} else {
		$.ajax({
			type: "GET",
			url: "/list/sparklist.php",
			dataType: 'json',
			data: {name:name,email:email,action:'subscribe'},
			success: function(data){
				GATEWAY_MESSENGER.alertbox(data.message);
				if(!data.error){
					$('form#newsletter_subscribe').remove();
					$('div#newsletter_deals').append('Thanks for subscribing');
				}
			}
		})
	}
}



var ads = null;

$(document).ready(function(){
	if($.browser.msie && $.browser.version<7){
		$('a',$('ul#nav li.tab.current+li')).css('background','none');
		// $('ul#nav li').hover(function(e){
		// 	$(e.target).next().css('background-image','none !important');
		// }, function(){});
		// $('a',$('ul#nav li:hover+li.tab')).css('background','none !important');
		$('ul#nav li:first-child a').css('background','none');
	}
	
	try {
		$('div#dept_subnav li.a2z')
			.menuHover({addBorder:'1px solid #007D9C',menu_height:200});
	} catch(e) {
		try { console.log(e); } catch (e) {};
	};
	
	$('form#newsletter_subscribe')
		.bind('submit', function(e){newsletter_subscribe(e)})
	.end();
	
	ads = $(".ads").adRefresh();
});



// POLLS

function checkVote(id)
{
	if ($.cookie("poll_id")) {
		var voted_array = $.cookie("poll_id").split(",");
		for (var poll in voted_array)
		{
			if (id === parseInt(voted_array[poll])) {
				$("#poll-form-"+ id).hide();
				if (!$("#poll-results-"+ id).html()) {
					$("#poll-form-"+ id).get(0).results_only.value = 1;
					submitVote(id);
				}
			}
		}
	}
};

function viewResults(id)
{
	$("#poll-form-"+ id).get(0).results_only.value = 1;
	submitVote(id);
	$("#poll-form-"+ id).show().end();
};

function updateResults(p, b, f) {
	$.post(
		"/system/plugins/PollPosition/vote.cgi",
		{__mode:"save_poll_choice", poll_id:p, blog_id:b, type:"poll", results_only:1, folder:f},
		function(r, s){
			if (typeof(r) == "string" && r.length > 0) {
				var p = /\<img\s*src\=\"(.+)\"\s*\/\>/.exec(r);
				if (typeof(p) == "object" && p.length == 2) {
					$("#poll-total-5")
						.before("<img src='"+ p[1] +"'/>");
				}
			}
		}
	)
};

function submitVote(id)
{
	var form = $("#poll-form-"+ id);
	
	for (var i=0; i<form.get(0).poll_choice.length; i++) {
		if (form.get(0).poll_choice[i].checked) {
			var selected = 1;
		}
	};
	
	if (selected || (1 === parseInt(form.get(0).results_only.value))) {
		form.ajaxSubmit({
			success: function(responseText, status)
			{
				if (typeof(responseText) !== "undefined") {
					
					if (responseText.match('unpublished')) {
						responseText = '<p class="unpublished">'+ responseText +'</p>';
						
					} else if (responseText.match('currently closed')) {
						responseText = '<p class="currently-closed">'+ responseText +'</p>';
						
					} else if (responseText.match('not a valid option')) {
						responseText = '<p class="error">' + responseText + '</p>';
						form.show().end();
						
					} else {
						if (0 === parseInt(form.get(0).results_only.value)) {
							var voted = $.cookie("poll_id");
							if (voted) {
								id = voted + "," + id;
							};
							$.cookie("poll_id", id, {expires:1});
						};
						
						form.get(0).results_only.value = 0;
					};
					
					$("#poll-results-"+ id)
						.html(responseText)
					.end();
				}
			},
			
			error: function(req, statusText, status) {
				form.show().end();
				
				if (typeof(responseText) !== "undefined") {
					$("#poll-results-"+ id)
						.html("<p><strong>Error</strong><br/>"
							+ "HTTP status: " + status + "<br/>"
							+ "Status code message: " + statusText + "</p>"
						)
					.end();
				}
			},
			
			url: "/system/plugins/PollPosition/vote.cgi"
		});
		
		$("#poll-results-"+ id)
			.html('<img src="/system/mt-static/images/indicator.gif" width="66" height="66"/>')
		.end();
		
		form.hide().end();
		
	} else {
		alert('Please make a selection before submitting to this poll');
	};

	return false;
};
