// JavaScript Document
$(init);
function init()
{
	$(".videoLink").colorbox();
	if(selPG=="")
		populateMenu("");
	else
	{
		arrayCrt = new Array();
		arrayCrt[0] = 'node_'+selPG;
		populateMenu(arrayCrt);
	}
	$("div#page span").each
	(
	 	function()
		{
			$(this).css({"font-size":"1em"});
			$(this).css({"font-family":"Arial, Helvetica, sans-serif"});
		}
	 );
	//Change font size
	$("#aFontLarge").click
	(
		function(e)
		{
			e.preventDefault();
			$(this).removeClass("lang");
			$(this).addClass("lang_sel");
			$("#aFontSmall").removeClass("lang_sel");
			$("#aFontSmall").addClass("lang");
			$("body").css({"font-size":"12px"});
		}
	);
	$("#aFontSmall").click
	(
		function(e)
		{
			e.preventDefault();
			$(this).removeClass("lang");
			$(this).addClass("lang_sel");
			$("#aFontLarge").removeClass("lang_sel");
			$("#aFontLarge").addClass("lang");
			$("body").css({"font-size":"10px"});
		}
	);
	//Search
	$("#aSearch").click
	(
	 	function()
		{
			$("#input_search").attr("value",jQuery.trim($("#input_search").attr("value")));
			if($("#input_search").attr("value")!="")
				$("#formSearch").submit();
		}
	 );
	//Contact
	$('div.form form').ajaxForm({success: showFormResponse});
	//Lightbox
	$('code').each(function(){
		$(this).html($.trim($(this).html()));
	});
	
	$('.lightbox').lightbox({
		fileLoadingImage: 'images/lightbox/loading.gif',
		fileBottomNavCloseImage: 'images/lightbox/closelabel_'+SITE_LG+'.gif'
	});
}
function populateMenu(openedNode)
{
	$('#tree').tree
	(
	    {
            data : 
            {
                type : 'json',
                async : true,
                opts : 
                {
                    method : "POST",
                    url : "common/menu.php"
                }
            },
            ui : 
            {
			    theme_name : 'abmenu',
			    dots:false,
			    animation : 400
			},
			selected: openedNode,
			callback :
            {
                onselect : function(NODE,TREE_OBJ)
				{
					if($(NODE).hasClass("leaf"))
					{
						var url = $(NODE).attr("id").replace("node_","");
						if(url!="home")
							newLocation = "page.php?pg="+url;
						else
							newLocation = "index.php";
						currentLocation = document.location+"";
						if(currentLocation.indexOf(newLocation)==-1)
							document.location = newLocation;
					}
					else
					{
						TREE_OBJ.toggle_branch.call(TREE_OBJ, NODE);
					}
				    $('#'+$('#node_'+getParameterByName('pg')).attr("id")+" a:first-child").addClass("current");
				    $('#'+$('#node_'+getParameterByName('pg')).attr("id")+" li a").removeClass("current");
				}
			},
            types :
			{
				"default" :
				{
					icon :
					{
						image : "none",
						position:false
					},
					draggable:false
				},
				"level1" :
				{
					icon :
					{
						image : "images/bullet_level1.jpg",
						position:false
					},
					draggable:false
				},
				"level2" :
				{
					icon :
					{
						image : "images/bullet_level2.jpg",
						position:false
					},
					draggable:false
				},
				"level3" :
				{
					icon :
					{
						image : "images/bullet_level3.jpg",
						position:false
					},
					draggable:false
				}
			}
		}
    );
}

function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
//------------------------------------------------------------------------------------------------------
//Form response
function showFormResponse(responseText, statusText) 
{
	if(statusText=='success')
	{
		if(responseText!="<success/>")
		{
			//show error
			$('.success').css({'display':'none'});
			$('.error').css({'display':'block'});
			$('dd#dd_error').html(responseText);
		}
		else
		{
			//show success
			$('.error').css({'display':'none'});
			$('.success').css({'display':'block'});
			$('div.form .text').attr("value","");
		}
	}
}
