﻿    $(function () {

        $('#Styles').change(function () { window.location = "/" + $(this).val(); });

        $("#slider-range").slider({
            range: true, min: 60, max: 180, values: [64, 145],
            slide: function (event, ui) {
                $("#amount").val(getBoardSize(ui.values[0]) + ' - ' + getBoardSize(ui.values[1]));
            },
            stop: function (event, ui) { updateResults(); }
        });

        $("#price-range").slider({
            range: true,
            min: 50,
            step: 50,
            max: 1500,
            values: [100, 1575],
            slide: function (event, ui) {
                $("#price").val('$' + ui.values[0] + ' to $' + (ui.values[1]));
            },
            stop: function (event, ui) { updateResults(); }
        });

        $("#amount").val(getBoardSize($("#slider-range").slider("values", 0)) + ' to ' + getBoardSize($("#slider-range").slider("values", 1)));
        $("#price").val('$' + $("#price-range").slider("values", 0) + ' to $' + $("#price-range").slider("values", 1));
        $('#amount, #price').focus(function () { $(this).blur(); });

        $('#boardContent a.close').click(function () { $('#boardContent').hide(); $('#results').show(); return false; });

         $("#loading").ajaxStart(function(){ $(this).fadeIn(); });

        setupLinks();
         
    });

function setupLinks() {
    // Load surfboard content;
    $('#boardList a').click(function () {
        
       var myUrl =  $(this).attr("href");
          $.ajax({
            url: $(this).attr("href"),
            success: function (data) {
           
             $('#boardData').html(data);
               
              var images = $(data).find('img');
              var imageCount = images.length;
              var loaded = 0;
              
              if (imageCount>0)
              {
               //load board images
                 images.load(function () {
                ++loaded;              
                       if (loaded >= imageCount) {   
                                $('#BoardImages a').lightBox();      
                                }
                    })
              }
            
            $("#loading").hide(); 
            $('#results').hide();
            $('#boardContent:hidden').show(); 
            $("#specifications tr:odd th, #construction tr:odd th").addClass("specalt"); 
            $("#specifications tr:odd td, #construction tr:odd td").addClass("altrow"); 
              $('#BoardImages a, a.lightBox').lightBox();      
            
            },
            
          });
          return false;
        });
}
	
	var prevKey='';
	var key = '';
	function updateResults()
	{
	var minPrice = $("#price-range").slider("values", 0);
	var maxPrice =$("#price-range").slider("values", 1) ;
	var minHeight = $("#slider-range").slider("values", 0);
	var maxHeight = $("#slider-range").slider("values", 1) ;
  key = minPrice+maxPrice+minHeight+maxHeight;
	if (key != prevKey) {
 
   $('#results').hide();
	 $.get("/", {minPrice: minPrice
	                   , maxPrice: maxPrice
	                   , minHeight: minHeight
	                   , maxHeight:maxHeight
	                   , Style:  $("#Styles").val()}
	                   , function(data){
                              $('#results').html(data);
                           
                                var images = $(data).find('img');
                                var imageCount = images.length;
                                var loaded = 0;
                                 if (imageCount>0) {
                                         images.load(function () {
                                          ++loaded;              
                                                 if (loaded >= imageCount) {$("#loading").hide();   $('#boardContent:visable').hide();     $('#results').show();  }
                                              })
                                      setupLinks();   
                                 }
                                 else
                                 {
                               
                                 $("#loading").hide();   $('#boardContent:visable').hide();     $('#results').show(); 
                                 }
                                $("#loading").hide();   $('#boardContent:visable').hide();     $('#results').show(); 
	                   });
	                  prevKey = key; 
	       }
	}
	
	function  getBoardSize(height){
	   var feet = Math.round((height-6)/12);
	   var inches = parseInt(height) - (feet * 12);
	   return feet + "' " + inches +  "''";
	}