jQuery.noConflict();
jQuery(document).ready(function(){itemCompare.initialize();});
var itemCompare = {

  maxItems : 5,
  selectedObjectCount : 0,
  selectedObjects : new Array(),
  lastSelectedObject : null,
  justAddedText : 'Added to Compare<br/>You can add upto REPLACE_N_MORE more products or <a href="#">compare now</a>.',
  youreDoneText : 'You have selected 5 products to compare <a href="#">compare now</a>',
  noMoreAllowedText : '<span style="color: #FF0000">Only 5 items can be compared at one time. <a href="#">compare now</a></span>',
  compareThisText : 'Compare',

  postURL : '/gp/comparison',
  
  paginationLinks: null,

  initialize : function() {

    var parentThis = this;

    this.checkboxes = jQuery('#content').find('.itemComparison > input.compCheckbox');
    this.paginationLinks = jQuery('#content').find(".paginationDetail").find("a");

    this.addHandlers();

    var itemDisplayList = new Array();
    var preSelectedAsins = this.getParamsFromQS('itemDisplayList');
    if (preSelectedAsins != null && preSelectedAsins.length != 0) {
      itemDisplayList = preSelectedAsins.split(',');
    }

    this.selectedObjectCount = (itemDisplayList.length > this.maxItems) ? this.maxItems : itemDisplayList.length;
    for (i=0; i<this.selectedObjectCount; i++) {
      this.selectedObjects[itemDisplayList[i]] = true;
    }

    this.checkboxes.each(
      function(idx, domEl) {
        var asin = parentThis.getAsinFromDomEl(domEl);
        if (parentThis.selectedObjects[asin]) {
          domEl.checked = true;
          parentThis.lastSelectedObject = domEl; 
        } else {
          parentThis.selectedObjects[asin] = false;
        }
      }
    );

    if (this.lastSelectedObject != null) {
      var msg = this.justAddedText.replace("REPLACE_N_MORE", this.maxItems - this.selectedObjectCount);
      if (this.selectedObjectCount == this.maxItems) {
        msg = this.youreDoneText;
      }
      this.showMessageOnItem(this.lastSelectedObject, msg);
    }

  },

  addHandlers : function() {
    var parentThis = this;
    this.checkboxes.each(
      function(idx, domEl) {
        jQuery(domEl).click(function(){parentThis.checkBoxHit(domEl);});
      }
    );

    this.paginationLinks.click(function(){
      parentThis.nextPage(this);
    });
    
    jQuery('#content > form').submit(function(){return false;});
    jQuery('div.compareButton > input').click(function(){parentThis.compare();});
  },

  checkBoxHit : function(checkbox) {
    var returnable = false;
    var asin = this.getAsinFromDomEl(checkbox);
    if (!this.selectedObjects[asin]) {
      if (this.selectedObjectCount == this.maxItems-1) {
        this.selectedObjectCount ++;
        this.highlightItem(checkbox);
        this.showMessageOnItem(checkbox, this.youreDoneText);
      } else if(this.selectedObjectCount == this.maxItems) {
        this.showMessageOnItem(checkbox, this.noMoreAllowedText);
        jQuery(checkbox).attr('checked', false);
        returnable = true;;
      } else {
        this.selectedObjectCount ++;
        this.highlightItem(checkbox);
        this.showMessageOnItem(checkbox, this.justAddedText.replace("REPLACE_N_MORE", this.maxItems - this.selectedObjectCount));
      }
      if (this.lastSelectedObject != null) {
        this.showMessageOnItem(this.lastSelectedObject, this.compareThisText);
      }
      this.lastSelectedObject = checkbox;
      if (returnable) return;
    } else {
      this.selectedObjectCount --;
      this.unhighlightItem(checkbox);
      this.showMessageOnItem(this.checkbox, this.compareThisText);
      if (this.lastSelectedObject != null) {
        this.showMessageOnItem(this.lastSelectedObject, this.compareThisText);
      }
      var allHighlighted = jQuery('#content').find('div.itemCompHighlight');
      if (allHighlighted.length != 0) {
        var firstHighlighted = allHighlighted[0];
        var firstCheckbox = jQuery(firstHighlighted).find('input.compCheckbox');
        this.showMessageOnItem(firstCheckbox, this.justAddedText.replace("REPLACE_N_MORE", this.maxItems - this.selectedObjectCount));
        this.lastSelectedObject = firstCheckbox;
      }
    }
    this.selectedObjects[asin] = !this.selectedObjects[asin];
  },

  getAsinFromDomEl : function(checkbox) {
    return jQuery(checkbox).attr('value');
  },

  getParamsFromQS : function(keyRequired) {
    var qs = window.location.search.substring(1).split('&');
    for (i=0; i<qs.length; i++) {
      var key = qs[i].split('=')[0];
      var value = qs[i].split('=')[1];
      if (key == keyRequired) {
        return unescape(value);
      }
    }
    return null;
  },


  /* 
   * update all state variable before calling these methods
   */
  highlightItem : function(checkbox) {
    var itemDiv = this.getItem(checkbox);
    itemDiv.removeClass('item');
    itemDiv.addClass('itemCompHighlight');
  },

  unhighlightItem : function(checkbox) {
    var itemDiv = jQuery(checkbox).parent().parent().parent();
    itemDiv.removeClass('itemCompHighlight');
    itemDiv.addClass('item');
  },

  getItem : function(checkbox) {
    return jQuery(checkbox).parent().parent().parent();
  },
  
  showMessageOnItem : function(checkbox, string) {
    var parentThis = this;
    var itemDiv = this.getItem(checkbox);
    itemDiv.find('span.compareTitle').html(string);
    itemDiv.find('span.compareTitle').find('a').click(function(){parentThis.compare();return false;});
  },
 
  compare : function() {
    if (this.selectedObjectCount <=1 ) {
      jQuery('div#impMsgBox').show();
      return;
    } else {
      jQuery('div#impMsgBox').hide();
    }
    window.location.href = this.getURL();
  },   

  getItemDisplayListString : function() {
    var selectedAsins = new Array();
    for (var asin in this.selectedObjects) {
      if (this.selectedObjects[asin]) {
        selectedAsins.push(asin);
      }
    }
    return selectedAsins.join(',');
  },

  getURL : function() {
    var queryParams = 'itemDisplayList=' + this.getItemDisplayListString() + '&';
    queryParams += 'nodeID=' + nodeID + '&';
    queryParams += 'refLink=' + escape(location.href)  + '&';
    queryParams += 'category=' + category;
    return this.postURL + '?' + queryParams;
  },

  nextPage : function(link) {
    var jObj = jQuery(link);
    var href = jObj.attr('href');
    href += '&itemDisplayList=' + this.getItemDisplayListString();
    jObj.attr('href', href);
  }

};
