function getFlashMovie(movieName) {
	movieName = swfobject.getObjectById(movieName);
	return movieName;
}

var viewingHeader = true;
var header_news = 1;
var loadedCode = "";
var imagesLoaded = false;
var brightcove_stopped = false;
var artist_menu_open = false;
var expecting_hash_change = true;
var backPressed = false;

$(document).ready(function(){

	Cufon.replace('.cufonthis', { hover: true })

	$('#artists').cycle({ 
   		timeout: 0, 
    	speed:   800, 
    	startingSlide: 0
	});
	
	signupform();
	
	//stop the browser from scrolling down the page on first load
	setTimeout("$('html, body').animate({scrollTop:0}, 'fast');", 1000);
	
	$('#menubar').css("top", -$('#menubar').height());
	$('#mainSiteContent').css("top", $(window).height()+500);
	$('html, body').css("overflow", "hidden");
	
	$(window).bind("resize", resizeWindow);
	
	function resizeWindow( e ) {
		if(viewingHeader){
			$('#mainSiteContent').css("top", $(window).height()+500);
		}else{
			http://defjam.retrofuzz.com/mceuploads/backgrounds/ghostface-killah.jpg
			$('#bottomAligned').css("bottom", $(window).height()+500);
			$('#logo').css("bottom", $(window).height()-150);
		}
	}

	$('#header_news_tab').click(function(){
		gotoHeaderNews();
	});

	$('#header_releases_tab').click(function(){
		gotoHeaderReleases();
	});
	
	$('#discover_more').click(function(){
		gotoContent();
	});
	
	$('#li_home').click(function(e){
		e.preventDefault();
		gotoHeader();
		return false;
	});

	$('#li_news').click(function(){
		gotoNews();
		return false;
	});
	
	$('#li_releases').click(function(){
		gotoReleases();
		return false;
	});
	
	$('#li_tv').click(function(){
		gotoTV();
		return false;
	});
	
	$('#li_bio').click(function(){
		gotoBio();
		return false;
	});

	$('#artists_ul li').click(function(){
		gotoArtist($(this).attr('art_id'), true, false);
		artist_menu_open = false;
			$('#artist_menu').fadeOut();
	});
	
	$('#artists_button').click(function(){
		if(artist_menu_open){
			artist_menu_open = false;
			$('#artist_menu').fadeOut();
		}else{
			artist_menu_open = true;
			$('#artist_menu').fadeIn();
		}
	})
	
	
	//CHECK THE URL HASH HERE
	//IF UNDEFINED DO THE DEFAULT
	
	if (!hashURLincluded) {
		gotoNews();
		loadContent('#discover_more', 'ajax.php?module=discover_more&art_id=' + current_artist, false);
		loadContent('#twitterbody', 'ajax.php?module=twitterbody&art_id=' + current_artist, false);
		loadContent('#headerVideo', 'ajax.php?module=bcplayer&art_id=' + current_artist, false);
		loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=news', false);
		loadContent('#header_news', 'ajax.php?module=header_news&content=news&art_id=' + current_artist, false);
		loadedCode = "$('#artists').cycle(pic_pos[current_artist]);$('body').animate({ backgroundColor: \"#\"+hex_codes[current_artist] }, 800);";
		if(imagesLoaded){
			finishedLoadingImages();
		}
	}else{
		//wait for carousel images to load then do this:
		current_artist = hashArtist;
		loadInitialContent();
		loadedCode = 'gotoArtist(hashArtist, false, true);';
		if(imagesLoaded){
			finishedLoadingImages();
		}
	}
	
	
	//BIND AN EVENT FOR WHEN THE HASH ELEMENT CHANGES
	// Bind the event.
		$(window).hashchange( function(){
			// Alerts every time the hash changes!
			if(expecting_hash_change){
				expecting_hash_change=false;
			}else{
				//change location because back button has been pressed	
				
				hashURLincluded = false;
				
				if (window.location.hash.length > 1) {
			
					hashURLincluded = true;
					
					var hashElements = window.location.hash.split("/");
					
					//GET THE ARTIST ID
					for (var i = 0; i < artist_names.length; i++) {
						if (artist_names[i]!=undefined && artist_names[i] == hashElements[1]) {
							hashArtist = i;
						}
					}
					
					//Get an artist from the name array
					if(hashArtist==-1){
						var count = 0;
						var selected = undefined;
						while(selected==undefined&&count<artist_names.length){
							selected = artist_names[count];
							hashArtist = count;
							count++;
						}
					}
					
					hashSlug = hashElements[2];
					
					if(hashElements[0]=="#news"){
						hashView = 0;
					}else if(hashElements[0]=="#releases"){
						hashView = 1;
					}else if(hashElements[0]=="#tv"){
						hashView = 2;
					}else if(hashElements[0]=="#biographies"){
						hashView = 3;
					}else{
						hashView = 0;
					}
					
					gotoArtist(hashArtist);
					
					if (hashView == 2 && current_view != 2) {
						loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=video', false);
						clearMenu();
						$('#li_tv').addClass('menu_selected');
						$('#li_tv').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_tv').width() + "&h=87&zc=1) no-repeat 0px 11px");
						current_view = 2;
					}
					
					if(hashView == 3){
						loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=bios&art_id=' + current_artist, false);
						clearMenu();
						$('#li_bio').addClass('menu_selected');
						$('#li_bio').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_bio').width() + "&h=87&zc=1) no-repeat 0px 11px");
						current_view = 3;
					}
					
					if(hashView == 0){
						clearMenu();
						$('#li_news').addClass('menu_selected');
						$('#li_news').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_news').width() + "&h=87&zc=1) no-repeat 0px 3px");
						
						if (hashSlug != "" && hashSlug != undefined) {
							loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=news&slug=' + hashSlug, false);
						}
						else {
							loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=news', false);
						}
						current_view = 0;
						
					}
					
					if(hashView == 1){
						if (hashSlug != "" && hashSlug != undefined ) {
							loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=releases&slug=' + hashSlug, false);
						}else {
							loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=releases', false);
						}
						clearMenu();
						$('#li_releases').addClass('menu_selected');
						$('#li_releases').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_releases').width() + "&h=87&zc=1) no-repeat 0px 9px");
						current_view = 1;
					}
					
					backPressed = true;
					
				}
			}
		})
		
});

