var Templates = new Array();
var Interval = null;
Template = ["","center.ejs", "two_col.ejs", "photo_gallery.ejs"];

$(window).load(function(){
	
	//Read the url
	Hash.read();
	
	
	if(Hash.hash.p != undefined){
		Page.go(true, Hash.hash.p);
	} else {
		Page.go(true, "index");
	}
	
	//Hide labels on forms
	Forms.addEvents();
	
	//Add functionality to the back button
	window.setInterval(Hash.check, 500);
	
	$("#top_nav li").hover(function(){
		
		if(!$(this).hasClass("active"))
			$("a", this).stop(true, false).animate({marginTop:-15}, 150)
			
	},function(){
		
		if(!$(this).hasClass("active"))
			$("a", this).stop(true, false).animate({marginTop:0}, 150);
		
	})
	
});

Page = {
	
	go : function(el, path){
		
		if(path == undefined)
			path = $(el).attr("href");
		
		if(path.indexOf(BASEURL, 0) == 0){
			L = BASEURL.length;
			path = path.substr(L);
		}

		Hash.set('p',path);
		
		API.query(path, function(json){
			
			if(json.link_to != ""){
				Page.go(true, json.link_to);
				return false;
			}
				
			var template = Template[json.template];
			
			html = new EJS({url: '/media/templates/'+template}).render(json);
			
			if(json.template == 3) {
				$("#fancybox").html(html);
				$.fancybox($("#fancybox"));
			} else {
				
				var title = Hash.hash.p.split("/")[0];
				if(title && title!= "") {
					$("#body_content").html('<h1 id="page_title" class="title-'+title+'">'+title+'</h1>').show();
					$("#body_content").append(html)
				} else {
					
				}

			}

				
			$("#body_content").animate({opacity : 1},250);

			$(".sidebar .menu").extendable();
			
			$(".sidebar a").unbind("click");
			$(".sidebar a").click(function(){
				Page.go(this);
				return false;
			});
			
			if($(".rotater"))
				$(".rotater").rotater();
				
			$(".content a").each(function(){
				if($(this).attr("target") == "_self")
				{
					$(this).bind("click", function(){
						Page.go(this);
						return false;
					})
				}
			})
			
			$(".index a img").hover(
			function(){
				$(this).fadeTo(250, 0.6)
			},
			function(){
				$(this).fadeTo(150, 1)
			})
			
		});

	}
	
}

API = {
	url : '/ohcms/page_api.php',
	query : function(path, callback){

		Loading.start();
		
		$("#body_content").animate({opacity : 0},250);
		
		var firstSlash = path.indexOf("/")
		
		if(firstSlash == -1)
			el = $("#nav-"+path);
		else {
			el = $("#nav-"+path.substr(0,firstSlash));
		}

		if(el != null){
			$(el).animate({marginTop:0}, 200);
			$("#top_nav li.active").removeClass("active");
			$(el).parent("li").addClass("active");
			$("body").removeClass();
			$("body").addClass(path);
		}

		setTimeout(function(){

		$.get(API.url, "path="+path, function(json){
			
			if(callback != undefined)
				callback(json);
				
			Loading.stop();

		}, 'json');

		}, 1000)


	}
}

/* Loading Icons */
Loading = {
	Interval : 0,
	pos : 0,
	
	loading : function(){
		Loading.pos = Loading.pos - 12
		
		if(Loading.pos < -48)
			Loading.pos = 0;
			
		bgPos = "0 " + Loading.pos + "px";
		$("#loading").css({backgroundPosition: bgPos});
	},
	
	start : function(){
		if(Loading.Interval == 0) {
			Loading.Interval = setInterval('Loading.loading()', 120);
			$("#loading").show().animate({opacity : 1},250)
		}
	},
	
	stop : function(){
		clearInterval(Loading.Interval);
		Loading.pos = 0;
		Loading.Interval = 0;
		$("#loading").css({backgroundPosition: '0 0'});
		$("#loading").animate({opacity : 0},250, function(){
			$(this).hide();
		})
	}
}

Forms = {
	
	addEvents : function(){
		
	$("input.text").each(function(){
	
		//Prevent the labels from showing if the user hits back
		if($(this).val() != "")
			$(this).prev("label").hide();
		
		$(this).focus(function(){
			
			$(".note").hide();
			
			if($(this).val() == "") {
				$(this).prev("label").fadeTo(50, 0.5);
				$(this).nextAll("label.note").show();
			}

		});
		
		$(this).keypress(function(){
			$(this).prev("label").hide();
		})
		
		$(this).blur(function(){
			
			$(this).nextAll(".note").hide();
			
			if($(this).val() == "") {
				$(this).prev("label").show().fadeTo(200, 1);
			} else {
				$(this).prev("label").hide();
			}
			
			$(this).removeClass("focus");
				
		});
		
	});
		
	$("label").mousedown(function(){
		return false;
	});
		
	}
	
}
