// -js- untick other prices
// -js- tick current price
// -js- rebuild query
// -js- refresh products list

function popup(n){
  if (last>0){
   document.getElementById("ans"+last).style.display = "none"
  }
  if (n != last){
   document.getElementById("ans"+n).style.display =  "block"
   last = n
  } else {
   last = 0
  }
}

function reset_pagination_vars(){
  pagination_page_val = 1;
}

function load_filters_page( filter_kill_name_param , filter_kill_name_val ){
    // set height
    if ( $("#filter_block").length > 0 ) {
      t_height = $('#filter_block').height();
      t_height -= 140;
      $('.loading_div_inner').height( t_height );
      $('.loading_div').fadeIn("fast");
    }

    $('#products_and_filters').load("xt_filter_block.php", { top_seller : top_seller_val, special : special_val, sort_order : sort_order_val, display_amount : display_amount_val, pagination_page : pagination_page_val, category_id : category_id_val , price : price_val, brand : brand_val, features : feature_val, filter_kill_name : filter_kill_name_param , filter_kill_val : filter_kill_name_val  } , function(){
      bind_filters();
    });
}

function bind_filters(){

  // ===
  $('.jq_sort_order').change( function(event){
    sort_order_val = $(this).attr('value');
    load_filters_page( false, false );
  });

  // === jq_page_qty ( 10 20 30 )
  $('.jq_page_qty').bind("click",function(){
    //reset_pagination_vars();
    attr_name = $(this).attr('name');
    display_amount_val = attr_name;
    load_filters_page( false, false );
  });


  // === pagination ( 1 2 3 next>> )
  $('.jq_pagination').bind("click",function(){

    attr_name = $(this).attr('name');
    
    if( attr_name == "next" ){
      pagination_page_val = parseInt(pagination_page_val) + 1;
    }

    if( attr_name == "prev" ){
      pagination_page_val = parseInt(pagination_page_val) - 1;
    }

    if( attr_name != "prev" & attr_name != "next" ){
      pagination_page_val = attr_name;
    }
    load_filters_page( false, false );
  });


  // === "top_seller" hit
  $('.jq_top_seller_checkbox').bind("click" ,  function(){

    reset_pagination_vars();

    if( $(this).attr("checked") ){
      top_seller_val = 1;
    } else {
      top_seller_val = "";
    }

    load_filters_page( "top_seller", top_seller_val );
  });


  // === "other" hit
  $('.jq_special_checkbox').bind("click" ,  function(){
     reset_pagination_vars();

     if( $(this).attr("checked") ){
        special_val = 1;
      } else {
        special_val = "";
      }
      load_filters_page( "special", special_val );
  });


  // === "price" checkbox hit
  $('.jq_price_filter_checkbox').bind("click" ,  function(){
      reset_pagination_vars();

      if( $(this).attr("checked") ){
        // -js- untick all prices
        $('.jq_price_filter_checkbox').attr("checked",false);
        // -js- tick current price
        $(this).attr("checked",true);
        // -js- refresh products list
        price_val = $(this).attr("name");
      } else {
        price_val = "";
      }
      load_filters_page( "price" , price_val );
  });


  // === "brand" checkbox hit
  $('.jq_brand_filter_checkbox').bind("click" ,  function(){

      reset_pagination_vars();

      // rebuild brand value
      brand_val = "";
      $(".jq_brand_filter_checkbox").each(function(){
        if( $(this).attr("checked") ){
          if( brand_val ) brand_val += "#";
          brand_val += $(this).attr("name");
        }
      });

      load_filters_page( "brand" , brand_val );
  });


  //=== "feature" checkbox hit
  $('.jq_feature_filter_checkbox').bind("click" ,  function(){

      reset_pagination_vars();

      // rebuild feature_val
      feature_val = -1;
      $(".jq_feature_filter_checkbox").each(function(){
        if( $(this).attr("checked") ){
          if( feature_val ) feature_val += ",";
          feature_val += $(this).attr("name");
        }
      });


      load_filters_page( "feature", feature_val );
  });

}