function finishedLoadingImages(){
	$('#loading_image').css("display", "none");
	imagesLoaded = true;
	eval(loadedCode);
}

function init_carousel(){
	// CAROUSEL CODE 
	
	var itemWidth = 430;
	var currentPos = [];
	
	//these are set using php
	currentPos[1] = 1;
	currentPos[2] = 1;
	
	/****MIGHT NOT NEED THIS BIT IF WE SWITCH TO FLOATED ELEMENTS****/
	//Space out all of the carousel items
	$('.selector_carousel').each(function(){
		
		//var num = $(this).attr('num');
		//currentPos[num] = 1;
		
		var xpos = 0;
		$('.carousel_item',$(this)).each(function(){
			$(this).css('left',xpos);
			xpos += itemWidth;
		});
	});
	
	/*************************************************************** */
	
	//for each set of selector buttons
	$('.selector').each(function(){
		//get the num of the selector
		var num = $(this).attr('num');
		var maxpos = $(this).attr('maxpos');
		
		//for each of the selector dots
		$('.selector_item',$(this)).each(function(){
			//get its pos
			var pos = $(this).attr('pos');
			
			//when we click this pos button
			$(this).click(function(){
				currentPos[num] = pos;
				
				//remove selected from all selectors
				$('.selector_item', $(this).parent()).removeClass('selector_selected');
				
				$(this).addClass("selector_selected");
				//move the carousel to the right position
				$('.carousel_holder', $('.selector_carousel[num="'+num+'"]')).animate({
					left: -((pos - 1) * (itemWidth))
				});
			});
		});
		
		//do the next button
		$('.selector_next',$(this)).click(function(){
			//if we've got room to move
			if((currentPos[num]+1)<=maxpos){
				//remove selected classes
				$('.selector_item', $(this).parent()).removeClass('selector_selected');
				currentPos[num]++;
				//add back selected class
				$('.selector_item[pos="'+currentPos[num]+'"]', $(this).parent()).addClass('selector_selected');
				//shift the holder
				$('.carousel_holder', $('.selector_carousel[num="'+num+'"]')).animate({
					left: -((currentPos[num] - 1) * (itemWidth))
				});
			}
		});
		
		//do the prev button
		$('.selector_prev',$(this)).click(function(){
			if (currentPos[num]>1) {
				$('.selector_item', $(this).parent()).removeClass('selector_selected');
				currentPos[num]--;
				$('.selector_item[pos="'+currentPos[num]+'"]', $(this).parent()).addClass('selector_selected');
				$('.carousel_holder', $('.selector_carousel[num="' + num + '"]')).animate({
					left: -((currentPos[num] - 1) * (itemWidth))
				});
			}
		});
	});
}

