$(document).ready(function(){

	/* subnav */
	$("#nav ul li").hover(function() {
		$('div',this).slideDown(50).show();
		$('div',this).parent().hover(function() {
		}, function(){	
			$('div',this).slideUp(20);
	});
	}).hover(function() { 
		$(this).addClass("subhover");
	}, function(){
		$(this).removeClass("subhover");
	});
    
	/* countdown */
	countdownFormat = 'dHMS';
	countdownLayout = '<span>'+'<strong>{dn}</strong> days</span> '+'<span><strong>{hn}</strong> hours</span> '+'<span><strong>{mn}</strong> mins</span> '+'<span><strong>{sn}</strong> secs</span>';

	/* login */
	$("li#show-login a").click(function() {
		$("#login").slideToggle(100, function(){});
		$(this).parent().toggleClass("login-shown");
		return false;
	});

	/* stage */
	$('.stage-cycle').cycle({
		fx: 'fade'
	});
	$('.stage-inner-cycle').cycle({
		fx: 'scrollLeft', 
		easing: 'easeInOutExpo', 
		timeout: 3000,
		speed: 250
	});
	
	/* lightbox */

	/*
$('#gallery-list a').lightBox();
	$('.equi-item-img a').lightBox();
	$('.lg a').lightBox();
*/
	
	/* fancybox */
	
	$('#gallery-list a').fancybox({
			'title'  : this.alt,
			'href'   : this.href
		});
	
	$('.equip a').fancybox();
	
	$('.gym-gallery a').fancybox({
			'title'  : this.alt,
			'href'   : this.href
		});
   
     $(".equipvid").fancybox({
       	 'overlayShow' : true,
       	 'overlayColor' : '#000',
         'autoscale' : false,
         'transitionIn' : 'none',
         'transitionOut': 'none',
         'padding' : 10,
         'width'  : 640,
         'height' : 390,
         'href'    : this.href
       });

	/* contact */
	$(".form-contact").validate({
		rules: {
			email1: {
				required : true,
				equalTo : "#email2"
			}
		}
	});

	/* classes */
    $('#showBookedClasses').click(function(){
             $("#bookedEvent").slideToggle("slow");
             if($('#showBookedClasses').text() == "Show Booked Classes"){
                $('#showBookedClasses').text("Hide Booked Classes");
             }else{
                $('#showBookedClasses').text("Show Booked Classes");
             }
    });

	/* faqs */
	$("dl.faq-list dt a").click(function() {
		$(this).parent().next().slideToggle(100, function(){});
		return false;
	});
	
	/* equipment */
	$("ul.equip-list li .equipinfo").click(function() {
		$(".tooltip",this).toggle('fast',function(){});
	});
	
	/* join waiting list */
	
	$(".waiting span").hover(function() {
		$(".tooltip",this).toggle('fast',function(){});
	});
    
    /* donations */
    $('#choosegymdetails-agree_donate').click(function(){
        $('#choosegymdetails-is_taxpayer').attr("checked","checked");
        $('#choosegymdetails-is_taxpayer').attr('disabled',!this.checked);
        $('#choosegymdetails-donate_amount').attr('disabled',!this.checked);
    });
    
     $('#choosegymdetails-agree_donate').attr("checked","checked");
     
     $('#choosegymdetails-is_taxpayer').attr("checked","checked");
    
    //$('#choosegymdetails-donate_amount').attr("disabled",false);
    
    /* insurance offer join form logic */
    
    $('#generalinfo-insuranceoffer').click(function() {
    	$('#generalinfo-emailcomm').attr("checked","checked");
    	$('#generalinfo-3rdparty').attr("checked","checked");
    });
    
    $('#generalinfo-emailcomm').click(function() {
    
    	var marketBox = $(this);
    	if(marketBox.not (':checked'))
    	{
    		if($('#generalinfo-insuranceoffer').is (':checked')) {
    			alert("You must accept marketing communication from Pure Gym in order for us to send you information about the Health Insurance offer.");
    			$('#generalinfo-emailcomm').attr("checked","checked");
    		}
    		
    	}
    });
   
 	$('#generalinfo-3rdparty').click(function() {
    
    	var thirdBox = $(this);
    	if(thirdBox.not (':checked'))
    	{  
   			if($('#generalinfo-insuranceoffer').is (':checked')) {
    			alert("You must accept marketing communication from 3rd party in order for us to pass your details to CPN for the Health Insurance offer.");
    			$('#generalinfo-3rdparty').attr("checked","checked");
    		}
    	}
   });
   
   
    /* validate firesafety */
    $('#firesafety-save').attr('disabled',true);
    $('#firesafetybox').click(function(){
        var checked = $('#firesafetybox:checked').val();
        if(checked !== undefined){
            $('#firesafety-save').removeAttr('disabled');
        }else{
            $('#firesafety-save').attr('disabled', 'disabled');
        }
    });
    
    /* validate declaration */
    $('#declaration-agree').attr('checked', false);
    $('#declaration-save').attr('disabled',true);
    $('#declaration-agree').click(function(){
        var checked = $('#declaration-agree:checked').val();
        if(checked !== undefined){
            $('#declaration-save').removeAttr('disabled');
        }else{
            $('#declaration-save').attr('disabled', 'disabled');
        }
    });
    
    /* validate firesafety members join */
    $('#firesafety-continue').attr('disabled',true);
    $('#firesafety-safety-members').click(function(){
        var checked = $('#firesafety-safety-members:checked').val();
        if(checked !== undefined){
            $('#firesafety-continue').removeAttr('disabled');
        }else{
            $('#firesafety-continue').attr('disabled', 'disabled');
        }
    });
    
    /* check affiliate code */
    $('#checkcode').click(function(){
        $.ajax({
           url: 'aj/aj_check_affiliate_code.php?affiliatecode='+$('#generalinfo-affiliatecode').val(),
           cache: false,
           success: function(msg){
                
                $('#fieldset-marketinfo span').remove();
                $('#generalinfo-affiliatecode').parent().append("<span>"+msg+"</span>");
                
           }     
        });
    })
	$('#generalinfo-affiliatecode').change(function(){
        if($(this).val() == ""){
			$('#fieldset-marketinfo span').remove();
		}
    })

});

function change_gym(el)
{
	var gym_id = $(el).val() 
	url = "../aj/aj_change_gym.php?gym_id=" + gym_id +"&random=" + Math.random();
    $.getJSON( url, handlePostcodeAjaxReply1 );
}

function handlePostcodeAjaxReply1(data,textStatus)
{
	window.location.reload();
}

function gotojoin(url)
{
    window.location = url;
}
