$(document).ready(function(){
	var links = $('ul#links-container li');
	jQuery.each(links, function() {
		if ($(links).index(this) % 2 == 1) {
			$(this).css({'background':'#f2f2f2'});
		}
	});
	var gridAlbums = $('.photo-container');
	jQuery.each(gridAlbums, function() {
		var img = $('img', this)[0];
		var newSrc = $(img).attr('src').replace(/thumb_/, "");
		//$(img).wrap("<div class='grid-album-img-mask'></div>");
		$(img).wrap("<a href='"+newSrc+"' class='thickbox' rel='gallery-album'></a>");

		var click = "<a href='" + newSrc + "' class='thickbox' rel='gallery-album'>";
		click += '<div class="enlarge-button">Click here to enlarge</div>';
		click += '</a>';
		
		//$('.grid-description', this).append(click);
		$(this).bind("mouseenter",function(){
			$(img).css({opacity:0.7});
			//$('.grid-description', this).fadeIn();
    	}).bind("mouseleave",function(){
			$(img).css({opacity:1});
			//$('.grid-description', this).hide();
		});
	});
	$('a.thickbox').fancybox({
			'zoomOpacity'			: true,
			'overlayShow'			: false,
			'zoomSpeedIn'			: 500,
			'zoomSpeedOut'			: 500
	});
	$('#index-images').cycle({
        delay: 5000,
        speed: 1000
  });
	var mind = 0;
	var d = new Date();
	var curr_hour = d.getHours();
	if (curr_hour > 11) mind = 1;
	$("#date_1_cal").datepicker({altField: '#date_1', minDate: mind, beforeShowDay: noOffDays});
	$("#date_2_cal").datepicker({altField: '#date_2', minDate: mind+1, beforeShowDay: noOffDays});
});
function noOffDays(date) {
	var day = date.getDay();
	return [(day > 0 && day < 7), ''];
}
function setIDs() {
	var form = $('form')[0];
	var items = $('.order-view-cart-qty-input');
	var idsArray = new Array;
	var i = 0;
	jQuery.each(items, function() {
		idsArray[i] = $(this).attr('id').split('_')[1] + "," + $(this).val();
		i++;
	});
	idsStr = idsArray.join(";");
	$('#ids').val(idsStr);
	return true;
}
function updateChocolate() {
	if ($('#chocolate').attr('checked')) {
		$('#choco-options').show();
	}
	else {
		$('#choco-options').hide();
	}
}
function fValidate() {
	var canSubmit = true;
	var validCC = true;
	jQuery.each($('.required'), function() {
		$(this).removeClass('badInput');
		if ($(this).val() == "") {
			canSubmit = false;
				$(this).addClass('badInput');
		}
		if ($(this).attr('id').indexOf('cc_num') != -1) {
			validCC = checkCC($(this).val());
			if (!validCC) $(this).addClass('badInput'); 
		}
	});
	if (canSubmit == true && validCC == true) {
		$('form').submit();
	}
	else {
		$('#flash').html("Please make sure all required fields are filled out and contain valid information.").fadeIn();
	}
}
function checkCC(s) {
  var i, n, c, r, t;
	//amex = 3, 15 long
	//mc = 5, 16 long
	//visa = 4, 13 or 16 long
  // First, reverse the string and remove any non-numeric characters.
	if ($('#cc_type').val() == "amex") {
		if (s.length != 15 || s.charAt(0) != 3) return false;
	}
	else if ($('#cc_type').val() == "mc") {
		if (s.length != 16 || s.charAt(0) != 5) return false;
	}
	else { //is visa
		if ((s.length != 16 && s.length != 13) || s.charAt(0) != 4) return false;
	}
  r = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      r = c + r;
  }

  // Check for a bad string.

  if (r.length <= 1)
    return false;

  // Now run through each single digit to create a new string. Even digits
  // are multiplied by two, odd digits are left alone.

  t = "";
  for (i = 0; i < r.length; i++) {
    c = parseInt(r.charAt(i), 10);
    if (i % 2 != 0)
      c *= 2;
    t = t + c;
  }

  // Finally, add up all the single digits in this string.

  n = 0;
  for (i = 0; i < t.length; i++) {
    c = parseInt(t.charAt(i), 10);
    n = n + c;
  }

  // If the resulting sum is an even multiple of ten (but not zero), the
  // card number is good.

  if (n != 0 && n % 10 == 0)
    return true;
  else
    return false;
} 
function checkLocation() {
	geocoder = new GClientGeocoder();
	dStr = document.forms[0].address_1.value + " " + document.forms[0].address_2.value + " " + document.forms[0].city.value + ", " + document.forms[0].state.value + " " + document.forms[0].zip.value;
	geocoder.getLocations(dStr, function (response) {
		if (!response || response.Status.code != 200)  {
			//do nothing. couldnt' geocode... but let's move on
			fValidate();
		}
		else  {
			deliveryLocation = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
			fioriLocation = {lat:40.024698, lon:-105.282013, address:"2620 Broadway Boulder, CO 80304"};
			calculateDistance();
		}
	});
}
function calculateDistance()
{
	try
	{
		var glatlng1 = new GLatLng(deliveryLocation.lat, deliveryLocation.lon);
		var glatlng2 = new GLatLng(fioriLocation.lat, fioriLocation.lon);
		var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(1);
		var cost = 0;
		if (miledistance < 9) {
			cost = 9;
		}
		else if (miledistance >= 9 && miledistance < 16) {
			cost = 18;
		}
		else if (miledistance >= 16 && miledistance <21) {
			cost = 25;
		}
		else {
			cost = 28;
		}
		$('#location_distance').val(miledistance);
		$('#location_cost').val(cost);
	}
	catch (error)
	{
		//do nothing.
	}
	fValidate();
}