$(function() {
	for(var i in $.browser) {
		if ($.browser[i])
			BROWSER = i + (($.browser.msie)?$.browser.version.charAt(0):'');
	}
	if ( BROWSER=='msie6' ) {
		$(document).pngFix(); 
	}

	$('.track-packages').each(function() {
		var tag = '/'+this.name.replace(/:/g,'/');
		if($(this).is('form')) {
			var a = $(this).attr('action');
			$(this).submit(function() {
				URCHIN?urchinTracker(tag):pageTracker._trackEvent('form', 'submit', tag);
				if(tag.indexOf("reservations")>0) {
					$(this).attr({action:a+'?'+$(this).serialize(), method:'post'});
					URCHIN?__utmLinkPost(this):pageTracker._linkByPost(this);
				}
			});
		} else {
			$(this).click(function(e) {
				URCHIN?urchinTracker(tag):pageTracker._trackPageview(tag);
				if(tag.indexOf("reservations")>0) {
					e.preventDefault();
					var l = URCHIN?__utmLinkerUrl(this.href):pageTracker._getLinkerUrl(this.href);
					$(this).attr('target')=='_blank'?window.open(l,''):window.location=l;
				}
			});
		}
	});


	$(".standard").each(function(){
		var form = $(this);
		if(BROWSER=='msie6'||BROWSER=='msie7') {
			$(".textfield")
				.focus(function() { $(this).css({backgroundColor: "#fdfcfa"}); })
				.blur(function() { $(this).css({backgroundColor: "#ffffff"}); });
			$('.required', form).each(function() {
				$(this).parent().addClass('ie-icon-required');
			}); 
		}
	});

	//Home push and hours of operation h3 sifr
	$('div#home-push h3, div#hours h3, div#sidebar-push h3').sifr({ strSWF: '/atwater/fonts/cambria-italic.swf', strColor: '#FFC220', strWmode: 'transparent', intPadding: [0, 0, 0, 0]},	{ expressInstall: true });		

	// DHR logo
		$("div#dhr-logo").flash(
			{src:"http://www.destinationhotels.com/flash/branding/flash_logoDhr.swf?link=vailcascade",
			 width:250,
			 height:25,
			 wmode:"transparent",
			 allowScriptAccess:"always",
			 quality:"high"},
			 {version:"8"});
             
    /* Loyalty Form state filtering */
    $('.filter-state').change(function(){
        if($(this).val()=='United States' || $(this).val()=='Canada'){
            $('.phone-format').show();   
        }else{
            $('.phone-format').hide();  
        }   
        
        $.get("ajax-filter-states.php",
           {country:$(this).val()},
           function(returned_data)
             {
                 $('div.select-other').hide();
                $('span.states-filter').html(returned_data);
                set_select_other();
             });
    });
	
	//Print recipe
	$("#recipe-wrapper").each(function(){
		var recipeContainer = $(this);
		var recipe = $("#recipe",recipeContainer).html(); //This grabs the recipe content
		$("#recipe-print a").click(function(){
			//alert(recipe);
			//This adds the content below all content for jqmodal
			$("#footer-wrapper").after('<div id="print-recipe-wrapper" class="jqmWindow"><a href="#" class="jqmClose popup-top-right-close"></a><div id="print-options"><a href="javascript:window.print()">print</a> | <a href="#" class="jqmClose">close</a></div><div id="print-recipe">'+recipe+'</div></div>'); 
			var recipeClose=function(hash){
				$("#print-recipe-wrapper").remove();
				hash.w.hide()
				hash.o.remove();
			};
			var openFnrecipe=function(hash){
				hash.w.show();
				var difference = $(window).width() - $('#print-recipe-wrapper').width();
				difference = difference / 2;
				$('#print-recipe-wrapper').css({left:difference+275,top:$(window).scrollTop()+20});
			};
			$('#print-recipe-wrapper').jqm({onShow: openFnrecipe,onHide:recipeClose}).jqmShow();
			return false;
		});
		$("#print-command").click(function(){
			window.print()
			return false;
		});
	});
	
	// Packages
	$('.package-wrapper').each(function(){
		var package = $(this);
		$('.package-details a', package).toggle(function() {
			$('.share-panel', package).hide();
			$.get('includes/package-details.php', { package_id: this.id.substr(3), channel: this.rel },function(data) { 
				$('.package-long', package).hide().html(data).slideDown('slow');
			});
			$(this).html("Hide Details");
		},function() { 
			$('.share-panel', package).hide();
			$('.package-long', package).slideUp('slow').html('').hide();
			$(this).html("View Details");
		});
	});
	
	// Share button
	$('.rss-item').each(function(){
		var rssItem = $(this)
		$('.share-link', rssItem).click(function() {
			$('.share-panel', rssItem).hide();
			$('.share-panel', rssItem).css('display','inline');
			return false;
		});
		$('.share-close', rssItem).click(function(){
			$('.share-panel', rssItem).slideUp('fast');
			return false;
		});
	});
	
	// Blog post details
	$(".post").each(function(){
		var blogPost = $(this);
		$(".post-details-click a", blogPost).toggle(function(){
			$.get('includes/scrapbook-details.php', { event_id: this.id.substr(3), channel: this.rel },function(data) { 
				$('.post-description', blogPost).hide().html(data).slideDown('slow');
			});
			$(this).html("Hide Details");
		},function() { 
			$('.share-panel', blogPost).hide();
			$('.post-description', blogPost).slideUp('slow').html('').hide();
			$(this).html("View Details");
		});
	});
	
	// Blog comment functionality
	$("#comments-posted").each(function(){
		$("#blog-comment").hide();
		$("#comment-btn a").click(function(){
			$("#blog-comment").show().focus();
			$(this).hide();
		});
	});
	
	// Photo Gallery flash
	$("#photo-gallery").flash(
			{src:"flash/photo-gallery.swf",
			 width:565,
			 height:501,
			 wmode:"transparent",
			 quality:"high"},
			 {version:"8"},
				function(htmlOptions) {
					htmlOptions.flashvars.xmlPath = "/atwater/flash/xml/images.xml";
					$(this).html($.fn.flash.transform(htmlOptions));
			});
	
	// Photo gallery popup
	$("#view-photos").each(function(){
		var openFnphotos=function(hash){
			hash.w.show();
			var difference = $(window).width() - $('#photo-gallery-wrapper').width();
			difference = difference / 2;
			$('#photo-gallery-wrapper').css({left:difference+275,top:$(window).scrollTop()+20});
		};
		$(this).click(function(){
			$('#photo-gallery-wrapper').jqm({onShow: openFnphotos}).jqmShow();
			return false;
		});
	});

	// Video Gallery flash
	$("#video-gallery").flash(
			{src:"flash/video-gallery.swf",
			 width:565,
			 height:421,
			 wmode:"transparent",
			 quality:"high"},
			 {version:"8"},
				function(htmlOptions) {
					htmlOptions.flashvars.xmlPath = "/atwater/flash/xml/videos.xml";
					$(this).html($.fn.flash.transform(htmlOptions));
			});


	// Video gallery popup
	$("#view-videos").each(function(){
		var openFnvideos=function(hash){
			hash.w.show();
			var difference = $(window).width() - $('#video-gallery-wrapper').width();
			difference = difference / 2;
			$('#video-gallery-wrapper').css({left:difference+275,top:$(window).scrollTop()+20});
		};
		$(this).click(function(){
			$('#video-gallery-wrapper').jqm({onShow: openFnvideos}).jqmShow();
			return false;
		});
	});
	
	// Homepage flash
		$(".home #masthead").flash(
			{src:"flash/homepage.swf",
			 width:660,
			 height:436,
			 wmode:"transparent",
			 allowScriptAccess:"always",
			 quality:"high"},
			 {version:"8"});

	// Sidebar Signup label text
	$('input#email').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));
		}

		$(this).focus(function() {
			if($(this).val() === $(this).attr('title')) {
				$(this).val('').addClass('focused');
			}
		});

		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).removeClass('focused');
			}
		});
	});


	// Set current date in console
	$("#console").each(function(){
		var current = new Date();
		var currentMonth = current.getMonth();
		var currentYear = current.getYear();
		var currentDate = current.getDate();
		currentMonth++;
		$("#ResMonth",this).attr("value",currentMonth);
		$("#ResDate",this).attr("value",currentDate);
		$("#ResYear",this).attr("value",currentYear);
	});

             
	set_select_other();
});

function set_select_other(){
    $("select.select-other2").change( function () {
        if($(this).val() === "--") {
            $(this).parent().parent().next().show();
            if($(this).is(".required")) {
                $(this).parent().parent().next().find("label").addClass("required");
                $(this).parent().parent().next().find("input").addClass("required");
            }
        } else {
            $(this).parent().parent().next().hide().find("input").val("");
        }
    });   
}