jQuery(document).ready(
	function()
	{
		/*==================================================
			Variables
			---
			Initiated variables for later use in code
		--------------------------------------------------*/
		
		/*==================================================
			Replace menulogin in /site
			---
		--------------------------------------------------*/
		try{
			var url = root_url + refresh_header_login;
			jQuery.get(url, null, function(data){
				jQuery("#menuLogin").empty().append(data);
				if(jQuery('[name=username]', jQuery('[name=loginForm]')[0]).length){
					jQuery('[name=username]', jQuery('[name=loginForm]')[0])[0].onkeyup = function(){ 
						showOrHideRememberCheckBox();
					}
				}
			});
		}catch(ex){}
		
		/*==================================================
			Gets news from CMS Made Simple
			---
		--------------------------------------------------*/
		try{
			if(jQuery("#start_news").length){
				jQuery.get(start_news_url, null, function(data){
					try{
						var holder = jQuery("#start_news")[0];
						holder.innerHTML = data.toString();
						setTimeout(function(){
							initSIFR();
						}, 50);
					}catch(ex){}
						
					/*==================================================
						Curvy corners
						---
						Rounded corners for the logged in
						user initial page
					--------------------------------------------------*/
					jQuery(".contentPod_314").corner({
						tl: { radius: 5 },
						tr: { radius: 5 },
						bl: { radius: 5 },
						br: { radius: 5 },
						antiAlias: false,
						autoPad: true,
						validTags: ["div"]
					});
				});
			}
		}catch(ex){}
		
		
		/*==================================================
			Gets poll from CMS Made Simple
			---
		--------------------------------------------------*/
		try{
			jQuery.get(poll_url, null, function(data){
				try{
					var holder = jQuery("#poll1 .changeableContent")[0];
					holder.innerHTML = data.toString();
					try{
						var js_src = jQuery("script", holder)[0].src;
						jQuery("script", holder).remove();
						var element = document.createElement("script");
						element.type = "text/javascript";
						element.src = js_src;
						holder.appendChild(element);
					}catch(ex){}
					
					/*==================================================
						Curvy corners
						---
						Rounded corners for the logged in
						user initial page
					--------------------------------------------------*/
					/*
					jQuery(".contentPod_314").corner({
						tl: { radius: 5 },
						tr: { radius: 5 },
						bl: { radius: 5 },
						br: { radius: 5 },
						antiAlias: false,
						autoPad: true,
						validTags: ["div"]
					});
					*/
				}catch(ex){}
			});
		}catch(ex){}
		
		/*==================================================
			Rembember me
			---
			Add onkeyup for username
		--------------------------------------------------*/
		try{
			jQuery('[name=username]', jQuery('[name=loginForm]')[0])[0].onkeyup = function(){ 
				showOrHideRememberCheckBox();
			}
		}catch(ex){ }
		
		/*==================================================
			Menu
			---
			Select current menu
		--------------------------------------------------*/
		try{
			if(selected_menu != undefined && selected_menu != ""){
				var found = false;
				var matching = selected_menu.replace(' ', '').toLowerCase().replace('ä', 'a').replace('å', 'a').replace('ö', 'o');
				matching = jQuery.trim(matching);
					
				// Search based on link href 
				var elements = jQuery('#mainmenu a');
				for(var i=0; i<elements.length; i++){
					var element = elements[i];
					if(element.href != undefined){
						var template = jQuery.jqURL.get('template', null, element.href);
						if(template) template = template.replace(' ', '').toLowerCase();
						if(template == matching){
							var parent = element.parentNode;
							jQuery(parent).addClass('selected');
							found = true;
						}
					}
				}
				// Search based on link label
				if(!found){
					var elements = jQuery('#mainmenu a, .sectionheader span');
					for(var i=0; i<elements.length; i++){
						var element = elements[i];
						var label = element.innerHTML.replace(' ', '').toLowerCase().replace('ä', 'a').replace('å', 'a').replace('ö', 'o');
						label = jQuery.trim(label);
						
						if(label.toString() == matching.toString()){
							var parent = element.parentNode;
							if(parent.nodeName.toLowerCase() == 'li') jQuery(parent).addClass('selected');
							else jQuery(parent.parentNode).addClass('selected');
						}
					}
				}
			}
			//alert(jQuery.jqURL.get('menu'));
			
		}catch(ex){ }
		
		/*==================================================
			ToolTip
			---
			ToolTip for usernames
		--------------------------------------------------*/
		try{
			jQuery('.avatar').tooltip({
				delay: 200,
				showURL: false,
				bodyHandler: function() {
					if(this.avatar_url != undefined){
						return '<img src="'+this.avatar_url+'" width="75" alt="" />';
					}
				}
			});
		}catch(ex){}
		

		/*==================================================
			Searchbox
			---
			Toggle size and value
		--------------------------------------------------*/
		jQuery('#header_searchBox').focus(function(){
			jQuery(this).animate({width: "150px"}, 100);
		});
		jQuery('#header_searchBox').blur(function(){
			if(this.empty){
				jQuery(this).animate({width: "50px"}, 100);
			}
		});	
		
		/*==================================================
			Tabs
			---
			Fake tabs, load and other one and the whole
			things stop working, must find an other
			way to program this
		--------------------------------------------------*/
		jQuery('#tabs #one').click(function(){			
			jQuery('#poll1').show();
			jQuery('#poll2').hide();
		});
		jQuery('#tabs #two').click(function(){			
			jQuery('#poll2').show();
			jQuery('#poll1').hide();
		});
		
		/*==================================================
			Download - Extra information
			---
			This is called when a user want's to see more
			information in the download section.
		--------------------------------------------------*/
		try{
			jQuery(".btnShow").click(function(){
				jQuery(this).next().slideToggle("fast");
			});
		} catch(err){ }
		
		/*==================================================
			Whitebox
			---
			Function initiation for the rounded corners
			of the boxes that hold the information
		--------------------------------------------------*/
		try{ initWhiteBoxes(); } catch(err){ }
		
		try{ initFormFields(); } catch(err){ }
		
		/*==================================================
			Add to friends
			---
			Toggle script to add friends
		--------------------------------------------------*/
		jQuery('#confirmation').click(function(){
			jQuery('#confirmation').toggle("fast");
		});
		/*==================================================
			Remove account
			---
			Toggle script to remove account
		--------------------------------------------------*/
		jQuery('#addToFriends').click(function(){
			jQuery('#addFriendInterface').toggle("fast");
		});
		
		/*==================================================
			Lightbox
			---
			Activate lightbox
		--------------------------------------------------*/
		try { 
			var lightBoxs = jQuery('[class*=lightBox]');
			for(var i = 0; i<lightBoxs.length; i++){
				jQuery('a', lightBoxs[i]).lightBox();	
			}
		} catch(err){ }	
		
		try { 
			jQuery('.lightAlbum a').lightBox();
		} catch(err){ }
		
		
		
		/*==================================================
			Form
			---
			Reset form values (FireFox issue)
		--------------------------------------------------*/
		var form = jQuery('form');
		for(var i=0; i<form.length; i++){
			try {
				form[i].reset();
			}
			catch(e) {}
		}
		  
		/*==================================================
			Draggable
			---
			Make element draggable
		--------------------------------------------------*/
		try{
			jQuery('.draggable').easydrag(true);
		}catch(ex){ }
		
		
		try{
			jQuery.history.init(searcher.anchorCallback);
			jQuery("a[@rel='history']").click(function(){
				jQuery.history.load(this.href.replace(/^.*#/, ''));
				return false;
			});
			
		}catch(ex){ }
		/*==================================================
			Submenu
			---
			Submenu handler
		--------------------------------------------------*/
		try {
			var subMenuTotal = jQuery(".mainMenu").find('ul > li > ul');
			var MM_link = jQuery(".mainMenu").find("ul > li > .sectionHeader");
			var submenu = MM_link.parent().find('ul');
			
			// Hides the whole submenu
			subMenuTotal.hide();
			
			// Makes sure the user can not click
			MM_link.click(function(){ return false; });
			
			initDropDown();
			
		}catch(err){}
		
		/*==================================================
			Skinning
			---
			Wrap all submits inside proper divs
		--------------------------------------------------*/	
	    try { 
	    	if (can_skin_form_elements === true) {
			  (new SkinFormElements()).skinElementType('select', 'holder');
	    	}
	    } catch (ex) { }		
	}
);

function initDropDown(){
	var menu = document.getElementById('mainmenu');
	if(menu){
		var li = menu.getElementsByTagName('li');
		for(var i=0; i<li.length; i++){
			var ul = li[i].getElementsByTagName('ul');
			if(ul.length == 1){
				li[i].onmouseover = function(){ openSubMenu(this) };
				li[i].onmouseout = function(){ closeSubMenu(this) };
			}
		}
	}
}
function openSubMenu(target){
	if(target){
		var ul = target.getElementsByTagName('ul');
		if(ul.length == 1){
			var t = ul[0];
			ul[0].style.display = 'block';
		}
	}
}
function closeSubMenu(target){
	if(target){
		var ul = target.getElementsByTagName('ul');
		if(ul.length == 1){
			var t = ul[0];
			ul[0].style.display = 'none';
		}
	}
}
function showOrHideRememberCheckBox(){
	/*==================================================
		Login
		---
		Login form at the top
	--------------------------------------------------*/
	try{
		var username = jQuery('[name=username]', jQuery('[name=loginForm]')[0])[0];
		var password = jQuery('[name=password]', jQuery('[name=loginForm]')[0])[0];
		
		if(utilities.hasDefaultValue(username)){			
			jQuery('.remember').slideUp(200);	
		}else{
			jQuery('.remember').slideDown(200);	
		}
	}catch(ex){
		
	}
}
function initFormFields(){
	try{
		var fields = jQuery("input");
		for(var i = 0; i<fields.length; i++){
			var field = fields[i];
			if(field.type.toLowerCase() == "text"){
				if(field.first == undefined) field.first = true;
				try{
					if(field.onfocus != undefined && field._existing_on_focus === undefined && field.first == true) field._existing_on_focus = field.onfocus;
				}catch(ex){}
				field.first = false;
				
				field.default_value = field.value;
				field.onfocus = function(){ 
					this.select(); 
					if(!this.first && this._existing_on_focus != undefined) this._existing_on_focus();
				}
				field.changed = function(){
					if(this.value != this.default_value) return true;
					else return false;
				}
				field.empty = function(){
					var value = jQuery.trim(this.value);
					if(value == "") return true;
					else return false;
				}
			}
		}	
	}catch(ex){
		if(debug) alert("Fel i initFormFields: "+ex);
	}
}












