﻿$(function(){

  isIeOld = ( $.browser.msie && $.browser.version < 8.0 );

  $( '#rightColumn' ).scrollFollow( { 
    speed: 300,
    offset: 10
  } );

  $("#bookSubCategoryList .bookSummary:even").css( {"background-color": "#dbd1bc" } );
  $("#bookSubCategoryList .bookSummary:odd").css( {"background-color": "#ffffff" } );

  $("#subnavigation").hide();
  
  setupCartQuantityChangers();
  
  setupCartAddSubtract();
  
  setupDropDownNavigation();
  
  setupLeftNavigation();

});

function setupCartAddSubtract(){
  $("body").find(".cartSubtract").each( function(){
    $(this).click( function(){
      var $currentQuantityHolder = $(this).parent().find(".currentQuantity");
      var value = $currentQuantityHolder.attr('value');
      if( ( value * 1 ) != 0 ){
        var newValue = (value * 1) - 1;
        $currentQuantityHolder.attr('value', newValue );
      }
      var webjectId = $(this).parent().attr("class");
      var actionString = "/Cart/Subtract/" + 
                       webjectId.replace("-","!");
                       
      
      setContentReviewTableHtml( $(this) );
                       
      $.get( actionString, function(response){
        $("#cart .table").html(response);
        
        if( !cartHasItems() || isIeOld ){
          location.reload();
        }
        
        setupCartQuantityChangers();
      });
      
      return;
    });
  });
  
  $("body").find(".cartAdd").each( function(){
    $(this).click( function(){
    
      var $currentQuantityHolder = $(this).parent().find(".currentQuantity");
      var value = $currentQuantityHolder.attr('value');
      var newValue = (value * 1) + 1;
      $currentQuantityHolder.attr('value', newValue );
      var webjectId = $(this).parent().attr("class");
      var actionString = "/Cart/Add/" + webjectId.replace("-","!");
      
      setContentReviewTableHtml( $(this) );
      
      if( !cartHasItems() || isIeOld ){
        $.post( actionString, function(data){
          location.reload();
        });
        return;
      }
                       
      $.get( actionString, function(response){
        $("#cart .table").html(response);
        setupCartQuantityChangers();
      });
      return;
    });
  });
}

function setContentReviewTableHtml( $object ){
  var parent = $object.parent().parent().parent().parent();
  
  if( parent.attr('class') == "bookSummary" ) return;
  
  if ( $("#reviewOrder") != null ){
    var quantity = parent.find( '.currentQuantity' ).attr('value');
    if( quantity == "0" ){
      parent.remove();
      updateTotals( parent.parent() );
      return;
    }
    var yourPrice = parent.find( '.itemYourPrice' ).html();
    var rrp = parent.find( '.itemRRP' ).html();
    
    var newTotalRRP = quantity * rrp;
    var newYourPrice = quantity * yourPrice;
    
    parent.find( '.itemRRPTotal' ).html( padCurrencyNumber(newTotalRRP) );
    parent.find( '.itemYourPriceTotal' ).html( padCurrencyNumber( newYourPrice ));
    
    updateTotals( parent.parent() );
  }
}

function updateTotals( $object ){
  //Update freight totals
  var totalQuantity = 0;
  var freightTotal = "$3.50";
  var singleFreight = true;
  
  $object.parent().find( '.currentQuantity' ).each( 
    function(){
      totalQuantity = totalQuantity + $(this).attr('value');
    }
  );
  
  if( totalQuantity > 1 ){
    freightTotal = "$6.00";
    singleFreight = false;
  }
  
  $object.parent().find( '.freightTotal' ).html( freightTotal );
  
  var totalPrice = 0.0;
  
  //Update your total
  $object.parent().find( '.itemYourPriceTotal' ).each(
    function(){
      totalPrice += parseFloat( $(this).html() );
    }
  );
  
  if( singleFreight ){
    totalPrice += 3.5;
  } else{
    totalPrice += 6;
  }
  
  $object.parent().find( '.yourPriceTotal' ).html( "$" + padCurrencyNumber( totalPrice ) );
}

function padCurrencyNumber( number ){
  number = number + "";
  var splitNum = number.split('.');
  if( splitNum.length > 1 ){
    var paddedNumber = splitNum[1];
    if( paddedNumber.length == 2 ){
      return number;
    }
    if( paddedNumber.length == 1 ){
      return splitNum[0] + "." + splitNum[1] + "0";
    }
  }
  return number + ".00";
}

function setupDropDownNavigation(){
  $("#navigation ul li .dropDownNavigation").hover(
    function(){
      $(this).parent().find("a:first").addClass("hover");
    },
    function(){
      $(this).parent().find("a:first").removeClass("hover");
    }
  );
}

function cartHasItems(){
  var totalQuantity = 0;
  $('#cart').find( '.currentQuantity' ).each( 
    function(){
      totalQuantity = totalQuantity + $(this).attr('value');
    }
  );
  
  if( totalQuantity == 0 ){
    return false;
  }
  
  return true;
}

function setupLeftNavigation(){
  $("#leftCategoryNavigation .category ul.subCategories").hide();
  
  $("#leftCategoryNavigation .category").find("h3").each( function(){
    $(this).css( { "margin-bottom": "-1px" } );
    if( $(this).find("span").attr("class") == "current" ){
      $(this).parent().find(".subCategories").show();
    }    
    $(this).click( function(){
      var $subCategoryToShow = $(this).parent().find(".subCategories");
      if(  !$subCategoryToShow.is(':visible') ){
        $subCategoryToShow.slideDown("normal");
        $(this).find("span").addClass('current');
        return;
      }
      $subCategoryToShow.slideUp("normal");
      $(this).find("span").removeClass('current');
    });
    $(this).hover( function(){
      $(this).css( { "cursor": "pointer" } );
    }, function( ){
      $(this).css( { "cursor": "normal" } );
    });
  });
}

function setupCartQuantityChangers(){
  $(".currentQuantity").keypress(function (e){
    if ( e.which == 13 ){
      var value = $(this).attr('value');
      var webjectId = $(this).parent().attr("class");
      
      var webjectId = $(this).parent().attr("class");
    
      if( $("#bookSubCategoryList ." + webjectId + " .currentQuantity") != null ){
        if( $("#bookSubCategoryList ." + webjectId + " .currentQuantity").attr('value') != value ){
          $("#bookSubCategoryList ." + webjectId + " .currentQuantity").attr('value', value);
        }
      }

      var actionString = "/Cart/SetQuantity/" + webjectId.replace("-","!") + "/" + value;
      
      if( !cartHasItems() || isIeOld ){
        $.post( actionString, function(data){
          location.reload();
        });
        return;
      }

      $.get( actionString, function(response){
       $("#cart .table").html(response);
       setupCartQuantityChangers();
      });
      
      return false;
    }
  });

  $(".currentQuantity").change( function(){
    var value = $(this).attr('value');
    
    var webjectId = $(this).parent().attr("class");
    
    if( $("#bookSubCategoryList ." + webjectId + " .currentQuantity") != null ){
      if( $("#bookSubCategoryList ." + webjectId + " .currentQuantity").attr('value') != value ){
        $("#bookSubCategoryList ." + webjectId + " .currentQuantity").attr('value', value);
      }
    }

    var actionString = "/Cart/SetQuantity/" + webjectId.replace("-","!") + "/" + value;
    
    if( !cartHasItems() || isIeOld ){
      $.post( actionString, function(data){
        location.reload();
      });
      return;
    }

    $.get( actionString, function(response){
     $("#cart .table").html(response);
     setupCartQuantityChangers();
    });
  });
}