﻿/*global jQuery console alert document escape unescape*/
"use strict";
jQuery.noConflict();

(function ($) {
	var window = this;
	 
	$.log = function (message) {
		if (window.console && window.console.debug) {
			console.debug(arguments);
		} else if (window.console && window.console.log) { 
			console.log(arguments);
		} else {
			alert(message);
		}
	};

	$.objectWalk = function (obj, func, args) {
		var i,
			ar = args ? args : [];

		if (typeof func !== "function") {
			throw new Error("file: custom.js, Error: " + func + " is not a function");
		}
		func.apply(obj, ar);

		for (i in obj) {
			if (obj[i] && typeof obj[i] === "object" && (!obj[i].length && obj[i].length !== 0)) {
				$.objectWalk(obj[i], func, ar);
			}
		}
	};

	$.runAll = function () {
		var that = this,
			obj;

		$.objectWalk(that, function () {
			var prop;
			if (this[arguments[0]]) {
				if (typeof this[arguments[0]] === "function") {
					this[arguments[0]]();
				} else if (typeof this[arguments[0]] === "object" && (!this[arguments[0]].length && this[arguments[0]].length !== 0)) {
					obj = this[arguments[0]];
					/* Problem at line nn character nn: Bad for in variable 'prop'. */
					for (prop in obj) {
						if (typeof obj[prop] === "function") {
							obj[prop]();
						}
					}
				}
			}
		}, [arguments[0]]);
	};

	$.runInit = function (obj) {
		$.runAll.apply(obj, ["init"]);
	};

	$.setCookie = function (c_name, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name + "=" + escape(value) +
		";domain=.sl.se;path=/" +
		((expiredays === null) ? "" : ";expires=" + exdate.toGMTString()); 
	};
	 
	$.getCookie = function (c_name) {
		var c_start,
			c_end;
		if (document.cookie.length > 0)
		{ 
			c_start = document.cookie.indexOf(c_name + "=");
			if (c_start !== -1) {
				c_start = c_start + c_name.length + 1;
				c_end = document.cookie.indexOf(";", c_start);
				if (c_end === -1) {
					c_end = document.cookie.length;
				}
				return unescape(document.cookie.substring(c_start, c_end));
			}
		}
		return "";
	};

}(jQuery));