jQuery(document).ready(function() {
	
	$ = jQuery;
	
	//init mp3 player
	$('#mp3-player').fancyMusicPlayer({autoPlay: false});
	
	
	//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
		var pageTitle = getPageTitle(History.getState().url);
		document.title = pageTitle + ' - ' + docTitle;
		loadPage(pageTitle);
		
	});
	
	// Bind to State Change
	History.Adapter.bind(window,'statechange',function(){

		var state = History.getState();
		loadPage(getPageTitle(state.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) { return false; }
		ajaxRunning = true;
		
		var menuItem = this;
		if($(this).hasClass('internal-link')) {
			//search for the menu item with the same link
			var menuItems = $("aside nav ul li a");
			for(var i=0; i < menuItems.length; ++i) {
				if(($(this).attr('href').search($(menuItems[i]).attr('href'))) >= 0) {
					menuItem = menuItems[i];
					break;	
				}
			}
		}
		
		History.pushState(null, $(menuItem).text() + ' - ' + docTitle , '?page='+$(menuItem).text());
		
		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(pageTitle) {
		
		$(currentMenu).stop().fadeTo(100, 0.5);
		
		//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]).text() == pageTitle) {
				currentMenu = menuItems[i];
				break;
			}
		}
		//no menu item, use the first one
		if(!currentMenu) {
			History.pushState(null, menuItems.first().text() + ' - ' + docTitle , '?page='+menuItems.first().text());
			return false;
		}
		
		//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);
		firstTime = false;
		
		//load page with ajax
		$('section#main').stop().fadeOut(200, function() {
			$.ajax({
				url: currentMenu.href,
				cache: false,
				error: function() {
					$('section#main').html("<p>Ups! "+currentMenu.href+" could not be found!</p>");
					$('section#main').stop().fadeIn(400, function() {ajaxRunning = false;});
					
				},
				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 getPageTitle(url) {
		
		var pageTitle;
		var pageIndexFirst = url.indexOf("?page=");
		if(pageIndexFirst == -1) {
			//no sub page found, must be the start page
		    pageTitle = $('aside nav ul li a:first').text();
		}
		else {
			//search for the sub page title
			pageTitle = url.substring(pageIndexFirst+6);
			if(!History.enabled) {
				pageTitle = pageTitle.substring(0, subPage.indexOf('&_'));
			}
		}	
		return pageTitle;
		
	};
});



