/*global $, DD_roundies, document, jQuery, swfobject*/
"use strict";

var page;
var newsCount;
var currentTheme;
var currentMenu;
var flashMovie;

var fadeTime = 300;

jQuery.easing.def = "easeOutExpo";

// Detecting Browser Version
function detectBrowser() {	
	var isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
	
	if (isIE6) {
		document.location = "upgrade.html";
	}
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function loadTop() {
	
	var flashvars = {}, params = {}, attributes = {};
	
	params.menu = "false";
	params.wmode = "transparent";
	
	attributes.swliveconnect = "true";

	swfobject.embedSWF('topbar.swf', 'musicPlayer', '800', '30', '9.0.0', 'expressInstall.swf', flashvars, params, attributes);
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// THEMES
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function blackSite() {
	
	$('#container').fadeOut(fadeTime, function() {
	
		currentTheme = "black";
		$.cookie("theme", "black", { expires: 60 });
		
		$("#theme").attr({href : "css/theme_black.css"});
		$("h1 img").attr({src : "images/bhlogo_white.png"});
		$("#splash").attr({src : "images/splash_black.jpg"});
		
		DD_roundies.addRule('#mainNav a', '0px', true);				
		DD_roundies.addRule('table.shows', '0px', true);
		DD_roundies.addRule('dl.album a', '0px', true);
		DD_roundies.addRule('.storeItem', '0px', true);
		DD_roundies.addRule('.links', '0px', true);
		DD_roundies.addRule('#datepicker', '0px', true);
		DD_roundies.addRule('#pages a', '0px', true);
		DD_roundies.addRule('.contact', '0px', true);
		
		//var flashMovie = getFlashMovieObject("musicPlayer");
	    //flashMovie.SetVariable("/:mainBKGD", 0x000000);
	    $('#container').delay(fadeTime * 2).fadeIn(fadeTime);
	});
}

function whiteSite() {
	
	$('#container').fadeOut(fadeTime, function() {
	
		currentTheme = "white";
		$.cookie("theme", "white", { expires: 60 });
		
		$("#theme").attr({href : "css/theme_white.css"});
		$("h1 img").attr({src : "images/bhlogo_black.png"});
		$("#splash").attr({src : "images/splash_white.jpg"});
				
		DD_roundies.addRule('#mainNav a', '8px 8px 0px 0px', true);
		DD_roundies.addRule('table.shows', '8px', true);
		DD_roundies.addRule('dl.album a', '8px', true);
		DD_roundies.addRule('.storeItem', '8px', true);
		DD_roundies.addRule('.links', '8px', true);
		DD_roundies.addRule('#news', '8px', true);
		DD_roundies.addRule('#datepicker', '8px', true);
		DD_roundies.addRule('#pages a', '8px', true);
		DD_roundies.addRule('.contact', '8px', true);
		
		// var flashMovie = getFlashMovieObject("musicPlayer");
		// flashMovie.SetVariable("/:mainBKGD", 0xFFFFFF);	
		
		$('#container').delay(fadeTime).fadeIn(fadeTime);
	});
}

function redSite() {
	$('#container').fadeOut(fadeTime, function() {
		
		currentTheme = "red";
		$.cookie("theme", "red", { expires: 60 });
	
		$("#theme").attr({href : "css/theme_red.css"});
		$("h1 img").attr({src : "images/bhlogo_white.png"});
		$("#splash").attr({src : "images/splash_red.jpg"});
					
		DD_roundies.addRule('#mainNav a', '8px 8px 0px 0px', true);
		DD_roundies.addRule('table.shows', '0px', true);
		DD_roundies.addRule('dl.album a', '0px', true);
		DD_roundies.addRule('.storeItem', '0px', true);
		DD_roundies.addRule('.links', '0px', true);
		DD_roundies.addRule('#datepicker', '0px', true);
		DD_roundies.addRule('#pages a', '0px', true);
		DD_roundies.addRule('.contact', '0px', true);
	
		// var flashMovie = getFlashMovieObject("musicPlayer");
		// flashMovie.SetVariable("/:mainBKGD", 0xFFFFFF);
		
		$('#container').delay(fadeTime).fadeIn(fadeTime);
	});
}

function initTheme() {
		
	currentTheme =  $.cookie("theme");
		
	if (currentTheme == "black") {	
		blackSite();
	} else if (currentTheme == "white"){
		whiteSite();
	} else {
		redSite();
	}
		
	$('#themes a').tooltip({
		track: false,
		delay: 100,
		fade: 150,
		top: 50,
		left: -10,
		showURL: false
	});
}

function showLiker() {
	$('#liker').animate({
		right: 0
	}, 1000);
}

function hideLiker() {
	$('#liker').animate({ 
		right: -1000
	}, 1000);	
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Site Location 
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function queryString(theString) {
	var splitString = theString.split("#");
	return splitString[1];
}

function loadPage(thePage) {
		
	if (thePage == "home.html") {
		$(currentMenu).removeClass('selected');	
		hideLiker();
	} else {
		showLiker();	
	}
	
	$("#content").hide().load("pages/" + thePage, function (){
		$("#content").fadeIn(500);
	});
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// NEWS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function loadData () {
	$("#newsInner").hide();	
		
	if (currentTheme == "white") {	
		$("#news").append("<img id='loader' src='images/loader_white.gif'/>");
	} else {
		$("#news").append("<img id='loader' src='images/loader_black.gif'/>");
	}
		
	$("#loader").css('margin-top', '100');
	
	$("#newsInner").load("php/getNews.php", {page: page}, function () {
		$("#loader").remove();	
		$("#newsInner").fadeIn(1000);
	});	
}

function newerPosts () {
	page--;
	loadData();
	
	$(".itemCount").html('item ' + (page + 1) + ' of ' + newsCount);
	
	if (page === 0) {
		$("#newer").hide();
	} else if (page < newsCount) {
		$("#older").fadeIn(500);
	}
}

function olderPosts () {
	page++;
	loadData();
	
	$(".itemCount").html('item ' + (page + 1) + ' of ' + newsCount );
			
	if (page == 1) {
		$("#newer").fadeIn(500);
	} else if (page > newsCount - 2) {
		$("#older").hide();
	}
}

function filterItems (searchText) {
	var theDate, items;
	
	theDate = searchText.getMonthName() + " " + searchText.getDate() + ", " + searchText.getFullYear();
			
	$(".newsItem").hide();
	
	if (searchText == '') {
		$(".newsItem").show();
	} else {
		items = $(".newsItem:contains(" + theDate + ")").show();
	}
			
	if (items.length === 0) {
		$(".newsItem").show();
		alert("no entries found");
	}
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// NEW FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function initNews() {
	
	page = 0;
	newsCount = 7;
	
	$(".itemCount").html('item ' + (page + 1) + ' of ' + newsCount);
		
	$('#datepicker').datePicker({
		inline:true,
		startDate: '01/01/1970',
		endDate: (new Date()).asString()
	})
	.bind(
		'dateSelected',
		function(e, selectedDate, $td) {
			filterItems(selectedDate);
	})
	.val("4/10/2009").trigger('selected');

	loadData();	
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// NEW FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function initSite () {
	detectBrowser();
		
	$('#jsDisabled').hide();
	initTheme ();
	
	loadTop();
 			   			
	var currentPage = queryString(window.location.hash);

	if (currentPage == null){
		currentPage = "home";
	}
						
	loadPage(currentPage + ".html");
 			
	if (currentPage == "/news") {
		currentMenu = $("#mainNav li:nth-child(1)");
	} else if (currentPage == "/bio") {
		currentMenu = $("#mainNav li:nth-child(2)");
	} else if (currentPage == "/members") {
		currentMenu = $("#mainNav li:nth-child(3)");
	} else if (currentPage == "/discography") {
		currentMenu = $("#mainNav li:nth-child(4)");
	} else if (currentPage == "/press") {
		currentMenu = $("#mainNav li:nth-child(5)");
	} else if (currentPage == "/shows") {
		currentMenu = $("#mainNav li:nth-child(6)");
	} else if (currentPage == "/merch") {
		currentMenu = $("#mainNav li:nth-child(7)");
	} else if (currentPage == "/contact") {
		currentMenu = $("#mainNav li:nth-child(8)");	
	} else if (currentPage == "/links") {
		currentMenu = $("#mainNav li:nth-child(9)");
	} else {
		// do nothing
	}
	
	if (currentMenu != null) {
		currentMenu.addClass("selected");
	}
	
	$('#mainNav li').bind('click', function() {
		$(currentMenu).removeClass('selected');
		$(this).addClass('selected');
		currentMenu = this;  			
	});
}
