/**
 * @author [Cassette]
 * @copyright [2011]
 * Software on this site is copyright 2011
 * Cassette Pty Ltd.
 * All Rights Reserved. Copying, Editing or
 * Distributing this software is strictly
 * forbidden without permission from
 * Cassette Pty Ltd.
 * for more information please contact
 * Cassette www.cassette.co
 */
//
// @author [Cassette]
// @copyright [2011]
// Software on this site is copyright 2011
// Cassette Pty Ltd.
// All Rights Reserved. Copying, Editing or
// Distributing this software is strictly
// forbidden without permission from
// Cassette Pty Ltd.
// for more information please contact
// Cassette www.cassette.cc 
//


var next_slider_timer = null;
window.is_animating = false;
window.is_resizing = false;
window.page_state = "home";
window.home_animation = false;
window.old_hash = "";
window.qued_hashes = [];
window.home_static_height = 0;

$(document).ready(function(){
	
	//configure lazy loader
	setup_lazy_load();
	
	home_get_static_height();
	
	// history monitor
	$(window).bind("hashchange", update_page_state);

	//	setup_network_wheel();
	setup_img_rollers();

	// home
	set_home_images_height();
	setup_home_slider();
	set_home_images_width();
	
	//both
	watch_resize();
	
	//show_slide(startup_slide, true);
	window.old_hash = "startingpoint";
	update_page_state();
	
	setup_other_page_items();
	
}); 


function setup_img_rollers() {
	$("img.roll, input[type=image].roll").each(function() {$(this).hover(function() {img_roll_on(this);}, function() {img_roll_off(this);});});
}
function img_roll_on(img) {
	var src = img.src.replace(".jpg","_roll.jpg");
	src = src.replace(".png","_roll.png");
	src = src.replace("_on_roll","_roll");
	src = src.replace("_roll_roll","_roll");
	img.src = src;
}
function img_roll_off(img) {
	var src = img.src.replace("_roll.jpg",".jpg");
	src = src.replace("_roll.png",".png");
	img.src = src;
}

function watch_resize() {
	
	var timer = null;
	var timer2 = null;
	
	$(window).resize(function() {
		window.is_resizing = true;
		
		if(window.page_state == "home") {
			set_home_images_height();
			set_home_images_width();
		} else {
			set_content_slide_height();
			set_content_slide_width();
		}
		
		if(timer2 != null) clearTimeout(timer2);
		timer2 = setTimeout(function() {
			window.is_resizing = false;
		},1000);
		
	});
	
}

function set_content_slide_width() {
	var w = $(window).width();
	if (w > 959) {
		$('.scrollable .items').css({left: 0});
		$('.scrollable .items > div').css({width: w + "px"});
	} else {
		$('.scrollable .items > div').css({width: "960px"});
	}
}
function set_content_slide_height() {	
	// Scrollable settings
	$(".scrollable").height($(".slide-active").outerHeight());
	$(".scrollable .items").height($(".slide-active").outerHeight());
}










function setup_home_slider() {
	$("HTML").addClass("JS");
	var i = $(window).width();
	if (i > 959){$('#wrapper-home .items > div').css({width: i});}

	// Scrollable settings
	$("#wrapper-home #content").css({visibility: "visible"});
	$("#wrapper-home #content").scrollable({easing: 'easeInOutCirc', speed: 600, circular: true, onSeek: function() {window.home_animation = false;}});

	// Window resize code
	window.api = $("#wrapper-home #content").data("scrollable");

	set_home_next_slide();
}

function set_home_images_height() {
	
	var max_height = 485;
	var min_height = 250;

	var el_height = window.home_static_height;

	var win_height = $(window).height();
	var new_height = win_height - el_height;
	new_height = parseFloat((new_height > max_height ? max_height : (new_height < min_height ? min_height : new_height))).toFixed(0);
	$("#wrapper-home #content").height(new_height + "px");
}

