function toggleRegionMenu(menu, show) {
	if (show)
		menu.css("display", "block");
	else
		menu.css("display", "none");
}

function setMenuPosition(regionSelector, regionDD, offset) {
	var x = $(regionSelector).width() - $(regionDD).width() + offset;
	$(regionDD).css("left", x + "px");
}

function setRegionDD(regionSelector, regionDD, offset, onclick) {
	$(regionSelector).mouseover(function() {
		if (offset) setMenuPosition($(regionSelector), $(regionDD), offset)
		toggleRegionMenu($(regionDD),true);
	});
	$(regionSelector).mouseout(function() {
		toggleRegionMenu($(regionDD),false);
	});
	if (onclick) { $(regionSelector + " a.btn-selector").click(onclick);  }
}

//Workaround for issue with Chrome where input text boxes are not "grabbing" focus
function setRegionDDWithTextBox(regionSelector, regionDD, offset, onclick) {
	var timeoutID = 0;
	$(regionSelector).mouseover(function() {
		if (offset) setMenuPosition($(regionSelector), $(regionDD), offset)
		if(timeoutID){
			  clearTimeout(timeoutID);
			  timeoutID = null
		}
		toggleRegionMenu($(regionDD),true);
	});
	$(regionSelector).mouseout(function() {
		timeoutID = setTimeout(function() { toggleRegionMenu($(regionDD),false); }, 20);	
	});
	if (onclick) { $(regionSelector + " a.btn-selector").click(onclick);  }
}

function setMoreDD(moreSelector, moreDD) {
	$(moreSelector).mouseover(function() {
		toggleRegionMenu($(moreDD),true);
	});
	$(moreSelector).mouseout(function() {
		toggleRegionMenu($(moreDD),false);
	});
}

function setDropdownHoverStyles() {
	var menus = ['#dd-schedule','#dd-fighters','#dd-media','#dd-shop'];
	
	$.each(menus, function(i, val) {
		var links = $(val + ' .subheader');
		$(links).each(function(index) {
			$(this).hover(
				function() {
					if (index > 0)
						$(links.get(index-1)).addClass('neighbor');
				},
				function() {
					if (index > 0)
						$(links.get(index-1)).removeClass('neighbor');
				});
		});
	});
}

function validateNewsletter(objectId, errorObjectId) {
	var value = $(objectId + " input[name='email']").val().replace(/^\s+/,'').replace(/\s+$/,'');
	if (!/^[a-zA-Z_0-9-'\+~]+(\.[a-zA-Z_0-9-'\+~]+)*@([a-zA-Z_0-9-]+\.)+[a-zA-Z]{2,7}$/.test(value)) {
		$(errorObjectId).show()
		return false;
	}
	else {
		return true;
	}
}

var ready = false;
function readyFunction() {
    if (ready || $('.main-menu').length == 0)
        return;
    
	$(".main-menu").mainMenu();
	setRegionDD("#region-selector","#dd-regions", -1, function() { return false });
	setRegionDD("#schedule-region-selector","#dd-schedule-regions", 0, null);
	setRegionDD("#header-region-selector","#header-regions", -1, null);
	setRegionDDWithTextBox("#header-newsletter-selector","#header-newsletter", -1, function() { return false });
	setRegionDD("#countdown-contents .countdown-eventlink", "#countdown-dropdown .countdown-dd-inner", 0, function() { return false });
	setMoreDD("#more-selector","#dd-more-links");
	$("input.searchBox-hint").searchBox("searchBox-blur");
	$(".btn-red,.btn-red-arrow,.btn-red-shadow,.btn-red-shadow-arrow,.btn-red-lg-shadow-arrow,.btn-ltgray,.btn-ltgray-arrow,.btn-ltgray-darrow,.btn-ltgray-darrow-border,.btn-gray,.btn-gray-arrow,.btn-gray-darrow,.btn-gray-darrow-border,.btn-dkgray-med-shadow-darrow,.btn-dkgray-shadow,.btn-dkgray-shadow-arrow,.btn-dkgray-lg-shadow-arrow,.btn-dkgray-shadow-darrow,.btn-dkgray-shadow-darrow-border,.btn-clear,.btn-clear-darrow").ufcButtons();
	$("#header-newsletter .newsletter-btn-clear, #top-nav .countdown .btn-clear-darrow-border, .newsletter-btn-red").ufcButtons({'addClickEvent':false});
	setDropdownHoverStyles();

    if (typeof Cufon != 'undefined') {
        Cufon.replace('.promo h4');
        Cufon.replace('.promo-item .ad-text span');
    }

    ready = true;
}

