//some soundmanager settings
if(soundManager) {
	soundManager.url = 'swf/';
	soundManager.flashVersion = 9;
	soundManager.useHTML5Audio = true;
}

jQuery(document).ready(function() {
	
	$ = jQuery;
	
	//init mp3 player
	$('#mp3-player').fancyMusicPlayer({autoPlay: true});
	
	
	//hover handler for the social bar buttons
	$('#social-bar a').hover(
	  function() {
		  $(this).data('title', this.title);
		  $('#social-bar').children('p').remove();
		  $('#social-bar').append("<p>"+this.title+"</p>").children('p').hide().stop().slideDown(200);
		  this.title = '';	
	  },
	  function() {
		  $('#social-bar').children('p').stop().slideUp(100);
		  this.title = $(this).data('title');
		  $(this).removeData('title');
	  }
	);
	
	
	//bind handlers for the main content area
	$('.play-now').live('click', function() {
		$.fancyMusicPlayer.addTrack(this.href, this.title,'', true);
		return false;
	});
	
	
	//----------------------
	//--DEEPLINKING SCRIPT--
	//----------------------
	var currentMenu, History = window.History, firstTime = true, docTitle = document.title, ajaxRunning = true;
    	
	History.Adapter.onDomLoad(function() {
		//first initial
		loadPage(getPageUrl(History.getState().url));
	});
	
	// Bind to State Change
	History.Adapter.bind(window,'statechange',function(){
		loadPage(getPageUrl(History.getState().url));
		//History.log('statechange:', State.data, State.title, State.url);
	});
	
	//click and hover handler for the menu items
	$('aside nav ul li a, .internal-link').live('click', function() {
		if(ajaxRunning || this == currentMenu) { return false; }
		ajaxRunning = true;
		
		var menuItem = $(this);
		History.pushState(null, menuItem.text() + ' - ' + docTitle , '?page='+menuItem.attr('href'));
		
		//set a new background image
		if(this.rel != null && this.rel != '') {
			$('body').css('backgroundImage', 'url('+this.rel+')');
		}
		
		return false;
	}).css({
		'opacity': 0.5
	}).hover(
	  function() {
		  $(this).stop().fadeTo(300, 1);
	  },
	  function() {
		  if(currentMenu != this) {
			  $(this).stop().fadeTo(200, 0.5);
		  }
	  }
	).get(0);
	
	//load page by title
	function loadPage(pageUrl) {

		//search for the menu item by title
		var menuItems = $("aside nav ul li a");
		for(var i=0; i < $("aside nav ul li a").size(); ++i) {
			if($(menuItems[i]).attr('href') == pageUrl) {
				$(currentMenu).stop().fadeTo(100, 0.5);
				currentMenu = menuItems[i];
				//set a new background image
				if(currentMenu.rel != null && currentMenu.rel != '') {
					$('body').css('backgroundImage', 'url('+currentMenu.rel+')');
				}
				break;
			}
		}
		
		if(currentMenu) {
			//fade in and move arrow to the current menu item
			$(currentMenu).stop().fadeTo(firstTime ? 0 : 500, 1);
			$('aside nav #menu-arrow').animate({top: $(currentMenu).position().top}, firstTime ? 0 : 500);
		}
		else {
			$('aside nav #menu-arrow').animate({top: $("aside nav ul li a:first").position().top}, firstTime ? 0 : 500);
		}
		firstTime = false;
		
		//load page with ajax
		$('section#main').stop().fadeOut(200, function() {
			$.ajax({
				url: pageUrl,
				cache: false,
				error: function() {
					History.pushState(null, '404 Error - ' + docTitle , '?page=404.html');
				},
				success: function(html) {
					//stop soundcloud player
					if($.scPlayer) { $.scPlayer.stopAll(); }
					
					$('section#main').html(html);
					
					$('section#main .ribbon').wrapInner("<div class='ribbon-center'></div>").prepend("<div class='ribbon-left'></div>").append("<div class='ribbon-right'></div>").addClass('clearfix');
					$('section#main .tag-list').addClass('clearfix').find('li').wrapInner("<div class='tag-left'></div>").append("<div class='tag-right'></div>").addClass('clearfix');
					$('section#main .tag').wrapInner("<div class='tag-left'></div>").append("<div class='tag-right'></div>").addClass('clearfix');
					$('section#main .rounded').wrap("<div class='rounded-border'></div>").addClass('rounded-image');
					$('section#main .new').before("<div class='new-star'></div>");
					$('section#main .zebra-table tbody tr:odd').addClass('odd');
					$('section#main .box-table tr').find('td:last').css({'border-right': 'none', 'border-left': 'none'});
					$('section#main .box-table th:last').css({'border-right': 'none', 'border-left': 'none'});
					$('section#main').stop().fadeIn(400, function() {ajaxRunning = false;});
					
					//soundcloud player
					if($.scPlayer) {
						var scPlayer = $('a.sc-player, div.sc-player').scPlayer({loadArtworks: 1000, randomize: true, autoPlay: false});
						
						//pause music player when soundcloud player starts playing
						$(document).bind('onPlayerPlay.scPlayer', function(evt){
						  $.fancyMusicPlayer.pause();
						});
					}
					
					//dont use deeplinking
	                $("a[rel^='prettyPhoto']").prettyPhoto({deeplinking: false}).find('img').hover(
					  function() {
						  $(this).stop().fadeTo(300, 0.8);
					  },
					  function() {
						  $(this).stop().fadeTo(300, 1);
					  }
					);
				}
			});
		});
		
	};
	
	//returns the true page title
	function getPageUrl(url) {
		
		var realUrl;
		var pageIndexFirst = url.indexOf("?page=");
		if(pageIndexFirst == -1) {
			//no sub page found, must be the start page
		    realUrl = $('aside nav ul li a:first').attr('href');
		}
		else {
			//search for the sub page title
			realUrl = url.substring(pageIndexFirst+6);
			if(!History.enabled) {
				realUrl = realUrl.substring(0, subPage.indexOf('&_'));
			}
		}	
		return realUrl;
		
	};
});