function set_home_images_width() {
	var a = 1 + $("#wrapper-home #content").data("scrollable").getIndex(); // If autoscroll is disabled, delete: 1 + .
	var w = $(window).width();
	if (w > 959) {
		var l = a * w
		$('#wrapper-home .items').css({left: + - +l});
		$('#wrapper-home .items > div').css({width: w + "px"});
	} else {
		$('#wrapper-home .items > div').css({width: "960px"});
	}
}
function set_home_next_slide() {
	next_slider_timer = setTimeout(function() {
		if(window.page_state == "home") {
			if(!window.is_resizing) {
				var win_width = $(window).width();
				var conf = window.api.getConf();
				if(win_width >= 1200) conf.speed = 900;
				if(win_width >= 1100 && win_width < 1200) conf.speed = 800;
				if(win_width >= 1000 && win_width < 1100) conf.speed = 700;
				if(win_width < 1000) conf.speed = 600;

				window.home_animation = true;
				$("#wrapper-home #content").data("scrollable").next();
			}
		}
		set_home_next_slide();
	},5000);
}






function update_page_state() {
	var hash = location.hash || "";
	if(hash.indexOf("#")!==false) hash = hash.substr(hash.indexOf("#")+1);
	if(hash != "") {
		if(hash.substr(0,1) != "!") {
			window.location.replace("#!" + hash);
			return;
		}
		hash = hash.substr(hash.indexOf("!")+1);
	}
	
	
	window.qued_hashes.push(hash);
	if(!window.is_animating) {
		switch_state(hash, switch_complete);
	}
}

function switch_complete() {
	setTimeout(function() {
		// switch complete

		// remove the item that was just processed from the que
		window.qued_hashes.shift();
		
		update_page_title();

		window.is_animating = false;
		if(window.qued_hashes.length > 0) {
			// only do the last one
			
			for(var i=0;i<window.qued_hashes.length-2;i++) { window.qued_hashes.shift(); }
			for(var i=0;i<window.qued_hashes.length-2;i++) { window.qued_hashes.shift(); }
			
			var hash = window.qued_hashes[0];
			switch_state(hash, switch_complete);
		} else {
			// do nothing
		}
		
	},50);
}

function switch_state(hash, callback) {
	var old_hash = window.old_hash || "";
	

	if(old_hash == hash) {
		callback();
		return;
	}

	// this is where we display our slides
	if(old_hash == "startingpoint" && (hash != "" && hash != "home")) {
		// show the screen without a transition from home to content
		window.is_animating = true;
		show_content(hash, callback);
	} else if(old_hash == "startingpoint" && (hash == "" || hash == "home")) {
		// do nothing
		window.is_animating = true;
		setTimeout(function() {
			callback();
		},0);
		callback();
	} else if(old_hash == "" || old_hash == "home" || old_hash == "startingpoint") {
		if(hash != "" && hash != "home") {
			// Transition to content slides.
			window.is_animating = true;
			transition_to_content(hash, callback);
		}
	} else if(old_hash != "" && (hash == "" || hash == "home")) {
		window.is_animating = true;
		transition_to_home(hash, callback);
	} else if((old_hash == "about-us" || old_hash == "community-involvement" || old_hash == "our-network") &&
						(hash == "about-us" || hash == "community-involvement" || hash == "our-network")) {
						
		window.is_animating = true;
		//transition_fade(hash, callback);
		transition_no_animation(hash, callback);

	} else if((old_hash == "team" || old_hash == "managers" || old_hash == "senior-staff") &&
						(hash == "team" || hash == "managers" || hash == "senior-staff")) {
		window.is_animating = true;
		//transition_fade(hash, callback);
		transition_no_animation(hash, callback);
	} else {
		window.is_animating = true;
		transition_slide(hash, callback);
	}

	update_page_properties(hash);

	var gapathname = window.location.pathname, gasearch = window.location.search, gahash = escape("#" + hash);
	try {
	var pageTracker = _gat._getTracker("UA-19653583-20");
	pageTracker._trackPageview(gapathname + gasearch + gahash);
	} catch(err) {}

	window.old_hash = hash;
}

function transition_fade(slide, callback) {

	var slide_width = $(window).width();
	slide_width = (slide_width < 960 ? 960 : slide_width);
	
	var class_slide = "sld-" + slide;
	var current_height = $(".slide-active").height();

	$(".scrollable .items").append($("." + class_slide));
	var slide_height = $("." + class_slide).height();

	$(".scrollable .items").fadeOut(200,function() {
		$("#hidden-slides").append($(".scrollable .items > div:not(." + class_slide + ")"));
		

		$("." + class_slide).width(slide_width);
		$(".slide-active").removeClass("slide-active");
		$("." + class_slide).addClass("slide-active");
		
		$(".scrollable .items").fadeIn(200, function() {
			callback();
		});
		
	});


	if(current_height != slide_height) {$("#wrapper-content #footer").animate({opacity: 0}, 20, "swing");}
	$(".scrollable, .scrollable .items").animate({height: slide_height + "px"}, 200, "swing", function() {
		$("#wrapper-content #footer").animate({opacity: 1}, 800, "swing");
	});
}