$(document).ready(function() {
	readyFunction();
});

// ---- SWFObject Global Settings ------- //
var flashVersion = "10.0.0";
var expressSWF = "expressInstall.swf"; //relative path, dynamically rendered based on section

// Global Params
var params = {};
//params.bgcolor="000000";
params.wmode = "transparent";
params.allowscriptaccess="always"

swfPath = "/UFC/flash/dataReporter.swf";

// For the alerts carousel
$(document).ready(function() { 
	if ($("#alert-list li").length > 1) {
		$("#alert-list").jcarousel({ 
			vertical:true, 
			scroll:1, 
			wrap:"circular", 
			auto:10,
			itemFirstInCallback: {
				onBeforeAnimation: function(carousel, item) {
					if ($(".urgent", item).length > 0) {
						$("#urgent-alert").fadeIn();
					}
					else {
						$("#urgent-alert").fadeOut();
					}
				},
				onAfterAnimation: function() { }
			},
			itemVisibleOutCallback: { 
				onAfterAnimation:function(carousel, item, i, state, evt) { 
					carousel.remove(i);
				} 
			} ,
			itemVisibleInCallback: {  
				onBeforeAnimation:function(carousel, item, i, state, evt) {
					var index = carousel.index(i, $("#alert-reference li").length);
					carousel.add(i, $("#alert-reference li").eq(index-1).html());
				}
			}
		});
		
	}
})

var shared_object;

function mediaShare(url, title) {
	shared_object = SHARETHIS.addEntry({
		title: title,
		url: url,
		offsetTop:"100px",
		offsetLeft:"300px"
	});
	
	shared_object.attachButton($("#media-share")[0]);
	$("#media-share").mouseover();
}


function applyTabs(tabId) {
	$(document).ready(function() {
		//On Click Event
		$("#"+tabId+" div.tab").click(function() {
			$("#"+tabId+" div.tab").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$("#"+tabId+"-content .tab-content").hide();
			$("#"+$(this).attr('id')+"-content").fadeIn();
			return false;
		});
	});
}

var millisecondsPerSecond = 1000;
var millisecondsPerMinute = 60 * millisecondsPerSecond;
var millisecondsPerHour = 60 * millisecondsPerMinute;
var millisecondsPerDay = 24 * millisecondsPerHour;

function applyCountdown(countdownId, milliseconds) {	
	if (milliseconds == 0) { return }
		
	var eventDate = new Date(milliseconds);
	
	function updateCountdown() {
		var millisecondsRemaining = eventDate.getTime() - new Date().getTime()
		if(millisecondsRemaining <= 0){
			displayPart("seconds", 0)
			displayPart("minutes", 0)
			displayPart("hours", 0)
			displayPart("days", 0)
		}
		else{
		displayPart("seconds", Math.floor(millisecondsRemaining / millisecondsPerSecond) % 60)
		displayPart("minutes", Math.floor(millisecondsRemaining / millisecondsPerMinute) % 60)
		displayPart("hours", Math.floor(millisecondsRemaining / millisecondsPerHour) % 24)
		displayPart("days", Math.floor(millisecondsRemaining / millisecondsPerDay))
		}
	}
	
	function displayPart(part, number) {
		if (number < 10) {
			$("#"+countdownId+"-"+part).text("0" + number.toFixed(0))
		}
		else {
			$("#"+countdownId+"-"+part).text(number.toFixed(0))
		}
	}
	
	$(document).ready(function() { 
		updateCountdown();
		setInterval(updateCountdown, 1000);
	})
}


function applyBackground(backgroundImage, linkUrl, linkMethod) {
	$('#background-ad-target').css('background', 'url('+backgroundImage+') top center no-repeat');
	$('#background-ad-target').click(function(event) {
		if (event.target.id == 'content') {
			//if (linkMethod){
			//	linkMethod();
			//}
			if (linkUrl) {
				window.open(linkUrl)
			}
		}
	})
}


