var playing;
playing = true;
var videoplaying;
videoplaying = false;

$(document).ready( function() {
		var currentLocation;
	// swfobjects							
		var so = new SWFObject("flash/player.swf", "topFlashFile", "190", "20", "8", "#ffffff");
		so.addParam("wmode", "transparent");
		so.addParam("allowScriptAccess", "always");
		so.write("topflash");
	
	// let's the magic start!
		historyHandler = new historyHandling();
		
	//
		var flash;
});

function getSWF(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
      if(document[movieName].length != undefined){
          return document[movieName][1];
      }
        return document[movieName];
    }
}

function getCurrentLocation(){
	return currentLocation;
}

function playVideoTrack(path) {
	videoplaying = true;		
	getSWF('videoplayerflashfile').playVideoTrack(path);
}

function playAudioTrack(path, title) {
	playing = true;
	videoplaying = false;		
	getSWF('topFlashFile').playAudioTrack(path, title);
}

function jumpToSection(locationFileName) {
	getSWF('topFlashFile').jumpToNewSection(locationFileName);
}

function pauseTopFlashAudio() {
	playing = false;		
	getSWF('topFlashFile').pauseFlashAudio();
}

function playTopFlashAudio() {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			flash = window.topFlashFile;
		} else {
			flash = window.document.topFlashFile;
		}
		if(playing != true){
			playing = true;
			getSWF('topFlashFile').playFlashAudio();	
		}
	videoplaying = false;		
}

function getVideoplaying(){
	return videoplaying;
}

function getDPSStatus(){
	return playdps;
}

function setVideoplaying(){
	videoplaying = true;
}

// because ajax is used it's needed to add event handlers to new links that appear when the content is changed
function addHistoryToLinks () {
	
	
	// checking if #main has non-ajax class which disables ajax links for whole content
		if ( $( "#main .content" ).attr( "class" ).search( /non-ajax/ ) == -1 )
		{
			// not() makes it possible to put external links in the source.
				$( "a" ).not( ".not-ajax" ).bind ( "click", function () {
					
					// add file name to the address and history
						unFocus.History.addHistory( this.href.substr( this.href.lastIndexOf( "/" ) + 1 ) );
					
					return false;
				});
		}
	// enabling ajax everywhere except for #main
		else
		{
			// not() makes it possible to put external links in the source.
				$( ".sidebar a, #masthead a, #footer a" ).not( ".not-ajax" ).bind ( "click", function () {
					
					// add file name to the address and history
						unFocus.History.addHistory( this.href.substr( this.href.lastIndexOf( "/" ) + 1 ) );
					
					return false;
				});		
		}
	
}

// dealing with menu selected item
function menuClick ( name ) {
	
	// removing existing classes
		$( "#nav li a.ui-tabs-selected" ).removeClass( "ui-tabs-selected" );
	
	
	// you will need to change this part of code if you change classes' names
		switch ( name )
		{
			
			case "albums":
				tab = 0;
			break;
			
			case "artist":
				tab = 1;
			break;
			
			case "tour":
				tab = 2;
			break;
			
			case "buzz":
				tab = 3;
			break;
			
			case "store":
				tab = 4;
			break;
			
			default:
				tab = -1;
			break;
			
		}
	
	// selecting the item
		if ( tab >= 0 )
		{
			$( "#nav li:eq("+tab+") a" ).addClass( "ui-tabs-selected" );
		}
		
			currentLocation = name;
	

}

