$(document).ready(function(){

	$('p.phoneNumber').hover(function(){
		$('.letters').hide();
		$('.numbers').show();
	} , function() {
		$('.letters').show();
		$('.numbers').hide();
	});
	
	if ($("#quotes").length > 0) {
	
		$('#quotes') 
		.before('<div id="quoteNav">') 
		.cycle({ 
		    fx:     'scrollRight', 
		    speed:  1000, 
		    timeout: 5000, 
		    pager:  '#quoteNav' 
		});
		
	}
	
	if ($("#behind_scenes_slider ul li").length > 0) {
	
		$('#behind_scenes_slider ul') 
		.cycle({ 
		    fx:     'scrollRight', 
		    speed:  1000, 
		    timeout: 12000
		});
		
	}
	
	if ($("#slideArea").length > 0) {
	
		$('#slideArea').codaSlider();
		
	}	
	
	$(".home .showPanel").hover(function(){
			$(".showDetails", this).animate({width:420, height:328, bottom:0},{queue:false,duration:200});
			$(".moreInfo", this).show();
		} , function() {
			$(".showDetails", this).animate({width:420, height:65, bottom:30},{queue:false,duration:400});
			$(".moreInfo", this).hide('fast');
	});
	
	$(".home #hauntedHouse, .home #photos, .home #kids").hover(function(){
			$(".itemDetails", this).animate({width:260, height:85},{queue:false,duration:100});
			$(".moreInfo", this).show();
		} , function() {
			$(".itemDetails", this).animate({width:260, height:45},{queue:false,duration:200});
			$(".moreInfo", this).hide('fast');
	});
	
	if ($(".fancybox, .fancybox-price, .fancybox-terms").length > 0) {
		$(".fancybox li a").fancybox({
			'overlayColor': '#000',
			'titlePosition': 'inside', 
			'centerOnScroll' : true
		});
		$(".fancybox-price, .fancybox-terms").fancybox({
			'overlayColor': '#000',
			'width' : 600,
			'height' : 330,
			'autoDimensions' : false, 
			'centerOnScroll' : true
		});
	}
	
	if ($(".lightbox").length > 0) {
		$("a.lightbox").each( function() {
			$(this).attr('href', $(this).attr('href')+"/lightbox")
		})
		$(".lightbox").fancybox({
			'overlayColor': '#000',
			'width' : 820,
			'height' : 500,
			'centerOnScroll' : true,
			'titleShow' : false,
			'scrolling' : 'no',
			'type' : 'iframe'
		});
	}
	
	if ($('.cart_home select[name="country_code"]').length > 0)
	{
		$('.cart_home select[name="country_code"]').change( function() {
			$('#cartForm').submit();
		})
	}
	
	if ($('.checkout_home').length > 0)
	{
		if ($('#use_billing_info').is(':checked'))
		{
			$('#use_billing').siblings().hide();
		}
		$('#use_billing_info').change(function() {
			$('#use_billing').siblings().toggle();
		})
		
		$('#country_code').change(function() {
			if ($('#use_billing_info').is(':checked'))
			{
				$('#cart-summary').fadeOut();
				$('#hidden_country_code').val( $('#country_code').val() );
				
				$('#shipping_country_code').val( $('#country_code').val() );
				$.post( $("#customer_form").attr( 'action' ), 
						$("#customer_form").serialize() ,
						function( data ) {
							$("#customer_form").find( 'input[name="XID"]' ).val( data.XID );
	
							$.get('/commerce/cart-summary/', function(data) {
								$('#cart-summary').html(data);
								$('#cart-summary').fadeIn();
							});
						}, "json"
				);
			}
		});
		
		$('#shipping_country_code').change(function() {
			$('#cart-summary').fadeOut();
			$('#hidden_country_code').val( $('#shipping_country_code').val() );
			$.post( $("#customer_form").attr( 'action' ), 
					$("#customer_form").serialize() ,
					function( data ) {
						$("#customer_form").find( 'input[name="XID"]' ).val( data.XID );

						$.get('/commerce/cart-summary/', function(data) {
							$('#cart-summary').html(data);
							$('#cart-summary').fadeIn();
						});
					}, "json"
			);
		});
		
	}
	
	
	
	if ($("#added-to-cart").length > 0) {
		parent.$('#cart-total span').text( parseInt(parent.$('#cart-total span').text())+1 );
		$("#added-to-cart").fadeIn(1000).delay(2000).animate({
		    top: -100
			}, 1000
		);
	}
	
	if ($(".inline").length > 0) {	
		$("a.inline").fancybox({ 
			'overlayColor': '#000'
		});
	}
	
	if ($("form").length > 0) {
		if ($("#checkoutForm").length > 0)
		{
			$("#checkoutForm").validate({
				submitHandler: function(form) {
					$("#btn_buy_now").hide();
					$("#checkout-processing").fadeIn();
					form.submit();
				}
			});
		}
		else if ($("#bookingForm").length > 0)
		{
			validator = $("#bookingForm").validate();
		}
		else
		{
			$("form").validate();
		}
	}

	if ($("form .instruction").length > 0) {

		$("input, select").focus(function(){
            $(this).parent().find('.instruction').show(); //show
        }).blur(function(){  
            $(this).parent().find('.instruction').hide(); //hide again
        });
	}
	
	if ($("#datepicker").length > 0) {
		
//		disableDays = [ [12, 31],[12,26] ];
		disableDays = [  ];
		
        function disableDates(date) {
            for (i = 0; i < disableDays.length; i++) {
                    if (date.getMonth() == disableDays[i][0] - 1
                    && date.getDate() == disableDays[i][1]) {
                    	return [false, disableDays[i][2] + '_day'];
                    }
            }
            return [true, ''];
        }
		
		$("#datepicker").datepicker({
			dateFormat: 'dd/mm/yy',
			minDate: +1,
			maxDate: "+1Y",
			showOn: "both",
			buttonImage: "/assets/images/calendar.gif",
			buttonImageOnly: true,
			beforeShowDay: disableDates
		});
		
/*
		$("#datepicker").focus( function() {
			if ($(this).val() == 'dd/mm/yyyy')
				$(this).val('');
		});
		$("#datepicker").blur( function() {
			if ($(this).val() == '')
				$(this).val('dd/mm/yyyy');
		});
*/
		
		
	}
	
	function nonWorkingDates(date){
        var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
        var closedDates = [];
        var closedDays = [[Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Sunday]];
        for (var i = 0; i < closedDays.length; i++) {
            if (day == closedDays[i][0]) {
                return [false];
            }

        }

        for (i = 0; i < closedDates.length; i++) {
            if (date.getMonth() == closedDates[i][0] - 1 &&
            date.getDate() == closedDates[i][1] &&
            date.getFullYear() == closedDates[i][2]) {
                return [false];
            }
        }

        return [true];
    }
	
    if ($("#subNav").length > 0) {
    	
    	$(".backtotop").hide();
    	
    	scrollNav = true;
    	
    	if ($.browser.msie && $.browser.version <= 6 ) scrollNav = false;
    	
		    $('#subNav a').add('a.scroll').click(function () {
		        var target = $(this.hash);
		        var hash = this.hash;
		
		        if (target.length) {
		            var targetOffset = target.offset().top;
		            $('html,body').animate({scrollTop: targetOffset}, 600);
		            return false;
		        }
		    });
		
		    var nav = $('#subNav');
		    nav.css({'position': 'absolute'});
		
		    var topmost_point = nav.offset().top;
		    var left_point = $('#content').offset().left;
		    var PADDING_TOP = 20; // MAGIC NUMBER
		    var REAL_TOP = topmost_point - PADDING_TOP;
		    var the_window = $(window);
		    var NAV_IS_FIXED = (nav.css('position') == 'fixed');

if ( scrollNav )
{

		    the_window.scroll(function () {
		        if (the_window.scrollTop() > REAL_TOP) {
		            if ($.browser.msie && $.browser.version == "6.0") {
		                nav.css('top', the_window.scrollTop() + PADDING_TOP);
		            } else if (!NAV_IS_FIXED) {
		                nav.css({
		                    left: left_point,
		                    top: PADDING_TOP,
		                    position: 'fixed'
		                });
		                NAV_IS_FIXED = true;
		            }
		            if ( ! $(".backtotop").is(':visible') )
		            	$(".backtotop").fadeIn('slow');
		        } else {
		            if (NAV_IS_FIXED) {
		                    nav.css({
		                    position: 'absolute',
		                    top: 0,
		                    left: ''
		                });
		                NAV_IS_FIXED = false;
		            }
		            if ( $(".backtotop").is(':visible') )
		            	$(".backtotop").fadeOut('slow');
		        }
		    });
		    
		    the_window.resize(function () {
		        left_point = $('#content').offset().left;
		        if (NAV_IS_FIXED) {
		            nav.css('left', left_point);            
		        }
		    });
	    }
	}

	
//	if ($('.sub_drax-4-kids').length > 0)
//	{
//		$('.bookNow, #qld-nav-bookings a').each( function() {
//			$(this).attr('href',$(this).attr('href')+'?m=d4k')	
//		})
//	}

		$('#bt_input_vip-a-reserve1 input').attr('class','required')
		$('#bt_input_vip-a-reserve input').attr('class','required')

	
	if ($('#bt_text_drax4kids').length > 0)
	{
		if ( $('#bt_input_drax4kids input').is(':checked') || $('#d4k').length > 0)
		{
			$('#bt_text_drax').show();
			$('#bt_input_vip-a-reserve1').hide();
			$('#bt_input_b-reserve1').hide();
			$('#bt_vip-a-reserve1').hide();
			$('#bt_b-reserve1').hide();
			$("#datepicker").datepicker('option', 'beforeShowDay', nonWorkingDates);
		}
		else
		{
			$('#bt_text_drax4kids').show();
			$('#bt_drax4kids').hide();
			$('#bt_input_drax4kids').hide();
		}
		
		$('#bt_text_drax4kids a').click( function() {
			$('a.seating input').removeAttr('checked');
			$('#bt_input_drax4kids input').attr('checked','checked');
			
			validator.resetForm();

			$('#bt_text_drax4kids').hide();
			$('#bt_text_drax').show();

			$('#bt_drax4kids').show();
			$('#bt_input_drax4kids').show();
			
			$('#bt_input_vip-a-reserve1').hide();
			$('#bt_input_b-reserve1').hide();
			$('#bt_vip-a-reserve1').hide();
			$('#bt_b-reserve1').hide();
			
			$("#datepicker").datepicker('option', 'beforeShowDay', nonWorkingDates);
			
			return false;
		});

		$('#bt_text_drax a').click( function() {
			$('a.seating input').removeAttr('checked');
			$('#bt_input_b-reserve1 input').attr('checked','checked');

			validator.resetForm();

			
			$('#bt_text_drax4kids').show();
			$('#bt_text_drax').hide();

			$('#bt_drax4kids').hide();
			$('#bt_input_drax4kids').hide();
			
			$('#bt_input_vip-a-reserve1').show();
			$('#bt_input_b-reserve1').show();
			$('#bt_vip-a-reserve1').show();
			$('#bt_b-reserve1').show();
			
			$("#datepicker").datepicker('option', 'beforeShowDay', '');
			
			return false;
		});

					
	}

});

var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();
