
/*
 * dependencies: jquery, swfobject, swfaddress
 */


var sectionNames = ['overview', 'videos', 'fight'];
var initialIndex = 0;
var sectionHeights = [442, 580, 514];
var currentIndex = -1;
var isSwfReady = false;

var setSection = function(section, subsection) {

	currentIndex = $.inArray(section.toLowerCase(), sectionNames);
	
	// make sure is valid section
	if (currentIndex < 0) {
		currentIndex = initialIndex;
		section = sectionNames[currentIndex];
	}
	
	// tell swf to go to new section
	var swfObj = $('#swfContent');
	swfObj[0].setSection(section, subsection);

	setNav(currentIndex)
	
};

var setNav = function(currentIndex) {
	$("#event-navigation span.event-link-selected")
		.removeClass("event-link-selected")
		.addClass("event-link")
		
	$("#event-navigation span.event-neighbor-selected")
		.removeClass("event-neighbor-selected")
		
	$("#"+sectionNames[currentIndex]+"-nav").addClass("event-link-selected").removeClass("event-link")
	if (sectionNames[currentIndex] == "fight") {
		var existingHtml = 	$("#footer-breadcrumb-current span").html();
		existingHtml = existingHtml.replace($("#fight-nav a").html(), "");
		existingHtml = existingHtml.replace($("#videos-nav a").html(), "");
		
		$("#footer-breadcrumb-current span").html(existingHtml + " " + $("#fight-nav a").html());
	} else if (sectionNames[currentIndex] == "videos") {
		var existingHtml = 	$("#footer-breadcrumb-current span").html();
		existingHtml = existingHtml.replace($("#fight-nav a").html(), "");
		
		$("#footer-breadcrumb-current span").html(existingHtml + " " + $("#videos-nav a").html());
	} else if (sectionNames[currentIndex] == "overview") {
		var existingHtml = 	$("#footer-breadcrumb-current span").html();
		existingHtml = existingHtml.replace($("#fight-nav a").html(), "");
		existingHtml = existingHtml.replace($("#videos-nav a").html(), "");
		
		$("#footer-breadcrumb-current span").html(existingHtml);
	}
	
	if (currentIndex + 1 < sectionNames.length) {
		$("#"+sectionNames[currentIndex+1]+"-nav").addClass("event-neighbor-selected")
	}
	else {
		$("#chatter-nav").addClass("event-neighbor-selected")
	}
}

var handleAddressChange = function(e) {
	if (isSwfReady) {
		var cleanStr = e.path.substr(1);
		var sections = cleanStr.split('/');
		setSection(sections[0], sections[1]);
	}
};

// called from flash when going to new section
var updateSwfContainerHeight = function() {
	
	if (currentIndex === -1) return;
	
	// update swf container to fit content
	var height = sectionHeights[currentIndex] + 'px';
	$('#eventWrapper').css('height', height);
	
	// this isnt working for me in firefox..
	//$('#eventWrapper').animate({'height': height}, 300);
	
};


// called from flash when ready
var swfReady = function() {
	isSwfReady = true;
	var cleanStr = SWFAddress.getValue().substr(1);
	var sections = cleanStr.split('/');
	setSection(sections[0], sections[1]);
};

//called from flash when updating fight card matches
var updateEventMatch = function(matchId) {
	var value = '/' + sectionNames[2];
	if (matchId !== '') {
		value += '/' + matchId;
	}
	SWFAddress.setValue(value);
};

var initializeLinks = function(){
    $(window).hashchange( function() {
        if (location.hash == '#/fight' || location.hash == '') {
            loadData('fight');
        } else if (location.hash.indexOf('#/fight/') != -1) {
            loadData('fightdetails');
        } else {
            if (location.hash == '#/overview') loadData('overview');
            else loadData('');
        }
    });
}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleAddressChange);

function handleEventHowWatchTabs() {
	$(".howToWatchContent").hide();
	var tab = $("#whichTab").val();
	if(tab == "buy") {
		$("#eventTabTickets").addClass('active');
		$("#buy").fadeIn();
	}
	else if(tab == "tv") {
		$("#eventTabTV").addClass('active');
		$("#tv").fadeIn();
		applyOfflineScrolling();
	}
	else if(tab == "online") {
		$("#eventTabOnline").addClass('active');
		$("#online").fadeIn();
		applyOnlineScrolling();
	}
	else if(tab == "stream") {
		$("#eventTabStream").addClass('active');
		$("#stream").fadeIn();
		applyStreamScrolling();
	}
	else if(tab == "bar") {
		$("#eventTabBar").addClass('active');
		$("#bar").fadeIn();
	}
	else {
		$("#eventTabTickets").addClass('active');
		$("#buy").fadeIn();
	}
	//On Click Event
	$("#eventHowWatchTabs div").click(function() {
		var id = $(this).attr('id');
		$("#eventHowWatchTabs div").removeClass('active');
		$(this).addClass('active');
		$(".howToWatchContent").hide();
		if(id == "eventTabTickets") {
			$("#buy").fadeIn();
		}
		if(id == "eventTabTV") {
			$("#tv").fadeIn();
			applyOfflineScrolling();
		}
		if(id == "eventTabOnline") {
			$("#online").fadeIn();
			applyOnlineScrolling();
		}
		if(id == "eventTabStream") {
			$("#stream").fadeIn();
			applyStreamScrolling();
		}
		if(id == "eventTabBar") {
			$("#bar").fadeIn();
		}
	});
}

function applyOfflineScrolling() {
	$("#scrollPane-offline").jScrollPane({scrollbarWidth:18, scrollbarMargin:0});
}
function applyOnlineScrolling() {
	$("#scrollPane-online").jScrollPane({scrollbarWidth:18, scrollbarMargin:0});
}
function applyStreamScrolling() {
	$("#scrollPane-stream").jScrollPane({scrollbarWidth:18, scrollbarMargin:0});
}

function findBarByZip() {
	var zip = $("#eventHowWatchBar #zipCode").val();
	window.open('http://bars.ufc.com/find-bar?search=' + zip)
}

function expandDetail(index){
	// Hide all
	var current = $("#inPersonDetails"+index).is(":visible")
	$("[id^='inPersonDetails']").hide();

	// If current visible, hide it.  
	// Else if current hidden, show it.
	if(current) {
		$("#inPersonDetails"+index).hide();
	}
	else {
		$("#inPersonDetails"+index).show().css("display", "block");
	}
}

$(document).ready(function() {
	handleEventHowWatchTabs();
	$("[id^='inPersonDetails']").hide();
});


