jQuery(window).bind('hashchange', function() {
	var hash = document.location.hash;
	hashArray = Array('#');
	hash = hash.replace(hashArray, '');
	if(hash != ''){
		if(!hash.search(/special/i)){
			ajaxSpecial(hash, '');
		}
		else{
			ajaxUrl(hash, '');
		}
	}
	else ajaxUrl('frontpage.php', '');
});
jQuery(document).ready(function(){
	var hash = document.location.hash;
	hashArray = Array('#');
	hash = hash.replace(hashArray, '');
	if(hash != ''){
		if(!hash.search(/special/i)){
			ajaxSpecial(hash, '');
		}
		else{
			ajaxUrl(hash, '');
		}
	}
	else ajaxUrl('frontpage.php', '');
	
	loadHabboInfo();
	var motto = '';

	jQuery('body').css({'display':'none'});
	jQuery('body').fadeIn(500);
		
	jQuery("#news_content #title.left, #news_content #title.right").live('click', function(){
		jQuery("#news_content .contents").each(function(){
			jQuery(this).slideToggle("slow");
		});
	});
	jQuery(".collapseButton").live('click', function(){
		jQuery(".collapseContent[id="+ jQuery(this).attr('id')+"]").slideToggle("slow");
	});
	jQuery(".dropdownMenu").live('click', function(){
		jQuery(".dropdownList[id="+ jQuery(this).attr('parent')+"]").slideToggle("slow");
	});
	jQuery("#edit_motto .motto").live('click', function(){
		if(motto == ''){
			motto = jQuery(this).html();
			motto = motto.replace("&lt;", "<");
			motto = motto.replace("&gt;", ">");
			motto = motto.replace("&hearts;", "<3");
			motto = motto.replace("&amp;", "&");
			jQuery(this).html('<textarea id="edit"></textarea>');
			jQuery("#edit_motto .motto textarea").focus().val(motto);
		}
	});
	jQuery("#edit_motto .motto textarea").live('keypress', function(event){
		var mottoet = jQuery(this).val();
		if(event.keyCode == 13){
			mottoet = mottoet.substr(0,36);
			jQuery.post(root +"includes/actions.php?action=saveMotto", {motto: mottoet}, function(data){
				if(data != ''){
					alert(data);
				}
				else{
					jQuery("#edit_motto .motto").html(mottoet);
					motto = '';
				}
			});
			return false;		
		}
		if(mottoet.length > 36){
			if(event.keyCode == 8){
				return true;
			}
			else{
				return false;
			}
		}
	});
	jQuery("[rel^=special]").live('click', function(){
		var url = jQuery(this).attr('data');
		var dataen = '';
		if(url.indexOf("?", 0) != -1){
			url = url.split("?");
			dataen = url[1];
			url = url[0];
		}
		ajaxSpecial(url, dataen);
		return false;		
	});
	jQuery("[rel^=settings]").live('click', function(){
		var url = jQuery(this).attr('data');
		var dataen = '';
		if(url.indexOf("?", 0) != -1){
			url = url.split("?");
			dataen = url[1];
			url = url[0];
		}
		jQuery("#news_content .news .contents").each(function(){
			jQuery(this).slideUp("slow");
		});
		ajaxUrl(url, dataen);
		return false;		
	});
	jQuery("[rel^=box]").live('click', function(){
		var url = jQuery(this).attr('data');
		var dataen = '';
		var title = jQuery(this).attr('title');
		if(url.indexOf("?", 0) != -1){
			url = url.split("?");
			dataen = url[1];
			url = url[0];
		}
		ajaxPopup(url, dataen, title, jQuery(this).attr('close'));
	});
	jQuery("#funpop .close").live('click', function(){
		ajaxPopupClose();
	});
	jQuery("#pagenation a").live('click', function(){
		wall.changePage(jQuery(this).attr('data'));
	});
});

function loadHabboInfo(){
	jQuery.get(root+"includes/habbo.php", function(data){
		data = data.split("%split%");
		jQuery("#habbo-online").html(data[0]);
		jQuery("#habbo-build").html(data[1]);
	});
}

function ajaxSpecial(url, data){
	jQuery("#bigField").html('<center><div id="loading-img"></div></center>');
	jQuery("#specialReload").show();
	jQuery("#specialReload a").attr('data', url+'?'+data);
	
	if(data != '') var hashUrl = url+'?'+data;
	else var hashUrl = url;
	
	hashUrl = hashUrl.replace('.php', '');
	
	document.location.hash = hashUrl;
	
	if(url.indexOf('?') != -1){
		urlen = url.split('?');
		data = urlen[1];
		url = urlen[0];
	}
	jQuery("link[name=backgrounds]").attr("href",root+"themes/backgrounds.php");
	jQuery("link[name=stickers]").attr("href",root+"themes/stickers.php");
	jQuery("link[name=special]").attr("href",root+"themes/special.css");
	
	jQuery.ajax({
		type: "GET",
		url: url,
		data: data,
		error: function(){
			jQuery("#bigField").html('<div id="mid"><div class="blue"><div id="box_top"></div><div id="box_mid"><div id="button_big" class="blue">Fant ikke siden</div><div id="content"><strong>Vi fant ikke siden du ville laste inn</strong><br/><em>Prøv å last inn en annen side istedenfor da :)</em></div></div><div id="box_bot"></div></div></div><br style="clear:both;" />');
		},
		success: function(data){
			jQuery("#bigField").html(data);
			jQuery("body").scrollTop(0);
		},
	});	
}

