var sWidth;
var sHeight;
var Page;

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

function getBrowser(){
	if($.browser.mozilla)
		return("mozilla");
	else
	if($.browser.msie)
		return("msie");
	else
	if($.browser.opera)
		return("opera");
	else
	if($.browser.safari)
		return("safari");
	else
		return("unknown");
}

function showImage(path,width,height,caption){
	$("#show-content").hide();
	$("#show-loading").show();
	$("#show-content").attr('style','');
	$("#show-content").hide();
	$("#show-loading").show();
	var pagesize=getPageSize();
	if(width>pagesize[0])
	{
		var factor=pagesize[0]/width;
		width*=factor;
		height*=factor;
	}
	else
	if(height>pagesize[1])
	{
		var factor=pagesize[1]/height;
		width*=factor;
		height*=factor;
	}
	sWidth = width;
	sHeight = height;
	$("#show-image").attr('src','');
	$("#show-image").attr('src',path);
	$("#show-caption").html(caption);
	$("#show").fadeIn('slow');
};

function getPageSize(){
	var viewportwidth;
	var viewportheight;
	if (typeof window.innerWidth != 'undefined')
	{
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}
	else if (typeof document.documentElement != 'undefined'
	&& typeof document.documentElement.clientWidth !=
	'undefined' && document.documentElement.clientWidth != 0)
	{
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}
	else
	{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return(new Array(viewportwidth,viewportheight));
};

function ShowPopup(text){
	$("#popup-content").html(text);
	$("#popup").fadeIn("slow");
};

function WriteComment(post){
	$("#comm_new_"+post).slideDown('slow');
};

function LoadPage(page)
{
	var p = page.toUpperCase();
	document.title = 'XenoN Core | '+p+' | The Engine Of Fourth Dimension';
	$("#loading").fadeIn("fast",function(){
		$("#content").fadeOut("slow",function(){
			$.ajax({
				method: "get",
				url: "pages/"+page+".php",
				success: function(html){
					$("#content").html(html);
					Page = page;
					$("#content").fadeIn("slow",function(){
						$("#loading").fadeOut("fast");
					});
				}
			});
		});
	});
}

function AddComment(post){
	if($("input#form_comm_name").val()==''||$("textarea#form_comm_comment").val()=='')
	alert('Name and Comment fields cannot be empty!');
	else
	$("#loading").fadeIn("fast",function(){
		$.post(
			"pages/commentadd.php",
			{
				"id": post,
				"name": $("input#form_comm_name").val(),
				"email": $("input#form_comm_email").val(),
				"website": $("input#form_comm_website").val(),
				"comment": $("textarea#form_comm_comment").val()
			},
			function(html){
				$("#loading").fadeOut("fast",function(){
					LoadPage(Page);
					ShowPopup('<span style="font-size: 18px;">Your comment is waiting for acceptation</span><br /><br /><span style="font-size: 14px;">(Click anywhere to close this message)</span>');
				});
			}
		);
	});
	$("#comm_new_"+post).slideUp('slow');
};

function ShowComments(post){
	$("#comm_show_"+post).html('Click to reload');
	$("#loading").fadeIn("fast",function(){
		$("#comm_"+post).fadeOut("slow",function(){
			$.ajax({
				method: "GET",
				url: "pages/commentslisting.php?id="+post,
				success: function(html){
					$("#comm_"+post).html(html);
					$("#comm_"+post).fadeIn("slow",function(){
						$("#loading").fadeOut("fast");
					});
				}
			});
		});
	});
};

$(document).ready(function(){
	
	var browser = getBrowser();
	var pagesize = getPageSize();
	
	$("#menu-general").hover(
		function(){$(this).addClass("menu-general-active")},
		function(){$(this).removeClass("menu-general-active")}
	);
	
	$("#menu-download").hover(
		function(){$(this).addClass("menu-download-active")},
		function(){$(this).removeClass("menu-download-active")}
	);
	
	$("#menu-gallery").hover(
		function(){$(this).addClass("menu-gallery-active")},
		function(){$(this).removeClass("menu-gallery-active")}
	);
	
	$("#menu-resources").hover(
		function(){$(this).addClass("menu-resources-active")},
		function(){$(this).removeClass("menu-resources-active")}
	);
	
	$("#menu-support").hover(
		function(){$(this).addClass("menu-support-active")},
		function(){$(this).removeClass("menu-support-active")}
	);
	
	$("#menu-general").click(function(){
		LoadPage('general');
	});
	
	$("#menu-download").click(function(){
		LoadPage('download');
	});
	
	$("#menu-gallery").click(function(){
		LoadPage('gallery');
	});
	
	$("#menu-resources").click(function(){
		LoadPage('resources');
	});
	
	$("#menu-support").click(function(){
		LoadPage('support');
	});
	
	$("#site").click(
		function(){window.location="http://xenon.psichix.com";}
	);
	
	$("#author").click(function(){
		window.location="http://psichix.com";
	});
	
	$("#popup").click(function(){
		$(this).fadeOut("slow");
	});
	
	$("#show").click(function(){
		$(this).fadeOut("slow");
		$("#show-image").attr('src','');
	});
	
	$("#content").load("pages/general.php");
	Page = 'general';
	
	$("#loading-content").width(100);
	$("#loading-content").height(100);
	$("#loading-content").offset({left: pagesize[0]/2-50,top: pagesize[1]/2-50});
	$("#loading").hide();
	$("#popup").hide();
	$("#show-image").load(function(){
		$("#show-content").width(sWidth);
		$("#show-content").height(sHeight);
		$("#show-content").offset({left: (pagesize[0]/2)-(sWidth/2)+$(document).scrollLeft(),top: (pagesize[1]/2)-(sHeight/2)+$(document).scrollTop()});
		$("#show-image").attr('width',sWidth);
		$("#show-image").attr('height',sHeight);
		$("#show-loading").fadeOut("slow");
		$("#show-content").fadeIn("slow");
	});
	$("#show-loading").width(100);
	$("#show-loading").height(100);
	$("#show-loading").offset({left: pagesize[0]/2-50,top: pagesize[1]/2-50});
	$("#show-content").hide();
	$("#show").hide();

});