function transition_slide(slide, callback) {
	show_slide(slide,false, callback);
}

function transition_to_content(slide, callback) {
	
		window.page_state = "content";

		show_slide(slide, true);

		var h = $(window).height();
		$("#wrapper-home").css({height: h + "px"});
		$("#wrapper-content").css({"min-height": h + "px"});
		$("#wrapper-content").show();

		set_content_slide_height();
		set_content_slide_width();
		$("#wrapper-home").animate({marginTop: -h + "px"},1200, "easeInOutQuint", function() {
			$("#wrapper-home").hide();
			$("#wrapper-content").css({"min-height": "100%"});
			callback();
		});

}

function show_content(slide, callback) {

		window.page_state = "content";

		show_slide(slide, true);

		var h = $(window).height();
		$("#wrapper-home").css({height: h + "px"});
		//$("#wrapper-content").css({"min-height": h + "px"});
		$("#wrapper-content").css({"min-height": "100%"});
		$("#wrapper-content").show();

		set_content_slide_height();
		set_content_slide_width();

		$("#wrapper-home").hide();
		callback();
		
}

function transition_to_home(hash, callback) {

		window.page_state = "home";

		var h = $(window).height();
		$("#wrapper-home #footer").hide();
		$("#wrapper-home").css({height: h + "px", marginTop: -h + "px"});
		$("#wrapper-home").show();

		set_home_images_height();
		set_home_images_width();

		//easeInQuart
		$("#wrapper-home").animate({marginTop: "0px"},1200, "easeInOutQuint", function() {
			$("#wrapper-home").css({height: "auto", "min-height": "100%"});
			$("#wrapper-home #footer").fadeIn("slow");

			// remove the content slides from the scrollable window
			$("#hidden-slides").append($("#wrapper-content .scrollable .items > div"));
			$("#wrapper-content").hide();

			set_home_images_height();
			set_home_images_width();

			callback();
		});

}

function transition_no_animation(slide, callback) {
	var slide_width = $(window).width();
	slide_width = (slide_width < 960 ? 960 : slide_width);
	
	var class_slide = "sld-" + slide;

	$(".slide-active").removeClass("slide-active");
	$("." + class_slide).width(slide_width);
	$("." + class_slide).addClass("slide-active");

	var slide_height = 0;
	$(".scrollable .items").append($("." + class_slide));
	slide_height = $(".sld-" + slide).height();
	$(".scrollable, .scrollable .items").css({height: slide_height + "px"});
	
	$("#hidden-slides").append($(".scrollable .items > div:not(." + class_slide + ")"));
	
	$("#wrapper-content").css({"min-height": "100%"});

	
	set_content_slide_width();
	if(callback) callback();
}


function transition_drop_down2(slide, callback) {
	var slide_width = $(window).width();
	slide_width = (slide_width < 960 ? 960 : slide_width);
	
	var class_slide = "sld-" + slide;

	$(".slide-active").removeClass("slide-active");
	$("." + class_slide).width(slide_width);
	$("." + class_slide).addClass("slide-active");

	var slide_height = 0;
	$(".scrollable .items").append($("." + class_slide));
	slide_height = $(".sld-" + slide).height();
	$("." + class_slide).hide();

	$("#wrapper-content #footer").stop().animate({"opacity": 0}, 20, "swing");

	setTimeout(function() {
		$(".scrollable").animate({height: slide_height + "px"}, 300, "swing", function() {
			$("#wrapper-content").css({"min-height": "100%"});
		});
	}, 200);

	$(".scrollable .items").animate({height: "0px"}, 400, "easeInOutCirc", function() {
		$("#hidden-slides").append($(".scrollable .items > div:not(." + class_slide + ")"));
		$("." + class_slide).show();
		$(".scrollable .items").animate({height: slide_height + "px"}, 400, "easeInOutCirc", function() {
			set_content_slide_width();
			if(callback) callback();
			$("#wrapper-content #footer").stop().animate({"opacity": 1}, 800, "swing");
		});
	});
}

