jQuery(function($) {

	var current_subpage = '';

	var hide_subpages = function(section_class) {
		$("#white").hide();
		$(".pop").hide();
	};

	var display_subpage = function(section_class) {
		hide_subpages();
		$("#white").show();
		var $sec = $(".section-"+section_class);
		$sec.show();
		check_subcontent_arrows($sec);
		window.location.href = window.location.href.split('#')[0]+'#'+section_class;
		current_subpage = section_class;
	};

	var check_subcontent_arrows = function(el) {
		var $el = $(el);
		var $sc = $el.find(".sub_content");
		var $scc = $el.find(".sub_content_container");
		var sctop = parseInt($sc.css('top'));
		$el.find(".scroll_down").hide();
		$el.find(".scroll_up").hide();
		var left = $el.hasClass('pop') ? '52px' : '25px';
		var right = $el.hasClass('pop') ? '70px' : '43px';
		if($sc.height()+sctop > $scc.height() && sctop < 0) {
			$el.find(".scroll_down").css({
											'display'	:	'block',
											'left'		:	left
										});
			$el.find(".scroll_up").css({
											'display'	:	'block',
											'left'		:	right
										});
		}
		else if($sc.height()+sctop > $scc.height()) {
			$el.find(".scroll_down").css({
											'display'	:	'block',
											'left'		:	left
										});
		}
		else if(sctop < 0) {
			$el.find(".scroll_up").css({
											'display'	:	'block',
											'left'		:	left
										});
		}
		return false;
	}

	$("#white,.pop a.pop_close").click(function(){
		window.location.href = window.location.href.split('#')[0]+'#';
		hide_subpages();
		return false;
	});

	$("a.pop-link").click(function(){
		display_subpage($(this).attr('href').split('#')[1]);
	});

	$(".pop_next").click(function(e){
		var subpages = $("a.pop-link");
		var load_next = 0;
		subpages.each(function(i,e){
			var $e = $(e);
			if(load_next==1) {
				display_subpage($e.attr('href').split('#')[1]);
				load_next = -1;
			}
			else if (load_next==0 && $e.attr('href').split('#')[1]==current_subpage) {
				load_next = 1;
			}
		});
		if(load_next==1) {
			display_subpage($(subpages[0]).attr('href').split('#')[1]);
		}
		return false;
	});

	$(".scroll_down").click(function(e){
		var $t = $(this);
		var $scroll = $t.closest(".scroll_container");
		var $sc = $scroll.find(".sub_content");
		var sc_top = parseInt($sc.css('top'));
		var scc_height = $scroll.find(".sub_content_container").height();
		if($sc.height()+sc_top > scc_height) {
			$sc.animate(
					{
						top: sc_top-scc_height+'px'
					},
					400,
					function(){
						check_subcontent_arrows($scroll);
					}
			);
		}
		return false;
	});

	$(".scroll_up").click(function(e){
		var $t = $(this);
		var $scroll = $t.closest(".scroll_container");
		var $sc = $scroll.find(".sub_content");
		var sc_top = parseInt($sc.css('top'));
		var scc_height = $scroll.find(".sub_content_container").height();
		if(sc_top < 0) {
			$sc.animate(
					{
						top: sc_top+scc_height+'px'
					},
					400,
					function(){
						check_subcontent_arrows($scroll);
					}
			);
		}
		return false;
	});

	var pop_page = window.location.href.split('#')[1];
	if(pop_page) {
		display_subpage(pop_page);
	}

	if($("a.pop-link").length > 1) {
		$(".pop_next").css('display','block');
	}

	$(".scroll_container").each(function(i,e){
		check_subcontent_arrows(e);
	});
});