function ajaxUrl(url, data){
	if(!document.getElementById("innerCircle")){
		jQuery("#specialReload").hide();
		jQuery("#bigField").html('<center><div id="loading-img"></div></center>');
		jQuery("#bigField").load(root+"includes/content.php", function(){
			ajaxUrl(url, data);
		});
	}
	else{
		jQuery("#innerCircle").html('<div class="blue"><div id="box_top"></div><div id="box_mid"><div id="button_big" class="blue">Laster inn</div><div id="content"><center><div id="loading-img"></div></center></div></div><div id="box_bot"></div>');
		jQuery("link[name=uploads]").attr("href",root+"themes/default/upload.css");
		Shouts.update();
		updateOnline();
		
		if(data != '') var hashUrl = url+'?'+data;
		else var hashUrl = url;
		hashUrl = hashUrl.replace('.php', '');
		document.location.hash = hashUrl;
		jQuery.ajax({
			type: "GET",
			url: url,
			data: data,
			error: function(){
				jQuery("#innerCircle").html('<div class="blue"><div id="box_top"></div><div id="box_mid"><div id="button_big" class="blue">Fant ikke siden</div><div id="content"><strong>Vi fant ikke siden du ville laste inn</strong><br/><em>Prøv å last inn en annen side istedenfor da :)</em></div></div><div id="box_bot"></div>');
			},
			success: function(data){
				jQuery("#innerCircle").html(data);
			},
		});	
	}
}

function ajaxPopupClose(){
	jQuery("#funpop").each(function(){
		jQuery(this).fadeOut('fast',function(){
			jQuery(this).remove();
		});
	});
}

function ajaxPopup(url, data, title, closing){
	jQuery("#funpop").each(function(){
		jQuery(this).remove();
	});
	jQuery("body").append('<div id="funpop"><div id="top"></div><div id="middle"><div id="title">'+title+' <div class="close">'+closing+'</div></div><div id="content">Laster inn</div><br /></div><div id="bottom"></div></div>');
	var windowWidth = window.innerWidth;
	var windowHeight = Number(window.innerHeight/2);


	var boxHeight = Number(jQuery("#funpop").height()/2);
	var boxWidth = jQuery("#funpop").width();
	jQuery("#funpop").css({"left" : Number(windowWidth/2)-Number(boxWidth/2) +"px", "top": (windowHeight-boxHeight+window.pageYOffset)+"px"});
	jQuery("#funpop").fadeIn('fast');
	jQuery("#funpop").draggable({containment: "body", handle: "#title", scroll: true});

	jQuery.ajax({
		type: "GET",
		url: url,
		data: data,
		success: function(data){
			jQuery("#funpop #content").html(data);
			var boxHeight = Number(jQuery("#funpop").height()/2);
			var boxWidth = jQuery("#funpop").width();
			jQuery("#funpop").animate({"left" : Number(windowWidth/2)-Number(boxWidth/2) +"px", "top": (windowHeight-boxHeight+window.pageYOffset)+"px"});
		},
	});	

}

function postAlert(msg, url){
	var r= confirm(msg);
	if(r == true){
		location.href = url;
	}
}
function postAlertAjax(msg, url, reload, reload_url){
	var r=confirm(msg);
	if(r== true){
		jQuery.get("./includes/"+url, function(data){
			if(data == null || data == ""){
				if(reload == true){
					var dataen = '';
					
					if(reload_url.indexOf("?", 0) != -1){
						reload_url = reload_url.split("?");
						dataen = reload_url[1];
						reload_url = reload_url[0];
					}
	
					if(reload_url == null || reload_url.length == 0 || reload_url == ""){
						location.href = reload_url;
					}
					else{
						ajaxUrl(reload_url, dataen);
					}
				}
			}
			else{
				alert(data);
			}
		});
		return false;
	}
}
function submitForm(form, url, reload, reload_url){
	jQuery.post("./includes/"+ url, jQuery(form).serialize(), function(data){
		if(data == null || data == ""){
			if(reload == true){
				var dataen = '';
				
				if(reload_url.indexOf("?", 0) != -1){
					reload_url = reload_url.split("?");
					dataen = reload_url[1];
					reload_url = reload_url[0];
				}

				if(reload_url == null || reload_url.length == 0 || reload_url == ""){
					location.href = reload_url;
				}
				else{
					ajaxUrl(reload_url, dataen);
				}
			}
		}
		else{
			alert(data);
		}
	});
	return false;
}
function submitBox(form, url, reload, reload_url, reload_title, reload_close){
	jQuery.post("./includes/"+ url, jQuery(form).serialize(), function(data){
		if(data == null || data == ""){
			if(reload == true){
				var dataen = '';
				
				if(reload_url.indexOf("?", 0) != -1){
					reload_url = reload_url.split("?");
					dataen = reload_url[1];
					reload_url = reload_url[0];
				}

				if(reload_url == null || reload_url.length == 0 || reload_url == ""){
				}
				else{
					ajaxPopup(reload_url, dataen, reload_title, reload_close);
				}
			}
		}
		else{
			alert(data);
		}
	});
	return false;
}

function updateOnline(){
	jQuery.get("./includes/actions.php?action=getOnline", function(data){
		data = data.split("%split%");
		jQuery("#totalBank").html(data[0]);
		jQuery("#totalOnline").html(data[1]);
		jQuery("#onlineList").html(data[2]);
		jQuery("#newPm").html(data[3]);
		jQuery("#latestNews").html(data[4]);
		jQuery("#latestArticles").html(data[5]);
		jQuery("#guideList").html(data[6]);
	});
}
