var CAROUSEL_SPEED = 6000;
var CAROUSEL_TRANSITION_SPEED = 600;
var PAGE_FADE_OUT_SPEED = 400;
var PAGE_FADE_IN_SPEED = 1000;
var MEDIA_FADE_SPEED = 600;
var media_choice; //populated in profile.html - "S" = slideshow, "V" = Video
var slideshow_timeout;
var vid_file_url;
var initialised = false;

$(document).ready(function() {
	
/*	//Rollover colours for lower menu
	$('#bottommenu div.items a').livequery('mouseover', function() {
		$(this).animate({
			'color': 'white'
		}, 340);
	});
	
	$('#bottommenu div.items a:not(.selected)').livequery('mouseout', function() {
		$(this).animate({
			'color': '#999999'
		}, 340).animate({
			'color': '#6D6E71'
		}, 1000);
	});
	
	$('#bottommenu div.items a:not(.selected)').livequery('click', function() {
		$(this).animate({
			'color': '#93883B'
		}, 150);
	});
	
	$('#bottommenu div.items a.selected').livequery('mouseout' , function(){
		$(this).animate({
			'color': '#93883B'
		}, 2000);
	});

*/		
	// INFOTAB SETUP

	$("#infotab").livequery('click', function() {
		var info = $("#infopane");
		if (info.css("bottom")!="0px") {
			info.data("showing", true);
			info.stop(true, false);
			info.animate({ bottom: 0 }, 440, "easeInOutQuart");
			stop_media();
			$(".carousel-control").css("visibility", "hidden");
		} else {
			hide_info_pane();
		}
	});
	$("#infopane a.closebutton").livequery('click', hide_info_pane);
	$("#infopane a.vidicon").livequery('click' ,function() {
		change_to_video(MEDIA_FADE_SPEED, true);
		hide_info_pane();
	});
	$("#infopane a.imgicon").livequery('click', function() {
		change_to_slideshow(MEDIA_FADE_SPEED, true);
		hide_info_pane();
	});
	$("#infopane a.shareicon").livequery('click', function() {
		switch_to_share();
	})
	$("#infopane a.tellafriend").livequery(function() {
		$(this).attr("href", "#");
		$(this).click(switch_to_tellafriend);
	})
	
	
	$('.carousel-control').livequery('click', function() {
		pauseSlideshow();
		var vid_player = document.getElementById('vid_player');
		if (vid_player && vid_player.sendEvent) { //If there is a video
			vid_player.sendEvent("PLAY", false); // Stop it
			change_to_slideshow(0, true);
		}		
	});
	
	//Pause the slideshow on mouseover
	$("#carousel_container").livequery("mouseover", pauseSlideshow);
	$("#carousel_container").livequery("mouseout", startSlideshow);
	$(".carousel-control").livequery("mouseover", pauseSlideshow);
	$(".carousel-control").livequery("mouseout", startSlideshow);

	$(".bottommenu .scrollable.vertical").livequery(function() {
		$(this).scrollable({
			vertical: true,
			size: 4,
			next: 'a.nexta', 
	    	prev: 'a.preva' 
		});
	});

	//Set flags once each image has loaded
	$("#carousel_container").livequery("load", function() {
		$(this).data("loaded", true);
	});

});

var setup_carousel = function() {
	//The carousel won't initialise if its destination element is hidden.
	//console.log("setup carousel");
	$('div#carousel_container').jCarouselLite({
		btnNext:'.next',
		btnPrev:'.previous',
		circular:true,
		visible:1,
		speed:CAROUSEL_TRANSITION_SPEED,
		easing:"easeInOutQuart",
		width: 600,
		height: 337
	});
}

var setup_vidplayer = function() {
	var vid_container = document.getElementById('vid_container');
	if (vid_container) {
		var so_vid = new SWFObject('/media/swf/mediaplayer/player.swf', 'vid_player', '600', '337', '9');
		so_vid.addParam('allowfullscreen', 'true');
		so_vid.addParam('allowscriptaccess', 'always');
		so_vid.addParam("wmode", "transparent");
		so_vid.addVariable("autostart","true");
		so_vid.addVariable("skin", "/media/swf/mediaplayer/modieus/modieus.swf");
		so_vid.addVariable("file", vid_file_url); //set in template;
		so_vid.addVariable("controlbar", "over");
		so_vid.write('vid_container');
	}	
}

