/*-----------------------------------------------------------------------------------------------

 tandberg arkitekter mnal 2008
 website by eDIR/ ee (Espen Schjelderup, Espen Hofsvang) © Copyright 2008

 cookie scripts: let them eat cake!

-----------------------------------------------------------------------------------------------*/

$(document).ready(function() {

/*-----------------------------------------------------------------------------------------------
 bind scrolltables to cookie
-----------------------------------------------------------------------------------------------*/
	$('#pt1').tablesorter({
		widgets: ['cookie']
	});
	$('#pt2').tablesorter({
		widgets: ['cookie']
	});
	$('#pt3').tablesorter({
		widgets: ['cookie']
	});
});

/*-----------------------------------------------------------------------------------------------
 kakebakerscript
-----------------------------------------------------------------------------------------------*/
var kake = {
	starte: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0; i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	bake: function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else var expires = "";
		document.cookie = name + "=" + value + expires + "; path=/";
		this[name] = value;
	}
};
kake.starte();

/*-----------------------------------------------------------------------------------------------
 jquery cookie plugin
-----------------------------------------------------------------------------------------------*/
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        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(); // use expires attribute, max-age is not supported by IE
        }
        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 { // only name given, get cookie
        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]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/*-----------------------------------------------------------------------------------------------
 cookiejar * used to store objects, arrays or multiple values in one cookie, under one name
-----------------------------------------------------------------------------------------------*/
(function($) {
    $.cookieJar = function(name, options) {
        if (!$.parseJSON) return false;
        if (!$.toJSON) return false;
        if (!$.cookie) return false;
        return new function() {

            function log(s) {
                if (typeof console != 'undefined' && typeof console.log != 'undefined') {
                    console.log('cookiejar:' + self.cookieName + ' ' + s);
                } else {
                    alert(s);
                }
            };

            function save() {
                if (self.options.debug) log('save ' + $.toJSON(self.cookieObject));
                return $.cookie(self.cookieName, $.toJSON(self.cookieObject), self.options.cookie);
            };

            function load() {
                var cookieJSON = $.cookie(self.cookieName);
                if (typeof cookieJSON == 'string') {
                    if (self.options.debug) log('load ' + cookieJSON);
                    self.cookieObject = $.parseJSON(cookieJSON, true);
                } else {
                    if (self.options.debug) log('load new');
                    self.cookieObject = {};
                    save();
                }
            }

            this.set = function(name, value) {
                if (self.options.debug) log('set ' + name + ' = ' + value);
                self.cookieObject[name] = value;
                return save();
            };

            this.get = function(name) {
                if (!self.options.cacheCookie) {
                    load();
                }
                if (self.options.debug) log('get ' + name + ' = ' + self.cookieObject[name]);
                return self.cookieObject[name];
            };

            this.remove = function(name) {
                if (self.options.debug) log('remove ' + name);
                if (typeof name != 'undefined') {
                    delete(self.cookieObject[name]);
                } else {
                    self.setFromObject({});
                }
                return save();
            };

            this.setFromObject = function(object) {
                if (typeof object == 'object') {
                    if (self.options.debug) log('setFromObject');
                    self.cookieObject = object;
                    return save();
                }
            };

            this.toObject = function() {
                if (self.options.debug) log('toObject');
                return self.cookieObject;
            };

            this.toString = function() {
                if (self.options.debug) log('toString = ' + $.toJSON(self.cookieObject));
                return $.toJSON(self.cookieObject);
            };

            this.destroy = function() {
                if (self.options.debug) log('destroy');
                self.cookieObject = {};
                return $.cookie(self.cookieName, null, self.options.cookie);
            };

            this.construct = function(name, options) {
                self.options = $.extend({
                    cookie: {
                        expires: 365
                    },
                    cacheCookie:    true,
                    cookiePrefix:   'grid',
                    debug:          false
                }, options);

                self.cookieName     = self.options.cookiePrefix + name;
                load();
                return self;
            };

            var self = this;
            self.construct(name, options);
        };
    };
})(jQuery);

/*-----------------------------------------------------------------------------------------------
 tablesorter cookie
-----------------------------------------------------------------------------------------------*/
(function($) {
    $.tablesorter.addWidget({
        id: 'cookie',
        format: function(table) {
            var sortList = table.config.sortList;
            var tablesorterCookieJar = $.cookieJar('organiser', {
                cookie: {
                    path: '/'
                }
            });
            if ( sortList.length > 0) {
                tablesorterCookieJar.set($(table).attr('id'), sortList);
            } else {
               var sortList = tablesorterCookieJar.get($(table).attr('id'));
               if (sortList && sortList.length > 0) {
                    jQuery(table).trigger('sorton', [sortList]);
               }
            }
        }
    });
})(jQuery);
