
function getURLVar(urlVarName) {
	var urlHalves = String(document.location).toLowerCase().split('?');
	var urlVarValue = '';

	if (urlHalves[1]) {
		var urlVars = urlHalves[1].split('&');

		for (var i = 0; i <= (urlVars.length); i++) {
			if (urlVars[i]) {
				var urlVarPair = urlVars[i].split('=');

				if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;
}
function saveIt(name, value) {
	Cookies.create(name, value, 1);
}
function readIt(name) {
	return Cookies[name];
}
var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: 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;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
Cookies.init();
var SpecialModel = function() {
	var load = false;
	var special_collection	=	[];
	var special_times		=	[];
	var format_time = function (sec) {
		var out = "<p class='special_item_render'>Offer expires in <br />";
		var min = Math.floor(sec/60);
		var sec = sec-(min*60);
		if (min > 0) out += min + ' min ';
		if (sec > 0) out += sec + ' sec ';
		out += "</p>";
		return out;
	}
	var display = function () {
		var run = false;
		for (var i = 0; i < special_collection.length; i++) {
			var id = special_collection[i];
			var val = Number(special_times[i]);
			var val_next = val - 1;
			if (val_next >= -1) {
				if (val == 0) {
					$(".special_"+id).hide();
				} else {
					$(".special_"+id).html(format_time(val));
				}
				special_times[i] = val_next;
				if (!run) run = true;
			}
		}
		if (run) {
			setTimeout(display, 1000);
		}
		saveIt('special_collection', special_collection.join(','));
		saveIt('special_times', special_times.join(','));
	}
	/*if (!load) {
		$.post(
			'index.php?route=product/special/jsonlist',
			{},
			function(reply){
				if (reply.list.length > 0) {
					//save
					saveIt('special_collection', reply.collection);
					saveIt('special_times', reply.times);
					//read
					special_collection	=	readIt("special_collection").split(",");
					special_times		=	readIt("special_times").split(',');
					load = true;
					display();
				}
			},
			"json"
		);
	}*/
	this.init  = function(collection, times) {
		 special_collection	=	collection.split(',');
		 special_times		=	times.split(',');
		 display();
	}
}
function addBookmark(url, title){
	if (window.sidebar)
		return window.sidebar.addPanel(title, url, "");
	else if ( window.external )
		return window.external.AddFavorite( url, title);
	else if (window.opera && window.print)
		return true;
	return true;
}
