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})
		.end();
	} catch (e) {};
	
	$('form#newsletter_subscribe')
		.bind('submit', function(e){newsletter_subscribe(e)})
	.end();
	
	try {
		ads = $(".ads").adRefresh();
	} catch (e) {};
});



// 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 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;
};


// omniture

function omniParseUrl(ctype, csect)
{
	try {
		if (typeof(ctype) == "string" && ctype.length == 0) {
			var url = window.location.href;
			
			url = url.replace(/http[s]*\:\/{2}/, "");
			
			var path = url.split("?")[0];
			
			var query = {};
			try {
				query = url.split("?")[1].split("&");
				if (query.length > 0) {
					var tmp = {};
					for (var q in query) {
						tmp[query[q].split("=")[0]] = query[q].split("=")[1];
					};
					query = tmp;
				};
			} catch (err_q) { };
			
			path = path.replace(/\/$/, "").split("/");
			path.shift();
			
			if (typeof(query.q) != "undefined") {
				// search
				iv_contenttype = "Search";
				iv_section = "Search";
				iv_searchType = query.q;
				
			} else if (path[0] == "info") {
				// video
				iv_section = "Videos";
				
				var trail = null;
				try {
					if ($("div[class*='breadCrumb']").length > 0) {
						trail = $("div[class*='breadCrumb']")[0];
						trail = $(trail).text().replace(/\n+/g, "").split(/[^a-z0-9\s\-\.]/i);
						var tmp = [];
						for (var t in trail) {
							trail[t] = trail[t].replace(/^\s+|\s+$/g, "");
						};
						if (trail[0].toLowerCase() == "add this rss home") {
							trail.shift();
						};
						if (trail[0].toLowerCase() == "home") {
							trail.shift();
						};
						if (trail[0].toLowerCase() == "video") {
							trail.shift();
						};
					};
				} catch (errt) {};
				
				if (path.length == 1) {
					// main - ../info
					iv_contenttype = "ChannelPage";
					
				} else if ($.inArray("m", path) > -1) {
					// Video - ../info/m/22006327/pets-and-allergies.htm
					iv_contenttype = "Video";
					
				} else if (path.length == 2) {
					// Topic - ../info/bowl-and-collar/
					iv_contenttype = "Topic";
					iv_subsection1 = (trail != null && typeof(trail[0]) != "undefined" ? trail[0] : "");
					
				} else if (path.length == 3) {
					// SubTopic - ../info/bowl-and-collar/beds.htm
					iv_contenttype = "SubTopic";
					
					if (trail != null) {
						iv_subsection1 = (typeof(trail[0]) != "undefined" ? trail[0] : "");
						iv_subsection2 = (typeof(trail[1]) != "undefined" ? trail[1] : "");
					} else {
						if (path[0].toLowerCase() == "info") {
							path.shift();
						};
						iv_subsection1 = (typeof(path[0]) != "undefined" ? path[0] : "");
						iv_subsection2 = (typeof(path[1]) != "undefined" ? path[1] : "");
					};
				};
			} else {
				// add alt condition
			};
		};
	} catch (e) {
		try {
			console.log(e);
		} catch (e) {};
	};
}


