var doRotate = true;

currentLink = "";
$(document).ready(function () {

    // navmenu
    menuItem();

    // featuredtabs on homepage
    currentLink = $('#tabs ul li a:first');
    $('#tabs div.tabFeatured').hide();
    $('#tabs div:first').show();
    $('#tabs ul li:first').addClass('active');
    $('#tabs ul li a').click(function () {
        stopRotate();
        switchTab($(this));
        return false;
    });
    rotate();
    //currentLink.click(stopRotate);

    // top ten reasons on himepage
    tenReasonsHandler();

    // to fix IE7 issue on navmenu
    customizeTabs();

    // to fix column height difference on UFC, WWE, and Events page
    if ($(".interior_mainContent_twoCol").length > 0) {
        matchTheColumn();
    }
});

function matchTheColumn() {
    var height1 = $(".singleCol_long:first").height();
    var height2 = $(".singleCol_long:last").height();
    if (height1 > height2) {
        $(".singleCol_long:last").height(height1);
    } else {
        $(".singleCol_long:first").height(height2);
    }
}


function rotate() {
	if (doRotate) {
		switchTab(currentLink);
		currentLink = currentLink.attr('href') == '#featured-4' ? $('#tabs ul li a:first') : currentLink.parent().next().children('a');
		setTimeout(rotate, 10000);
	}
}

function stopRotate() {
	clearTimeout();
	doRotate = false
}

function switchTab($this) {
	$('#tabs ul li').removeClass('active');
	$this.parent().addClass('active');
	var currentTab = $this.attr('href');
	$('#tabs div.tabFeatured').hide();
	//$(currentTab).show();
	$(currentTab).fadeIn("slow");
}

function tenReasonsHandler() {

	$(".reason:first").fadeIn(); // show first step

	// attach backStep button handler
	// hide on first step
	$("#prevReason").click(function () {
		var $step = $(".reason:visible"); // get current step
		if ($step.prev().hasClass("reason")) { // is there any previous step?
			$step.hide().prev().fadeIn();  // show it and hide current step

			/*// disable backstep button?
			if (!$step.prev().prev().hasClass("reason")) {
				$("#prevReason").hide();
			}
			
			// enable nextstep button?
			if ($step.hasClass("reason") || $step.next().hasClass("reason")) {
				$("#nextReason").show();
			}*/

		}else{
			$step.hide();
			$(".reason:last").fadeIn();
		}
	});

	// attach nextStep button handler       
	$("#nextReason").click(function () {
		var $step = $(".reason:visible"); // get current step

		if ($step.next().hasClass("reason")) { // is there any next step?
			$step.hide().next().fadeIn();  // show it and hide current step
			/*$("#prevReason").show();   // recall to show backStep button*/
			
		}else{
			$step.hide();
			$(".reason:first").fadeIn();
		}
		
		/*// disable nextstep button?
		if (!$step.next().next().hasClass("reason")) {
			$("#nextReason").hide();
		}*/

	});

}

function menuItem(){
	$('#topMenu > li').bind('mouseover', jsddm_open);
	$('#topMenu > li').bind('mouseout',  jsddm_timer);
	subMenuItem();
}

function subMenuItem(){
	$(".submenu").find('li').hover(function(){
		$(this).css("background-color", "#f2f2f2");
		$(this).find('a').css("color", "#444444");
	},
	function(){
		$(this).css("background-color", "#3391da");
		$(this).find('a').css("color", "#FFFFFF");
	});

}

function customizeTabs(){
	$(".submenu:first").css("left", "166px");
	$(".submenu:eq(1)").css("left", "331px");
	$(".submenu:last").css("left", "495px");
}

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('display', 'block');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('display', 'none');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}


document.onclick = jsddm_close;
