﻿
$(function() {


  // Simple accordion for product details  
  
  SimpleAccordion = function(containerId) {
  
    var index = -1;
    var current = 0;
    var effectDuration = 1000;
    
    var container = $(containerId);
       
    $('.toggler', container).each(function() {      
      index++;      
      $(this)
        .attr('acindex', index)
        .attr('acocol', $(this).css('backgroundColor'))
        
        .click(function(event) {
          event.preventDefault();
          newIndex = $(this).attr('acindex');
          if(newIndex == current) return;
          
          $('.accordion:eq('+current+')', container).animate({ height: 'hide', opacity: 'hide'}, effectDuration);
          $('.accordion:eq('+newIndex+')', container).animate({ height: 'show', opacity: 'show'}, effectDuration);
          
          $('.toggler:eq('+current+')', container)            
            .animate({ 'backgroundColor': $('.toggler:eq('+current+')', container).attr('acocol')})
            .find('a')
              .css({fontWeight:'normal', 'color': '#222'});
            
          $('.toggler:eq('+newIndex+')', container)            
            .animate({ 'backgroundColor': '#ffffff'})
            .find('a')
              .css({fontWeight:'bold', 'color':'#d8040c'});
          
          current = newIndex;
        })
        
    });
    
    $('.toggler:eq(0)', container).css({backgroundColor: '#ffffff', fontWeight:'bold'});
    $('.toggler:gt(0) a', container).css({fontWeight:'normal', 'color': '#222'});
    $('div.accordion:gt(0)', container).hide();
    $('div.accordion .accordionTitle', container).hide();
    
  };
  
  
  
  
  
  DynamicProductList = function() {
  
    var effectsSpeed = 750;
    var htmlLoaded = false;
    var fadeComplete = false;
    var loadHtml = '';
    
    function init() {

			if($.fn.Tooltip) {
				$('.dynamicproductlist img').Tooltip(
					{
						delay: 0,
						track: true,
						showURL: false,
						fixPNG: false,
						bodyHandler: function() {return '<h3>' + this.tooltipText + '</h3>';}
						
					});
					
				$('#tooltip div').css('behavior', 'url(/static/iepngfix/iepngfix.htc)');
				$('#tooltip h3').css('behavior', 'url(/static/iepngfix/iepngfix.htc)');
			}
    }
    
    init();
    
    $('.product-manual-search').hide();
    
    if($.browser.ie && parseInt($.browser.version) < 7) {
    
      $('.productfilter-brand').click(function() {    
        updateProducts();
      });
      
      $('.productfilter-category').click(function() {
        updateProducts();
      });
    }
    else {
      $('.productfilter-brand').change(function() {    
        updateProducts();
      });
      
      $('.productfilter-category').change(function() {
        updateProducts();
      });
    }
    
    $('.productfilter :checkbox').click(function() {    
      updateProducts();
    });
  
    function updateProducts() {
    
      htmlLoaded = false;
      fadeComplete = false;
      
      $('.dynamicproductlist').fadeOut(effectsSpeed, onFadeComplete);
    
      var colFilters = new Array();
      
      $('.productfilter :checkbox').each(function() {
        if($(this)[0].checked)
          colFilters[colFilters.length] = $(this).parent().attr('ovalue');
      });
     
      
      $.ajax({
        async: true,
        type: "POST",
        data: {
          brandId: $('.productfilter-brand').val(),
          categoryId: $('.productfilter-category').val(),
          colFilters: colFilters          
          },
        url: '/products/service.aspx',
        success: onLoadComplete
       });
    }
    
    function onFadeComplete() {
      fadeComplete = true;
      $(this).remove('ul');
      if(htmlLoaded) show();
    }
    
    function onLoadComplete(html, status) {
      loadHtml = html;
      htmlLoaded = true;
      if(fadeComplete) show();
    }
    
    function show() { 
      $('.dynamicproductlist')
        .empty()
        .append(loadHtml)
        .fadeIn(effectsSpeed);
        
      init(); 
      
    }
    
    
    
    
  };
  
  
  new SimpleAccordion('.product-accordion');
  
  new DynamicProductList();
  
  
});

