if (typeof $.cookie === 'undefined') {
    alert('productcompare.jquery.js: cookie.jquery.js is missing!');
}

// Cookie.Debug = true; // USE ONLY WHEN DEBUGGING.

// Define version.
var PRODUCTCOMPARE_JQUERY_JS = 1.0;

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

ProductCompare = function(settings) {
    if(settings == null || typeof settings == 'undefined') settings = {};
    // alert('ProductCompare');
    var instance = this;
    this.Settings = $.extend({}, ProductCompare.Defaults, settings);
    this.Settings.SaveOnUnload = false; // !window.opera;

    // Load previous state from cookie.    
    this.Settings.Items = this.loadState();
    
	if(this.Settings.SaveOnUnload) {
		// Save checked elements on window.unload.
		$(window).unload(function() { instance.saveState(); });
    }
    
    // Get all elements that is marked as state-indicators (i.e. checkboxes).
    var elements = $("." + this.Settings.SelectorName);
    
    // Handle click-event on each input-element and alter registered state accordingly.
    elements.click(function(event) {
        var element = $(this);
        var id = element.attr("name").substring(instance.Settings.NamePrefix.length);
        var checked = element.attr("checked");
        instance.setState(id, checked);
    });

    // Initialize input-element states based on previous states.
    elements.each(function() {
        var element = $(this);
        var id = element.attr("name").substring(instance.Settings.NamePrefix.length);
        if(jQuery.inArray(id, instance.Settings.Items) >= 0) element.attr("checked", true);
    });
};

// Default settings.
ProductCompare.Defaults = {
    NamePrefix: "compare_"
    ,CookieName: "ProductCompareCookie"
    ,SelectorName: "ProductCompareInput"
};

// Load state from a cookie.
ProductCompare.prototype.loadState = function() {
    var data = $.cookie(this.Settings.CookieName);
	if(data == null || data == "") {
	    // alert('loadState: no state');
	    return new Array();
	}
	// alert('loadState: ' + data + ' - items: ' + data.split(",").length);
	return data.split(",");
};

// Save current state to cookie.
ProductCompare.prototype.saveState = function() {
    var data = this.Settings.Items.join(",");

    // alert('saveState: ' + data + ' - items: ' + this.Settings.Items.length);

    if (data != "") {
        // Populate cookie with the id with value 1,2,3 etc (where 1,2... are checked product numbers).
        $.cookie(this.Settings.CookieName, data);
    } else {
        // Remove cookie.
        $.cookie(this.Settings.CookieName, null);
    }
};

// Set state of a given id / productnr.
ProductCompare.prototype.setState = function(id, state) {
    var index = jQuery.inArray(id, this.Settings.Items);
    if(state == true) {
        if(index == -1) {
            this.Settings.Items.push(id);
        }
    } else {
        if(index != -1) {
            this.Settings.Items.remove(index);
        }
    }
    
    // alert('setState(' + id + ', ' + state + ')');
    
    if(!this.Settings.SaveOnUnload) {
        this.saveState();
    }
};

// Clear all states.
ProductCompare.prototype.clearState = function() {
    var elements = $("." + this.Settings.SelectorName + ":checked");
    elements.each(function() {
        $(this).attr("checked", false);
    });
    
    this.Settings.Items = null;
    this.Settings.Items = new Array();
    
    if(!this.Settings.SaveOnUnload) {
        this.saveState();
    }
};

$(document).ready(function() {
    //if(typeof ProductCompareSettings != 'undefined') {
    ProductCompare.Instance = new ProductCompare();
    //}
});