$(document).ready(function() {

  // === dynamically load the filters page (If div is present)
  if ( $("#products_and_filters").length > 0 ) {
    load_filters_page( false, false );
  }

  // === debug
  $('.jq_top_seller_val').bind("click" ,  function(){
    alert( top_seller_val );
  });

  // == 
  $(document).pngFix();
  
  // === Basket Hover
  $(".basket-top").hover(function() {
		$("#my-basket").addClass("my-basket_active");
    $("#myBasketLink").addClass("myBasket_active");
		$(".basket-top").show();
//		$("#sales-hotline").hide();
  },function(){
    $(".basket-top").css("display", "none" );
		$("#my-basket").removeClass("my-basket_active");
    $("#myBasketLink").removeClass("myBasket_active");
		$("#myBasketItemsLink").removeClass("myBasketItems_active");
//		$("#sales-hotline").show();
  });  

  $("#my-basket").hover(function() {
//		$("#sales-hotline").hide();
		$("#my-basket").addClass("my-basket_active");
    $("#myBasketLink").addClass("myBasket_active");
		$("#myBasketItemsLink").addClass("myBasketItems_active");
    $(".basket-top").show();
  },function(){
		$(".basket-top").hide();
		$("#my-basket").removeClass("my-basket_active");
		$("#myBasketLink").removeClass("myBasket_active");
		$("#myBasketItemsLink").removeClass("myBasketItems_active");
//		$("#sales-hotline").show();
    
  });

  // === Top seller drop down ( Block top sellers )
  $('.jq_top_seller_drop').change( function(event){
	  fr_url = $(this).attr('value');
    window.location = "/fset.php?cat=" + fr_url;
  });

  // === Thumb Hover ( Product Details page )
  $(".jq_prod_details_thumb").hover(
    function () {
      img_big = "images/big/" + $(this).attr('name');
      img_super = "images/super/" + $(this).attr('name');
      style_to_add = 'background-image: url( ' +  img_big + ')'
      $('.main_big_image').attr( "style" , style_to_add );
      $('.enlarge_link').attr( "href" , img_super );
    },
    function () {
    }
  );

  // Specification Open / Close ( Product details page )
  $('.jq_spec_closed').bind("click" ,  function(){
    $('.jq_spec_closed').hide();
    $('.jq_spec_opened').show();
    $('#spec_table').show();
  });
  $('.jq_spec_opened').bind("click" ,  function(){
    $('.jq_spec_closed').show();
    $('.jq_spec_opened').hide();
    $('#spec_table').hide();
  });

  // === RH block - free delivery
  $('.jq_delivery_recheck').bind("click" , function(){
    $('.delivery_no').hide();
    $('.delivery_yes').hide();
    $('.jq_form_field').attr( 'value' , "" );
    $('.post_code_check').fadeIn("slow");
  });
  
  // === RH Block - Deliver Post code check
  $('.jq_delivery_submit').bind("click" ,  function(){
      //Get post code
      var post_code = $('#delivery_postcode').attr("value");
      if(post_code){

          post_code = post_code.toUpperCase();
      
          //Validate
          //Get part 1 (all or up to space)
          var mySplitResult = post_code.split(" ");
          post_code = mySplitResult[0];


          //valid ?
          if( post_code.length < 3 || post_code.length > 4 ){
            //Ensure 3 or 4 chars long
            if( post_code.length < 3 ){
              alert("The post code appears too short");
            }
            if( post_code.length > 4 ){
              alert("The post code appears too long. Ensure you have added a space (e.g. DA11 0PA)");
            }
          } else {
            //Check if it's in list          
            $('.post_code_check').hide();

            found = false;
            for (i=0;i<arr_post_codes.length;i++){
              if( post_code == arr_post_codes[i] ){
                found = true;
              }
            }
            if( found ){
              $('.delivery_yes').fadeIn("slow");
            } else {
              $('.delivery_no').fadeIn("slow");
            }
          }
      }

  })


  // === RH Block - Newsletter
  $('.jq_newsletter_name').bind("click" ,  function(){
    if( $(this).attr( "value" ) == "Name..." ){
      $(this).attr( "value", "" );
    }
  })
  // === RH Block - Newsletter
  $('.jq_newsletter_email').bind("click" ,  function(){
    if( $(this).attr( "value" ) == "Email address..." ){
      $(this).attr( "value", "" );
    }
  })

  // === Quick Find Hover - Show / Hide
  $('.jq_quick_find').hover(
    function () {

      version = jQuery.browser.version;
      if( version == "16.0" ){

      } else {
        $('#quick_find').show();
        $('.quick_find').addClass("nav_on_quick_find");        
      };

    }, 
    function () {

      if( version == "16.0" ){

      } else {
        $(this).removeClass("nav_on_quick_find");
        $('#quick_find').hide();
      };

    }
  );

  // === Quick Find Hover etc
  $('#quick_find').hover(
      function () {
        $('#quick_find').show();
        $('.quick_find').addClass("nav_on_quick_find");
      }, 
      function () {
        $('.quick_find').removeClass("nav_on_quick_find");
        $('#quick_find').hide();
      }
    );


   // === Tabbed sections
   $('#tabbed_section2 li').bind("click", function(){

     tab_index = $( this ).attr("name");
     $( ".tab_section" ).hide();
     $( ".tab_section_"+tab_index ).show();

     current_class = $('#tabbed_section2').attr("class");
     $('#tabbed_section2').removeClass(current_class);
     $('#tabbed_section2').addClass("tab_"+tab_index);

   });
   
});



