jQuery(function(){

	// Logo behaviour
		jQuery('h1#logo')
			.attr('title','Return to home page')
			.click(function() {
				window.location = '/';
			});

    // External links with REL
	    jQuery('a[rel="external"]').live('click', function(e) {
	    	e.preventDefault();
	    	window.open( jQuery(this).attr('href') );
	    });

    // Showtimes LI order
		jQuery("li.selected").prependTo("ul.posters");

    // ToogleSlide Functions
		jQuery(".togglehide").hide();

    //  Party room slide toggle
		jQuery('.toggleshow').click(function() {
			jQuery('.togglehide').slideToggle('slow');
			return false;
		});

    // coming soon slide toggle
		jQuery('#comingsoon ul li').hover(function() {
			jQuery(this).find('.togglehide').stop(true, true).fadeIn('slow');
		},function() {
			jQuery(this).find('.togglehide').stop(true, true).fadeOut('slow');
		});


    // Coming Soon LI height / width



	// Share toolbar
		jQuery('#share').mouseenter(function() {
			jQuery(this).animate({width:'150'}, 200);
			jQuery('.addthis_toolbox', jQuery(this)).animate({width:'150'}, 200);
		})
		.mouseleave(function() {
			jQuery(this).animate({width:'0'}, 200);
			jQuery('.addthis_toolbox', jQuery(this)).animate({width:'0'}, 200);
		});

		jQuery("#slider").jCarouselLite({
			circular: false,
			btnNext: ".next",
			btnPrev: ".prev"
		});

		jQuery("#slider2").jCarouselLite({
			circular: false,
			visible: 4,
			btnNext: ".next",
			btnPrev: ".prev"
		});


		jQuery('.prev, .next').click(function(e) {
			e.preventDefault();
		});


	// forms action hack
	
	jQuery('#primary form.wpcf7-form').each(function() { 
		jQuery(this).attr('action', '?sent=true');
	});
	
	jQuery('.blank form.wpcf7-form').each(function() { 
		jQuery(this).attr('action', '?modal-sent=true');
	});	
	
	// forms value formatting

	jQuery("#dateofbirth, #dateofbirth2, #dateofbirth3, #dateofbirth4").focus(function() {
		jQuery(this).mask("99/99/9999");
	});
	
	jQuery("#phone, #phone2, #phone3, #phone4").focus(function() {			
		jQuery(this).mask("999-999-9999");
	});

	// Placeholder fallback for older browsers
		if (!Modernizr.input.placeholder)
		{

			// set placeholder values
			jQuery(this).find('[placeholder]').each(function()
			{
				jQuery(this).val( jQuery(this).attr('placeholder') );
			});

			// focus and blur of placeholders
			jQuery('[placeholder]').focus(function()
			{
				if (jQuery(this).val() == jQuery(this).attr('placeholder'))
				{
					jQuery(this).val('');
					jQuery(this).removeClass('placeholder');
				}
			}).blur(function()
			{
				if (jQuery(this).val() == '' || jQuery(this).val() == jQuery(this).attr('placeholder'))
				{
					jQuery(this).val(jQuery(this).attr('placeholder'));
					jQuery(this).addClass('placeholder');
				}
			});

			// don't submit placeholder values
			jQuery('[placeholder]').closest('form').submit(function()
			{
				jQuery(this).find('[placeholder]').each(function()
				{
					if (jQuery(this).val() == jQuery(this).attr('placeholder'))
					{
						jQuery(this).val('');
					}
				})
			});
		};
		
        /* Colorbox */
        jQuery('a.colorbox-trailer').unbind();
        
        jQuery('a.colorbox-trailer').click(function() {
			jQuery('#colorbox').addClass('black');	
			jQuery('#cboxOverlay').addClass('black-overlay');			
            jQuery(this).colorbox({inline: true, href: jQuery(this).attr('href'), width: "700px", height: "850px", opacity: ".9"});   
        });
	    jQuery(document).bind('cbox_closed', function() { 
				jQuery('#colorbox').removeClass('black');	
				jQuery('#cboxOverlay').removeClass('black-overlay');			
		});

        jQuery('a.colorbox-small').colorbox({inline: true, href: jQuery(this).attr('href'), innerWidth: "380px", innerHeight: "515px"});
        jQuery('a.colorbox-large').colorbox({inline: true, href: jQuery(this).attr('href'), innerWidth: "853px", innerHeight: "480px"});      
          
	// Home page featured carousel
		jQuery('#featured > ul').wrap('<div id="wrap-featured" />');

		jQuery('#wrap-featured')
			.append('<div class="next" />')
			.append('<div class="prev" />')
			.append('<div id="auto" />');

		jQuery('.snippet .blurb').html('');

		jQuery('.snippet').append('<div id="qnav" />');

        var da = jQuery(window).width() - 290;
		var tw = da * jQuery('#wrap-featured > ul li').length;
		jQuery('#wrap-featured > ul').width(tw);

		var cur = 1;

		jQuery('#wrap-featured .next').click(function() {
			tmr = window.clearInterval(tmr);
			slideto(cur + 1);
		});

		jQuery('#wrap-featured .prev').click(function() {
			tmr = window.clearInterval(tmr);
			slideto(cur - 1);
		});

		jQuery('#auto').click(function() {
			slideto(cur + 1);
		});

		jQuery('#qnav a').live('click', function(e) {
			e.preventDefault();
		});

		jQuery('#qnav a:not(.current)').live('click', function(e) {
			e.preventDefault();
			tmr = window.clearInterval(tmr);

			var index = jQuery(this).attr('rel');
			slideto(index);
		});

		// build some stuff for each list item
		jQuery('#wrap-featured > ul > li').each(function(i) {
			var jQuerythis = jQuery(this);

			jQuerythis.width(da);
			jQuery('#qnav').append('<a href="" rel="' + (i + 1) + '"/>');
		});

        var frontSlidesCount = jQuery('#wrap-featured > ul > li').length;
        var frontSlides = jQuery('#wrap-featured > ul > li');
        var frontSlide = jQuery('#wrap-featured > ul');
        var frontBlurbs = jQuery('#wrap-featured > ul > li .blurb');
        var frontQNav = jQuery('#qnav a');
        var frontBlurb = jQuery('.snippet .blurb');

		function slideto(i) {
			if (i != 0 && i < (frontSlidesCount + 1) ) {

				var whereto = -(i-1) * da;
				frontSlide.stop(true, true).animate({ left: whereto }, 800);

				jQuery('#qnav a').removeClass('current');
				jQuery('#qnav a:eq(' + (i - 1) + ')').addClass('current');

				jQuery('.snippet .blurb').stop(true, true).fadeOut('slow', function() {
					jQuery(this).detach();
					if (i > cur) { cur++ };
					if (i < cur) { cur-- };

					//frontBlurb.hide();
                    //frontBlurb.html(jQuery(frontBlurbs[(i - 1)]).html()).fadeIn('slow');
					jQuery(frontBlurbs[(i - 1)]).appendTo('.snippet').stop(true, true).fadeIn('slow');
				})
			}

			if (i == (frontSlidesCount + 1)) {
				cur = 1;
				slideto(cur);
			}
			if (i == 0) {
				slideto(frontSlidesCount);
			}
		};

		//jQuery('#featured > ul > li:first-child').addClass('current');
        jQuery('#wrap-featured > ul').css('left', '0px');
		jQuery('#wrap-featured > ul').animate({left : "0px"}, 400, function() {
			jQuery(frontBlurbs[0]).appendTo('.snippet').fadeIn('slow');
		});

		jQuery('#qnav a:first-child').addClass('current');
		var tmr=window.setInterval("jQuery('#auto').trigger('click')",8000);
        if (jQuery('form.wpcf7-form').length) {
            jQuery('form.wpcf7-form').lazyValidator();
        }
        jQuery("#newsletter").lazyValidator();


    // Members benefits functionality hacks

    	jQuery('#second-applicant, #third-applicant, #fourth-applicant, #same-address-container').hide();
    	jQuery('#first-applicant legend span').text('Sign Up Here For Your Membership!');
    	jQuery('select#type').change(function() {
    		var selected = jQuery("option:selected", jQuery(this)).val();
    		console.log(selected);

    		if (selected == 'Couples Membership') {
    			
    			jQuery('#first-applicant legend span').text('First Applicant');

    			jQuery('#third-applicant.open .toggle-form').trigger('click');
    			jQuery('#fourth-applicant.open .toggle-form').trigger('click');
    			jQuery('#third-applicant, #fourth-applicant').hide();

    			jQuery('#second-applicant').show();
    			jQuery('#second-applicant:not(.open) .toggle-form').trigger('click');

    			jQuery('#same-address-container').fadeIn();

    		}

     		else if (selected == 'Family Membership') {

     			jQuery('#first-applicant legend span').text('First Applicant');

    			jQuery('#second-applicant, #third-applicant, #fourth-applicant').show();
    			jQuery('#second-applicant:not(.open) .toggle-form').trigger('click');
    			jQuery('#third-applicant:not(.open) .toggle-form').trigger('click');
    			jQuery('#fourth-applicant:not(.open) .toggle-form').trigger('click');
    			
    			jQuery('#same-address-container').fadeIn();
    			
    		}

    		else {
    			jQuery('#second-applicant.open .toggle-form').trigger('click');
    			jQuery('#third-applicant.open .toggle-form').trigger('click');
    			jQuery('#fourth-applicant.open .toggle-form').trigger('click');
    			jQuery('#second-applicant, #third-applicant, #fourth-applicant').hide();

    			jQuery('#first-applicant legend span').text('Sign Up Here For Your Membership!');
    		}


    	})

 });


