$(document).ready(function(){
	$("#hideIE6Message").click(function(){
		$.ajax({
			type: 'POST',
			url: 'UtilityMethods/hideIE6Message.php',
			dataType: "html",
			success: function(response) { 
				$("#ie-message").hide();
			}
		});
	});
	
	if(typeof startPix == 'function') startPix();
	$('#datepicker').datepicker({onSelect: function(dateText, inst){document.location = 'calendar.htm?date=' + dateText;},dateFormat: 'yymmdd'});
	
	var hoverTimeout = 0;
	$("#Menu ul.topnav li").hover(
		function(){
			if(hoverTimeout != 0) clearTimeout(hoverTimeout);
			var this_element = this;
			hoverTimeout = setTimeout(function() {
				hoverTimeout = 0;
				if($(this_element).find("ul.subnav").length>0){
					$(this_element).find("ul.subnav").slideDown('fast').show();
				}
			}, 150);
		},
		function(){
			if(hoverTimeout!=0)clearTimeout(hoverTimeout);
			$(this).find("ul.subnav").slideUp('fast').show();
			if(hoverTimeout!=0){
				$(this).find("ul.subnav").hide();
				hoverTimeout=0;
			}
		}
	);
	
	// This adds a span around the designated element.
	$('.extraBox .blobContent table td img, .social .logo, .isRounded, #actionDiv a, .announcement img').wrap('<span></span>');
	// This appends the 'rounding' divs. 
	$('.extraBox .blobContent table td span, .social span, .isRounded, #actionDiv span, .announcement span').append('<div class="corner_tl"/><div class="corner_tr"/><div class="corner_bl"/><div class="corner_br"/>');
	// The subnav is special, and rates its own append.
	$('#Menu .subnav').append('<div class="subnavCap"></div><div class="corner_tr"/>');
	$('.roundedTab').append('<div class="corner_tl"/><div class="corner_tr"/>');

	// Control set for Featured Events slider.
	$('.featEvents').anythingSlider({
		easing: "swing",
		autoPlay: true,
		delay: 7000,
		startStopped: false,
		animationTime: 700,
		hashTags: false,
		buildNavigation: true,
		pauseOnHover: true,
		startText: "Play",
		stopText: "Pause",
		navigationFormatter: null
	});
	
	$('input[type=radio].star').rating({ 
		callback: function(value){ 
			$.ajax({
				type: 'POST',
				url: 'UtilityMethods/processRating.php',
				data: "bid=" + $("#businessId").val() + "&rating=" + value,
				dataType: "html",
				success: function(response) { 
					if(response!='') {
						var output = response.split('|');
						if(output.length==3) {
							$('input[type=radio].star').rating("select", output[0]);
							$('.ratingLabel').html(((output[1]>0 && output[2]>0) ? '('+output[1]+' of '+output[2]+' votes)':'(not yet rated)'));
							alert("The businesses rating has been updated and recalculated.");
						}
					}
				}
			});
		} 
	});
	
	$("input[type=button],input[type=submit]").click(function(event){
		if($(".password").length>0) {
			var lastIndexOf = window.location.href.lastIndexOf("/");
			var pageName = window.location.href.substring(lastIndexOf+1, window.location.href.length);
			$.ajax({
				type: 'POST',
				data: "pass=" + $(".password").val() + "&page=" + pageName,
				url: 'UtilityMethods/submitPassword.php',
				dataType: "html",
				success: function(response) { 
					if(response!="") {
						window.location.href = response;
					} else {
						alert("The password you supplied is incorrect. Please try again.");
					}
				}
			});
			event.preventDefault();
		}
	});
});

//Function to add event listener
function addEvent(obj, evType, fn){
	if(obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	}else if(obj.attachEvent){
		return obj.attachEvent("on"+evType, fn);
	}else{
		return false;
	}
}

//Function to handle cycling images on the rotating banners
function rotatingBanner(namespace, imageId, timeDelay, Pix, Lnk) {
	var x = 0;
	var slideshowImages = new Array(Pix.length);
	for (var i=0; i<slideshowImages.length; i++) {
		slideshowImages[i] = new Image();
		slideshowImages[i].src = Pix[i];
	}
	this.startPix = function() { setInterval(namespace + ".slideshow()", timeDelay); }
	this.slideshow = function() {
		x++; if (x >= Pix.length) x = 0;
		if (document.getElementById){
			var elm = document.getElementById(imageId);
			if(elm!=null) {
				elm.src = slideshowImages[x].src;
				elm.parentNode.href=Lnk[x];
			}
		}
		return true;
	}
}
//Google analytics utility class
(function($) {
	var pageTracker;
	//Track a pageview, e.g.:$.gaTrackPage('UA-0000000-0');
	$.gaTrackPage = function(account_id) {
		var host = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
		var src = host + 'google-analytics.com/ga.js';
		//load the Google Analytics javascript file:
		$.ajax({
			type:'GET',
			url:src,
			dataType:'script',
			cache:true,
			success:function(){
				pageTracker = _gat._getTracker(account_id);
				pageTracker._trackPageview();
			}
		});
	};
 
	//Track an event, e.g.: $('a.somelink').click(function() {$.gaTrackEvent('feed', 'click', 'somelink', 'asdasda');});
	$.gaTrackEvent = function(category, action, label, value) { if (typeof pageTracker != undefined) pageTracker._trackEvent(category, action, label, value); };
})(jQuery);
