/*
	Author: Andy Homan
	Date: 4/4/2010
	Version: 1.0
	(c) Process Creative
	www.processcreative.com.au
*/



window.onload = function()
{
	$('#initial-matte').fadeOut(3000,function()	{
		$(this).remove();
	});
};


/* Dynamic Nav Menu — Init & Config
--------------------------------------------------------------------------- */

function initMenu() {
  $('#menu ul').hide(); 		// hide all sub menus
  $('#menu ul.set').show();		
  $('#menu li a').click(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#menu ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
        }
      }
    );
  }
$(document).ready(function() {
	initMenu();
	$("#menu>li>ul").find("li:last").addClass("last");
	$("#menu>li.break>ul").find("li:last").removeClass("last");
	if($("#title+#jquery_jplayer").get(0)||$("#title+img").get(0)){
		// if first item is an image, audio or movie add class for alignment purposes
		$("#title").addClass("mediaFirst");
	}
});


/* AJAX hijack nav requests
--------------------------------------------------------------------------- */



$(document).ready(function () {
	//document.domain = "www.firstcutstudio.com"; // Permint secure cross script sharing for IE8
	
	
	// detecting for IE6 and 7
	if(jQuery.support.boxModel)	{
		$('body').append('<div id="initial-matte" style="position: fixed; width: 100%; height: 100%; background: white; z-index: 99999">&nbsp;</div>');
	} 
	/*
	else {
			$("#wrapper").css('display', 'none');
	}*/
	
	
	
	$("#menu a").not('[href=#]').add('#goldHdr a').each(function(){
		this.href = this.href.replace("http://www.firstcutstudio.com/", "#/");
	});
	//Check if url hash value exists (for bookmark)
	//if hash call getPage
	
	$.history.init(pageload);	
	
	var segments = document.location.hash.split("/");
	var lastSeg = segments[segments.length-2];
	
	//highlight the selected link
	$('#' + lastSeg).addClass('set').parents('ul:first').css('display', 'block');

	//Seearch for link with most recent clicked nav link
	$("#menu a").not('[href=#]').add('#goldHdr a').click(function (lastSeg) {
	
		//grab the full url
		var hash = this.href;

		//remove the # value
		hash = hash.replace(/^.*#/, '');
		
		
		if(("#"+hash) != document.location.hash){ //Make sure we havn't just clicked on the same page
		
		//for back button
	 	$.history.load(hash);	

	 	//clear the selected class and add the class class to the selected link
	 	$("#menu a").not('[href=#]').removeClass('set');

		if ($(this).parents('ul:first').attr('id') == "menu" || $(this).parent().attr('id') == "goldHdr"){
			$('#menu ul:visible').slideUp('normal');
		}
		
	 	$(this).addClass('set');
		
	 	//hide the content and show the progress bar
	 	$('#ajaxWrapper').fadeOut("slow");
	 	//$('#loading').show();

	 	//run the ajax
		
		//getPage();

		//cancel the anchor tag behaviour
		return false;
		}
	});	


});

function pageload(hash) {
	//if hash value exists, run the ajax
	if (hash) getPage();    
}

function getPage() {

	//generate the parameter for the php script
	var segments = document.location.hash.replace(/^.*#/, '');
	//alert(segments);
	//var data = "";
	
	$.ajax({
		url: "/ajax"+segments,	
		type: "GET",		
		//data: data,		
		cache: false,
		success: function (html) {	
			//hide the progress bar
			//$('#loading').hide();	

			//add the content retrieved from ajax and put it in the #content div
			$('#ajaxWrapper').html(html);

			//display the body with fadeIn transition
			$('#ajaxWrapper').fadeIn('slow');		
		}		
	});
}


/* jPlayer Audio Playlist – Init & Config
--------------------------------------------------------------------------- */

function initAudio(){
	//	Set up Audio Player (if it exists)
	if($("#jquery_jplayer").get(0)){
		var playItem = 0;

		var jpPlayTime = "";

		$("#jquery_jplayer").jPlayer({
			ready: function() {
				displayPlayList();
				playListInit(false); // Parameter is a boolean for autoplay.
			},
			swfPath: "assets/swf/"
		})
		.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime) {
			jpPlayTime.text($.jPlayer.convertTime(playedTime));
		})
		.jPlayer("onSoundComplete", function() {
			playListNext();
		});

		function  resetTime(index){
			for (i=0; i < myPlayList.length; i++) {
				$(".playTime_"+i).text("00:00");
			}
			jpPlayTime = $(".playTime_"+index);
		}

		function displayPlayList(totalTime) {
			for (i=0; i < myPlayList.length; i++) {
				$("#jplayer_playlist ul").append("<li id='jplayer_playlist_item_"+i+"'><a class='jp-play' href='#'>Play</a><a class='jp-stop' href='#'>Stop</a><div class='track'>"+ myPlayList[i].name +"</div><div class='time'><span class='playTime_"+i+"'>00:00</span> / "+myPlayList[i].total+"</div></li>");
				jpPlayTime = $(".playTime_"+i);
		
				// on item play
				$("#jplayer_playlist_item_"+i+" .jp-play").data( "index", i ).click( function() {
					var index = $(this).data("index");
					resetTime(index);
				
					$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current");
					$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current");
				
				
					if (playItem != index) {
						playListChange( index );
						return false
					} else {
						$("#jquery_jplayer").jPlayer("play");
						return false
					}
				});
		
				// on item stop
				$("#jplayer_playlist_item_"+i+" .jp-stop").data( "index", i ).click( function() {
					var index = $(this).data("index");
					resetTime(index);
				
					$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current");
				
					if (playItem != index) {
						playListChange( index );
						$("#jquery_jplayer").jPlayer("stop");
						return false
					} else {
						$("#jquery_jplayer").jPlayer("stop");
						return false
					}
				});
		
		
			}
		}
	
	
		function playListInit(autoplay) {
			if(autoplay) {
				playListChange( playItem );
			} else {
				playListConfig( playItem );
			}
		}
		function playListConfig( index ) {
			playItem = index;
			$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3);
		}
		function playListChange( index ) {
			playListConfig( index );
			$("#jquery_jplayer").jPlayer("play");
		}
		function playListNext() {
			var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
			playListChange( index );
		}
	}
}
