﻿//debug('product.js');

var enabledLeftArrowImage = '/images/plut_results/arrow_left.gif';
var disabledLeftArrowImage = '/images/plut_results/arrow_left_disabled.gif';
var enabledRightArrowImage = '/images/plut_results/arrow_right.gif';
var disabledRightArrowImage = '/images/plut_results/arrow_right_disabled.gif';

var selectAllProductTypes = function() {
   //debug('selectAllProductTypes');
   $('#ProductSelector input').each(function(i)
   {
     //debug(i);
      this.checked = "true"; 
   });  
}

var getOfferListWidth = function(){
    //debug('getOfferListWidth');
    $('#OffersList').css('width', ($('#OffersList > li').length * getOfferWidth()) );
}

var getOfferWidth = function(){
    //debug('getOfferWidth');
    return $('#OffersList > li').innerWidth();
}

var displayPreviousOffer = function() {
    //debug('displayPreviousOffer');
    updateOfferPosition(getOfferWidth() * 3);
}

var displayNextOffer = function() {
    //debug('displayNextOffer');
    updateOfferPosition(-(getOfferWidth() * 3));
}

var getSpecificAd = function(elm) {
    adNumber = parseInt(elm.innerHTML);
    //debug('displaySpecificAd : adNumber = ' + adNumber);
    displaySpecificAd(adNumber);
}    
 
var displaySpecificAd = function(adNumber) {    
    var newValue = (-(adNumber - 1) * (getOfferWidth()*3)) + 'px';
    $('#OffersList').animate({marginLeft: newValue}, {duration:'slow', complete:setOfferNavigation});
    setOfferGroupNavigation(adNumber);    
}

var setOfferGroupNavigation = function(adNumber){
    $('.offerLink').each(function(i){$(this).removeClass('disabledLink');});
    $('.offerLink:eq('+ (adNumber -1) + ')').addClass('disabledLink');
    //$(elm).addClass('disabledLink');
}

var updateOfferPosition = function(value) {
  //debug('updateOfferPosition : value = ' + value);
  var newValue = value + parseFloat($('#OffersList').css('margin-left'));
  //debug('                    : current = ' + $('#OffersList').css('margin-left'));
  //debug('                    : newValue = ' + newValue);
  //debug('                    : offerLength = ' + getOfferWidth() * $('#OffersList > li').length -1); 
  if(parseFloat($('#OffersList').css('margin-left')) % getOfferWidth() == 0)
  {
      if ((newValue <= 0) && (newValue >= -(getOfferWidth() * $('#OffersList > li').length -1)))
      {
          //$('#OffersList').animate({marginLeft: newValue}, {duration:'slow', complete:setOfferNavigation});
          displaySpecificAd(-((newValue / (getOfferWidth() * 3))) + 1);          
          //debug('                    : true');
      }
       else 
      {
          //debug('                    : false');
      }
  }
}