function loadContent(selector, url, effects){	
if((navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true){
	effects = false;
}
	if (effects) {
		$(selector).fadeTo(200, 0);
		setTimeout("$.getJSON('"+url+"', function(data){$('"+selector+"').html(data.html);Cufon.refresh();eval(data.script);$('"+selector+"').fadeTo(200, 1);});", 300);
	}else {
		$.getJSON(url, function(data){
			$(selector).html(data.html);
			Cufon.refresh();
			eval(data.script);
		});
	}
}


function updateHash(id, content_name){
	
	if (!backPressed) {
		expecting_hash_change = true;
	}else{
		backPressed = false;
	}
	var curPage = "";
	if(current_view==0){
		curPage = "news";
	}else if(current_view==1){
		curPage = "releases";
	}else if(current_view==2){
		curPage = "tv";
	}else if(current_view==3){
		curPage = "biographies";
	}
	
	if(id===undefined){
		window.location.hash = curPage+"/"+artist_names[current_artist];
	}else{
		window.location.hash = curPage+"/"+artist_names[current_artist]+"/"+content_name;
	}
}

function gotoHeaderReleases(){
	$('#header_news_tab').removeClass("header_tab_selected");
	$('#header_news_tab').addClass("header_tab");
	$('#header_releases_tab').removeClass("header_tab");
	$('#header_releases_tab').addClass("header_tab_selected");
	Cufon.refresh();
	header_news = 2;
	loadContent('#header_news', 'ajax.php?module=header_news&content=releases&art_id=' + current_artist, false);
}

function gotoHeaderNews(){
	$('#header_releases_tab').removeClass("header_tab_selected");
	$('#header_releases_tab').addClass("header_tab");
	$('#header_news_tab').removeClass("header_tab");
	$('#header_news_tab').addClass("header_tab_selected");
	Cufon.refresh();
	header_news = 1;
	loadContent('#header_news', 'ajax.php?module=header_news&content=news&art_id=' + current_artist, false);
}

function clearMenu(){
	$('#li_news').removeClass('menu_selected');
	$('#li_news').css("background", "none");
	$('#li_releases').removeClass('menu_selected');
	$('#li_releases').css("background", "none");
	$('#li_tv').removeClass('menu_selected');
	$('#li_tv').css("background", "none");
	$('#li_bio').removeClass('menu_selected');
	$('#li_bio').css("background", "none");
	Cufon.refresh();
}


function gotoNews(){
	if (current_view != 0) {
		clearMenu();
		$('#li_news').addClass('menu_selected');
		$('#li_news').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_news').width() + "&h=87&zc=1) no-repeat 0px 3px");
		
		//If we're on the TV section refresh everything
		if (current_view == 2 || current_view == 3) {
			loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=news', false);
		//Otherwise just refresh col_1 and col_2 row_2
		}else{
			loadContent('#col1', 'ajax.php?module=col1&content=news', false);
			loadContent('#col2_row2', 'ajax.php?module=col2_row2&content=news', true);
		}
		current_view = 0;
	}
}

function gotoReleases(){
	if (current_view != 1) {
		clearMenu();
		$('#li_releases').addClass('menu_selected');
		$('#li_releases').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_releases').width() + "&h=87&zc=1) no-repeat 0px 9px");
		
		//If we're on the TV section refresh everything
		if (current_view == 2 || current_view == 3) {
			loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=releases', false);
		//Otherwise just refresh col_1 and col_2 row_2
		}else{
			loadContent('#col1', 'ajax.php?module=col1&content=releases', false);
			loadContent('#col2_row2', 'ajax.php?module=col2_row2&content=releases', true);
		}
		
		current_view = 1;
	}
}

function gotoTV(){
	if (current_view != 2) {
		clearMenu();
		$('#li_tv').addClass('menu_selected');
		$('#li_tv').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_tv').width() + "&h=87&zc=1) no-repeat 0px 11px");
		loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=video', false);
		current_view = 2;
	}
}

function gotoBio(){
	if (current_view != 3) {
		clearMenu();
		$('#li_bio').addClass('menu_selected');
		$('#li_bio').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_bio').width() + "&h=87&zc=1) no-repeat 0px 11px");
		loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=bios&art_id='+current_artist, false);
		current_view = 3;
		modVP = null;
	}
}

function enableScrolling(){
	$('html, body').css("overflow-y", "auto");
}

function disableScrolling(){
	$('html, body').css("overflow-y", "hidden");
}

function gotoArtist(val, load, initial){

	if (current_artist != val && !initial) {
	
		$('#artists').cycle(pic_pos[val]);
		$('body').animate({ backgroundColor: "#"+hex_codes[val] }, 800);
		current_artist = val;
		loadContent('#socnet_holder', 'ajax.php?module=socnet_holder&art_id='+current_artist, true);
		if (header_news == 1) {
			loadContent('#header_news', 'ajax.php?module=header_news&content=news&art_id=' + current_artist, false);
		}
		else {
			loadContent('#header_news', 'ajax.php?module=header_news&content=releases&art_id=' + current_artist, false);
		}
		loadContent('#discover_more', 'ajax.php?module=discover_more&art_id=' + current_artist, false);
		loadContent('#twitterbody', 'ajax.php?module=twitterbody&art_id=' + current_artist, false);
		loadContent('#headerVideo', 'ajax.php?module=bcplayer&art_id=' + current_artist, false);
		if (current_view == 0) {
			if (load) {
				loadContent('#col1_col1', 'ajax.php?module=col1_col1&content=news&art_id=' + current_artist, true);
			}
			loadContent('#col1_col2', 'ajax.php?module=col1_col2&content=news_archive&art_id=' + current_artist, true);
		}else if (current_view == 1) {
			//CHANGE TO RELEASES HERE
			if (load) {
				loadContent('#col1_col1', 'ajax.php?module=col1_col1&content=releases&art_id=' + current_artist, true);
			}
			loadContent('#col1_col2', 'ajax.php?module=col1_col2&content=releases_archive&art_id=' + current_artist, true);
		}else if (current_view == 3) {
			//CHANGE TO RELEASES HERE
			if (load) {
				loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=bios&art_id='+current_artist, false);
			}
		}
	}
	
	if(initial){
	
		$('#artists').cycle(pic_pos[val]);
		$('body').animate({ backgroundColor: "#"+hex_codes[val] }, 800);
		current_artist = val;
		
		//this is a page call to load data based on slug
		
		
	}
}

function loadInitialContent(){
	if (header_news == 1) {
			loadContent('#header_news', 'ajax.php?module=header_news&content=news&art_id=' + current_artist, false);
		}
		else {
			loadContent('#header_news', 'ajax.php?module=header_news&content=releases&art_id=' + current_artist, false);
		}
		loadContent('#discover_more', 'ajax.php?module=discover_more&art_id=' + current_artist, false);
		loadContent('#twitterbody', 'ajax.php?module=twitterbody&art_id=' + current_artist, false);
		loadContent('#headerVideo', 'ajax.php?module=bcplayer&art_id=' + current_artist, false);
		//if we are looking for a slug just include the slug
		//if we are looking for an artist just include the artist
		
		current_view = hashView;
		if (current_view == 0) {
			if (hashSlug != "" && hashSlug != undefined) {
				loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=news&slug=' + hashSlug, false);
			}
			else {
				loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=news', false);
			}
			clearMenu();
			$('#li_news').addClass('menu_selected');
			$('#li_news').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_news').width() + "&h=87&zc=1) no-repeat 0px 3px");
		}else if (current_view == 1) {
			if (hashSlug != "" && hashSlug != undefined ) {
				loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=releases&slug=' + hashSlug, false);
			}else {
				loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=releases', false);
			}
			clearMenu();
			$('#li_releases').addClass('menu_selected');
			$('#li_releases').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_releases').width() + "&h=87&zc=1) no-repeat 0px 9px");
		}else if (current_view == 2) {
			loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=video', false);
			clearMenu();
			$('#li_tv').addClass('menu_selected');
			$('#li_tv').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_tv').width() + "&h=87&zc=1) no-repeat 0px 11px");
		}else if (current_view == 3) {
			loadContent('#mainSiteContent', 'ajax.php?module=mainSiteContent&content=bios&art_id=' + current_artist, false);
			clearMenu();
			$('#li_bio').addClass('menu_selected');
			$('#li_bio').css("background", "url(../thumb.php?src=_graphics/menu_selected.png&align=b&w=" + $('#li_bio').width() + "&h=87&zc=1) no-repeat 0px 11px");
		}
		gotoContent();
}

function gotoContent(){
	viewingHeader = false;
	setTimeout("$('#menubar').animate({top:0}, 400);", 600);
	setTimeout("enableScrolling();", 1000);
	$('#bottomAligned').animate({bottom: $(window).height()+500}, 1000, "swing");
	$('#logo').animate({bottom: $(window).height()-150}, 1000, "swing");
	$('#headerVideo').animate({left: -200}, 1000, "swing");
	$('#mainSiteContent').animate({top: 400}, 1000, "swing");
	stopTinyCove();
}

function gotoHeader(){

	viewingHeader = true;
	$('#menubar').animate({top:-$('#menubar').height()}, 400);
	disableScrolling();
	if (jQuery.browser.msie) {
  		if (parseInt(jQuery.browser.version) == 6) {
			$('#bottomAligned').animate({bottom: -120}, 1000, "swing");
		}else{
			$('#bottomAligned').animate({bottom: 0}, 1000, "swing");
		}
	}else{
		$('#bottomAligned').animate({bottom: 0}, 1000, "swing");
	}
	
	$('#mainSiteContent').animate({top: $(window).height()+500}, 1000, "swing");
	$('#logo').animate({bottom: 150}, 1000, "swing");
	$('#headerVideo').animate({left: 20}, 1000, "swing");
	$('html, body').animate({scrollTop:0}, 'slow');
	if (current_view == 0 || current_view == 1) {
		stop_that_cove(medVP);
	}
	else if (current_view == 2) {
		stop_that_cove(bigVP);
	}
	if (tinyVP !== undefined && tinyVP !== null) {
		tinyVP.setVolume(1);
	}
}

//  SIGNUP FORM SUMISSION
function signupform(){
	$('#email_form').submit(function(event){
		event.preventDefault();
		if ($('input[name=signup_email]').val() == ""){
			return;
		}
		
		var dataString = "email="+$('input[name=signup_email]').val();
		$.ajax({type: "POST",url: "signup.php",
			data: dataString,
		   		success: function(retVal){
					$('#signupmessage').css('display', 'none');
					$('#signupmessage').html(retVal).fadeIn(function(){
						$('input[name=signup_email]').val('');
					});
		   }
		});
	});
}


/****************************
 * GET BC PLAYER REFERENCES
 ***************************/

var tinyVP;
var medVP;
var bigVP;

var tinyStarted = false;

var stopTiny = false;
// called when template loads, this function stores a reference to the player and modules.
function onTemplateLoaded(experienceID) {

	if(experienceID=="tinyPlayer"){
		var bcExp = brightcove.getExperience(experienceID);
		tinyVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
	}else if(experienceID=="medPlayer"){
		var bcExp = brightcove.getExperience(experienceID);
		medVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
	}else if(experienceID=="bigPlayer"){
		var bcExp = brightcove.getExperience(experienceID);
		bigVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
	} 

}

function stopTinyCove(){
	if (tinyVP != undefined) {
		tinyVP.setVolume(0);
	}
	if (tinyStarted) {
		tinyVP.pause(true);
		tinyStarted = false;
	}else{
		if (tinyVP != undefined) {
			if(tinyVP.getCurrentVideo()!=null){
				tinyVP.pause(true);
				tinyVP.setVolume(0);
				tinyStarted = true;
			}
		}
		setTimeout("stopTinyCove()", 5000);
	}
}

function stop_that_cove(vp){
	if (vp !== undefined && vp !== null) {
		vp.pause(true);
	}
}


