        // Create a new Product Object.
        Product = new Object();
        // Debug Property - verbose output.
        Product.debug=0;
        // Browser detection.
        Product.browsertype=false;
        // Message Object
        Product.messageObject=false;
        // Last clicked product.
        Product.lastClicked=false;
        // Set a boolean to lock actions whilst there is a request or is animating.
        Product.isBusy=false;
        
        // Get current URL, split from possible anchor.
        // var loc1 = location.href.split("#");
        // Product.loc1 = loc1[0];
        // Product.loc1 = "/index:productcomparison";
        var current_url = unescape(location.href);
        var xstart = current_url.lastIndexOf("/") + 1;
        var xend = current_url.length;
        var hereName = current_url.substring(xstart,xend);
        var herePath = current_url.substring(0,xstart);
        Product.loc1 = herePath + 'index.html:productcomparison';

        // Public Method.
        Product.Comparison=function() {
            Product.browsertype=this.getBrowser();

            // Page type - based on what divs are present.
            this.pagetype='normalpage';

            // Comparison Batch
            this.comparisonbatch= new Array();

            // Our message <p> classes.
            this.messageokclass='ok';
            this.messageerrorclass='error';

            // Init Messager
            try {
                this.initMessager();
            } catch(e) { }
            // If the Comparison form is present.
            if($('frm_comparison')) {
                this.pagetype='comparisonpage';
                this.batchAction('comparisons', 'displaycomparisonlist');
                this.initCheckboxes();
            }
    },
    Product.Comparison.prototype={
        initCheckboxes: function() {
            // Check we are on the right type of page.
            if(this.pagetype=='comparisonpage') {
                // Use browsertype.
                if(Product.browsertype=='ie' || Product.browsertype=='ie6' || Product.browsertype=='ie7') {
                    // Internet Explorer
                    try {
                    $$('.checkbox').each(function(el) {
                        var Product_id = el.value;
                        var Product_name = el.getProperty("rel");
                        if(!Product_name || Product_name=='') { Product_name = "Item Name"; }
                            el.addEvent('mouseup', function(el2) {
                            var ele2 = el2;
                            Product.lastClicked = Product_id;
                            var eel = "c" + Product.lastClicked;
                            var ele = $(eel);
                            try {
                            if(ele.checked==false) { this.batchAction('comparisons', 'save_item', Product_id); }
                            else if(ele.checked==true) { this.batchAction('comparisons', 'delete_item', Product_id); }
                            else {
                                throw('Chapter Eight says - Could not get checked status.');
                            }
                            } catch(e) { alert(e); }
                        }.bind(this));
                    }.bind(this));
                    } catch(e) { }
                } else if(Product.browsertype=='sa' || Product.browsertype=='sa2' || Product.browsertype=='sa3') {
                    // SAFARI
                    try {
                    $$('.checkbox').each(function(el) {
                        var Product_id = el.value;
                        var Product_name = el.getProperty("rel");
                        if(!Product_name || Product_name=='') { Product_name = "Item Name"; }
                        el.addEvent('mouseup', function(ele) {
                            Product.lastClicked = Product_id;
                            try {
                            if(ele.target.checked==true) { this.batchAction('comparisons', 'save_item', Product_id); }
                            else if(ele.target.checked==false) { this.batchAction('comparisons', 'delete_item', Product_id); }
                            else {
                                     if(Product.lastClicked) {
                                        var rescueEl = "c" + Product.lastClicked;
                                        var r=$(rescueEl);
                                        if(r) {
                                            if(ele.target.checked==false) { this.batchAction('comparisons', 'save_item', Product_id); }
                                            else if(ele.target.checked==true) { this.batchAction('comparisons', 'delete_item', Product_id); }
                                            else { throw('Chapter Eight says - Could not get checked status.'); }
                                        } else { throw('Chapter Eight says - Could not get DOM element.'); }
                                    } else { throw('Chapter Eight says - Could not determine element id.'); }
                                }
                            } catch(e) { alert(e); }
                        }.bind(this));
                    }.bind(this));
                    } catch(e) { }
                } else {
                    // FIREFOX/MOZILLA
                    try {
                    $$('.checkbox').each(function(el) {
                        var Product_id = el.value;
                        var Product_name = el.getProperty("rel");
                        if(!Product_name || Product_name=='') { Product_name = "Item Name"; }
                        el.addEvent('mouseup', function(ele) {
                            // var ele = $(el2);
                            Product.lastClicked = Product_id;
                            try {
                            if(ele.target.checked==false) { this.batchAction('comparisons', 'save_item', Product_id); }
                            else if(ele.target.checked==true) { this.batchAction('comparisons', 'delete_item', Product_id); }
                            else {
                                     if(Product.lastClicked) {
                                        var rescueEl = "c" + Product.lastClicked;
                                        var r=$(rescueEl);
                                        if(r) {
                                            if(ele.target.checked==false) { this.batchAction('comparisons', 'save_item', Product_id); }
                                            else if(ele.target.checked==true) { this.batchAction('comparisons', 'delete_item', Product_id); }
                                            else { throw('Chapter Eight says - Could not get checked status.'); }
                                        } else { throw('Chapter Eight says - Could not get DOM element.'); }
                                    } else { throw('Chapter Eight says - Could not determine element id.'); }
                                }
                            } catch(e) { alert(e); }
                        }.bind(this));
                    }.bind(this));
                    } catch(e) { }                                   
                } // ff
            }
        },
        batchAction: function(batchname, batchaction, productid) {
        // Based on the supplied action, delegate which server side Method to hit, options and callback.
        var batch = batchname;
        var action = batchaction;
        var product_id=productid;
        var url=false;
        var postBody=false;
        var options=false;
        var callback=false;
       
        if(!product_id) { product_id='all'; }
        if(!batch) { batch='comparisons'; }
        if(!action) { action='displaycomparisonlist'; }
       
        if(product_id && batch && action) {
            try {
                if(action=='save_item') {
                    url = "/index:toggleBatchItem";
                    postBody = Json.toString( { requesttype: "ajax", batchaction: action, batchname: batch, item: product_id } );
                    callback = this.returnedBatchAction;
                }
                else if(action=='delete_item') {
                    url = "/index:toggleBatchItem";
                    postBody = Json.toString( { requesttype: "ajax", batchaction: action, batchname: batch, item: product_id });
                    callback = this.returnedBatchAction;
                }
                else if(action=='unset_batch') {
                    url = "/index:unsetBatch";
                    postBody = Json.toString( { requesttype: "ajax", batchaction: action, batchname: batch, item: product_id });
                    callback = this.returnedBatchAction;
                }
                else if(action=='get_batch') {
                     url = "/products/index:getBatch";
                     postBody = Json.toString({requesttype: "ajax", batchaction: action, batchname: batch, item: product_id});
                     callback = this.registerComparisonItems;
                }
                else if(action=='displaycomparisonlist') {
                    url ="/products/index:getBatch";
                    postBody = Json.toString({ requesttype: "ajax", batchaction: action, batchname: batch, item: product_id });
                    callback = this.displayComparisonList;
                }
               
                if(!postBody || postBody =="" || postBody =="{}") {
                    postbody = Json.toString({ requesttype: "ajax", batchaction: "displaycomparisonlist", batchname: "comparisons", item: 'all' });
                }
               
                if(url && postBody && callback) {
                    options = { method: 'post', evalScripts: true, postBody: postBody, onComplete: callback };
                    if(options && options !=="{}" && options !=="") {
                       // Make the remote request.
                       Product.isBusy=true;
                       var aj = new Ajax(url, options).request();
                    } else { throw('Chapter Eight says - Unable to build final request.'); }
                } else { throw('Chapter Eight says - Unable to build request part 1.'); }
            } catch(e) { return false; }
       }
    },
    returnedBatchAction: function(result) {
        if(Product.isBusy) {
            // console.log("Busy");
        }
        try {
            if(result) {
                // Based on the returned numeric code, update local message div.
                var lc = Product.lastClicked;
                var lastClick = $("c" + lc);
                var lastInform = $("i" + lc);
                var fx = lastInform.effects({duration: 1000, transition: Fx.Transitions.Quart.easeOut});        
                if(result==0) {
                    // console.log("was 0");
                }
                else if(result==1) {
                    lastClick.checked = true;
                    fx.start({
                        'opacity': 0,
                        'background-color': '#FEFFBF'
                    }).chain(function(){
                        lastInform.setHTML("Added to Comparisons. <br/><a href='" + Product.loc1 + "'>View Comparisons</a>");
                        this.start({ 'opacity':1 });
                    }).chain(function() {
                        if(lastClick.checked == true && lastInform.innerHTML == "") {
                            // console.log("mismatched checkbox");
                            //   lastInform.setHTML("Added to Comparisons. <br/><a href='" + Product.loc1 + ":productcomparison'>View Comparisons</a>");
                            //   this.start({ 'opacity':1 });
                        }        

                    });
                }
                else if(result==3) {
                    lastClick.checked = false;
                    //
                }
                else if(result==2) {
                    lastClick.checked = false;
                    fx.start({
                        'opacity': 0,
                        'background-color': '#FEFFBF'
                    }).chain(function(){
                        lastInform.setHTML("Removed");
                        this.start({ 'opacity':1 });
                    }).chain(function() {
                        this.start.delay(2000, this, { 'opacity':0, 'background-color': '#FFFFFF' });
                    }).chain(function() {
                        if(lastInform.innerHTML !== "" && lastInform.innerHTML !== "&nbsp;") {
                            lastInform.setHTML("&nbsp;");
                        }
                        // lastInform.setStyle('opacity',1);
                    });
                }
                else if(result==4) {
                    lastClick.checked = false;
                    //
                }
                else if(result==5) {
                    lastClick.checked = false;
                    //
                }
                else if(result==6) {
                    lastClick.checked = false;
                    fx.start({
                        'opacity': 0,
                        'background-color': '#FEFFBF'
                    }).chain(function(){
                        lastInform.setHTML("A maximum of 4 can be compared.<br/><a href='" + Product.loc1 + "'>View Comparisons</a>");
                        this.start({ 'opacity':1 });
                    }).chain(function() {
                        this.start.delay(2000, this, { 'opacity':0, 'background-color': '#FFFFFF' });
                    }).chain(function() {
                        if(lastInform.innerHTML !== "" && lastInform.innerHTML !== "&nbsp;") {
                            lastInform.setHTML("&nbsp;");
                        }
                    });              

               } else { throw('Chapter Eight - Bizarre error.'); }

             //   if(lastClick.checked == true && lastInform.innerHTML == "") {
               //     console.log("mismatched checkbox");
                 //   lastInform.setHTML("Added to Comparisons. <br/><a href='" + Product.loc1 + ":productcomparison'>View Comparisons</a>");
                 //   this.start({ 'opacity':1 });
              //  }

           } else { throw('Chapter Eight says - No Data.'); }
       } catch(e) { }
       Product.isBusy=false;
    },
    displayComparisonList: function(result) {
        try { this.comparisonbatch = Json.evaluate(result); } 
        catch(e) { }
        
        if(this.comparisonbatch) {
            // Using the returned JSON Object's properties, we can build a visual list
            var c=1;
            var b = new Array();
            for(var property in this.comparisonbatch) {
                if(this.comparisonbatch.hasOwnProperty(property)) {
                    // We have a JSON Object with product_ids as properties.
                    b[c] = this.comparisonbatch[property];
                    // console.log(b[c]);
                }
                c++;
            }
            if(c > 1) {
                // This has been altered to show 1 large message.
                var showcomparisons = function() {
                var m = new Message.Obj();
                m.setMessage("<p class='compared clearfix' style='padding: 1em 0.5em 1em 0.5em; border-top: 2px solid #000; border-bottom: 2px solid #000;'>You have saved Comparison Items. <a href='" + Product.loc1 + "'>View Comparisons.</a> </p>", true);
            }.delay(0);
        }
        // Update Checkboxes
        if(b && b.length > 0) {
            try {
                if($('frm_comparison')) {
                    $$('.checkbox').each(function(el) {
                        
                        var Product_id = el.value;
                        var Product_name = el.getProperty("rel");
                        if(!Product_name) { Product_name = "Item Name"; }
                        // IE specific.
                        if(Product.browsertype=='ie' || Product.browsertype=='ie6' || Product.browsertype=='ie7') {
                            if(Product_id) {
                                var rescueEl = "c" + Product_id;
                                var el=$(rescueEl);
                            } else { var el = false; }
                        }
                        // check if its in the array
                        var checkbox_in_batch = b.test(Product_id);
                        if(checkbox_in_batch) {
                            var elInform = $("i" + Product_id);
                            elInform.setHTML("");
                            elInform.setHTML("Added to Comparison items. <br/><a href='" + Product.loc1 + "'>View Comparisons</a>");
                            el.checked = true;
                        } else {
                            el.checked =false;
                        }
                    }.bind(this));
                }
            } catch(e) { }
        }
      }
    },
    registerComparisonItems: function(result) {
        this.comparisonbatch= Json.evaluate(result);
    },
    initMessager: function() {
        try {
            if($('messagemarker')) {           
                Product.messageObject = new Message.Obj();
            }
        } catch(e) { return false; }
    },
    getBrowser: function() {
        var browsertype=false;
        if(window.ie) { browsertype='ie'; }        
        if(window.ie6) { browsertype='ie6'; }    
        if(window.ie7) { browsertype='ie7'; }    
        if(window.gecko) { browsertype='mo'; }    
        if(window.webkit) { browsertype='sa'; }    
        if(window.webkit419) { browsertype='sa2'; }    
        if(window.webkit420) { browsertype='sa3'; }    
        if(window.opera) { browsertype='op'; }
        return browsertype;
    }
    } // end
window.addEvent('domready', function() {
  if($('frm_comparison')) {
      try
      {
          var compare=false;
          var compare = new Product.Comparison();         
          if(!c) { throw('Chapter Eight says - Unable to create Comparison Object.'); }
      } catch(e) { return false; }
  }

});