var setOfferNavigation = function(){
  debug('setOfferNavigation');
   //debug('    currentMargin:' + parseFloat($('#OffersList').css('margin-left')));
   //debug('    offerWidth:' + parseInt(getOfferWidth()));
   var currentlyOn =  parseFloat($('#OffersList').css('margin-left')) / parseInt(getOfferWidth());
   //debug('    currentlyOn : ' + currentlyOn);   
   var offerTotal = $('#OffersList > li').length;
   //debug('    offerTotal : ' + offerTotal);

   if (parseFloat($('#OffersList').css('margin-left')) == 0) {
     $('#PreviousOffer').addClass('disabledLink');
     $('#ControlLeft img').attr("src", disabledLeftArrowImage);
   } else {
     $('#PreviousOffer').removeClass('disabledLink');
     $('#ControlLeft img').attr("src", enabledLeftArrowImage);
   }
   
   //debug('  $(\'#OffersList > li\').length :' + $('#OffersList > li').length);
   //debug('  parseInt(($(\'#OffersList > li\').length + 2)/3) :' + parseInt(($('#OffersList > li').length + 2)/3));
   //debug('  getOfferWidth() * 3 :' + getOfferWidth() * 3);
   debug('  left:  (-(getOfferWidth() * 3) * parseInt(($(\'#OffersList > li\').length + 2)/3)) :' + (-(getOfferWidth() * 3) * (parseInt(($('#OffersList > li').length + 2)/3) - 1)) );
   debug('  right:  parseFloat($(\'#OffersList\').css(\'margin-left\') :' + parseFloat($('#OffersList').css('margin-left')) );
   debug( (-(getOfferWidth() * 3) * parseInt(($('#OffersList > li').length + 2)/3)) >= parseFloat($('#OffersList').css('margin-left')) );

   if( (-(getOfferWidth() * 3) * (parseInt(($('#OffersList > li').length + 2)/3) - 1)) >= parseFloat($('#OffersList').css('margin-left')) )
    {
     $('#NextOffer').addClass('disabledLink');
     $('#ControlRight img').attr("src", disabledRightArrowImage);
   } else {
     $('#NextOffer').removeClass('disabledLink');
     $('#ControlRight img').attr("src", enabledRightArrowImage);
   }
   
}

var viewAllFeatures = function(elm) {
  $(elm).css('display', 'none');
  $(elm).next('.iconMinusBlack').css('display', 'block');
  $(elm).parents('.productTypeListHeader').next('.productListings').find('.iconPlusBlue').each(function(i){$(this).css('display', 'none');});
  $(elm).parents('.productTypeListHeader').next('.productListings').find('.featureList').each(function(i){$(this).css('display', 'block');});
}

var hideAllFeatures = function(elm) {
  $(elm).css('display', 'none');
  $(elm).prev('.iconPlusBlack').css('display', 'block');
  $(elm).parents('.productTypeListHeader').next('.productListings').find('.iconPlusBlue').each(function(i){$(this).css('display', 'block');});
  $(elm).parents('.productTypeListHeader').next('.productListings').find('.featureList').each(function(i){$(this).css('display', 'none');});
}

var viewFeatures = function(elm) {
 //debug('viewFeatures');
 $(elm).css('display', 'none');
 $(elm).next('.featureList').css('display', 'block');
}

var hideFeatures = function(elm) {
 //debug('hideFeatures');
 $(elm).parents('.featureList').prev('.iconPlusBlue').css('display', 'block');
 $(elm).parents('.featureList').css('display', 'none');
}

var setEventListeners = function() {
    //debug('setting listeners');
    $('#ProductSelectAll').click(function(){selectAllProductTypes();}); 
    
    $('#PreviousOffer').click(function(){displayPreviousOffer();});
    $('#NextOffer').click(function(){displayNextOffer();});
    $('.offerLink').each(function(i){$(this).click(function(){getSpecificAd(this);});});    
        
    $('#ControlLeft').click(function(){displayPreviousOffer();});
    $('#ControlRight').click(function(){displayNextOffer();});    
        
    $('.iconPlusBlue').each(function(i){$(this).click(function(){viewFeatures(this);});});  
    $('.iconMinusBlue').each(function(i){$(this).click(function(){hideFeatures(this);});});
    $('.iconMinusBlue2').each(function(i) { $(this).click(function() { hideFeatures(this); }); });
    $('.iconPlusBlack').each(function(i) { $(this).click(function() { viewAllFeatures(this); }); });     
    $('.iconMinusBlack').each(function(i){$(this).click(function(){hideAllFeatures(this);});});            
    
}

$(document).ready(function() {
  //debug('dom ready');
  $(document).pngFix(); 
  setEventListeners();
  getOfferListWidth();
  setOfferNavigation();
  setOfferGroupNavigation(1);
});


function debug(what){
	if(window.console && window.console.firebug){
		//console.log(what);
	}	
	else{
		//alert(what);
	}
}

