var filterCats = []; 

$(function() {
  // This controls the target of various links
	$("#content a[href^='http://']").attr("target","_blank");
 	$("#content a[href^='https://']").attr("target","_blank");
	$("#content a[href*='.pdf']").attr("target","_blank");
		   
	// booking console
	$('.toggle-promo').click(function(e){
		e.preventDefault();
		$('#promo-wrapper').toggle(0,function(){
			if($(this).is(':visible')) {
				$('#promo-code').focus();
				$('.toggle-promo').css("background-position","0px -57px");
			}
			else {
				$('.toggle-promo').css("background-position","0px -3px");				
			}
		});
	});

	$('#promo-wrapper a').click(function(e){
		e.preventDefault();
		$('#promo-wrapper').hide();
	});	

	var featCount = $('#property-filter-wrapper .featured').length;
	var addBack;
	var addBackCount = 0;
	$("div.featured").each(function() {
		$(this).addClass('feat-'+addBackCount);
		addBackCount += 1;
		if ( (featCount == 1) || $(this).hasClass('feat-0') ) {
			$(this).prependTo("#property-filter-wrapper");
		} else {
			addBack = $(this).detach();
			addBack.insertAfter('.feat-'+(addBackCount-2));
		}
	});
		
	// Booking Console animation
	$('#choose-hotel').each(function() {
		$(this).addClass('inactive');		
		$('#choose-hotel h1 a').toggle(function() {
			$(this).closest('div').animate({width:'962', left:'20'},'1000', function() {
				$('#choose-hotel').removeClass('inactive').addClass('active');
				$('#console-tooltip').addClass('active');
				$(this).hover(function() {
					$('#console-tooltip.active').css('visibility','visible');
				}, function(){
					$('#console-tooltip').css('visibility','hidden');
				});
			});
		}, function() {
			$('#choose-hotel-wrapper').removeClass('active');
			$(this).closest('div').animate({width:'146', left:'834'},'1000', function() {
				$('#console-tooltip').removeClass('active');
				$('#choose-hotel').removeClass('active').addClass('inactive');
			});
		});
	});
	
	$('form#reservations-console  #advanced').change(function() {
		var inputVal = $(this).val	();
		if (inputVal.length>0)
			$('form#property-console #property').val(inputVal);
								   
	});
	
	$('form#property-console #property').change(function() {
		var inputVal = $(this).val	();
		if (inputVal.length>0)
			$('form#reservations-console #advanced').val(inputVal);
								   
	});
	
	// This controls how the search widget functions (target)
	$('form#reservations-console').submit(function() {
		var arriveVal = $('form#reservations-console #arrive').val();
		var departVal = $('form#reservations-console #depart').val();
		if ( (arriveVal.length<1) && (departVal.length<1) ) {
			$(this).attr('action','/hotels-resorts/results.php');			
			$(this).attr('target','_self');
			$('form#property-console').submit();
			return false;
		} else {
			$(this).attr('action','/direct/console-process.php');			
			$(this).attr('target','_self');
		}
	});
	
	
	// Clear field on user click
	$('.clearme').one('focus', function() {
	  $(this).val('');
	});
	
	// preppopulate form select
	$('#rfp').each(function() {
		if ( window.location.hash ) {
			var linkHash = location.hash;
			$(this).find('#'+linkHash).attr('selected','selected');
		}
	});
    
	// fix for wide select options cutting off in IE
	//$('select#architecture').ieSelectStyle();
	//$('select#propstyle').ieSelectStyle();
	
	// home masthead/push cycle
	$('#jcycle-timeline-push').each(function() { $(this).cycle({ 
		fx:     'scrollHorz', 
		speed:  1000, 
		timeout: 7000, 
		next:   '#next-push', 
		prev:   '#previous-push',
		//before:
		//after:
		cleartypeNoBg:true
		});
	});		

	$('#jcycle-masthead-push').each(function() { $(this).cycle({ 
		fx:     'scrollHorz', 
		speed:  1000, 
		timeout: 7000, 
		next:   '#next-push', 
		prev:   '#previous-push',
		//before:
		//after:
		cleartypeNoBg:true
		});
	});		

	// home breadcrumb only on primary nav items
	$('.default #breadcrumb').each(function() {
		if ( (($(this).find('.breadcrumb-trail').length) > 1) || ($(this).closest('#breadcrumb-wrapper').find('h1').text() == 'Reservations') ) {
			$(this).find('.breadcrumb-primary').remove();
		}
		if ( $(this).closest('#wrapper').find('#masthead-breadcrumb div:first-child').text() == 'Reservations' ) {
			//console.log('match');
			$(this).find('.breadcrumb-primary').remove();
			$(this).prepend('<a href="/members/">HHA Members</a> <span class="breadcrumb-trail"> | </span>');
		}
		//$(this).find('.breadcrumb-trail:last').remove();
	});

	// test for secondary nav
	$('.default #secondary-nav-wrapper').each(function() {
		if ( ($(this).find('#secondary-nav ul').length) == 0 ){
			$(this).addClass('empty');
		}
	});
	
	// quaternary nav tweaks
	$('#nav-quaternary li').not($('.last')).append('<span class="breadcrumb-trail"> | </span>');
	$('#nav-destination-quaternary li').not($('.last')).append('<span class="breadcrumb-trail"> | </span>');

	$('.default #quaternary-nav-wrapper').each(function() {
		if ( ($(this).find('#nav-quaternary ul').length) == 0 ){
			$(this).addClass('empty');
		}
	});

	$('.destination #quaternary-nav-wrapper').each(function() {
		if ( ($(this).find('#nav-destination-quaternary ul').length) == 0 ){
			$(this).addClass('empty');
		}
	});

	/*$('#destination-secondary-photo-gallery a').click(function(event) {
		event.preventDefault();
		$('#gallery-modal').jqm({closeClass:'jqmClose'}).jqmShow();
	});*/
		
	$('#destination-secondary-photo-gallery a').click(function(e){
		e.preventDefault();
		$("#gallery-modal-content").empty();
		var videoURL = $('#gallery-modal-content').attr("rel");
		$('#gallery-modal-content').html('<iframe id="jqmContent" src="'+ videoURL +'" width="800" height="600"></iframe>');
	});

	var galleryopenFn=function(hash){
		hash.w.show();
		//hash.w.css({left:($(window).width()-800)/2,top:$(window).scrollTop()+100});
	};

	var galleryhideFn=function(hash){
		$("#gallery-modal-content").html('');
		$("#gallery-modal-content iframe").remove();		
		hash.w.hide();
		hash.o.remove();
	};

	$('#gallery-modal').jqm({trigger: '#destination-secondary-photo-gallery a', onShow: galleryopenFn, onHide: galleryhideFn});

	
	// temp fake package function
	$('.package-details').toggle(function(event) {
		event.preventDefault();
		$(this).closest('.package-wrapper').find('.package-long').slideDown('slow');
		$(this).closest('.package-wrapper').addClass('active');
	}, function(event) {
		$(this).closest('.package-wrapper').find('.package-long').slideUp('slow');
		$(this).closest('.package-wrapper').removeClass('active');
	});

	// press room
	$('.press-details').toggle(function(event) {
		event.preventDefault();
		$(this).closest('.press-channel-wrapper').find('.press-inner-wrapper').slideDown('slow');
		$(this).closest('.press-channel-wrapper').addClass('active');
	}, function(event) {
		$(this).closest('.press-channel-wrapper').find('.press-inner-wrapper').slideUp('slow');
		$(this).closest('.press-channel-wrapper').removeClass('active');
	});
	
	$('.press-channel-name').toggle(function(event) {
		event.preventDefault();
		$(this).closest('.press-channel-wrapper').find('.press-inner-wrapper').slideDown('slow');
		$(this).closest('.press-channel-wrapper').addClass('active');
	}, function(event) {
		$(this).closest('.press-channel-wrapper').find('.press-inner-wrapper').slideUp('slow');
		$(this).closest('.press-channel-wrapper').removeClass('active');
	});

	// subscribe
	$('.feed-details').toggle(function(event) {
		event.preventDefault();
		$(this).closest('.feed').find('.feed-wrapper').slideDown('slow');
		$(this).closest('.feed').addClass('active');
	}, function(event) {
		$(this).closest('.feed').find('.feed-wrapper').slideUp('slow');
		$(this).closest('.feed').removeClass('active');
	});

    // hack for http://www.historichotels.org/about-historic-hotels/national-trust.php content overwritten by admin (admin not allowing certain content)  
    $('body.nthp #quaternary-nav-wrapper').after(function() {
	    var nthpContent = '<div id="nthp-logo-wrapper">';
		nthpContent += '<div id="nthp-box"><img src="/images/inset/NTHP_5Line_Blue_transparentbkgrnd.gif" alt="" class="nthp-inset" /><br /><p class="nthp-linky"><a href="http://my.preservationnation.org/site/PageServer?pagename=Map_Your_Benefits" target="_blank">JOIN NOW</a></p></div>';
		nthpContent += '<div id="nthp-content">';
		nthpContent += '<p><strong>Book A Night and Receive a Free 1-Year Family Membership*</strong></p>';
		nthpContent += 'An unforgettable travel experience with Historic Hotels of America has never been easier or more fun. Travelers who book their hotel accommodations on our new website will receive a complimentary one-year family membership*<br />(a $30 value) to the National Trust for Historic Preservation.</p>';
		nthpContent += '<p><a href="/hotels-resorts/">Plan your trip now.</a></p>';	
		nthpContent += '<p class="terms">*Terms and conditions apply (see below)</p>';	
		nthpContent += '</div>';
		nthpContent += '</div>';
    	return nthpContent;
    });
 	

	// fix for class="current" breakage in property template
	if(!$('body').hasClass('mappage')) {
		var curPage = jQuery.url.segment(2); // returns pagename.ext
		var priSection = jQuery.url.segment(0); // returns first url segment beyond tld
	}
	
	$('#primary-destinations').each(function() {
		if (priSection == 'hotels-resorts') {
			$(this).addClass('current');
		}
	});
	
	// temp fake archive form
	$('#press-archive').change(function(){
        $('#archive-press').submit();
    });

     
    // seriously ugly hack for ie7 property event calendar when no events - remove when good solution implemented
    $('.destination #calendar-wrapper').each(function() {
    	if ( ($(this).closest('#content').find('#events').length) == 0 ) {
    		$(this).wrap('<div class="clearfix" />');
    	}
    });
    
    $('.destination .experience-none').each(function() {
    	$(this).closest('#experience-results-wrapper').addClass('solo');
    });

    $('.default .experience-none').each(function() {
    	$(this).closest('#deals-results-wrapper').addClass('solo');
    });
	
	$('div#search-results-wrapper ul li.item:even').css('clear','both');
	
	$('ul#addtl-hotel-program-member-list li input#addt-hotel-program-member-check').click(function() {
		if ($(this).is(':checked')) { 
			$("#addtl-hotel-program-member-list3").show(); 
		} else { 
			$("#addtl-hotel-program-member-list3").hide(); 
		}
	});
	
	$('ul#addtl-airline-program-member-list li input#addt-airline-program-member-check').click(function() {
		if ($(this).is(':checked')) { 
			$("#addtl-airline-program-member-list3").show(); 
		} else { 
			$("#addtl-airline-program-member-list3").hide(); 
		}
	});
	
	var listItemHeight=0;
	$('#search-results-wrapper').each(function() { 
		$('#search-results-wrapper li').each(function() { 
			var tempHeight = $(this).height();
			if (tempHeight>listItemHeight) {
				listItemHeight = tempHeight;	
			}
			
		});
		$('#search-results-wrapper li').css({'height': listItemHeight});
	});
	
	
	
	$('#hot-deals-state-form:not(.pack-loc-form) select#state').change(function(event) {
		$("#hot-deals-state-form").submit();
	});
	
	
	var openFnPartic=function(hash){	
		//$('#map-content').addClass(projectid);
		hash.w.show();
		//alert(hash.t);
		//hashURL = hash.t;
		$('#modal-participating').css({left:($(window).width()-$('#map-modal-search').width())/2,top:$(window).scrollTop()+140});				
	};

	var hideFn=function(hash){
		hash.w.hide()
		hash.o.remove();
	};
	
	$('#modal-participating').jqm({ajax:'@href', trigger: '.modal-participating-link a' ,closeClass:'jqmClose', onShow: openFnPartic, onHide: hideFn, target:'div#modal-participating-content'});


	
	
	$.fn._ie_select=function() { 
		return $(this).each(function() { 
			var a = $(this),
				p = a.parent();

			p.css('position','relative');

			var o = a.position(),
				h = a.outerHeight(),
				l = o.left,
				t = o.top;

			var c = a.clone(true);

			$.data(c,'element',a);

			c.css({
					zIndex   : 100,
					height   : h,
					top      : t,
					left     : l,
					position : 'absolute',
					width    : 'auto',
					opacity  : 0
			}).attr({
					id    : this.id + '-clone',
					name  : this.name + '-clone'
			}).change(function() {
					$.data(c,'element')
							.val($(this).val())
							.trigger('change')
			});

			a.before(c).click(function() {
					c.trigger('click');
			});
		}); // END RETURN
	}; // END PLUGIN

	// Usage
	if ($.browser.msie) {
			$('form#property-console select')._ie_select();
	}

});

function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

