$(document).ready(function(){
	$(window).bind( 'hashchange', function() {
		loadContentFromHash();
	});
	
	$('a[rel="external"]').live('click', function(){
		window.open(this.href);
        return false;
	});
	
	$('#loader').click(function(){
		$(this).fadeOut(2000);
	});
	
	$("#loader").fadeIn('slow');
	$('.ajaxlink-vid').click(function(){
		var vidToLoad = $(this).attr('href');
		loadVid(vidToLoad);
		return false;		
	});
	$("#jquery_jplayer").jPlayer({
		ready: function () {
			this.element.jPlayer("setFile", "/audio/bg_music.mp3");
			/*this.play();*/
		},
		swfPath: "/wyrostek/scripts",
		customCssIds: true
	}).jPlayer("cssId", "play", "play").jPlayer("cssId", "pause", "pause").jPlayer("onSoundComplete", function() {
		this.element.jPlayer("play");
	});
	$("#pause").hide();
	function showPauseBtn(){
		$("#play").hide(function(){
			$("#pause").show();
		});
	}
	function showPlayBtn(){
		$("#pause").hide(function(){
			$("#play").show();
		});
	}
	
	$("#nav_projekty").mouseover(function(){
		$("#subnav_media.visible, #subnav_rider.visible").hide('slide', {direction: 'up'}).removeClass('visible');
		if ($("#subnav_projekty").hasClass('visible') == false) {
			$("#subnav_projekty").show('slide', {direction: 'up'}).addClass('visible');
		}
	});
	$("#nav_media").mouseover(function(){
		$("#subnav_projekty.visible, #subnav_rider.visible").hide('slide', {direction: 'up'}).removeClass('visible');
		if ($("#subnav_media").hasClass('visible') == false) {
			$("#subnav_media").show('slide', {direction: 'up'}).addClass('visible');
		}
	});
	
	$("#nav_rider").mouseover(function(){
		$("#subnav_projekty.visible, #subnav_media.visible").hide('slide', {direction: 'up'}).removeClass('visible');
		if ($("#subnav_rider").hasClass('visible') == false) {
			$("#subnav_rider").show('slide', {direction: 'up'}).addClass('visible');
		}
	});
	
	$('#nav_news, #nav_biografia, #nav_dyskografia, #nav_koncerty, #nav_prasa, #nav_galeria, #nav_kontakt, #content_cont').mouseover(function(){
		$(".submenu.visible").hide('slide', {direction: 'up'}).removeClass('visible');
	});
	

	$("#gallery_thumbs a").fancybox();
	$("#inline").fancybox();
	$('#concerts_accordion').accordion({autoHeight: false, collapsible: true, active: $('.accordion-active')});

	$('#nav li a').hover(function(){
		$('span', this).fadeIn('fast');
	}, function(){
		$('span', this).fadeOut(0);
	});
	
	$('#toolbar li a').hover(function(){
		$('span', this).fadeIn('fast');
	}, function(){
		$('span', this).fadeOut('fast');
	});

	$('#nav li a, .ajaxlink-project, .ajaxlink-global').click(function(){
		if ($(this).attr("id") != 'nav_media') {
		window.location.hash = $(this).attr('href');
		var hash = window.location.hash.substr(1);
		if (hash.search('http://') != -1){
			var newHash = hash.substr(21);
			window.location.hash = newHash;
			hash = newHash;
		}	
		if ((hash != 'projekty.php') && (hash != 'projects.php') && (hash != 'solo.php') && (hash != 'corazon.php') && (hash != 'coloriage.php') && (hash != 'corazon.php?a=galeria') && (hash != 'coloriage.php?a=galeria') && (hash != 'media.php') && (hash != 'media.php?a=video')) {
			$(".submenu.visible").hide('slide', {direction: 'up'});
			$(".submenu.visible").removeClass('visible');
		}
		}
		return false;
	});

	$(window).trigger( "hashchange" );
	

	
});

function loadContentFromHash() {
	var hash = window.location.hash.substr(1);
	$('#nav li a').removeClass();
	if (hash=='media.php?a=video') {
		$("#jquery_jplayer").jPlayer("stop");
	}
    if(hash != '') {
		var href = $('#nav li a').each(function(){
			var toSearch= $(this).attr('href').substr(0,$(this).attr('href').length-4);
    	   	var searchName = hash.search(toSearch);  
    	   	if (searchName == 0) {
	    		$(this).addClass('active');
	    	}
  		});	
  		loadPageContent(hash, true);
    } else {
    	hash = "index.php";
    	loadPageContent(hash, false);
    }
    

}

