$(document).ready(function() {
	
	var widthLimit = 1024;
	var heightLimit = 768;
	var extraHeight = 80;
	var openChild = false;
	var childClick = 0;
	
	function doResize(){
		var pWidth = $(window).width();
		var pHeight = $(window).height();
		
		var docLarger = (pWidth < widthLimit) ? true : false;
		var width = (docLarger) ? pWidth : widthLimit;
		var height = (docLarger) ? pHeight : heightLimit;
		
		var leftAndRight = (Math.ceil((pWidth-widthLimit)/2))-10;
		
		$("#container").css("width",width).css("height",height);
		
		if($("#home_img").length){
			$("#home_img").css("width",width).css("height",height);
		}
		
		$(".cycle_img").each(function(){
			$(this).css("width",width).css("height",height);
		});
		
		if(pWidth >= (widthLimit + 20)){
			$('#ds_left,').css('left',leftAndRight).css('height',height);
			$('#ds_right').css('right',leftAndRight).css('height',height);
			$('#content_wrap').css('right',leftAndRight+41);
		} else {
			$('.ds').css("display","none");
			$('#content_wrap').css('right',leftAndRight+28);
		}
	}
	
	doResize();
	
	$("#nav_container").animate({
		top: '0px'
	});
	
	if($("#home_img").length){
		setTimeout(function(){
			$("#home_img").fadeOut(2000);
			$("#content_wrap").fadeIn(2000);
		},4000);
	}
		
	$(window).resize(function(){
		doResize();
	});

	$("#main_nav > li,#main_nav2 > li").click(function(){
		if(((openChild != false && $(this).parent().parent().attr("id") != openChild) || openChild == false) && childClick == false){
			if(openChild != false && openChild == $(this).attr("id")){
				$(this).children("ul").fadeOut("fast");
				openChild = false;
			} else {
				if(openChild != false){
					$("#"+openChild).children("ul").fadeOut("fast");
				}

				$(this).children("ul").fadeIn("fast");
				openChild = $(this).attr("id");
			}

			if(openChild != false && $(this).parent().parent().attr("id") == openChild){
				childClick = true;
			}
		}
	});

	$("#content_wrap").draggable({
		handle: "#content_hd",
		container: "window",
		start: function(){
			$("#content_logo").css("display","none");
			$("#content").css("display","none");
		},
		stop: function(){
			$("#content").css("display","block");
			$("#content_logo").css("display","block");
		}
	});

	$("#content_close").click(function(){
		$("#content_wrap").css("display","none");
	});
	
	Preloader();
	
});

function getPage(slug,children){
	
	pageTracker._trackPageview("/"+slug);
	
	if(children == "false"){
		
		$.post("../get-page.php",{slug:slug},function(data){
			var data = data.split("#jsvar#");
			if(data[4] != "no-page-extras"){
				var content = data[1]+data[4];
			} else {
				var content = data[1];
			}
			$("#content_hd").html("<h1>"+data[0]+"</h1>");
			$("#content").html(content);
			$("#content_wrap").css("position","absolute");
			
			if(data[2] != "no-carousel" && data[3] != curCarousel){
				
				$("#image_cycle").append(data[2]);
				$(".cycle_img_old").fadeOut(2000);
				$(".cycle_img_new").fadeIn(2000);
				
				setTimeout(function(){
					$(".cycle_img_old").remove();
					$(".cycle_img_new").removeClass("cycle_img_new").addClass("cycle_img_old");
				},2000);
				
				curCarousel = data[3];
				
			}
			$("#content_wrap").fadeIn(2000);
			
		});
	} else {
		$("#content_wrap").css("display","none");
	}
	
	return false;
}