var hash_tag = location.hash, hash_tag_pos, hash_tag_location, active_menu, new_sub_height;
var coming_from = 0, coming_from_div = 2, image_width = 1024, image_start = 0, padding = 200, speed = 3300, show_next = 1;
var travel_distance, window_width, current_pos, scroll_to_px, new_height, resize_height;


//SAMMY

  var app = $.sammy(function() {
  
  	this.get('#/', function(context) {
		context.log('Yo yo yo');
	});

	this.get('#/:folder/:parent/', function(context) {
		$('.pane').css({"visibility": "visible"});

		hash_tag = "#" + this.params['parent'];
		active_menu = "." + this.params['parent'];
		hash_tag_location = $(".pane").index( $(hash_tag) );
		
		$('.active').removeClass('active');
		$(hash_tag).addClass('active');
		$(active_menu).addClass('active');

		$('#profiles div[@class*=meta] li:not(.first) a').attr({ href: "#/page/profiles/" });
		$('#more_posts').attr({name: "updates",	href: "#/page/updates/"});
		show_next = 1;

		if (coming_from.length > 1){
			$(coming_from).removeClass("active");
			coming_from_div = $(".pane").index( $(coming_from) );
		}
		
		$(hash_tag).find('div').children().show();

		if (coming_from == hash_tag) { /* if you're going from one subpage to its parent */
			if (hash_tag == "#updates"){
				$.ajax({
					async: false,
					dataType: 'html',
					type: 'GET',
					url: '/category/updates/page/1/',
					success: function(data) {
						if (data.search('zzzzzzzz') < 0){
							$('.load_more').fadeOut();
						}
						$('#updates .pane_interior #posts').html(data);
						resize_height = $('#updates').height() + $("#footer").height()+80;
						$("#container").css({height: resize_height});
						$(".entry-categories a[href]").each(function(){
//							this.href = this.href.replace(/^http:\/\/www.marinelex.com/, "#/page");
						});
				    }
				});
			}
			$('.active').removeClass('active');
			$(hash_tag).addClass('active');
			$(active_menu).addClass('active');
			setTimeout(function(){slideToParent(0, hash_tag)}, 75);
		} else { /* if you're going from one parent page to a different page */
			travel_distance = Math.floor(image_start + ((coming_from_div - hash_tag_location)/10) * image_width);
			scroll_to_px = (hash_tag_location * $(window).width());
			current_pos = getScrollXY();
			slideTo(scroll_to_px, current_pos[0], current_pos[1], travel_distance, hash_tag, hash_tag_location);
		}
		
		gradientSize(hash_tag);

		coming_from = hash_tag;
/* 		context.log('coming from' + coming_from); */
    });
    
	this.get('#/:folder/:parent/:sub/', function(context) {
		$('.pane').css({"visibility": "visible"});

		hash_tag = "#" + this.params['parent'];
		active_menu = "." + this.params['sub'];
		hash_tag_location = $(".pane").index( $(hash_tag) );
		
		$('.active').removeClass('active');
		$(hash_tag).addClass('active');
		$(active_menu).addClass('active');
		
		$('#profiles div[@class*=meta] li:not(.first) a').attr({
			href: "#/page/profiles/" + this.params['sub'] + "/"
		});

		if ($(hash_tag + ' .pane_interior .shadow ' + active_menu).children().is(':hidden')){
			$(hash_tag + ' .pane_interior .shadow ' + active_menu).children().show();
		}
		$(hash_tag + ' .pane_interior .shadow .information:not(' + active_menu + ')').children(":not(h2)").hide();


		var children_of_parent = $(hash_tag + ' .pane_interior').children().size();
		var sub_slide_to_pos = $(hash_tag + ' ' + active_menu).offset();

		if (coming_from == hash_tag) { /* if you're going into a subpage or two subpages within a parent */
/* 			alert("subpage to subpage"); */
			var num_of_div = $('.pane_interior').find(active_menu).children();
			$.each(num_of_div, function(index){
				$(this).slideDown("fast");
				if (!num_of_div[index+1]){
					$(this).slideDown("fast", function(){
						sub_slide_to_pos = $(hash_tag + ' ' + active_menu).offset();
						slideToSub(sub_slide_to_pos.top, hash_tag);
					});
				}
				gradientSize(hash_tag);
			});
			
			$('.active').removeClass('active');
			$(hash_tag).addClass('active');
			$(active_menu).addClass('active');
		} else { /* if you're going from one page to a different parent's subpage */
/* 			alert("page to different parent's subpage"); */
			travel_distance = Math.floor(image_start + ((coming_from_div - hash_tag_location)/10) * image_width);
			scroll_to_px = (hash_tag_location * $(window).width());
			current_pos = getScrollXY();
			slideTo(scroll_to_px, current_pos[0], current_pos[1], travel_distance, hash_tag, hash_tag_location);
			$(hash_tag + ' ' + active_menu).children().slideDown("fast", function(){
					sub_slide_to_pos = $(hash_tag + ' ' + active_menu).offset();
			});
			setTimeout(function(){slideToSub(sub_slide_to_pos.top, hash_tag)},speed+500); /*scrolls to the position of sub div */
		}

		if (coming_from.length > 1){
			$(coming_from).removeClass("active");
			coming_from_div = $(".pane").index( $(coming_from) );
		}

		coming_from = hash_tag;
/* 		context.log('coming from' + coming_from); */
    });
    
    
    
	this.get('#/:folder/:holder/:parent/:category/', function(context) {
		$('.pane').css({"visibility": "visible"});

		hash_tag = "#" + this.params['parent'];
		category = this.params['category'];
		hash_tag_location = $(".pane").index( $(hash_tag) );
		
		$('.active').removeClass('active');
		$(hash_tag).addClass('active');
		$("."+category).addClass('active');
		
		$('#more_posts').attr({
			name: category,
			href: "#/page/category/updates/" + category + "/" 
		});
				
		$('.load_more').fadeIn();
		
		show_next = 1;
		
		travel_distance = Math.floor(image_start + ((coming_from_div - hash_tag_location)/10) * image_width);
		scroll_to_px = (hash_tag_location * $(window).width());
		current_pos = getScrollXY();
		slideTo(scroll_to_px, current_pos[0], current_pos[1], travel_distance, hash_tag, hash_tag_location);

		$.ajax({
			async: false,
			dataType: 'html',
			type: 'GET',
			url: '/category/updates/'+category+'/page/1/',
			success: function(data) {
				if (data.search('zzzzzzzz') < 0){
					$('.load_more').fadeOut();
				}
				$('#updates .pane_interior #posts').html(data);
				resize_height = $('#updates').height() + $("#footer").height()+80;
				$("#container").css({height: resize_height});
				$(".entry-categories a[href]").each(function(){
//					this.href = this.href.replace(/^http:\/\/www.marinelex.com/, "#/page");
				});

			}
		});
		
		gradientSize(hash_tag);
		
    });


  });
  

