/*
	JavaScript library for http://beatservicemusic.com
	Copyright © 2010 Allan Mäeots - all rights reserved - info@tehomg.com
*/
(function(window,document,undefined) {
	/* AJAX-based navigation */
	go = function(where) {
		where = where.split($('base').attr('href'))[1];
		if(pageNow != where) {
			pageNow = where;
			/* Fix the menu */
			$('#nav').find('a').stop().unbind().removeClass('act').removeClass('js');
			$('#nav_'+where.split('/')[0]).addClass('act').css('opacity',1);		
			/* Set URL */
			window.location.hash = "#/" + where;
			/* Get new page */
			$.get(where,function(data) {
				/* Analytics */
				_gaq.push(['_trackPageview','/'+where]);
				/* Scroll to top, replace the data */
				$('html,body').scrollTop(0);
				$('#loaderCell').html(data);
				/* Refresh JS */
				pgload();
			});
		}
		return false;
	};
	/* Back - Forward checker */
	(checklocation = function() {
		var url = document.location.toString();
		if(url.match('#/')) {
			url = url.split('#/')[1];
			if(pageNow != url) go($('base').attr('href')+url);
		}	
	})();
	/* Check it with an interval */
	setInterval(checklocation,500);	
	/* Form submits */
	submit_form = function(form) {
		$.post("http://beatservicemusic.com/contact", $("#"+form).serialize(),function(data) {
			$('#loaderCell').html(data);
			pgload();
		});
		return false;
	};
	/* Audioplayer */
	(play = function(id,play) {
		$('#player').load('e_demo.php?id='+id+'&play='+play);
		if(play) _gaq.push(['_trackPageview','/demo-clip-'+id]);
		return false;
	})(0,0);
	/* Links in new window */
	redir = function(where) {
		window.open(where);
		return false;
	};
	/* Page ON-LOAD actions (for AJAX) */
	var slideTmo,shuffleTmo,owlTmo,prevPos;
	(pgload = function() { 	
		/* Menu glow-animation */
		$('#nav').find('a').not('.act').addClass('js').css('opacity',0).hover(
			function() { $(this).stop().css('opacity',1); },
			function() { $(this).animate({opacity:0},300); }
		);	
		/* Discography glow-animation */
		$('#disco').find('img').not('.act').css('opacity',0.4).hover(
			function() { $(this).stop().css('opacity',1); },
			function() { $(this).animate({opacity:0.4},300); }
		);
		/* FP-Slides */
		var slideSelectors = ''; var s = 0;
		(slideIt = function(id) {
			clearTimeout(slideTmo);
			if(!slideSelectors) {
				$("#slides img").each(function() {
					s++;
					$(this).attr('id','galimg_'+s);
					slideSelectors += "<a href='#' onclick='slideIt(" + s + ");return false' id='galbutton_"+s+"'></a>"; 
				});
				$("#slides").html($("#slides").html() + "<p>" + slideSelectors + "</p>");
				$("#slides a").css({opacity:"0.2"});
			} else
				$("#slides img").fadeOut(100).css({'z-index':1});
			$("#slides a").animate({opacity:0.2},200);
			$("#galbutton_" + id).stop().animate({opacity:0.6},200);
			$("#galimg_"+id).unbind().css({'z-index':(s)}).fadeIn(1000);
			if(id < s) id++; else id = 1;
			slideTmo = setTimeout('slideIt('+id+')',10000);
		})(1);
		/* Image shuffler */
		var shuffleBox = $('#shuffle'); var shufflerImg = new Array(); var shufflerSlides = 17;
		(shuffler = function(state) {
			if(state) {
				var nextPos = Math.ceil(Math.random()*shufflerSlides);
				if(nextPos > 4 && nextPos != prevPos) {
					shuffleTmo = setTimeout(function() {
						prevPos = nextPos; shuffler(1);
					},120);
					shuffleBox.css('background-image','url('+shufflerImg[nextPos].src+')');
				} else
					shuffler(1);
			} else {
				if(shufflerImg.length < 1) {
					/* Preload images */
					for(i=0;i<=shufflerSlides;i++) {
						shufflerImg[i] = document.createElement('img');
						shufflerImg[i].src = 'LAYOUT/DEFAULT/SHUFFLE/pic_'+i+'.jpg';
					}
				}
			}
		})();		
		/* Trigger the shuffler */
		shuffleBox.hover(
			function() {
				shuffleBox.find('span').stop().hide();
				owlTmo = setTimeout(function() {
					clearTimeout(shuffleTmo);				
					shuffleBox.css('background-image','url('+shufflerImg[0].src+')');
					owlTmo = setTimeout(function() {
						shuffler(1);
					},500);
				},10000);
				shuffler(1);
			},
			function() { 
				clearTimeout(shuffleTmo);
				clearTimeout(owlTmo);
				shuffleBox.find('span').css({opacity:0,display:'block'}).animate({opacity:1},300,function() {
					shuffleDefault = shuffleDefault<=3?shuffleDefault+1:1;
					shuffleBox.css('background-image','url('+shufflerImg[shuffleDefault].src+')');
					$(this).hide();
				});
			}
		);
		/* Input autocomplete off */
		$('input').attr('autocomplete','off');			
		/* Contact form hash-image */
		$('.hash').unbind().click(function() { $('#hash').attr('src','e_hash.php?r='+Math.ceil(Math.random()*1000)); return false; });
	})();
})(this,document);
