/*
Vanilla Films - Javascript
Created by INVOLVED. - involved.com.au (15 August 2010)
email@involved.com.au
*/

var VanillaFilms = function() {

  function init() {
		VanillaFilms.attachSlideToHandler();
		VanillaFilms.attachAccordion();
		VanillaFilms.attachCharacterUpdater();
		VanillaFilms.attachTwitterFeed();
		VanillaFilms.attachSlideShow();
		VanillaFilms.attachVideoManager();
		VanillaFilms.attachVideoListeners();
		VanillaFilms.attachAddressChange();
		VanillaFilms.attachDebugger();
	}

	function attachDebugger() {
		var debug = false;
		if (debug) {
			$("#console").show();
		}
	}

	function log(message) {	
		$("#console").append("<li>" + message + "</li>");	
	}

	function attachSlideToHandler() {
		$(".slide_to").click(function() {
			log("slideTo => " + $(this).attr('rel'));
			slideTo($(this).attr('rel'));

			if(document.location.hash!='') {
				urlhash = document.location.hash.substr(1,document.location.hash.length);	
				urlhash = urlhash.replace(/^\//, '#/');
			} else {
				urlhash = "";
			}

			if ($(this).attr('href') == urlhash){
				log("ALREADY OPEN");				
				return false;
			}	
	    });
	}
	
	function slideTo(destination) {
		// IF IE
		if ($.browser.msie) {
			//no sliding if IE
		} else {	
			var destination_offset = "";
		
			if (destination == "#testimonials"){
				destination_offset = -100;
			} else {
				 destination_offset = -20;
			}
		
			setTimeout(function(){ 
				$(window).stop().scrollTo(destination, {
					duration: 500, 
					offset: {left: 0, top:destination_offset }
				});
			}, 1000 );
		}
	}
	
	function attachAddressChange() {
		
		$.address.init(function(event) {
			log("Address Change Init");	
		});
		
		$.address.externalChange(function(event) {
			log("External Change");
			if(document.location.hash!='') {
				urlhash = document.location.hash.substr(1,document.location.hash.length);	
				urlhash = urlhash.replace(/^\//, '');		

				if (urlhash == "testimonials"){
					 var link_to_open = ("li#" + urlhash + " a#get.header");
				} else if (urlhash == "vanilla") {
					var link_to_open = ("li#" + urlhash + " a.header");
					log("Page: vanilla");
					loadVideo("#showreel", "#showreel_stage");
				} else if (urlhash == "stories") {
					var link_to_open = ("li#" + urlhash + " a.header");
					log("Page: stories");
					loadVideo("#video_id1", "#video_stage");
				} else {
					log("Page: " + urlhash);
					var link_to_open = ("li#" + urlhash + " a.header");
				}
				
				$("#accordion > li").removeClass("active");
				$("#accordion > li .container").hide();
				
				window.setTimeout(function(){ 
					$("#" + urlhash).addClass("active");
					$("#" + urlhash + " .container").show(); 
				}, 100);
				
				slideTo(("#" + urlhash));
				window.setTimeout(function(){updateTitle();}, 50);
			} 
			
		});
		
		$.address.internalChange(function(event) {
			log("Internal Change");
			window.setTimeout(function(){updateTitle();}, 50);
		});
	}
	
	function updateTitle() {
		var new_title = "";
		
		if(document.location.hash!='') {
			urlhash = document.location.hash.substr(1,document.location.hash.length);	
			urlhash = urlhash.replace(/^\//, '');
				
			log("Title => " + urlhash);
				
			switch (urlhash) {
			    case 'vanilla':
					new_title = " | Teaser";
				break;
				case 'stories':
					new_title = " | Stories"; 
				break;
				case 'people': 
					new_title = " | People";
				break;
				case 'testimonials': 
					new_title = " | Testimonials";
				break;

				default: new_title ="";
			 }

			$.address.title("Vanilla. Get Involved." + new_title);
		}
	}
	
	function attachAccordion() {
		$('ul#accordion').accordion();

		$(".header").click(function() {			
			if ($("#accordion > li").hasClass("active")){
				$.address.value($(this).attr('href').replace(/^#/, ''));
			} else {
				$.address.value("/home")
			}
			$.address.update();
			return false;
		});
	}
	
	function attachVideoManager() {
		$("a.header").click(function(){
			if ( $(this).attr("rel") == "#stories" && $("#stories").hasClass("active") == true ){
				unloadVideo("#showreel_stage");
				loadVideo("#video_id1", "#video_stage");
			} else if ( $(this).attr("rel") == "#vanilla" && $("#vanilla").hasClass("active") == true ){
				unloadVideo("#video_stage");
				loadVideo("#showreel", "#showreel_stage");
			} else {	
				unloadVideo("#showreel_stage");
				unloadVideo("#video_stage");
			}
		});
	}
	
	function loadVideo(videoID, stage) {
		// IF IE
		
		if ($.browser.msie) {
			log("IE load");
			$(stage).show();
		} else {
			log("non IE load");
			$(stage).fadeTo(800, 1);
		}

		log("videoID => " + videoID);

		$(stage).html($(videoID).html());

		attachVideoListeners();
		log("loaded => " + stage + " => " + videoID);
	}

	function unloadVideo(stage) {
		log("unloaded => " + stage);

		// IF IE
		if ($.browser.msie) {
			log("IE unload");
			$(stage).hide();
		} else {
			log("non IE unload");
			$(stage).fadeTo(300, 0);
		}
		window.setTimeout(function(){ $(stage).html(" "); }, 1000);
	}

	
	function attachVideoListeners() {
		$(".pagination").click(function(){
			unloadVideo("#video_stage");

			var video_to_load = $(this).attr('rel');
			log("Video To Load href => " + video_to_load);
			window.setTimeout(function(){ loadVideo(video_to_load, "#video_stage"); }, 1000);
			return false;
		});
	}
	
	function attachCharacterUpdater() {
		$("a.header").click(function(){			
			if($("#vanilla").hasClass("active")){
				$("#stories").addClass("change_i")
			} else {
				$("#stories").removeClass("change_i")
			}
		});
		
		$("li#testimonials a").hover(
			function () {
				$("li#testimonials a").addClass("hover");
			},
			function () {
				$("li#testimonials a").removeClass("hover");
			}
		);
	}
		
	function attachTwitterFeed() {
		$("#tweets").tweet({
			username: "vanillafilm",
			count: 1,
			loading_text: ""
		});	
	}
		
	function attachSlideShow() {
		$('.slideshow').cycle({
			fx: 'fade',
			speed: 700, 
			timeout: 4000
		});
	}

	return {
		init: init,
		attachDebugger:attachDebugger,
		attachSlideToHandler: attachSlideToHandler,
		attachAddressChange: attachAddressChange,
		attachAccordion: attachAccordion,
		attachCharacterUpdater: attachCharacterUpdater,
		attachTwitterFeed: attachTwitterFeed,
		attachSlideShow: attachSlideShow,
		attachVideoManager: attachVideoManager,
		attachVideoListeners: attachVideoListeners,
		loadVideo: loadVideo,
		unloadVideo: unloadVideo
	};

}();

$(document).ready(function(){
    VanillaFilms.init();
});
