$(document).ready(function(){	

	if ($(".tabs").length>0)
	{
		$("#japanese-yen").hide();
		
		$(".tabs ul a").click( function() {
			$(".tabs ul a").removeClass("active");
			$("#australian-dollars, #japanese-yen").hide();
			id = $(this).attr("href");
			$(id).show();
			$(this).addClass("active");
			return false;	
		});
	}

	if ($("#slider").length>0)
	{
		$("#slider").cycle({
			fx: 'scrollLeft',
			timeout: 6000,
			pager: '#pager'
		});
	}

	if ($(".home #gallery").length>0)
	{
		$("#gallery #slide-container").cycle({
			fx: 'scrollHorz',
			timeout: 0,
			prev: '#prev',
			next: '#next'
		});
	
		$("#gallery #slide-container a.fancybox").fancybox();
	}


	if ($(".home .fb-livestream").length>0)
	{
		$(".fb-livestream").attr('href','http://cdn.livestream.com/embed/rhythmsnowsports?layout=4&amp;height=340&amp;width=560&amp;autoplay=true');
		$(".fb-livestream").fancybox({
			'type'			: 'iframe',
			'overlayColor'	: '#000',
			'overlayOpacity': 0.8
		});
	}
	
	$('#dob').datepicker({
				changeMonth: true,
				changeYear: true,
				yearRange: '1910:+0',
				dateFormat: 'dd/mm/yy'
			});
	$('#cf_arrival_date, #cf_departure_date').datepicker({
			changeMonth: true,
			changeYear: true,
			yearRange: '+0:+3',
			dateFormat: 'dd/mm/yy',
			beforeShowDay: availableDays
//			minDate: new Date()
//			minDate: new Date(2011, 11-1, 20),
//			maxDate: new Date(2012, 4-1, 20),
		});

var days = [
[11, 20],[11, 21],[11, 22],[11, 23],[11, 24],[11, 25],[11, 26],[11, 27],[11, 28],[11, 29],[11, 30],
[12, 1],[12, 2],[12, 3],[12, 4],[12, 5],[12, 6],[12, 7],[12, 8],[12, 9],[12, 10],[12, 11],[12, 12],[12, 13],[12, 14],[12, 15],[12, 16],[12, 17],[12, 18],[12, 19],[12, 20],[12, 20],[12, 21],[12, 22],[12, 23],[12, 24],[12, 25],[12, 26],[12, 27],[12, 28],[12, 29],[12, 30],[12, 31],
[1, 1],[1, 2],[1, 3],[1, 4],[1, 5],[1, 6],[1, 7],[1, 8],[1, 9],[1, 10],[1, 11],[1, 12],[1, 13],[1, 14],[1, 15],[1, 16],[1, 17],[1, 18],[1, 19],[1, 20],[1, 20],[1, 21],[1, 22],[1, 23],[1, 24],[1, 25],[1, 26],[1, 27],[1, 28],[1, 29],[1, 30],[1, 31],
[2, 1],[2, 2],[2, 3],[2, 4],[2, 5],[2, 6],[2, 7],[2, 8],[2, 9],[2, 10],[2, 11],[2, 12],[2, 13],[2, 14],[2, 15],[2, 16],[2, 17],[2, 18],[2, 19],[2, 20],[2, 20],[2, 21],[2, 22],[2, 23],[2, 24],[2, 25],[2, 26],[2, 27],[2, 28],[2, 29],[2, 30],[2, 31],
[3, 1],[3, 2],[3, 3],[3, 4],[3, 5],[3, 6],[3, 7],[3, 8],[3, 9],[3, 10],[3, 11],[3, 12],[3, 13],[3, 14],[3, 15],[3, 16],[3, 17],[3, 18],[3, 19],[3, 20],[3, 20],[3, 21],[3, 22],[3, 23],[3, 24],[3, 25],[3, 26],[3, 27],[3, 28],[3, 29],[3, 30],[3, 31],
[4, 1],[4, 2],[4, 3],[4, 4],[4, 5],[4, 6],[4, 7],[4, 8],[4, 9],[4, 10],[4, 11],[4, 12],[4, 13],[4, 14],[4, 15],[4, 16],[4, 17],[4, 18],[4, 19],[4, 20]
];

function availableDays(date) {
    for (i = 0; i < days.length; i++) {
      if (date.getMonth() == days[i][0] - 1
          && date.getDate() == days[i][1]) {
        return [true, ''];
      }
    }
  return [false, ''];
}



	$.validator.addMethod(
        "australianDate",
        function(value, element) {
            // put your own logic here, this is just a (crappy) example
            return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
        },
    	"Please enter a date in the format dd/mm/yyyy"
    );	
	$("#renter-form").validate({
	    rules: {
	      "item_options[cf_dateofbirth]": {
	      	required: true,
	        australianDate: true
	      }
	    }
	});
	$("#checkout-form").validate({
	    rules: {
	      "cf_arrival_date": {
	        australianDate: true
	      },
	      "cf_departure_date": {
	        australianDate: true
	      },
	      "confirm_email_address": {
			required: true,
			equalTo: "#email_address"
		  }
	      
	    }
	});
		
	$("#contact-form").validate({
	    rules: {
	      "confirm_email": {
			required: true,
			equalTo: "#email"
		  }
	      
	    }
	});
	
	if ( $('#which-level-text').length > 0 )
	{
		$('#which-level-text').hide();
		$('#which-level').click( function() {
			$('#which-level-text').toggle('slow');
			return false;
		});
	}
	
	$('.hire .moreInfo').hide();
	
	$('.hire a.more').click( function() {
		$('.moreInfo',$(this).parents('li')).toggle('slow');
		if ( $(this).text() == 'More information' )
		{
			$(this).text('Less information');
		}
		else
		{
			$(this).text('More information');
		}
	});
	
	if ($(".item-price").length>0)
	{
		$(".item-price").change( function() {
			total = parseFloat('0');
			$(".item-price").each( function() {
				if ($(this).val() != "")
				{
					el = $(this).val().split("_")
					total = total + parseFloat(el[2]);
				}
			} );
			$('#price').text("$"+total);
			if (total == 0) total = '';
			$('#temp-price').val(total);
		} );
		
	}
	
	if ($(".timed-collapse").length>0)
	{
		setTimeout(function() {
		   $(".timed-collapse").slideUp()
		 }, 5000);
	}
	if ($("#coupon-form").length>0)
	{
		$('#coupon-form').submit(function(){
			if ($('#coupon_code').val() == '')
			{
				$('#coupon_code_error').show();
				return false;
			}
			else {
				$('#coupon_code_error').hide();
			}
		})
	}
	$(window).scroll(
    	function() {
    		
    		var top = 0;
    		
    		top = $(window).scrollTop();
    		
            if (top > 130) {
               $("#cartPanel").css("top", 20);
            } else {
           	   $("#cartPanel").css("top", 150-top);
            } 
            
    	}
   	);
   	
   	if ($("#map").length>0)
   	{
   			// Google Maps
   		  	var address = 'Niseko, Hokkaidō Prefecture, Japan';
   		  	var geocoder; 	
   		 	var myOptions = {
   				zoom: 15,
   				center: new google.maps.LatLng(42.861118,140.704136),
   				mapTypeControl: false,
   				mapTypeId: google.maps.MapTypeId.ROADMAP
   			}

   			var map = new google.maps.Map(document.getElementById("map-frame"), myOptions);
   		 	var latlng = new google.maps.LatLng(42.861118,140.704136);


			map.setCenter(new google.maps.LatLng(42.861118,140.704136));

			 var image = new google.maps.MarkerImage("/assets/images/google-arrow.png",
			      // This marker is 20 pixels wide by 32 pixels tall.
			      new google.maps.Size(126, 56),
			      // The origin for this image is 0,0.
			      new google.maps.Point(0,0),
			      // The anchor for this image is the base of the flagpole at 0,32.
			      new google.maps.Point(63, 40));
			      
			var marker = new google.maps.Marker({ 
				map: map, 
				position: new google.maps.LatLng(42.859191,140.704619),
				title: "Rhythm Snowsports Japan"
			});

			var marker = new google.maps.Marker({ 
				map: map, 
				position: new google.maps.LatLng(42.862542,140.703664),
				title: "Rhythm Snowsports Japan"
			});
			
			

   		
   	}
   	

});