//ENDSAMMY

function backToTop(){
	$('#verticalContainer').animate({scrollTop: 0}, speed/3);
}

function gradientSize(hash_tag){
	if (hash_tag == 0){
		new_height = $(window).height();
	} else {
		new_height = $(hash_tag).height()+$("#footer").height()+80;
		if (new_height < $(window).height()){
			new_height = $(window).height();
		}
	}
	$('#gradient').css({height: new_height});
}

function animateFrame(scroll_to_px, travel_distance, hash_tag, hash_tag_location){
	var move_ticker = -(hash_tag_location * 280);

	$("#footer").fadeOut();
	$("#container").stop().animate({height: $(window).height()},speed/6);
	$('#paneWrapper').stop().animate({marginLeft: 0-scroll_to_px + 'px'}, speed);
	var image_moves = -(hash_tag_location * 50);
	$("#ticker").stop().animate({textIndent: move_ticker + 'px'}, speed);
	$("#image").stop().animate({textIndent: image_moves}, speed, function(){
		new_height = $(hash_tag).height()+$("#footer").height()+80;
		if (new_height < $(window).height()){
/* 			alert("after animate, no scroll bar, new_height = " + new_height); */
			$("#container").stop().animate({height: $(window).height()},speed/3, function(){
				gradientSize(0);
				$("#footer").fadeIn("fast");
				$('.pane:not(.active)').css({"visibility": "hidden"});
			});
		} else {
/* 			alert("after animate + scroll bar, new_height = " + new_height); */
			$("#container").stop().animate({height: new_height}, 0, function(){
				gradientSize(hash_tag);
				$("#footer").fadeIn("fast");
				$('.pane:not(.active)').css({"visibility": "hidden"});
			});
		}
	});
}

