// wcscompare.js

var COOKIE_NAME='H_COMPARISON_';
var COOKIE_DELIMITER='__';
var CATEGORY_SPLIT=':';
/*var comparisonList;*/

function ProductComparitor() {

	this.categoryId = "";

	this.remove = function(id) {
		var index = this.comparisonList.indexOf(id);
		
		if (index >= 0) {
			this.comparisonList.splice(index, 1);
			this.save();
		}
	}
	
	this.createCookie = 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.readCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			
			while (c.charAt(0)==' ') 
				c = c.substring(1,c.length);
				
			if (c.indexOf(nameEQ) == 0) 
				return c.substring(nameEQ.length,c.length);
		}
		
		return null;
	}
	
	this.save = function() {
		var value = this.categoryId + CATEGORY_SPLIT;
		if (typeof(this.comparisonList) != 'undefined') {
			var delimiter = '';
			
			for (i = 0; i < this.comparisonList.length; i++) {
				value += delimiter + this.comparisonList[i];
				delimiter = COOKIE_DELIMITER;
			}
		}
		
		this.createCookie(COOKIE_NAME, value);
	}
	
	this.clear = function() {
		this.createCookie(COOKIE_NAME, "");
		this.reset();
	}
	
	this.setElement = function(id, checked) {
		var index = this.comparisonList.indexOf(id);
		
		if (checked) {
			if (index < 0)
				this.comparisonList.push(id);
		}
		else {
			if (index >= 0)
				this.comparisonList.splice(index, 1);
		}
		
		this.save();
	}
	
	this.isChecked = function(id) {
		return this.comparisonList.indexOf(id) >= 0;
	}
	
	this.count = function() {
		return typeof(this.comparisonList) == 'undefined' ? 0 : this.comparisonList.length;
	}

	this.reset = function() { //BAD NAME = should be 'reload', not 'reset'
		var cookie = this.readCookie(COOKIE_NAME);
		
		if (cookie != null) {
			var categorySplit = cookie.indexOf(CATEGORY_SPLIT);
			var cookieCategoryId = cookie.substring(0, categorySplit);
			
			if (cookieCategoryId != "") {
				var cookieString = cookie.substring(categorySplit + 1);
				if (cookieString == "") {
					this.comparisonList = new Array(); //test for isEmpty
				}	
				else {
					if (this.categoryId != Number(cookieCategoryId)){				
						this.comparisonList = new Array();
						this.save();
					}
					else {
						this.comparisonList = cookieString.split(COOKIE_DELIMITER);	
					}	
				}			
			}
			else {
				this.comparisonList = new Array();
				this.save();
			}			

		} else {
			this.comparisonList = new Array();
			this.save();
		}
	}
	
	this.clear = function() {
		this.comparisonList = new Array();
		this.save();
	}
	
	this.setStoreId = function (storeId) {
		if(COOKIE_NAME != 'H_COMPARISON_' + storeId) {
			COOKIE_NAME='H_COMPARISON_' + storeId;
			this.reset();
		}
	}
	
	this.setCategoryId = function (categoryId) {
		this.categoryId = Number(categoryId);
	}
	
	this.reset();
}

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(elt /*, from*/) {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++) {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

var comparitor = new ProductComparitor();


