$(document).ready(function(){
	
	/*
if (window.location == $.session("getBaseUrl") + "/#") {
		window.location = $.session("getBaseUrl");
		return false;
	} 
	
	if (location.hash == "page_145") {
		location.hash = "";
	}
*/
	
	// Hide Page if it excists
	function resetpage() {
		$('#page_wrapper_holder').empty();
		$("#overview_holder").fadeTo(0, 0).hide();
		$("#overview_fix").fadeTo(0, 0).hide();
		$('#gc_navnext, #gc_navprevious, .prev_slidr, .next_slidr').show();
		$('#page_wrapper_bg, #page_wrapper_holder').hide();
	}
	
	function resetpost() {
		$('#Container').empty();
		$('#page_wrapper_bg, #page_wrapper_holder').show();
		$('#page_wrapper_holder').empty();
	}
	
   	var allYourBase = $.session("getBaseUrl");
   	
   	if (allYourBase.substr(-1) == '/') {
   		// The base url seems fine
   	} else {
   		// Else add a slash in the end
   		allYourBase = allYourBase + '/'
   	}
	
	var pathname = window.location;
	var base_url = allYourBase;

	$.historyInit(function (hash) {
		
		$("#gc-top ul li ul li a").each(function(){
		
		// Shows active menu item for posts on page load

			var hashz = this.href;
			var hashz = hashz.substr(-4);
    		var hashz = hashz.replace ('', '');
    		
    		var blahblah = hash.substr(5);
    		
    		//alert(hashz + ", " + blahblah);
    		
    		if(hashz == "p=" + blahblah){
    			resetActiveClasses();
    			$(this).addClass("gc-active");
    			$(".gc-cat").addClass("gc-active");
    			$(".gc-menulink, .gc-page").animate({'width': '124px'}, "fast");
    			return false;
    		}
		});
		
		$(".gc-page, .gc-post").each(function(){
			
			var hashz = this.href;
			var hashz = hashz.substr(-4);
    		var hashz = hashz.replace (/[^\d]/g, '');
    		
    		var blahblah = hash.substr(5);
    		
    		if(hashz == blahblah){
    			resetActiveClasses();
    			$(this).addClass("gc-active");
    			//$(".gc-menulink, .gc-page").animate({'width': '124px'}, "fast");
    			//$(this).parent().parent().prev().addClass("gc-active");
    			return false;
    		}

			
			
		});
		
	
	
		if(hash) {
			
			
			
			if (hash.substr(0,5) == 'post_') {
				
				resetpage();
			
				var permfix = hash.substr(5);
				$("#load").show();
				
				if ($.client.os == 'Windows') {
				
					if($.client.browser == 'Chrome' || $.client.browser == 'Opera') {
						// Chrome & Opera on PC, the only browsers that is fast enough
					} else {
					
						$("#loadr").show();
						
						var remember = $.cookie('rememBer');
		
						if (remember == 'yes') {
						
						    $.scrollTo('.image:last', 0, {easing:'easeInOutQuart', offset: 0, axis: 'x', onAfter:function(){ 
						        // After the scroll	
						        //alert(slidecunt);						
						        }			
						    });	
						
						} else {
							
						    $("#page").empty();
							
						    // fix the scroll flicker problem in browsers with slow javascript engines
						    
						    $.scrollTo(0, 0, {easing:'easeInOutQuart', offset: 0, axis: 'x', onAfter:function(){ 
						        // After the scroll	
						        //alert(slidecunt);						
						        }			
						    });	
						
						}
						
					}
				
				}
				
				$("#page").load(""+base_url+"?p=" + permfix, {}, function(){
					
   				});
			
				
			}
			
			else if (hash.substr(0,5) == 'page_') {
				
				//@DAVID - Created reset post to sort out display hide functions.
				resetpost();
				
				var permfix = hash.substr(5);
				
				$("#load").show();
				
				if ($.client.os == 'Windows') {
				
					if($.client.browser == 'Chrome' || $.client.browser == 'Opera') {
						// Chrome & Opera on PC, the only browsers that is fast enough
					} else {
					
						$("#loadr").show();
						
						$("#page").empty();
						
						// fix the scroll flicker problem in browsers with slow javascript engines
						
						$.scrollTo(0, 0, {easing:'easeInOutQuart', offset: 0, axis: 'x', onAfter:function(){ 
						    // After the scroll	
						    //alert(slidecunt);						
						    }			
						});	
						
					}
				
				}
				
				$("#page_wrapper_holder").load(""+base_url+"?p=" + permfix, {}, function(){
					
   				});
								
			}
			
			else if (hash.substr(0,4) == 'cat_') {
			
				resetpage();
			
				var permfix = hash.substr(4);
				$("#load").show();
				
				if ($.client.os == 'Windows') {
				
					if($.client.browser == 'Chrome' || $.client.browser == 'Opera') {
						// Chrome & Opera on PC, the only browsers that is fast enough
					} else {
					
						$("#loadr").show();
						
						$("#page").empty();
						
						// fix the scroll flicker problem in browsers with slow javascript engines
						
						$.scrollTo(0, 0, {easing:'easeInOutQuart', offset: 0, axis: 'x', onAfter:function(){ 
						    // After the scroll	
						    //alert(slidecunt);						
						    }			
						});	
						
					}
				
				}
				
				$("#page").load(""+base_url+"?cat=" + permfix, {}, function(){

   				});
			
			}
			
			else if (hash.substr(0,4) == 'tag_') {
			
				resetpage();
			
				var permfix = hash.substr(4);
				$("#load").show();
				
				if ($.client.os == 'Windows') {
				
					if($.client.browser == 'Chrome' || $.client.browser == 'Opera') {
						// Chrome & Opera on PC, the only browsers that is fast enough
					} else {
					
						$("#loadr").show();
						
						$("#page").empty();
						
						// fix the scroll flicker problem in browsers with slow javascript engines
						
						$.scrollTo(0, 0, {easing:'easeInOutQuart', offset: 0, axis: 'x', onAfter:function(){ 
						    // After the scroll	
						    //alert(slidecunt);						
						    }			
						});	
						
					}
				
				}
				
				$("#page").load(""+base_url+"?tag=" + permfix, {}, function(){

   				});
			
			}
			
			else if (hash.substr(0,7) == 'search_') {
			
				resetpage();
			
				var permfix = hash.substr(7);
				$("#load").show();
				
				if ($.client.os == 'Windows') {
				
					if($.client.browser == 'Chrome' || $.client.browser == 'Opera') {
						// Chrome & Opera on PC, the only browsers that is fast enough
					} else {
					
						$("#loadr").show();
						
						$("#page").empty();
						
						// fix the scroll flicker problem in browsers with slow javascript engines
						
						$.scrollTo(0, 0, {easing:'easeInOutQuart', offset: 0, axis: 'x', onAfter:function(){ 
						    // After the scroll	
						    //alert(slidecunt);						
						    }			
						});	
						
					}
				
				}
				
				$("#page").load(""+base_url+"?s=" + permfix, {}, function(){

   				});
			
			}
			
			else if (hash == 'start') {

				resetpage();
			
				var permfix = hash.substr(4);
				$("#load").show();
				
				if ($.client.os == 'Windows') {
				
					if($.client.browser == 'Chrome' || $.client.browser == 'Opera') {
						// Chrome & Opera on PC, the only browsers that is fast enough
					} else {
					
						$("#loadr").show();
						
						$("#page").empty();
						
						// fix the scroll flicker problem in browsers with slow javascript engines
						
						$.scrollTo(0, 0, {easing:'easeInOutQuart', offset: 0, axis: 'x', onAfter:function(){ 
						    // After the scroll	
						    //alert(slidecunt);						
						    }			
						});	
						
					}
				
				}
				
				$("#page").load(""+base_url+"?cat=1", {}, function(){

   				});
			
			}
			
			else if (hash == '') {
				
				var permfix = "145";
				
				$("#page_wrapper_holder").load(""+base_url+"?p=" + permfix, {}, function(){
					
   				});
				
			}
			
			else {
				
				var latestpost = $.session("getLatestPost");
				
				resetpage();
					
				$("#load").show();
				$("#page").load(""+base_url+"?p=" + latestpost, {}, function(){
						alert("Error. The page your looking for doesn't seem to exist. Probably you're playing around with the hash urls. You are being redirected to the root page.");
   				});

			}
 			
		    
		} else {
			
			var latestpost = $.session("getLatestPost");

			resetpage();
			  
			$("#load").show();
			$("#page").load(""+base_url+"?p=" + latestpost, {}, function(){
			
   			});
			
		}	

		//WORKING TO HERE
			
	});
	
	
	if (pathname == base_url) {
		
		//var latestpost = $.session("getLatestPost");
		
		//resetpage();
			
		$("#load").show();
		
		if ($.session("firstpagedisplay") == '1' || $.session("firstpagedisplay") == '2') {
			
			$("#page").load(""+base_url+"?p=" + latestpost, {}, function(){
				
				if ($.session("firstpagedisplay") == '2') {
					
					$('#overview_trigger').trigger('mouseover');
					$('#overview_trigger').trigger('click');
					
				}
				
				/*
				if ($.cookie("visited") == 'yes') {
			
				} else {
					$.cookie("visited", "yes");
					$('#welcome').css({'display':'block'});
					$('#welcome').click(function(){
						$(this).remove();
					});			
				}
				*/
					
   			});
   			
   		} else if ($.session("firstpagedisplay") == '3') {
   		
			$("#page").load(""+base_url+"?cat=1", {}, function(){
				
				/*
				if ($.cookie("visited") == 'yes') {
			
				} else {
					$.cookie("visited", "yes");
					$('#welcome').css({'display':'block'});
					$('#welcome').click(function(){
						$(this).remove();
					});			
				}
				*/
					
   			});
   			
   		}
   			
   		else if ($.session("firstpagedisplay") == '4') {
				
				var permfix = "145";
				
				$("#page_wrapper_holder").load(""+base_url+"?p=" + permfix, {}, function(){
					
   				});

   		} else {
   		
			$("#page").load(""+base_url+"?p=" + latestpost, {}, function(){
				
				/*
				if ($.cookie("visited") == 'yes') {
			
				} else {
					$.cookie("visited", "yes");
					$('#welcome').css({'display':'block'});
					$('#welcome').click(function(){
						$(this).remove();
					});			
				}
				*/
					
   			});
   		
   		}
   			
	}
	
	$("a.history").live('click', function(){
		var hash = this.href;
		var hash = hash.substr(-4);
    	var hash = hash.replace (/[^\d]/g, '');
		hash = 'page_' + hash;
		$.historyLoad(hash);
		return false;
	});
	
	$(".cat_trigger a").live('click', function(){
		var hash = this.href;
		var hash = hash.substr(-4);
    	var hash = hash.replace (/[^\d]/g, '');
		hash = 'cat_' + hash;
		$.historyLoad(hash);
		return false;
	});
	
	$(".tag_trigger a").live('click', function(){
		var hash = this.href;
		var hash = hash.replace(base_url, "");
		var hash = hash.replace("?tag=", "");
		hash = 'tag_' + hash;
		$.historyLoad(hash);
		return false;
	});
	
	$("#dropr_pages a").live('click', function(){
		var hash = this.href;
		var hash = hash.substr(-4);
    	var hash = hash.replace (/[^\d]/g, '');
		hash = 'page_' + hash;
		$.historyLoad(hash);
		return false;
	});
	
	$("#dropr_tags a").live('click', function(){
		var hash = this.href;
		var hash = hash.replace(base_url, "");
		var hash = hash.replace("?tag=", "");
		hash = 'tag_' + hash;
		$.historyLoad(hash);
		return false;
	});
	
	$("#dropr_categories a").live('click', function(){
		var hash = this.href;
		var hash = hash.substr(-4);
    	var hash = hash.replace (/[^\d]/g, '');
		hash = 'cat_' + hash;
		$.historyLoad(hash);
		return false;
	});
	
	$(".imgr a").live('click', function(){
		var hash = this.href;
		var hash = hash.substr(-4);
		
		var whatfuckingimage = $(this).parents().attr('id');
		var whatfuckingimage = whatfuckingimage.replace (/[^\d]/g, '');
		
		if (whatfuckingimage == 1) {
			whatfuckingimage = '0';
		} else {
			whatfuckingimage--;
		}		
		
		$.session("whatimage", "false");
   		$.session("whatimage", whatfuckingimage );
		
    	var hash = hash.replace (/[^\d]/g, '');
		hash = 'post_' + hash;
		$.historyLoad(hash);
		return false;
	});
	
	$(".page_item a").live('click', function(){
		var hash = this.href;
		var hash = hash.substr(-4);
    	var hash = hash.replace (/[^\d]/g, '');
		hash = 'page_' + hash;
		$.historyLoad(hash);
		return false;
	});
	
	$(".next_postr").live('click', function(){
	
    	if ($.cookie("prevURLnumber") == $.cookie("current_post_id")) {
			var hash = $.cookie("newest_post_id");
			hash = 'post_' + hash;
			$.historyLoad(hash);
			return false;    	
    	} else {
    	
    		var hash = $.cookie("prevURLnumber");
			hash = 'post_' + hash;
			$.historyLoad(hash);
			return false;   
    	}
	
	});
	
	$(".prev_postr").live('click', function(){
    	
    	if ($.cookie("nextURLnumber") == $.cookie("current_post_id")) {
    	 	
			var hash = $.cookie("oldest_post_id");
			hash = 'post_' + hash;
			$.historyLoad(hash);
			return false;   
			
    	} else {
    	
    	
    		var hash = $.cookie("nextURLnumber");
			hash = 'post_' + hash;
			$.historyLoad(hash);
			
			return false;   
    	}
    	
	});
	
	
	var clicked = false;
	
	$(".gc-menulink").live('mouseenter', function(){
		growMenu(this);
		
	});
	$(".gc-menulink").live('mouseleave', function(){
		//resetSubMenu();
		
		if(!clicked){
			$(".gc-menulink, .gc-page").stop().animate({'width': '124px'}, "fast");
		} else {
			$(this).not(".gc-cat").stop().animate({'width': '124px'}, "fast");
		}
	});
	
	$(".gc-cat").live('click', function(){
		//resetActiveClasses();
		if(!clicked){
			resetSubMenu();
			$(this).addClass("gc-active");
			growMenu(this);
			$(this).next().slideDown();
			clicked = true;
		} else if(clicked){
			resetSubMenu();
			//$(this).next().slideDown();
			clicked = false;
		}
		
		
	});

	$("#gc-top ul li ul li a").live('click', function(){
		//resetActiveClasses();
		$(this).addClass("gc-active");
		
		$(this).parent().parent().prev().addClass("gc-active");
		//growMenu($(this).parent().parent().prev());
		$(this).parent().parent().slideUp();
	
		var hash = this.href;
		var hash = hash.substr(-4);
    	var hash = hash.replace (/[^\d]/g, '');
		hash = 'post_' + hash;
		$.historyLoad(hash);
		
		clicked = false;
		return false;
		
	});
	
	$(".gc-post").live('click', function(){
	
		//resetActiveClasses();
		resetSubMenu();
		
		var hash = this.href;
		var hash = hash.substr(-4);
    	var hash = hash.replace (/[^\d]/g, '');
		hash = 'post_' + hash;
		$.historyLoad(hash);
		
		clicked = false;
		return false;
		
	});
	
	$(".gc-page").live('click', function(){		
		//resetActiveClasses();
		resetSubMenu();
		
		var hash = this.href;
		var hash = hash.substr(-4);
    	var hash = hash.replace (/[^\d]/g, '');
    	
    	
    	// exception for NEWS page   (TOM)
    	if(hash != '156') {

    	  	hash = 'page_' + hash;
		  	$.historyLoad(hash);
		  	clicked = false;
		  	return false;
		  	
		}
		
		
		
	});
	
	$(".gc-homelink").live('click', function(){
		window.location = $.session("getBaseUrl");
		return false;
	});
	
	
	function resetActiveClasses(){
	//alert("resetting");
		$("#gc-nav ul a, .gc-sub").removeClass("gc-active");
		//$(".gc-page, .gc-post").removeClass("gc-active");
	}
	

	function resetSubMenu(current){
		$("#gc-top ul li ul").slideUp();
		$(".gc-menulink, .gc-page").stop().animate({'width': '124px'}, "fast");
	}
	
	function growMenu(element){
		$(element).stop().animate({'width': '150px'}, "fast");
	}
	
});