var setup_vidplayer_landing = function() {
	var vid_container = document.getElementById('vid_container');
	if (vid_container) {
		var so_vid = new SWFObject('/media/swf/mediaplayer/player.swf', 'vid_player', '600', '337', '9');
		so_vid.addParam('allowfullscreen', 'true');
		so_vid.addParam('allowscriptaccess', 'always');
		so_vid.addParam("wmode", "transparent");
		so.addVariable('lightcolor','737e00');
		so_vid.addVariable("autostart","true");
		so_vid.addVariable("skin", "/media/swf/mediaplayer/modieus/modieus.swf");
		so_vid.addVariable("file", vid_file_url); //set in template;
		so_vid.addVariable("controlbar", "none");
		so_vid.write('vid_container');
	}	
}

var init_page = function(){


	if (!initialised) {
		initialised = true;
	
		hash = document.location.hash; // eg. "#/contact/"
		if (!hash || hash === "#") {		
			//Main content is initially hidden
			
			setup_vidplayer();
			setup_carousel();
			display_media();
			start_media();
			$("#main_content").fadeIn(PAGE_FADE_IN_SPEED, start_media);
			
		}
	}
}

var init_page_landing = function(){	
	if (!initialised) {
		initialised = true;
	
		hash = document.location.hash; // eg. "#/contact/"
		if (!hash || hash === "#") {		
			//Main content is initially hidden
			
			setup_vidplayer_landing();
			setup_carousel();
			display_media();
			start_media();
			$("#main_content").fadeIn(PAGE_FADE_IN_SPEED, start_media);
		}
	}
}


/********************************************/
// Overrides for default ajaxify behaviour
/********************************************/
jQuery.fn.ajaxify_contents.initial_pre_replace = function(incoming_data){
	$("#main_content").hide();
	jQuery.fn.ajaxify_contents.replace(incoming_data, true);
}

var text_hash = function(txt) {
	//remove all whitespace and return the result
	return (txt).replace(/\s|\n|\r	$/g,'');
}

var equal_text = function(outgoing_element, incoming_element) {
	//hacky hash to detect whether the text content is equal
	//(the DOMs are nominally equal, but maniuplated by javascript). 
	//this will work in most cases, but it's easy to think of cases that will break it.
	var outgoing_hash = text_hash(outgoing_element.text());
	var incoming_hash = text_hash(incoming_element.text());
	var result = outgoing_hash == incoming_hash;
	return result;
}

var fade_replace = function(outgoing_element, incoming_element) {
	incoming_element.hide();
	outgoing_element.fadeOut(PAGE_FADE_OUT_SPEED, function(){
		outgoing_element.replaceWith(incoming_element);
		incoming_element.fadeIn(PAGE_FADE_IN_SPEED, start_media);	
	});
}

var slideshow_fade_replace = function(outgoing_element, incoming_element) {
	incoming_element.hide();
	outgoing_element.fadeOut(PAGE_FADE_OUT_SPEED, function(){
		outgoing_element.replaceWith(incoming_element);
		setup_vidplayer();
		setup_carousel();
		display_media();
		incoming_element.fadeIn(PAGE_FADE_IN_SPEED, start_media);	
	});
}

jQuery.fn.ajaxify_contents.replace_element = function(incoming_element, incoming_element_id, initial){
	var outgoing_element = $("#" + incoming_element_id); //only replace if the incoming content is different
	switch (incoming_element_id) {
		case "main_content": //main content gets faded in and out
			if (initial || !equal_text(outgoing_element, incoming_element)) slideshow_fade_replace(outgoing_element, incoming_element);
			break;
		case "categorymenu":
		case "clientmenu":
		case "bottommenu":
		    if (initial || !equal_text(outgoing_element, incoming_element)) fade_replace(outgoing_element, incoming_element);
		    break;
		case "projectmenu":
			if (initial || !equal_text(outgoing_element, incoming_element)) fade_replace(outgoing_element, incoming_element);
			break;			
		default: //everything else just appears
			outgoing_element.replaceWith(incoming_element);
	}
}

/********************************************/
// End Overrides
/********************************************/



/******** INFOPANE STUFF *************/

