$(function() {

	$('#nav-primary > li.current-dining').each(function() {
		$(this).children('ul').hide().animate({height: 'show'}, 1000); //.remove().clone(true).prependTo('#nav-primary')										 
	});
	
	$('#nav-primary > li.current-spa').each(function() {
		$(this).children('ul').hide().animate({height: 'show'}, 1000); //.remove().clone(true).prependTo('#nav-primary')										 
	});
	
	$('body:not(".newsletter") div#content h2').sifr(
		{ strSWF: '/fonts/palatino-small-caps.swf', strColor: '#67513D', strWmode: 'transparent', intPadding: [0, 0, 30, 0], strFlashVars: 'offsetTop=-3&' }, 
		{ expressInstall: true }
	);
	
	$('body.romance div#content h1').sifr({ strSWF: '/fonts/franklin-gothic-book.swf', strColor: '#fff', strWmode: 'transparent', strFlashVars: 'textalign=center&' }, { expressInstall: true });
	
	$(".home #masthead").flash({src:"flash/terr_home.swf", width:750, height:360, wmode:"transparent", quality:"high"},{version:"8"});
	$(".resort #masthead").flash({src:"flash/terr_resort.swf",width:750,height:360,wmode:"transparent",quality:"high"},{version:"8"});
	$(".resort #masthead").flash({src:"flash/terr_resort.swf",width:750,height:360,wmode:"transparent", quality:"high"},{version:"8"});
	$(".pvp #masthead").flash({src:"flash/terr_pvp.swf",width:750,height:360, wmode:"transparent",quality:"high"},{version:"8"});
	$(".real-estate #masthead").flash({src:"flash/terr_realestate.swf", width:750,height:360,wmode:"transparent",quality:"high"},{version:"8"});
	$(".history #masthead").flash({src:"flash/terranea_history.swf",width:750,height:360,wmode:"transparent",quality:"high"},{version:"8"});
	$(".photos #masthead").flash({src:"flash/photo_gallery.swf",width:667,height:328,wmode:"transparent",quality:"high"},{version:"8"});
	$(".pvp-map #masthead").flash({src:"flash/terr_pvpsitemap.swf",width:750,height:360,wmode:"transparent",quality:"high"},{version:"8"});
	$(".resort-map #masthead").flash({src:"flash/terr_resort_siteplan.swf",width:750,height:360,wmode:"transparent",quality:"high"},{version:"8"});
	$(".estate-map #masthead").flash({src:"flash/terr_realestate_siteplan.swf",width:750,height:360,wmode:"transparent",quality:"high"},{version:"8"});
	$(".villas-map #masthead").flash({src:"flash/terr_villas.swf",width:750,height:360,wmode:"transparent",quality:"high"},{version:"8"});
	$(".casitas-map #masthead").flash({src:"flash/terr_casitas.swf",width:750,height:360,wmode:"transparent",quality:"high"},{version:"8"});
	$(".team #masthead").flash({src:"flash/terr_team_gallery.swf",width:750,height:360,wmode:"transparent",quality:"high"},{version:"8"});
	$("#webcam").flash({src:"http://www.earthcam.com/clients/terranea/terranea_player.swf",width:550,height:329,wmode:"transparent",quality:"high"},{version:"8"});
	
	$("div#nav div#console-flash-header").flash({src:"/flash/check-availability-v.swf",width:175,height:56,wmode:"transparent",quality:"high"},{version:"8"});
	$("div#content-console div#console-flash-header").flash({src:"/flash/check-availability-h.swf",width:136,height:48,wmode:"transparent",quality:"high"},{version:"8"});

	$("div#meeting-floorplan-map").flash(
		{src:"flash/meeting-map.swf",
		 width:695,
		 height:515,/*
		 wmode:"transparent",*/
		 quality:"high"},
		 {version:"8"},
			function(htmlOptions) { //Use this to specify a query string, take out if not needed.
				htmlOptions.flashvars.xmlPath = "flash/xml/meeting-map.xml";
				$(this).html($.fn.flash.transform(htmlOptions));
		});

	if ($('body').is('.newsletter')){
		var contents = '<div id="contents-list"><ul>';
		$('div.article').each(function(i){
			$(this).attr('id','article-'+i).append('<p><a href="#content">back to top</a></p>');
			contents+='<li><a href="#article-'+i+'">'+$(this).find('h3').text()+'</a><br /><span>'+$(this).find('h4').text()+'</span></li>';
		});
		contents+='</ul></div>';
		$('div#contents-wrapper').append(contents);
	}

	$('#register .hear-about').change(function() {
		var val = $(this).val().toLowerCase();
		if(val!='other'&&val!='unknown'&&val!='signage/drive-by') {
			$(this).parent().next().show().find('.'+val).attr({name: '00N70000001q7fs', id: '00N70000001q7fs'}).show().siblings('select').attr({name: '', id: ''}).val('').hide();
			$(this).parent().next().next().hide().find('input').val('');
		} else if(val=='other') {
			$(this).parent().next().next().show();
			$(this).parent().next().hide().find('select').attr({name: '', id: ''}).val('');
		} else {
			$(this).parent().next().next().hide().find('input').val('');
			$(this).parent().next().hide().find('select').attr({name: '', id: ''}).val('');
		}
	});
	
	$('#change-request').each(function() {
		$('select', this).change(function() { 
			if($(this).val()!='')
				window.location.href = $(this).val();
		});
		$('input', this).hide();
	});
	
	$('.show-extra').hide();
	$('.control-extra').click($.delegate({
		'.open-extra': function(e) {
			$(this).next('.show-extra').show().find('input').addClass('required');
		},
		'.close-extra': function(e) { 
			$(this).next('.show-extra').hide().find('input').removeClass('required').val('');
			$('.validation-error',$(this).next('.show-extra')).remove();
		}
	}));


	$('ul#videos-list').each(function() {
	    var iContainer = $('<div />').addClass('iframeWrapper')
		.html('<iframe class="videoIframe" frameborder="0"></iframe>')
		.jqm({ trigger:'div#video-content a.videoBox' })
		.addClass('jqmWindow')
		.append('<p class="closeBox"><a href="#">close this video</a></p>');
	    $('a', this).each(function() {
		$(this).click(function(e) {
		    e.preventDefault();
		    $('body').append(iContainer);
		    $(iContainer).find('iframe').attr('src', $(this).attr('href'));
		    iContainer.jqmShow().jqmAddClose( $('.closeBox a') );
			$('.closeBox a').click(function() { $('.jqmWindow').remove(''); });
		});
	    });
	});


	//social media sharing
	$('.share-link').bind('click',function() {
		$('.share-panel').hide();
		$(this).parents('.rss-item').find('.share-panel').css('display','inline');
		return false;
	});
	$('.share-close').click(function(){
		$(this).parents('.rss-item').find('.share-panel').slideUp('fast');
		return false;
	});
	
	
	// packages page
	
	$(".package-wrapper").each( function() {
		var x = $(this);
		$(".package-details a", x).toggle( function() {
			$('.share-panel', x).hide();
			$.get('/direct/package-details.php', { package_id: this.id.substr(3), channel: this.rel },function(data) { 
				$(".package-long", x).html(data).slideDown('slow');
			});
			$(this).removeClass("open").addClass("close");
		}, function() {
			$('.share-panel', x).hide();
			$(".package-long", x).slideUp().html('');
			$(this).removeClass("close").addClass("open");
		});
	});	
	
	// events page
	$(".event").each(function(){
		 var x = $(this);
       $('div.event-description').hide();
       $('a.view-details',x).toggle(function() {
       $('div.event-description',x).slideDown('slow');
			$(this).css('display', 'block').parents('div.event-details').addClass('open');
				return false;
				}, function() {
			$('div.event-description',x).slideUp('fast');
			$(this).css('display', 'block').parents('div.event-details').addClass('close').removeClass('open');
				return false;
			});
		});
	
	// Gift Certificates Form
	$('input#autoPopCbx').click( function() { 
		if ($('input#autoPopCbx').is(":checked")) {
			$('input#recipient_first_name').val( $('input#first_name').val() );	
			$('input#recipient_last_name').val( $('input#last_name').val() );
		} else {
			$('input#recipient_first_name').val('');	
			$('input#recipient_last_name').val('');
		}									  
	});
	$('form.giftform input#first_name').change( function() { 
		if ($('input#autoPopCbx').is(":checked")) {				
			$('input#recipient_first_name').val( $('input#first_name').val() );
		}
	});
	$('form.giftform input#last_name').change( function() { 
		if ($('input#autoPopCbx').is(":checked")) {				
			$('input#recipient_last_name').val( $('input#last_name').val() );
		}
	});
	
	// Camps Page
	$('.camp-wrapper').each(function() {
		var x = $(this);
		$('div.camp-long').hide();
		$('.camp-details a',x).toggle(function() {
			$('div.camp-long',x).slideDown('slow');
			$(this).removeClass("open").addClass("close");
			return false;
		}, 
		function() {
			$('div.camp-long',x).slideUp('fast');
			$(this).removeClass("close").addClass("open");
			return false;
		});
	});

//Reservation center:
	$('div#code-box').hide();
	$('a#promo-link').toggle(function() {
		$('div#code-box').slideDown();
		$(this).css('background-position', 'right bottom').parents('div#booking-console').addClass('check-long');
		return false;
	}, function() { 
		$('div#code-box').slideUp();
		$(this).css('background-position', 'right 0').parents('div#booking-console').removeClass('check-long'); 
		return false; 
	});

// gwo test for landing page
$('.gwo-test').click(function(){
		try {
				var pageTracker=_gat._getTracker("UA-6646110-1");
				pageTracker._trackPageview("/1988656027/goal");
		}catch(err){}
	});

});

jQuery.delegate = function(rules) {
	return function(e) {
		var target = $(e.target);
		for (var selector in rules)
			if (target.is(selector)) return rules[selector].apply(this, $.makeArray(arguments));
	}
}

