/* Welcome to the JavaScript of clubsoul.co.uk.  Feel free to copy any code snippets you find here.
 * Portions borrowed/learnt/acquired from http://hawksworx.com/playground/polaroiderizer 
 *
 * No warranty implied or infered whatsoever blah blah. */

var images = ['half_peter-piano.jpg','half_peter-gig.jpg','half_band-butl.jpg','half_kas-mich.jpg','half_kas-dance.jpg'];
displayQueue = [];

/* JQuery personal extensions */
$.fn.polaroidScroll = function() {			
	// set starting point
	var x = 600;
	var y = 210;
	this.css({top: y+'px', left: x+'px'});

	// set opacity of photo
	var picture = this.find('img');
	picture.css({opacity: '0'});

	// animate photo opacity and into view.
	this.animate({left:'530px'}, 400);
	picture.animate({opacity: '1'}, 3000, function(picture){
		// animate slowly out of view and opacity of entire object.
		$(this).parents('div.polaroid').animate({left:'600px', opacity:'0'}, 2000, function(){
			$(this).remove();
		});	
	});
	
	return this;				
};

function displayNext(){
	var i = displayQueue[qPos];
	if(i) {
		var p = $('<div class="polaroid"></div>').append($(i));
		p.appendTo('#display').polaroidScroll();
	} 
	qPos++;
	if(qPos >= displayQueue.length) {
		qPos = 0;
	}
	timer = setTimeout(function() { displayNext(); }, 5000);				
}

/* Custom function fu */
function prepImages(){
	for(i=0; i < images.length; i++){
		$('<img src="images/photos/'+ images[i] +'" alt="band photo" />').load(function(){
			var a = $('<a href="#" target="_BLANK"></a>').append($(this).clone());
			displayQueue.push(a);
		}).appendTo($('#staging'));
	}
	displayNext();
}

function processPageAnimations(){
	/* Unobtrusive opacity filters */
	/*$('div#logo-container').css({opacity: 0.5});*/
	$('img.slide-img ').css({opacity: 0});

	/*Index page animation*/
    $("div#logo-container").css({opacity:0.25, display: 'block'}).animate({left:100}, 700, 'easeOutBounce').animate({opacity:1.0}, 700, 'easeOutBounce');
	$('div > img#band-placeholder').animate({opacity:1}, 4000, 'linear').animate({opacity:0}, 1000, 'linear');
	
	/* Header & menu animation */
	$("div#titlebar-a").animate({opacity:0.25}, 1000, 'easeOutBounce').animate({opacity:1.0}, 700, 'easeOutBounce');
	$("div#titlebar-b").animate({opacity:1.0}, 700).animate({opacity:0.25}, 1000, 'easeOutBounce').animate({opacity:1.0}, 700, 'easeOutBounce');
	$("a:not(#head-home, #logo-anchor) img").hover(function(){$(this).fadeOut("fast").fadeIn("fast");});
	
	/* Setup images for home photo scroller */
	if($("div.info > div#display").length > 0){
		qPos = 0;
		prepImages();
	}
}

function processPhotoGallery(){
	/* Photo gallery animation */
	$('div#thumbs > a').click(function(event){
		var newImg = $('<div class="slide-img"><img class="site-polaroid" src="'+ $(this).attr('href') +'" /></div>').css({display: 'none'});
		event.preventDefault();
		$('div#photopanel').children().fadeOut(300).slideUp(300,function(){
			$(this).remove();
			newImg.appendTo('div#photopanel').fadeIn('slow');
		});
	});	
}

function showResponse(responseText, statusText) {
	$('#contact-form').slideToggle('slow', function(){
		$('<div id="sent" class="mail success">'+ responseText +'</div>').css({opacity: '0'}).appendTo('#enquiry-container')
		.animate({opacity:'1'}, 1000).animate({opacity:'1'}, 5000).animate({opacity:'0'}, 1000, function(){
			$(this).remove();
			$('#contact-form').slideToggle('slow');
		});
	});
}

/* handle validation and submit form if OK */
function processFormsSetup(){
    var options = { 
        success: showResponse, 
        clearForm: true, 
        resetForm: true, 
        timeout:   10000
    };
	if($('#contact-form').length > 0){
		$("#contact-form").validate({
			submitHandler: function(form) {
				$("#contact-form").ajaxSubmit(options);
				return false;
			}
		});
	}
}

/* Unobtrusive injection of flash movie player */
function processFlashMovies(){
if($('#audio_be-thankful').length > 0){
	$('#audio_be-thankful').flash({
		swf: 'player.swf', height:24,width:290, wmode: 'transparent',flashvars: {playerID: '1',soundFile: 'audio/be-thankful.mp3'}});
	$('#audio_carwash').flash({
		swf: 'player.swf', height:24,width:290, wmode: 'transparent',flashvars: {playerID: '2',soundFile: 'audio/carwash.mp3'}});
	$('#audio_ladies-night').flash({
		swf: 'player.swf', height:24,width:290, wmode: 'transparent',flashvars: {playerID: '3',soundFile: 'audio/ladies-night.mp3'}});
	$('#audio_sweet-love').flash({
		swf: 'player.swf', height:24,width:290, wmode: 'transparent',flashvars: {playerID: '4',soundFile: 'audio/sweet-love.mp3'}});
	$('#audio_thinking-of-you').flash({
		swf: 'player.swf', height:24,width:290, wmode: 'transparent',flashvars: {playerID: '5',soundFile: 'audio/thinking-of-you.mp3'}});
	$('#audio_i-feel-good').flash({
		swf: 'player.swf', height:24,width:290, wmode: 'transparent',flashvars: {playerID: '6',soundFile: 'audio/i-feel-good.mp3'}});
	$('#audio_streetlife').flash({
		swf: 'player.swf', height:24,width:290, wmode: 'transparent',flashvars: {playerID: '7',soundFile: 'audio/streetlife.mp3'}});
	$('#audio_dont-look-any-further').flash({
		swf: 'player.swf', height:24,width:290, wmode: 'transparent',flashvars: {playerID: '8',soundFile: 'audio/dont-look-any-further.mp3'}});
	}
}

function ap_stopAll(){
	//bug in jquery swfobject plugin.  This method to prevent notfound error
}

$(document).ready(function(){
	processPageAnimations();
	processPhotoGallery();
	processFormsSetup();
	processFlashMovies();
});