function show_slide(slide, startup, callback) {
	var slide_width = $(window).width();
	slide_width = (slide_width < 960 ? 960 : slide_width);
	
	var class_slide = "sld-" + slide;
	
	if(startup) {
		$("a[href*=#!" + slide + "]").addClass("active");
		$(".slide-active").removeClass("slide-active");
		$(".sld-" + slide).addClass("slide-active");
		$(".sld-" + slide).width(slide_width);
		$(".scrollable .items").append($("." + class_slide));
		
		if(callback) callback();
	} else {
		
		// work out the direction to slide.
		// based on where the menu item lies on the site.
		var nav = "homeservicesabout-usour-networkcommunity-involvementteammanagerssenior-staffresourcescontact-us";
		var old_pos = nav.indexOf(window.old_hash) || 0;
		var new_pos = nav.indexOf(slide) || 0;
		var direction = (new_pos > old_pos ? "slide_from_right" : "slide_from_left");
		
		$(".slide-active").removeClass("slide-active");
		$("." + class_slide).width(slide_width);
		$("." + class_slide).addClass("slide-active");
		
		var slide_height = 0;
		if(direction == "slide_from_right") {
			$(".scrollable .items").append($("." + class_slide));
		} else {
			$(".scrollable .items").css({left: "-" + slide_width + "px"});
			$(".scrollable .items").prepend($("." + class_slide));
			
		}
		slide_height = $(".sld-" + slide).height();

		$("#wrapper-content #footer").stop().animate({"opacity": 0}, 20, "swing");

		setTimeout(function() {
			$(".scrollable").animate({height: slide_height + "px"}, 300, "swing", function() {
				$("#wrapper-content").css({"min-height": "100%"});
			});
		}, 200);

		$(".scrollable .items").animate({left: (direction == "slide_from_left" ? "+" : "-") + "=" + slide_width, height: slide_height + "px"}, 800, "easeInOutCirc", function() {
			var left = parseInt($(".scrollable .items").css("left"),10);
			$("#hidden-slides").append($(".scrollable .items > div:not(." + class_slide + ")"));
			$(".scrollable .items").css({left: (left + slide_width) + "px"});
			$("#wrapper-content #footer").stop().animate({"opacity": 1}, 800, "swing");

			set_content_slide_width();

			if(callback) callback();
		});
	}
}

function update_page_properties(slide) {
	$(".active").removeClass("active");
	$("a[href*=#!" + slide + "]").addClass("active");

	
	// set up our transition-2 links if needed.
	// about us and team both have fade transitions within them.
	if(slide == "about-us" || slide == "our-network" || slide == "community-involvement") {
		$("#about-sub-menu").parents("li:first").removeClass("center").addClass("height-50");
		$("#about-sub-menu").show();
	} else {
		$("#about-sub-menu").parents("li:first").addClass("center").removeClass("height-50");
		$("#about-sub-menu").hide();
	}

	// set up our transition-2 links if needed.
	// about us and team both have fade transitions within them.
	if(slide == "team" || slide == "managers" || slide == "senior-staff") {
		$("#team-sub-menu").parents("li:first").removeClass("center").addClass("height-50");
		$("#team-sub-menu").show();
	} else {
		$("#team-sub-menu").parents("li:first").addClass("center").removeClass("height-50");
		$("#team-sub-menu").hide();
	}

	$(".in-page-nav a img").trigger("mouseout");
	$(".in-page-nav a img").trigger("mouseleave");
}

function update_page_title() {
	if(window.page_state=="content") { 
		document.title = "BSA Partnership - " + $(".slide-active h1").text() + " | Chartered Accountants | Melbourne CBD"; 
	} else { 
		document.title = "BSA Partnership | Chartered Accountants | Melbourne CBD"; 
	}
}


function home_get_static_height() {
	var el_height = 0;
	$(".c").each(function() {
		var el = $(this);
		el_height += parseInt(el.css("padding-bottom"), 10);
		el_height += parseInt(el.css("padding-top"), 10);
		el_height += parseInt(el.css("margin-bottom"), 10);
		el_height += parseInt(el.css("margin-top"), 10);
		el_height += el.height();
	});
	el_height += parseInt($("#wrapper-home #inner-wrapper2").css("padding-bottom"), 10);
	el_height += 50;
	window.home_static_height = el_height;
}

function setup_lazy_load() {
	
	$("#spotlight img").load(function() {
		$(this).fadeIn(2000);
	})
	
}


function setup_other_page_items() {
	  $(".hover-transparent").hover(function() { $(this).css({opacity: 0.5}); }, function() { $(this).css({opacity: 1}); })
}