function loadPageContent(pageToLoad, withAnimation) {
	var contentToLoad = pageToLoad+' #page_content';
	if (pageToLoad!='index.php' && $('#logo').is(':visible') == false ){ $('#logo').fadeIn(); }
	if (pageToLoad=='index.php' && $('#logo').is(':visible') == true ){ $('#logo').fadeOut(); }
	if (withAnimation == true) {
		
		$("#ajax-loader").fadeIn();
		$('#content_cont').fadeOut(600, function(){
		$('#content_cont').load(contentToLoad, function(){
		
		var col = $('#news.content');
		col.scroll(function(){
  			 if (col.outerHeight() == (col.get(0).scrollHeight - col.scrollTop())) {
  			 	$("#ajax-loader").fadeIn();
      			 var noBoxes = $('.regular_nb').length;	
      			 var pageNo = noBoxes+1;
      			 var newsToLoad = 'news.php?p='+pageNo;
      			 var divToLoad = '#anewsbox'+noBoxes;
      			 $.get(newsToLoad,function(data){
        			$("#newswrapper").append($('.regular_nb', data));
        			$("#ajax-loader").fadeOut();
				},'html'); 
      			 /*$('#wrapper').append('<div id="anewsbox'+noBoxes+'"></div>');
      			 $('#anewsbox'+noBoxes).load(newsToLoad);
      			 alert(newsToLoad);*/
      		}
		});

			$('.ajaxlink-vid').click(function(){
				var vidToLoad = $(this).attr('href');
				loadVid(vidToLoad);
				return false;
			});
			$('.audioplayer').click(function(){
				$("#jquery_jplayer").jPlayer("stop");
			});
			$('#audioplayer_1').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/libertango.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-1").jPlayer("cssId", "pause", "pause-1").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			$('#audioplayer_2').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/rag-musette.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-2").jPlayer("cssId", "pause", "pause-2").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_3').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/recenica.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-3").jPlayer("cssId", "pause", "pause-3").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_4').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/vivaldi.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-4").jPlayer("cssId", "pause", "pause-4").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_5').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/ctq-libertango.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-5").jPlayer("cssId", "pause", "pause-5").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_6').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/oblivion.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-6").jPlayer("cssId", "pause", "pause-6").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_7').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/vuelvo.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-7").jPlayer("cssId", "pause", "pause-7").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_8').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/eltango.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-8").jPlayer("cssId", "pause", "pause-8").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_9').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/mangione.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-9").jPlayer("cssId", "pause", "pause-9").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_10').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/siemionow.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-10").jPlayer("cssId", "pause", "pause-10").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_11').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/bulgar.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-11").jPlayer("cssId", "pause", "pause-11").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			$('#audioplayer_12').jPlayer({
				ready: function () {
					this.element.jPlayer("setFile", "/audio/qwerty.mp3");
					/*this.play();*/
				},
				swfPath: "/wyrostek/scripts",
				customCssIds: true
			}).jPlayer("cssId", "play", "play-12").jPlayer("cssId", "pause", "pause-12").jPlayer("onSoundComplete", function() {
				this.element.jPlayer("play");
			});
			
			/*AudioPlayer.embed("audioplayer_1", {soundFile: "http://wyrostek.com.pl/audio/libertango.mp3", titles: "Libertango"});  
			AudioPlayer.embed("audioplayer_2", {soundFile: "http://wyrostek.com.pl/audio/rag-musette.mp3", titles: "Rag Musette"});  
			AudioPlayer.embed("audioplayer_3", {soundFile: "http://wyrostek.com.pl/audio/recenica.mp3", titles: "Recenica"});  
			AudioPlayer.embed("audioplayer_4", {soundFile: "http://wyrostek.com.pl/audio/vivaldi.mp3", titles: "Winter"});  
			AudioPlayer.embed("audioplayer_5", {soundFile: "http://wyrostek.com.pl/audio/ctq-libertango.mp3", titles: "Libertango"});  
			AudioPlayer.embed("audioplayer_6", {soundFile: "http://wyrostek.com.pl/audio/oblivion.mp3", titles: "Oblivion"});  
			AudioPlayer.embed("audioplayer_7", {soundFile: "http://wyrostek.com.pl/audio/vuelvo.mp3", titles: "Vuelvo al"});  
			AudioPlayer.embed("audioplayer_8", {soundFile: "http://wyrostek.com.pl/audio/eltango.mp3", titles: "El Tango De"});  
			AudioPlayer.embed("audioplayer_9", {soundFile: "http://wyrostek.com.pl/audio/mangione.mp3", titles: "Children of Sanchez"});  
			AudioPlayer.embed("audioplayer_10", {soundFile: "http://wyrostek.com.pl/audio/siemionow.mp3", titles: "Giankino Horo"});  
			AudioPlayer.embed("audioplayer_11", {soundFile: "http://wyrostek.com.pl/audio/bulgar.mp3", titles: "Recenica"});  
			AudioPlayer.embed("audioplayer_12", {soundFile: "http://wyrostek.com.pl/audio/qwerty.mp3", titles: "Qwerty"}); */ 

			$('#concerts_accordion').accordion({autoHeight: false, collapsible: true, active: $('.accordion-active')});
			$("#gallery_thumbs a").fancybox();
			
			$("#rider").toggle(function(){
				$('.rider').fadeIn();
				return false;
			}, function(){
				$('.rider').fadeOut();
				return false;
			});
			$('.ajaxlink-global, .ajaxlink-project').click(function(){
				window.location.hash = ($(this).attr('href'));
				return false;
			});
			$("#ajax-loader").fadeOut();
			
			$('#content_cont').fadeIn(800);
		});
		});
	} else {
		$('#content_cont').load(contentToLoad);
		$("#ajax-loader").fadeOut();
	}
	
}

function loadVid(videoToLoad) {
	$("#ajax-loader").fadeIn();
	$('#video_cont').fadeOut(600, function(){
		$("#jquery_jplayer").jPlayer("stop");
		var contentToLoad = videoToLoad+' #videoitem';
		$('#video_cont').load(contentToLoad, function(){
			$("#ajax-loader").fadeOut();
			$('#video_cont').fadeIn(800);
		});
	});
}

$(window).load(function(){
	$('#loader').delay(1000).fadeOut(2000).delay(10000).queue(function(){

			var hash = window.location.hash;
			if(hash == '') {

				$('#news_button').trigger('click');
			}

	});
	$("#ajax-loader").fadeOut();
});
