$(document).ready(function(){
	
	// Hide Page if it excists
	function resetpage() {
		
		$('#page_wrapper_holder').empty();
		$("#overview_holder").fadeTo(0, 0).hide();
		$("#overview_fix").fadeTo(0, 0).hide();
		$('#logo').removeClass('disable');
		$('#getmymenuback, #getmymenubackbottom, #right_bottom, .prev_slidr, .next_slidr').show();
		$('#page_wrapper_bg, #page_wrapper_holder').css({'visibility':'hidden'}).hide();
				
	}
	
   	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) {
		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_') {
			
				$('#page_wrapper_holder').empty();
				
				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+"?category_name=" + 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 {
				
				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(){
			
   			});
			
		}
	});
	
	
	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 {
   		
			$("#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.replace(base_url, "");
		var hash = hash.replace("?category_name=", "");
		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.replace(base_url, "");
		var hash = hash.replace("?category_name=", "");
		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;   
    	}
    	
	});
	
	
});
