// slideshow vars

var slideInterval = 1;	
var slideHeight = 0;
var currentSlide = 0;
var slideControls;
var slideCount;
var slides;
var slideDuration = 7000;
var	slideHeight = 233;

// volume vars

var volumeMax = 10;
var volumeLevel = 0;
var dragStartX = 0;





$(document).ready(function(){
	
	
	
	
	
	
	
	
	// theme switcher
	
	$("li#themes a").click(function(e){
		e.preventDefault();
		className = $(this).attr('class');
		$("body").attr('class', className);
		createCookie("theme", className, 365);
	});
	
	
		
		
		
		
			
	// volume slider
	
	var volumeSlider = $("li#volume div.slider");
	var handle = volumeSlider.find('div');
	var handleWidth = handle.width();
	var sliderWidth = volumeSlider.width()
	
	volumeSlider.bind('dragstart', function(e){
		dragStartX = e.offsetX - e.cursorOffsetX;									
		$(this).addClass('active');		   
	});
	
	volumeSlider.bind('dragend', function(e){
		$(this).removeClass('active');		 
		createCookie("volume", volumeLevel, 365);
	});
		
	volumeSlider.bind('drag', function(e){
		var newX = e.offsetX - dragStartX - (handleWidth / 2);
		newX = Math.max(0, Math.min(newX, sliderWidth - handleWidth));
		handle.css({left: newX});
		volumeLevel = (newX / (sliderWidth - handleWidth)) * volumeMax;	
	});
	
		
	initialVolume = readCookie("volume");
	volumeLevel = initialVolume ? initialVolume : 0;
	handle.css({left: (volumeLevel / volumeMax) * (sliderWidth - handleWidth)});
		
		
		
		
		
		
		
		
		
		
	// promo slideshow
	
	slides = $('div#promo div#slideshow li');
	
	slideCount = $('div#promo div#slideshow li').length;
	
	slideControls = $('div#promo div#controls');
	
	slideControls.find(".play").click(function(){
		NextSlide();							 
		StartSlideshow();						
	});
	
	slideControls.find(".pause").click(function(){
		StopSlideshow();						
	});
	
	slideControls.find(".prev").click(function(){
		NextSlide(true);							 
		StopSlideshow();						
	});
	
	slideControls.find(".next").click(function(){
		NextSlide();							 
		StopSlideshow();						
	});
	
	StartSlideshow();
		
		
		
		
		
		
	
	
	
	
	// load flash movie
	
	var flashvars = {
		blipFile: "/assets/audio/blip.mp3",
		ditFile: "/assets/audio/dit.mp3"
	};
	
	swfobject.embedSWF("/assets/flash/player.swf", "swfholder", "100", "100", "9.0.0", null, flashvars);
	
	
	
	// sound-playing events
	
	$("div#nav ul a, div#footer ul a").hover(function(){
		$("#swfholder")[0].play(volumeLevel, true);  
	});
		
	$("a.button, input.submit").hover(function(){
		$("#swfholder")[0].play(volumeLevel, false);  
	});
		
		
		
		
		
	
});








// slideshow functions

function NextSlide(prev) {	
	prev ? currentSlide-- : currentSlide++;
	if (currentSlide < 0){
		currentSlide = slideCount - 1;
	}
	if (currentSlide > slideCount - 1) {
		currentSlide = 0;
	}
	
	
	$('div#promo div#slideshow ul').animate({top: (slideHeight * currentSlide * -1) + "px"}, 900);
	
	$('div#promo p span').fadeOut(200, function(){
		$(this).html(slides.eq(currentSlide).find('span').html());
		$(this).fadeIn(200);
	})
}

function StartSlideshow() {
	slideInterval = setInterval("NextSlide()", slideDuration);
	if (slideControls != null) {
		slideControls.removeClass('current');	
	}
}

function StopSlideshow() {
	window.clearInterval(slideInterval);
	if (slideControls != null) {
		slideControls.addClass('current');	
	}
}








// cookie functions

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

