$(document).ready( function(){ 

  // set up light box
  $('a.lightbox').lightBox({
    imageLoading: '/images/lightbox-ico-loading.gif',
    imageBtnClose: '/images/lightbox-btn-close.gif',
    imageBtnPrev: '/images/lightbox-btn-prev.gif',
    imageBtnNext: '/images/lightbox-btn-next.gif'
  })
	
  // setup rental login
  $('#rentalUsername').focus(function(){
    
    $('#rentalUsername').val('');
    
    $('#rentalUsername').css('color', '#000');
    
  });
  
  $('#rentalUsername').blur(function(){
    
    if($('#rentalUsername').val() == '')
    {
      $('#rentalUsername').val('username');
      
      $('#rentalUsername').css('color', '#CCC');
    }
    
  });
  
  $('#rentalPassword').focus(function(){
    
    $('#rentalPassword').val('');
    
    $('#rentalPassword').css('color', '#000');
    
  });
  
  $('#rentalPassword').blur(function(){
    
    if($('#rentalPassword').val() == '')
    {
      $('#rentalPassword').val('password');
      
      $('#rentalPassword').css('color', '#CCC');
    }
    
  });
  
  // newsletter area
  $('#reg_email').focus(function(){
    
    if($(this).val() == 'Email Address')
    {
      $(this).val('');
      
      $(this).removeClass('empty');
    }
    
  });
  
  $('#reg_email').blur(function(){
    
    if($(this).val() == '')
    {
      $(this).val('Email Address');
      
      $(this).addClass('empty');
    }
    
  });
  
});


/*
  newsletter email submission
*/
function newsletterSignup()
{
  // get email address
  reg_email = $('#reg_email').val();
  
  if(reg_email != '' && reg_email != 'Email Address')
  {
    $.post('/mail/newsletterSignup', {
      'reg_email': reg_email 
    },
    function(){
      $('div#newsletter').html('Thanks for signing up!');
    });
  }
  
}

/*
  setup cust calc modal
*/
$(function(){
  
  var quantity = $('#custQuantity'),
    width = $("#width"),
    width_in = $("#width_in"),
    height = $("#height"),  
    height_in = $("#height_in"),
    allFields = $([]).add(quantity).add(width).add(width_in).add(height).add(height_in),
    tips = $("#validateTips");
  
  // setup custom calc selects
  $('select.customSelect').each(function(i){

    $(this).change(function(){
      getOptionPrice(this, i);
    });
    
    getOptionPrice(this, i);

  });
  
  $('#custCalcTable input').blur(function(){
    calcPrice();
  });

  function updateTips(t)
  {
    tips.text(t);
  }

  function checkLength(o,n,min,max)
  {
  	if ( o.val().length > max || o.val().length < min )
  	{
  		o.addClass('ui-state-error');
  		updateTips(n + " can't be empty");
  		return false;
  	}
  	else
  	{
  		return true;
  	}
  }

  function checkRegexp(o,regexp,n)
  {
    if ( !( regexp.test( o.val() ) ) )
    {
      if(n != '')
      {
        o.addClass('ui-state-error');
        updateTips(n);
      }
      
      return false;
    }
    else
    {
      return true;
    }
  }
  
  

  
  
  function calcPrice()
  {
    cleanVars();
    
    if(quantity.val() != '' && quantity.val() != 0)
    {
		
  	  var unit_price = 	$('#unitPrice').val();

      var prod_sub = quantity.val() * unit_price;
      

	  
      $('#prodSubTotal').html(unit_price);
      

      // if(width.val() != '' && width.val() != 0 && height.val() != '' && height.val() != 0)
      if(width.val() != '' && width_in.val() != '' && height.val() != '' && height_in.val() != '')
      {
        var tot_size = (parseInt(width.val()) + (width_in.val() / 12)) * (parseInt(height.val()) + (height_in.val() / 12));
        
        $('#totSize').val(tot_size.toFixed(2));
        
        var total = prod_sub * tot_size;
        
        $('#totBase').val(total.toFixed(2));
		

        
        $('input.optionPrice').each(function(i){
				
				
				   var line_total;
				   
			if ($("select#option_lines_0").val()==13) {									 
           line_total = $(this).val() * quantity.val();
		   
			} else {
				           line_total = $(this).val() * tot_size * quantity.val();

			}
		   
		   
		   
          total += line_total;
          $('#option_calc_0').val(line_total.toFixed(2));
		  
	
		  
        });
        

        $('span#totalPrice').html(total.toFixed(2));
		
      }
    }
  }
  
  function cleanVars()
  {


    if(!checkRegexp(quantity,/^([0-9])+$/,""))
    {
      quantity.val(1);
    }
    else if(quantity.val() == '0')
    {
      quantity.val(1);
    }
    
    if(!checkRegexp(width,/^([0-9])+$/,""))
    {
      width.val(0);
    }
    
    if(!checkRegexp(width_in,/^([0-9])+$/,""))
    {
      width_in.val(0);
    }
    
    if(!checkRegexp(height,/^([0-9])+$/,""))
    {
      height.val(0);
    }
    
    if(!checkRegexp(height_in,/^([0-9])+$/,""))
    {
      height_in.val(0);
    }
  }
  
  function getOptionPrice(select, index)
  {
    $.get('/store/getOptionPrice', {
      option_line_id: $(select).val(),
      index: index
    },
    function(data){
      // alert(data.index);
      $('#option_price_' + data.index).val(data.price.toFixed(2));
      
      calcPrice();
    },
    'json');
  }
  
  $('div#custCalcWrap').dialog({
    resizable: false,
    autoOpen: false,
    bgiframe: true,
    width: 780,
    modal: true,
    buttons: {
      'Buy Now': function(){
        var bValid = true;
        allFields.removeClass('ui-state-error');

        bValid = bValid && checkLength(quantity,"Product quantity",1,4);
        bValid = bValid && checkLength(width,"Width in feet",1,3);
        bValid = bValid && checkLength(width,"Width in inches",1,3);
        bValid = bValid && checkLength(height,"Height in feet",1,3);
        bValid = bValid && checkLength(height,"Height in inches",1,3);

        // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
        bValid = bValid && checkRegexp(quantity,/^([0-9])+$/,"Product quantity must be a number");
        bValid = bValid && checkRegexp(width,/^([0-9])+$/,"Width in feet must be a number");
        bValid = bValid && checkRegexp(width_in,/^([0-9])+$/,"Width in inches must be a number");
        bValid = bValid && checkRegexp(height,/^([0-9])+$/,"Height in feet must be a number");
        bValid = bValid && checkRegexp(height_in,/^([0-9])+$/,"Height in inches must be a number");

        if (bValid) {
          // alert('Yay!')
          
          // submit form
          document.cust_options.submit();
        }
        else
        {
          // alert('Boo!');
        }
      },
      Calculate: function(){
        calcPrice();
      }
    },
    close: function(){
      allFields.val('').removeClass('ui-state-error');
    }
  });
  
  $('#customSizeButt').click(function(){
    $('div#custCalcWrap').dialog('open');
  });
  
});