function resizeContainer(hash_tag){
	var i = 0;
	if (i == 0){
		resize_height = $(hash_tag).height() + $("#footer").height() + 80;
		$("#container").css({height: resize_height});
		++i;
	}
}

function slideToSub(sub_slide_to_pos, hash_tag) {
	new_sub_height = $(hash_tag).height() + $("#footer").height()+80;
		if (new_sub_height < $(window).height()){
			new_sub_height = $(hash_tag).height() + $("#footer").height()+80;
			$("#container").stop().animate({height: $(window).height()},speed/3, function(){
				/* if window is bigger than container footer does nothing */
			});	
		} else {
			new_sub_height = $(hash_tag).height() + $("#footer").height()+80;
			
			$("#container").css({height: new_sub_height}, function(){
				/* if container if bigger than window, footer moves */
				$("#footer").animate({bottom: 0},speed/6);
			});
		}
	$('#verticalContainer').animate({scrollTop: sub_slide_to_pos}, speed/3);
	gradientSize(hash_tag);
}


function slideToParent(sub_slide_to_pos, hash_tag) {
	new_sub_height = $(hash_tag).height() + $("#footer").height()+100;
	/* alert(new_sub_height + " " + $(window).height() ); */
	$('#verticalContainer').animate({scrollTop: sub_slide_to_pos}, speed/3, function(){
		if (new_sub_height < $(window).height()){
			/* alert("doesn\'t need scrollbar"); */
			$("#container").stop().animate({height: $(window).height()},speed/3, function(){
				/* if window is bigger than container footer does nothing 
				/* */
				gradientSize(hash_tag);
			});	
		} else {
			/* alert("needs scrollbar"); */
			new_sub_height = $(hash_tag).height() + $("#footer").height()+80;
			
			$("#container").css({height: new_sub_height}, function(){
				/* if container if bigger than window, footer moves */
				$("#footer").animate({bottom: 0},speed/6);
				gradientSize(hash_tag);
			});
		}
	});
}


function slideTo(scroll_to_px, current_posY, current_posX, travel_distance, hash_tag, hash_tag_location) {
	if ( current_posY != 0 ) {
		$('#verticalContainer').animate({scrollTop: '0px'}, speed/3, function(){
			animateFrame(scroll_to_px, travel_distance, hash_tag, hash_tag_location);
		});
		current_posY = 0;
	} else {
		animateFrame(scroll_to_px, travel_distance, hash_tag, hash_tag_location);
	}
	gradientSize(hash_tag);
/* console.log('in slideTo Y = ' +current_posY); */
}

function slideFix(scroll_to_px) {
	$('#paneWrapper').css({"margin-left": 0-scroll_to_px + 'px'});
/* 	console.log('slide fixed ' + "-" + scroll_to_px + 'px'); */
}

function resizeEverything(){
	window_width = $(window).width();
	$("#paneWrapper").css("width", 100+window_width*($(".pane").length));
	$(".pane").css("width", window_width);
	$("#container").css("width", window_width);
	if (window_width > 1280){
		if (window_width < 1750){
			$("#ticker").css("width", window_width-(Math.ceil(window_width/8)));
			$("#image").css("width", window_width-(Math.ceil(window_width/8)));
		} else {
			$("#image").css("width", 1530);
			$("#ticker").css("width", 1530);
		}
	}
	gradientSize(hash_tag);
	hash_tag_pos = hash_tag_location * window_width;
	slideFix(hash_tag_pos);
}

function getScrollXY() {
	var scr_of_x = 0, scr_of_y = 0, pos_array = new Array(2);
	//Netscape compliant
	scr_of_y = $('#verticalContainer').scrollTop();
	scr_of_x = $('#paneWrapper').css("margin-left").replace("px", "");
/* 	console.log('in getScrollXY Y = ' +scr_of_y); */
	pos_array = [scr_of_y, scr_of_x];
	return pos_array;
}

function showOrHideProfiles(hash_tag, id_of_link){
	if ($('#profiles .pane_interior .shadow .' + id_of_link).parent().parent().is(':hidden')){
		$('#profiles .pane_interior .shadow .' + id_of_link).parent().parent().fadeIn("fast", function(){
			resizeContainer(hash_tag);
			gradientSize(hash_tag);
		});
	}
	$('#profiles .pane_interior .shadow .information:not(.' + id_of_link + ')').parent().parent().fadeOut("fast", function(){
		resizeContainer(hash_tag);
		gradientSize(hash_tag);
	});
}

