jQuery.cookie = function (key, value, options) {
    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


$(document).ready(function() {

	// Faster Browsing :-)
	$('#mainnav a, #subnav a').mousedown(function(event){
		event.preventDefault();
		window.location = $(this).attr('href');
	});
	
	// Enable change language  menu
	$('#langChangeBtn').click(function(event){
		event.preventDefault();
		$('#langChangeMenu').toggle(100);
		$('#loginDropDown').hide(50);
	});
	
	$('#en,#da').mousedown(function(event){
		event.preventDefault();
		var preferedLanguage = $(this).attr('id');
		var langLocation = $(this).attr('href');
		$.cookie('PreferedLanguage', preferedLanguage, { expires: 3650, path: '/'});
		window.location = langLocation;
	});
	
	// Enable login fields
	$('#login').click(function(event){
		event.preventDefault();
		var ddelem = $('#loginDropDown');
		var ddwidth = ddelem.outerWidth(false);
		var ddloginbtnwidth = $('#login').outerWidth(true) - 3;
		ddelem.css('margin-left', '-' + (ddwidth - ddloginbtnwidth)+ 'px');
		ddelem.toggle(100, function() {
			$('#ddusername').focus();
		});
		$('#langChangeMenu').hide(50);
	});

	// Change Frontend Content with buttons
		var fpButtons = $('#fp_btn_SimService, #fp_btn_SimOnline, #fp_btn_SimSecure, #fp_btn_BusinessAreas');
		var fprContent = $('#fp_simservice, #fp_simonline, #fp_simsecure, #fp_business_areas');
		fpButtons.click(function(event){
			event.preventDefault();

			fprContent.hide(0);

			switch ($(this).attr('id')) {

				case 'fp_btn_SimService':
					$('#fp_simservice').show();
				break;

				case 'fp_btn_SimOnline':
					$('#fp_simonline').show();
				break;

				case 'fp_btn_SimSecure':
					$('#fp_simsecure').show();
				break;

				case 'fp_btn_BusinessAreas':
					$('#fp_business_areas').show();
				break;

			}

			fpButtons.removeClass('active');
			$(this).toggleClass('active');

		});


	// Change SimOnline Content with buttons
		var soButtons = $('#so_btn_simo, #so_btn_unit, #so_btn_acti, #so_btn_alar, #so_btn_cons, #so_btn_user, #so_btn_repo, #so_btn_invo');
		var soContent = $('#so_simonline, #so_unit_fields, #so_activation, #so_alarms, #so_consumption_control, #so_users, #so_reports, #so_invoice');
		soButtons.click(function(event){
			event.preventDefault();

			soContent.hide(0);

			switch ($(this).attr('id')) {

				case 'so_btn_simo':
					$('#so_simonline').show();
				break;

				case 'so_btn_unit':
					$('#so_unit_fields').show();
				break;

				case 'so_btn_acti':
					$('#so_activation').show();
				break;

				case 'so_btn_alar':
					$('#so_alarms').show();
				break;

				case 'so_btn_cons':
					$('#so_consumption_control').show();
				break;

				case 'so_btn_user':
					$('#so_users').show();
				break;

				case 'so_btn_repo':
					$('#so_reports').show();
				break;

				case 'so_btn_invo':
					$('#so_invoice').show();
				break;
				
			}

			soButtons.removeClass('active');
			$(this).toggleClass('active');

		});

}); 
