$(document).ready(function() {
	
	Session.load();
  
	$('#navigation > ul > li > a[href="#"]').click(function(e) {
		e.preventDefault();
				
		var ul = $(this).siblings('ul');

		// Save node state into cookie on click.
		var state = ul.css('display') != 'none' ? 0 : 1;
		Session.set(this.parentNode.id, state);

		ul.slideToggle('fast');

	}).each(function() {

		// Restore node state from cookie on page load.
		var state = Session.get(this.parentNode.id);

		$(this).siblings('ul').css('display', state == 1 ? 'block' : 'none');
		
	});
	
	/*$("#tribalButton").mouseover(function() {
		$("#tribalButton").attr('src','/images/tribalButton_f2.gif');	
	});
	
	$("#tribalButton").mouseout(function() {
		$("#tribalButton").attr('src','/images/tribalButton.gif');	
	});*/
	
	/*$("#referenssit .references").mouseover(function () {
		$("#" + this.id + " .project_header").show();
		$("#" + this.id + " .project_reference").css("border", "1px solid #666");
		$("#" + this.id + " .project_header").css("margin-top", "-25px");
		$("#" + this.id + " .project_thumb").css("margin-top", "0px");
		$("#" + this.id + " .project_thumb").css("margin-left", "0px");
	    $("#" + this.id + " .project_reference").css("margin-bottom", "0px");
	});
	
	$("#referenssit .references").mouseout(function () {
		$("#" + this.id + " .project_header").hide();
		$("#" + this.id + " .project_header").hide();
		$("#" + this.id + " .project_reference").css("border", "0px");
		$("#" + this.id + " .project_header").css("margin-top", "0px");
		$("#" + this.id + " .project_thumb").css("margin-top", "1px");
		$("#" + this.id + " .project_thumb").css("margin-left", "1px");
	    $("#" + this.id + " .project_reference").css("margin-bottom", "1px");
	});*/

	
});


/**
 * Simple session abstraction.
 * Usage:
 * Session.load()             Restore internal state from cookie
 * Session.set(key, value)    Save key/value pair into cookie
 * Session.get(key)           Get value for key
 */
var Session = {
	
	params: {},
	cookie_name: 'session',
	
	load: function() {
		var cookie = $.cookie(this.cookie_name);
		if (cookie) {
			var pairs = cookie.split('&');
			$.each(pairs, function() {
				var p = this.split('=');
				Session.params[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
			});
		}
	},
	
	save: function() {
		var pairs = [];
		for (key in this.params)
			pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(this.params[key]));
		if (pairs.length > 0)
			$.cookie(this.cookie_name, pairs.join('&'), { expires: 30, path: '/' });
	},
	
	set: function(key, val) {

		this.params[key] = val;
		this.save();
	},
	
	get: function(key, default_value) {
		return this.params[key] != undefined ? this.params[key] : default_value;
	}
}





/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000))}else{date=options.expires}expires='; expires='+date.toUTCString()}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('')}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break}}}return cookieValue}};


/*
 * Copyright (c) 2006 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Addepted to select an option by Mathias Bank (http://www.mathias-bank.de)
 */

jQuery.fn.sortOptions = function(ascending, skip)
{
	var start_from = skip || 0;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			ascending = typeof ascending == "undefined" ? true : ascending;
			var optionsLength = this.options.length;
			var sortArray = [];
			for(var i = start_from; i<optionsLength; i++)
			{
				sortArray.push(
				{
					value: this.options[i].value,
					text: this.options[i].text
				});
			}
			sortArray.sort(
				function(option1, option2)
				{
					option1text = option1.text.toLowerCase();
					option2text = option2.text.toLowerCase();
					if(option1text == option2text) return 0;
					if(ascending)
					{
						return option1text < option2text ? -1 : 1;
					}
					else
					{
						return option1text > option2text ? -1 : 1;
					}
				}
			);
			for(var i = start_from; i<optionsLength; i++)
			{
				this.options[i].text = sortArray[i - start_from].text;
				this.options[i].value = sortArray[i - start_from].value;
			}
		}
	)
	return this;
}