function getScrollBarWidth () {
	var inner = document.createElement('p');
	inner.style.width = "100%";
	inner.style.height = "200px";

	var outer = document.createElement('div');
	outer.style.position = "absolute";
	outer.style.top = "0px";
	outer.style.left = "0px";
	outer.style.visibility = "hidden";
	outer.style.width = "200px";
	outer.style.height = "150px";
	outer.style.overflow = "hidden";
	outer.appendChild (inner);

	document.body.appendChild (outer);
	var w1 = inner.offsetWidth;
	outer.style.overflow = 'scroll';
	var w2 = inner.offsetWidth;
	if (w1 == w2) w2 = outer.clientWidth;

	document.body.removeChild (outer);

	return (w1 - w2);
};

function pop(url){
	window.open(url, '_blank', 'width=900,height=650,scrollbars=yes,menubar=yes');
}

$(window).resize(function() {
	resizeEverything();
});

$(document).ready(function () {
//	resizeEverything();
		
	$("#menu a[href], #footer a[href]").each(function(){ 
		this.href = this.href.replace(/^http:\/\/www.marinelex.com/, "#/page");
	});

	$(".entry-categories a[href]").each(function(){
		this.href = this.href.replace(/^http:\/\/www.marinelex.com/, "#/page");
	});

	$(".information").wrap("<div class=\"top-right-shadow\"><div class=\"bottom-left-shadow\"><div class=\"shadow\"></div></div></div>");

	hash_tag = '#home';
	hash_tag_location = $(".pane").index( $(hash_tag) );
	coming_from = '#home';
	image_moves = -(hash_tag_location * 50);
	resizeEverything();
	$("#image").css({"textIndent": image_moves});
	current_pos = getScrollXY();
    app.run('#/page/home/');

		if (new_height < $(window).height()){
/* 			alert("after animate, no scroll bar, new_height = " + new_height); */
			$("#container").stop().animate({height: $(window).height()},speed/3, function(){
				gradientSize(0);
				$("#footer").fadeIn("fast");
				$('.pane:not(.active)').css({"visibility": "hidden"});
			});
		} else {
/* 			alert("after animate + scroll bar, new_height = " + new_height); */
			$("#container").stop().animate({height: new_height}, 0, function(){
				gradientSize(hash_tag);
				$("#footer").fadeIn("fast");
				$('.pane:not(.active)').css({"visibility": "hidden"});
			});
		}

    
	$('a#more_posts').click(function(){
		++show_next;
		var updates_link = '/category/updates/page/' + show_next;
		var updates_category = $('a#more_posts').attr("name");
		
		if (updates_category != "updates"){
			updates_link = '/category/updates/' + updates_category + '/page/' + show_next;
		}
		$.ajax({
			async: false,
			type: 'GET',
			url: updates_link,
			success: function(data) {
				if (data.search('zzzzzzzz') < 0){
					$('.load_more').fadeOut();
				}
				$('#updates .pane_interior #posts').append(data);
				resize_height = $('#updates').height() + $("#footer").height()+80;
				$("#container").css({height: resize_height});
				$(".entry-categories a[href]").each(function(){
					this.href = this.href.replace(/^http:\/\/www.marinelex.com/, "#/page");
				});

		    }
		});

		gradientSize(hash_tag);

	});
	
	$('.clickable').hover(
	function(){
		$(this).parent().css({"background": "url(http://www.marinelex.com/wp-content/themes/deorchis/style/images/background/shadow-dark.png) bottom right"});
	},
	function(){
		$(this).parent().css({"background": "url(http://www.marinelex.com/wp-content/themes/deorchis/style/images/background/shadow.png) bottom right"});
	});
	
	$('.clickable h2').click(function(){
		$(this).parent().children().show("fast", function(){
			resizeContainer(hash_tag);
		});
	});

	$('#profiles li:not(.first)').append("<span>Show</span>");
	$('#profiles div[@class*=meta] li:not(.first)').click(function(){
		id_of_link = $(this).children().attr("class");
		showOrHideProfiles(hash_tag, id_of_link);
	});

	$('#s').focus(function(){
		$(this).attr({value: ""});
	});
	
});
