$(document).ready( function()
{
	$(".active_menu").css( "opacity", 0.00 );
	$(".banner").click( activateHome );

	loadStaticTree();
	
	$(window).bind("resize", function(e) {
		$("#sidebar").width( $("#sidebar ul").width()-10 );
		$("#content").width( $(".content_panel").width()-$("#sidebar ul").width()-60 );
	});
	
	if (window.location.hash != "")
	{
		page_name = window.location.hash.substr(1,window.location.hash.length);
	}

	$.tree_reference("sidebar").select_branch("#"+page_name+"[active_button_image]"); 
	
	if (admin_mode)
	{
		$("#edit_current_page_link").click(function()
		{
			window.location.href = "/cms/"+page_name+"/admin/edit/page/";
			return false;
		});
		$("#edit_current_menu_link").click(function()
		{
			window.location.href = "/cms/"+page_name+"/admin/edit/menu/";
			return false;
		});
	}
	
	for (tip in tooltips)
	{
		$(tip).qtip({
			content: tooltips[tip],
			show: "mouseover",
			hide: "mouseout",
			position: {
				corner: {
					target: "bottomRight",
					tooltip: "topRight",	
				},
				adjust: {
					screen: true,
					resize: true,
				},
			},
			style: {
				background: '#FFFFFF',
				textAlign: 'left',
				width: {
					max: 350,
				},
				border: {
					width: 2,
					color: "#2C2907",
				},
				color: "#837B17",
//				fontWeight: "bold",
				fontFamily: "arial",
				fontSize: 13,
				
			},
		});

	}


});	

function loadStaticTree()
{
	$(".sidebar").tree({
			ui : { theme_path: "/static/js/jsTree/source/themes/", theme_name : "pc" },
			callback : {
				onselect : function(NODE,TREE_OBJ) { treeSelect($(NODE).attr("id")); },
				onload : function(TREE_OBJ) { buildTopMenus(TREE_OBJ); }
			}
    });
}

function activateHome( event ) 
{
	$(".menu_item").fadeTo( "fast", 1.00 );	
	$(".active_menu").fadeTo( "fast", 0.00 );
	$(".active_menu").attr("active_menu_id", "home");
	$.tree_reference("sidebar").select_branch("#home");
	return false;

}

function buildTopMenus( tree ) 
{
	$(".menu_item").click(function()
	{
		var id = $(this).attr("id");
		$.tree_reference("sidebar").select_branch($("#"+id + "[active_button_image]"));
	});

}

function activateMenu( id ) 
{		
	$(".menu_item").fadeTo( "fast", 1.00 );	//fade in all menu items
		
	if (id != "home") 
	{
		var current = $("#"+id+"[active_button_image]");
		while ($.tree_reference("sidebar").parent(current).attr("id") != "home")
		{
			current = $.tree_reference("sidebar").parent(current);
		}
		var currentId = current.attr("id");
	
		var active_menu_item = $("#"+currentId+"[class='menu_item']");	//get the menu we're activating
		active_menu_item.fadeTo("fast", 0.40);	//fade in the active menu
		
	
		$(".active_menu").fadeTo( "fast", 0.00, function() { 
			$(".active_menu").css( "backgroundImage", "url(" + $("#"+currentId+"[active_button_image]").attr("active_button_image")+")");
			$(".active_menu").text( active_menu_item.text() );	//update the text on the active item
			$(".active_menu").attr("active_menu_id", currentId);
		} );
		$(".active_menu").fadeTo( "fast", 1.00 );
	}
	else
	{
		$(".active_menu").attr("active_menu_id", id);
	}



}

function treeSelect( id )
{
	
	if (admin_mode && page_name != id)
	{
		window.location.href = "/cms/"+id+"/admin/";	
	}
	
	var tree = $.tree_reference("sidebar");
	
	var current = $("#" + id + "[active_button_image]");
	var title = [current.find(" a:first").text()];
	while( current.attr("button_image") == "" ) {
		current = $.tree_reference("sidebar").parent( current )
		title.push(current.find("a:first").text());
	}


	if( $(".active_menu").attr("active_menu_id") != current.attr("id") ) {
		activateMenu( current.attr("id") );
	}
	
	tree.open_branch($("#"+id+"[active_button_image]"));

	if( $(".content").attr("loaded_content_id") != id) {
		loadContent( id );
		setTitle( title.reverse() );
	}
	

//	$("#sidebar").width( $("#sidebar ul").width()-10 );
//	$("#content").width( $(".content_panel").width()-$("#sidebar ul").width()-60 );

}

function setTitle( items ) 
{
	var newTitle = "PrintsCharming.com | ";
	for( i=0; i<items.length; i++ ) {
		newTitle += items[i];
		if( i< items.length-1 ) {
			newTitle += " > ";
			}
	}
	document.title = newTitle;

}

function loadContent( id ) 
{
	$(".content").metadata().id = id;
	$(".content").html( "<img class='load_spinner' src='/static/images/ajax-loader.gif'>" );
	$(".content").css("height", "800px" );
	var contentWidth = $("#content").width();
	var contentHeight = $("#content").height();
	var spinnerWidth = $(".load_spinner").width();
	var left = Math.round( (contentWidth/2)-(spinnerWidth/2) );
	$(".load_spinner").css("left", left+"px" );
	$(".load_spinner").css("top","200px");
	
	ajax_url = "/cms/"+id+"/ajax/";
	$.ajax( {
		type: "POST",
		url: ajax_url,
		cache: false,
		dataType: "json",
		success: function(data, status) {
			handleLoadSuccess(data, status, {"id": id});
		}
	} );	
	
}

function handleLoadSuccess(data, status, userData)
{
	$(".content").css("height","");
	$(".content").attr("loaded_content_id", data[0]["fields"]["slug"]);
	$(".content").html(data[0]["fields"]["published_html"]);
	page_name = data[0]["fields"]["slug"];
	window.location.hash = data[0]["fields"]["slug"];
	if (userData["id"] != data[0]["fields"]["slug"])
	{
		$.tree_reference("sidebar").select_branch("#"+data[0]["fields"]["slug"]+"[active_button_image]");
	}		
		
}