// google maps handling
function addMaps () {
	
	if ( $( '#google-map' ).is( 'div' ) ) {
	
		var Icon = new GIcon();
			Icon.image = "images/maps/marker.gif";
			Icon.iconSize = new GSize( 91, 63 );
			Icon.iconAnchor = new GPoint( 44, 63 );
			Icon.infoWindowAnchor = new GPoint( 44, 63 );
			
		var location = $( "#map" ).html();
	
		function createMarker( point, html ) {
			
			var marker = new GMarker( point, Icon );
			
			GEvent.addListener( marker, "click", function() {
														  
				marker.openInfoWindowHtml('<form action="http://maps.google.com/maps" method="get" class="directions"><p class="enter">Enter your starting address for directions.<\/p><p><input type="text" name="saddr" id="saddr" value="" /> <input type="submit" value="Go" class="submit" /> <input type="hidden" name="daddr" value="'+location+'" /> <input type="hidden" name="hl" value="en" /></p></form>');
				
			});
			
			return marker;
		}		
		
		geocoder = new GClientGeocoder();
		
		geocoder.getLatLng( location, function ( point ) {
				
			var map = new GMap2( document.getElementById( "map" ) );
			map.addControl( new GSmallMapControl() );
			map.addControl( new GMapTypeControl() );
			map.setCenter( point , 14 );
			map.setMapType( G_HYBRID_MAP );
			
			var marker = createMarker( point, 'Some text to display' )
			map.addOverlay(marker);
		
		});
		
	}
	
	if ( $( '#nimbitWidget' ).is( 'div' ) ) {
	
		var nimbitstore = new SWFObject("http://www.nimbitmusic.com/nrp/views/flash/npsshell.swf?nps=http://www.nimbitmusic.com/nrp/views/flash/ExpandedStore.swf&artistName=carrienewcomer&appWidth=800&appHeight=700&abgc=0xC5BFB1&abga=0&pfc=0x275561&pdfc=0x1E0E00&psfc=0x745C49&tba=0&tbc=0xffffff&tbbc=0xffffff&tbbs=0&wc=false&ss=http://www.carrienewcomer.com/flash/store_loader.swf&st=3", "nimbitstoreFlash", "800", "700", "9", "#C5BFB1"); 
		nimbitstore.addParam("wmode", "transparent"); 
		nimbitstore.addParam("allowScriptAccess", "always"); 
		nimbitstore.write("nimbitWidget");
		
	} else {
		playTopFlashAudio();
	}
	
	if ( $( '#lightbulb' ).is( 'div' ) ) {
	
		var lighbulbflash = new SWFObject("/flash/carrie_lightbulb.swf", "lighbulbflashpiece", "279", "392", "8", "#0A0A09"); 
		lighbulbflash.addParam("wmode", "transparent"); 
		lighbulbflash.addParam("allowScriptAccess", "always"); 
		lighbulbflash.write("lightbulb");
		
	}
	
	if ( $( '#videoplayer' ).is( 'div' ) ) {
		var videoplayerflash = new SWFObject("flash/mediaplayer.swf", "videoplayerflashfile", "640", "386", "7", "#FFFFFF"); 
		videoplayerflash.addParam("wmode", "transparent"); 
		videoplayerflash.addParam("allowfullscreen","true");	
		videoplayerflash.addParam("allowScriptAccess", "always"); 
		videoplayerflash.addVariable("file","kirac1.flv");
		videoplayerflash.addVariable("image","flash/kirac1.jpg");
		videoplayerflash.addVariable("bufferlength","2");
		videoplayerflash.addVariable("displayheight","9999");
		videoplayerflash.addVariable("overstretch","fit");
		videoplayerflash.addVariable("autostart","false");
		videoplayerflash.addVariable("repeat","list");
		videoplayerflash.addVariable('backcolor','0xFFFFFF');
		videoplayerflash.addVariable('frontcolor','0x000000');
		videoplayerflash.addVariable('lightcolor','0xFFFFFF');
		videoplayerflash.addVariable('screencolor','0xFFFFFF');
		videoplayerflash.write("videoplayer");
		
		
		
	}
	
	if ( $( '#videoplayerbuzz' ).is( 'div' ) ) {
		var videoplayerbuzzflash = new SWFObject("/flash/carrie_video_player_buzz.swf", "videoplayerflashfile", "320", "285", "8", "#000000"); 
		 videoplayerbuzzflash.addParam("wmode", "transparent"); 
		 videoplayerbuzzflash.addParam("allowScriptAccess", "always"); 
		 videoplayerbuzzflash.write("videoplayerbuzz");
		
	}
	
	if ( $( '#videogallery' ).is( 'div' ) ) {
		displayVideoGallery();
	}
	
	if ( $( '#dpscontest' ).is( 'div' ) ) {
		var playdps = true;
	}
	
	

}

// changing the content using ajax
function openWithAjax ( href ) {
	
	
		$( '#ajax-content' ).hide();
		$( '#loading' ).show();
	
	$( '#ajax-content' ).load( href+" #main", function() {
		
		$( '#ajax-content' ).show();
		$( '#loading' ).hide();
		
		menuClick( $( "#main" ).attr( "class" ) );
		addHistoryToLinks();
		addMaps();
	});
	
}


function historyHandling()
{
	this.historyListener = function( hash ) {
	if ( hash == "" )
		{
			hash = location.pathname.substr(location.pathname.lastIndexOf( "/" ) + 1 );
			if (hash == "" )
			{
				hash = 'index.html';
			}
		}
		openWithAjax( hash );
	}
			

	unFocus.History.addEventListener( 'historyChange', this.historyListener );
	
	$( '#ajax-content' ).before( '<div id="loading"><div class="inner1"><div class="inner2"><img src="images/loading.gif" alt="Loading..." width="32" height="32" style="margin-left: 20px;" /></div></div></div>' );

	this.historyListener( unFocus.History.getCurrent() );
}

