$(function(){
	$('.no-js').removeClass('no-js');

	// Fancybox defaults
	if($.fn.fancybox) {

		$.fn.fancybox.defaults.overlayColor = '#000';
		$.fn.fancybox.defaults.overlayOpacity = 0.8;
		$.fn.fancybox.defaults.padding = 0;
	}

	if( $('.more-news').length>0 ) {
		$('#show-more-news').parent('.hide').show().click(function(){
			$('.more-news').slideDown();
			$(this).slideUp();
		});
	}

	$('a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).addClass('external').attr({
		target: "_blank", 
		title: "Opens in a new window"
	}).parent().addClass('external');
	
	$('.slideshow').after('<ul class="pager" /><a class="prev">Previous</a><a class="next">Next</a>').cycle({
		activePagerClass: 'active',
		fx:     'fade',
		next: 	'.next',
		prev:	'.prev',
		speed:  '1000',
		timeout: 0,
		pager:  '.pager',
		pagerAnchorBuilder: function(idx, slide) { 
        	return '<li><a>' + idx + '</a></li>'; 
    	} 
    });
    
    //FAQs page
	var jumplinkCounter = 0;
	$('.jump-list').each(function(){
		jumplinkCounter++;
		var thisJumplinkCounter = jumplinkCounter;
		var thisJumplinkHeading = $(this).prev('h2').html();
		if(jumplinkCounter > 1) $('.create-jumplinks').append('<h3>'+thisJumplinkHeading+'</h3>');
		$('.create-jumplinks').append('<ul class="side-menu-'+thisJumplinkCounter+'" />');
		$(this).children('li').each(function(){
			var title = $(this).children('h2:first').html();
			var title_html = title.replace(/[^a-z0-9\-\_]+/gi, '').toLowerCase();
			$(this).children('h2:first').attr('id', title_html);
			$('.side-menu-'+thisJumplinkCounter).append('<li><a href="#'+title_html+'">'+title+'</a></li>');
		});
	});
	
        
        
        /*
         * add form submit checking
         */
        $("#validateForm").validationEngine({promptPosition : "topRight", scroll:false});
         // bind the form submit button to ajax submit
        $('#validateForm button').bind('click', function(){$('#validateForm').trigger('submit');});
        $('#validationForm .action.hidden').removeClass('hidden');
        
        $("#validateForm").submit(function() {
            if($("#validateForm").validationEngine('validate')){
                //alert('valid..process...');


                // do ajax processing here, using the action in the form 
                // var url = $('#validateForm').attr('action');
                $.post($('#validateForm').attr('action'), $("#validateForm").serialize(),

                function(data){

                    //alert(data.success)
                    if(data.success === false){
                        // fail
                        $('div.message.error ').fadeIn("slow");

                    } else {

                        // success
                        $("#validateForm").slideUp('fast', function(){

                            
                            $('#thankyou').slideDown("slow");
                            $(this).remove(); 
                            $('html,body').animate({scrollTop: $('#thankyou').offset().top});

                        });
                    }

                }, "json"); 

            }else{
                $('.message.error').removeClass('hidden')
            }
            return false;
        });  
        
        $("form select[name=Country]").change(function(){
            if ($(this).val() == 'NZ')
                {
                    $("input#websiteId").val($("input#websiteId").val() + " NZ");
                }
            else
                {
                    $("input#websiteId").val($("input#websiteId").val().replace(/\s*NZ/, ''));
                }
        });
        


/*
 * Age Verification
 ********************/

	var av_url = '/age-verification/';
	var av_url_check = '/site/age-verification/check.php';
	var av_cookie = $.cookie('av_old_enough');
	var av_cookie_lifetime = 1;

	$('#age-verification').live("submit", function() {

		$.fancybox.showActivity();
		$('#age-verification-error').hide();

		$.ajax({
			type     : "POST",
			cache    : false,
			url      : "/site/age-verification/check.php",
			data     : $(this).serializeArray(),
			dataType : 'json',
			success  : function(data) {
				if(data.old_enough) {
					$.cookie('av_dob', data.dob, { expires: av_cookie_lifetime, path: '/' });
					$.cookie('av_country', data.country, { expires: av_cookie_lifetime, path: '/' });
					$.cookie('av_old_enough', true, { expires: av_cookie_lifetime, path: '/' });
					$.fancybox.close();
				} else {
					$('#age-verification-error').text(data.error).slideDown();
					$.fancybox.hideActivity();
				}
				
			}
		});

		return false;

	});

	if (av_cookie != 'true')
	{
		// Require age verification
		$.fancybox({
			href: av_url,
			showCloseButton : false,
			hideOnOverlayClick : false,
			enableEscapeButton : false
		});

	}



});


function get(name){
	if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
		return decodeURIComponent(name[1]);
}
