/*************************************************************************
	Timberline Lodge JavaScript
	
	req: JQuery 1.2.6
	
	Author: Dane Hesseldahl
	Client: Nemo Design
	Copyright 2008, All Rights Reserved
	
*************************************************************************/

 // $(window).resize(function() {
 // 	var _width = $(this).width();
 // 	var _destX = ((_width - 1350) / 2).toString();
 // 	$('body').css('background-position',_destX + "px " + 0);
 // });

$(document).ready(function() {
	
	var $j = jQuery;
	
	$('#searchsubmit').click(function(){doSearch();});
	
	$("#s").keydown(function(event)
	{
		if(event.keyCode==13)
		{
			doSearch();
			return false;
		}
	});
	
	$('ul#events_content li:last').addClass('last');
	
	if ($.browser.msie) {
		$('#job_list dd').after('<br />');
		$('#weatherWidgetDisplay').css('cursor', 'finger').click(function() {
			var url = $('#weatherConditionsLink').attr('href');
			location = url;
		});
	}
	
	//ie6 only
	$.each($.browser, function(i, val) {
		if(i=="msie" && jQuery.browser.version.substr()=="6.0") {
			$('#explore li.thumbnail img').wrap('<a href="/explore"></a>');
		}
	});
	$.each(jQuery.browser, function(i) {
		if($.browser.msie){
			$('#mainNav li').addClass('fixIE').find('p').hide();
			$('#mainNav li').hover(function() {
				$(this).height(67).css('paddingTop', '0').find('p').show();
			}, function() {
				$(this).height(43).css('paddingTop', '24px').find('p').hide();
			});
		} else {
			$('#mainNav li.navClosed').hover(function() {
				$(this).stop().animate({paddingTop: '0px'}, 'fast');
			}, function() {
				$(this).stop().animate({paddingTop: '24px'}, 'fast');
			});
		}
	});
	///////////////////	

	$('#weatherSnowButton').one('click', function() {
		widgetClickSnow();
	});
	
	//$('#explore li.thumbnail:hover div').css('display', 'none');
	
	$('#explore li.thumbnail').hover(function() {
		$(this).find('div').fadeIn('fast');
	}, function() {
		$(this).find('div').fadeOut('fast');
	});
	
	$('ul.department_list').hide();
	
	$('#job_list h5').click(function() {
		$(this).next('ul.department_list').slideToggle(400);
	});
	
	$('#job_list h5:first').click();
		
	$('div.grid_post.tout').remove().clone(true).insertAfter('.grid_post:eq(2)');
	//$('div.grid_post.tout').css('padding-left','0');
	//$('div.grid_post.tout').clone(true).insertAfter('.grid_post:eq(2)');
	//$('div.grid_post.tout').remove();
	

	$('#tout_images').width(259);
	$('#tout_images').height(223);
	$('#tout_images').css('position', 'relative');
	
		$('#tout_images').before('<div id="nav">').cycle({
		fx: 'fade',
		timeout: 6000, 
		speed: 500,
		random: 0,
		startingSlide: 0,
		pager:  '#nav' ,
		after:     toutsOnAfter
		//before: 	toutsBefore
	});
	
	// select the first one this should only be used in random is turned off
	$($('#tout_images').parent().children("#nav").children("a")[0]).addClass('selected');

	
	$('div.grid_post').removeAttr('style');
	$('div.grid_post:even').addClass('even');
	$('div.grid_post:odd').addClass('odd')
	$('div.grid_post:eq(0), div.grid_post:eq(1)').addClass('top');
	
	$('#instructorSort a').click(function() {
		$(this).css('outline','none');
		$('#instructorSort a.current').removeClass('current');
		$(this).addClass('current');
		
		var filterVal = $(this).text().toLowerCase().replace(' ','-');
				
		if(filterVal == 'all') {
			$('div.grid_post.hidden').fadeIn('slow').removeClass('hidden');
		} else {
			$('div.grid_post').each(function() {
				if(!$(this).hasClass(filterVal)) {
					$(this).fadeOut('normal').addClass('hidden');
				} else {
					$(this).fadeIn('slow').removeClass('hidden');
				}
				$(this).removeClass('even').removeClass('odd').removeClass('top');
			});
		}
		$('div.instructor.grid_post:not(.hidden):even').addClass('even');
		$('div.instructor.grid_post:not(.hidden):odd').addClass('odd')
		$('div.instructor.grid_post:not(.hidden):eq(0), div.instructor.grid_post:not(.hidden):eq(1)').addClass('top');
		
		return false;
	});
	
	var browser = $.os.name;
	if (browser == "iphone" || browser == "ipod") {
		$('#mainNav li').unbind('hover').css('paddingTop', '0px');
	};
	
});

$(window).load(function() {
	$(this).resize();
});

function toutsOnAfter(curr,next,opts) {
	var curSlide = opts.currSlide;
	$(this).parent().parent().children("#nav").children("a").removeClass('selected');
	$($(this).parent().parent().children("#nav").children("a")[curSlide]).addClass('selected');
}

function widgetClickWeather() {
		$('ul.weatherWidgetToggle li').toggleClass('active').toggleClass('inactive');
		$('#weatherSummary, #weatherSnow').fadeToggle("fast");
		$('#weatherSnowButton').one('click', function() {
			widgetClickSnow();
		});
};

function widgetClickSnow() {
		$('ul.weatherWidgetToggle li').toggleClass('active').toggleClass('inactive');
		$('#weatherSummary, #weatherSnow').fadeToggle("fast");
		$('#weatherSummaryButton').one('click', function() {
			widgetClickWeather();
		});
};

$.fn.fadeToggle = function(speed, easing, callback) {
   return this.stop().animate({opacity: 'toggle'}, speed, easing, callback);
};


function doSearch() {
	var _s = $('#s').val();
	window.location.href = '/search/'+_s;
}

function validateSubscription() {
	var _err = "";
	
	if (!jQuery('#subscribe_name').val()) {
		_err = "<br/>You must provide a name<br/>";
		jQuery('#subscribe_error').html(_err);
		jQuery('#subscribe_name').focus();
		return false;
	}
	
	if (!jQuery('#subscribe_email').val()) {
		_err = "<br/>You must provide an email address<br/>";
		jQuery('#subscribe_error').html(_err);
		jQuery('#subscribe_email').focus();
		return false;
	}
	
	if (!validEmail(jQuery('#subscribe_email').val())) {
		_err = "<br/>Invalid Email Address<br/>";
		jQuery('#subscribe_error').html(_err);
		jQuery('#subscribe_email').focus();
		return false;
	}
	
	jQuery('#subscribe_error').html(_err);
	
	submit();
	return true;
}

function validEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (str.indexOf(at)==-1) {
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
	    return false
	}

	if (str.indexOf(at,(lat+1))!=-1) {
	    return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
	    return false
	}

	if (str.indexOf(dot,(lat+2))==-1) {
		return false
	}

	if (str.indexOf(" ")!=-1) {
	    return false
	}

	return true;				
}