function hide_info_pane() {
	var info = $("#infopane");
	info.data("showing", false);
	info.stop(true, false);
	$("#infocontent").show();
	$("#sharecontent").hide();			
	$("#tellafriendcontent").hide();
	info.animate({ bottom: "-"+info.outerHeight()+"px" }, 660, "easeInOutQuart", function() {
	});
	start_media();
	$(".carousel-control").css("visibility", "");

}

/************ video player + carousel stuff ***********/

function display_media(){
	// if there's no video at the start then we need to start the slideshow manually
	if (media_choice == 'V') {
		change_to_video(0, false);
	} else {
		change_to_slideshow(0, false);
	}
}


function start_media(){
	// if there's no video at the start then we need to start the slideshow manually
	if (media_choice == 'V') {
		startVideo();
	} else {
		startSlideshow();
	}
}


function stop_media(){
	// if there's no video at the start then we need to start the slideshow manually
	if (media_choice == 'V') {
		pauseVideo();
	} else {
		pauseSlideshow();
	}
}

//When the video has finished, move to the slideshow (if there is one)
function stateMonitor(obj){
	if(obj.newstate == 'COMPLETED'){
		if ($("#carousel_container").length!=0) {
			change_to_slideshow(200, true);
		}
	}
};

function add_vid_listeners() {
	var vid_player = document.getElementById('vid_player');
	if (vid_player && vid_player.addModelListener) { 
		vid_player.addModelListener('STATE', 'stateMonitor'); 
	} else { 
		setTimeout(add_vid_listeners,100); 
	}
}

function playerReady(obj) { // called when the player is loaded
	add_vid_listeners();
};

// Call change_to_slideshow and change_to_video to switch to slideshow and video respectively, then start_media.
function change_to_slideshow(speed, play) {
	
	media_choice = "S";
	//console.log("in change_to_slideshow");
	
	var vid_player = document.getElementById('vid_player');
	//Stop the video
	if (vid_player && vid_player.sendEvent) {
		vid_player.sendEvent("PLAY", false); //stop the video
	}
		
	//fade out video, start the slideshow when done if applicable
	if (play) {
		$('#vid_container').fadeOut(speed, start_media);		
	} else {
		$('#vid_container').fadeOut(speed);		
	}

	$('#infopane ul .imgicon').addClass('selected');
	$('#infopane ul .vidicon').removeClass('selected');
}

function change_to_video(speed, play) {
	media_choice = "V";
	//Stop the slideshop and hide it.
	//console.log("in change_to_video");
	pauseSlideshow();
	
	//Show the video, play if applicable
	if (play) {
		$('#vid_container').fadeIn(speed, start_media);
	} else {		
		$('#vid_container').fadeIn(speed);
	}

	$('#infopane ul .imgicon').removeClass('selected');
	$('#infopane ul .vidicon').addClass('selected');
}

function startSlideshow(){
	//console.log("start slideshow");
	clearTimeout(slideshow_timeout);
	slideshow_timeout = setTimeout(runSlideshow, CAROUSEL_SPEED);
}

function runSlideshow(){
	$('#hidden_next_button').click();
	clearTimeout(slideshow_timeout);
	slideshow_timeout = setTimeout(runSlideshow, CAROUSEL_SPEED);
}

function pauseSlideshow(){
	//console.log("pause slideshow");
	clearTimeout(slideshow_timeout);
}

function startVideo() {
	var vid_player = document.getElementById('vid_player');
	if (vid_player) {
		if (vid_player.sendEvent) {
			vid_player.sendEvent("PLAY", true);
		} else {
			setTimeout(startVideo, 100);
		}
	}
}

function pauseVideo() {
	var vid_player = document.getElementById('vid_player');
	if (vid_player) {
		if (vid_player.sendEvent) {
			vid_player.sendEvent("PLAY", false);
		} else {
			setTimeout(pauseVideo, 100);
		}
	}
}

/* end videoplayer stuff */

/* "share" nav */

function switch_to_share() {
	$("#infocontent").hide();
	$("#sharecontent").show();
}

function switch_to_tellafriend() {
	$("#sharecontent").hide();
	$("#tellafriendcontent").show();
}
/* end nav share */

//Last
var BULB_JS_LOADED = true;