function updateImage(shape, finish)
{
  imageShape  = shape.val();
  imageFinish = finish.val();

  defaultShape  = 'Corsican';
  defaultFinish = 'Iron';
  
  if(imageShape == '')
  {
    imageShape = defaultShape;
  }
  
  if(imageFinish == '')
  {
    imageFinish = defaultFinish;
  }
  
  newImage = cleanImageName('/assets/images/readyhang-product-image/' + imageShape + '_' + imageFinish + '.jpg');
  
  $('#product-image').attr('src', newImage);
}

function cleanImageName(string)
{
  return string.split(' ').join('').toLowerCase();
}

function getNewDate()
{
  date = new Date();
  return date.getTime();
}

jQuery(document).ready(function($){
  
  shapeSelect   = $('select[name=FinialShape]');
  finishSelect  = $('select[name=Finish]');
  
  //Shape Change
  $(shapeSelect).change(function() {
    result = $(this).val();
    
    $.get('/tpl-readyhang/product-options.php', { finial: result, nt: getNewDate() }, function(data) {
      $(finishSelect).html(data);
      updateImage(shapeSelect, finishSelect);
    });
  });
  
  $(finishSelect).change(function() {
    updateImage(shapeSelect, finishSelect);
  });
});

jQuery(document).ready(function($)
{
  var OutOfStock = false;
  
  var SmallString = $('#small-out-of-stock').text();
  var LargeString = $('#large-out-of-stock').text();

  SmallString = SmallString.split(',');
  LargeString = LargeString.split(',');
      
  var Small = $.protify(SmallString);
  var Large = $.protify(LargeString);

  function inStock(shape, finish, size)
  {
    currentSelection = cleanImageName(shape + '_' + finish);

    if(size == '28-48 in.')
    {
      if( Small.include(currentSelection) )
      {
        OutOfStock = true;
      } else {
        OutOfStock = false;
      }
    }

    if (size == '36-76 in.')
    {
      if( Large.include(currentSelection) )
      {
        OutOfStock = true;
      } else {
        OutOfStock = false;
      }
    }

    return OutOfStock;
  }
  
  $('#purchase-form select').change(function() {
    shapeVal  = $('select[name=FinialShape]').val();
    finishVal = $('select[name=Finish]').val();
    sizeVal   = $('select[name=Size]').val();

    OutOfStock = inStock(shapeVal,finishVal,sizeVal);
 
    if( OutOfStock )
    {
      $('#out-of-stock-message').fadeIn('slow');
    } else {
      $('#out-of-stock-message').hide();
    }
  });

  $("#purchase-form").validate({
    errorLabelContainer: $("#message"),
    rules: {
      FinialShape: {
        required: true
      },
      Finish: {
        required: true
      },
      Size: {
        required: true
      },
      Quantity: {
        required: true,
        min: 1
      }
    },
    submitHandler: function(form) {
      shapeVal  = $('select[name=FinialShape]').val();
      finishVal = $('select[name=Finish]').val();
      sizeVal   = $('select[name=Size]').val();

      OutOfStock = inStock(shapeVal,finishVal,sizeVal);

      if( OutOfStock )
      {
        alert("The item you selected is not currently in stock. Please select a different item.");
      } else {
        var productName = 'The ReadyHang System (' + 'Finial Shape: ' + shapeVal + ', Finish: ' + finishVal + ', Size: ' + sizeVal + ')';

        $('input[name=item_name]').attr('value', productName);

        if(sizeVal == '36-76 in.')
        {  
          $('input[name=amount]').attr('value', '49.99');
        }

        form.submit();
      }
   }
  });

});

jQuery(document).ready(function($) {
	var blank = new Image();
	blank.src = 'assets/images/blank.gif';
 
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {

		$('img[src$=.png]').each(function() {
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			} else {
				fixPng(this);
			}
		});
	}
 
	function fixPng(png) {
		var src = png.src;
		if (!png.style.width) { png.style.width = $(png).width(); }
		if (!png.style.height) { png.style.height = $(png).height(); }
		png.onload = function() { };
		png.src = blank.src;
		png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='image')";
	}
 
});

jQuery(document).ready(function($) {
  $("#player").flowplayer('/flowplayer/flowplayer.commercial-3.0.7.swf');

});

