$(document).ready(function() {
	mac_pc();
	if ($('input.text').length > 0) clear_text();
	if ($('input.daterange').length > 0) dates();
	if ($('#weather').length > 0) weather();
	if ($('#faqs').length > 0) faqs();
	if ($('#calendar_slideout').length > 0) slideout_setup();
	if ($('#quick_form').length > 0) quick_setup();
	if ($('.slideshow').length > 0) slideshow_setup();
	if ($('#images').length > 0) home_setup();
	if ($('.tooltip').length > 0) add_tooltip();
	if ($('#what_refresh').length > 0) what_refresh();
	if ($('#infinite').length > 0) infinite_setup();
	if ($('#volunteer_form').length > 0) form_setup();
	if ($('#event_select').length > 0) {
		$('h1 select.custom').selectmenu({
			style:'dropdown',
			width:225,
			change:function(e,o) {
				window.location = o.value;
			}
		},'disabled',true);
	}
	if ($('.twitter_block').length > 0) {
		$('#tweety').tweet({
			count: 1,
			username: "GrandCenter",
			template: "{text}"
		});
	}
	if ($('input.required').length > 0) form_validation();
	if ($('img#bg').length > 0) bg_fix();
});
function bg_fix() {
	var img = $('img#bg').attr('src');
	$('body').append('<div id="imgbg"><img src="'+img+'" alt="" /></div>');
	$('#imgbg').hide();
	function fix_it() {
		if ($(window).width() < 960) {
			$('#imgbg').show();
			$('img#bg').hide();
		} else {
			$('#imgbg').hide();
			$('img#bg').show();
		}
	}
	fix_it();
	$(window).resize(function() { fix_it(); });
	
	function fix_top() {
		var t = $(window).scrollTop();
		$('#imgbg').css({'top':t+'px'});
	}
	fix_top();
	$(window).scroll(function() { fix_top(); });
}
function form_validation() {
	$('input.submit').click(function() {
		var e = 0;
		$('input.required').each(function() {
			if ($(this).val() == '') {
				e++;
				$(this).addClass('error_pending');
			} else {
				$(this).removeClass('error_pending');
			}
		});
		$('textarea').each(function() {
			if ($(this).val() == '') {
				e++;
				$(this).addClass('error_pending');
			} else {
				$(this).removeClass('error_pending');
			}
		});

		if (e > 0) {
			$('.error').removeClass('error');
			$('.error_pending').removeClass('error_pending').addClass('error');
			return false;
		} else {
			return true;
		}
	});
}
function form_setup() {
	$('#volunteer_form select.custom').each(function() {
		if ($(this).hasClass('state')) {
			$(this).selectmenu({
				style:'dropdown',
				width:70
			});
		} else {
			$(this).selectmenu({
				style:'dropdown',
				width:282
			});			
		}
	});
}
function infinite_setup() {
	if ($('.page_calendar_search_results').length > 0) {
		$('#infinite').infinitescroll({
		    navSelector  : "#pagination",    
		    nextSelector : "#pagination a:first",
		    itemSelector : "#infinite div.item",
			loadingText	 : "Loading more posts",
			loadingImg   : "/library/images/ajax-loader-news.gif",
			donetext     : "<img src='/library/images/text-no-more.gif' alt='There are no more posts to load' />"
		});	
	} else {
		$('#infinite').infinitescroll({
		    navSelector  : "#pagination",    
		    nextSelector : "#pagination a:first",
		    itemSelector : "#infinite div.post",
			loadingText	 : "Loading more posts",
			loadingImg   : "/library/images/ajax-loader-news.gif",
			donetext     : "<img src='/library/images/text-no-more.gif' alt='There are no more posts to load' />"
		});
	}
}
function dates() {
	$('input.daterange').datepicker({
		dateFormat:'mm/dd/y'
	});
}
function what_refresh() {
	$('a#what_refresh').click(function() {
		ajax_call();
		return false;
	});
	function ajax_call() {
		$.ajax({
			url:$('#ajax_url').attr('value'),
			type:'POST',
			beforeSend:function() {
				$('#ajax_content').html('<div class="loader texttitle">Loading</div>');
			},
			success:function(results) {
				$('#ajax_content').html(results);
			}
		});
	}
}
function add_tooltip() {
	if ($('#home_stretch').length > 0) {
		$('#home_stretch img').each(function() {
			$(this).attr('alt','');
		});
	}
	$('a.tooltip').tooltip({
		fade:300,
		showURL:false
	});
}
function home_setup() {
	$('img.faded').fadeTo(0,.6);
	$('img.faded').hover(function() {
		$(this).animate({'opacity':1},{duration:300,queue:false});
	},function() {
		$(this).animate({'opacity':.6},{duration:300,queue:false});
	});
	function w_resize() {
		var sh = 1215,
			sw = 319;
		if ($(window).width() > 960) {
			var w = $(window).width();
		} else {
			var w = 960;
		}
		var h = w/(sh/sw);
		if ($('body').hasClass('msie') && $('body').hasClass('v7')) {
			var dw = w-($('.col_a').width()+$('.col_b').width()+$('.col_c').width());
			$('.col_d').css({'width':dw+'px'});
		}
		$('#images').css({'height':h+'px','width':w+'px'});
	}
	w_resize();
	$(window).resize(function() {
		w_resize();
	});
	
	$('a#home_refresh').click(function() {
		$.ajax({
			url:$('#ajax_url').attr('value'),
			type:'POST',
			beforeSend:function() {
				$('#ajax_refresh').html('<div class="loader texttitle">Loading</div>');
			},
			success:function(results) {
				$('#ajax_refresh').html(results);
			}
		});
		return false;
	});
}
function faqs() {
	if ($('.faq').length > 1) {
	$('.faq a').click(function() {
		var ind = $('.faq a').index(this);
		$('.faq a:gt('+ind+'),.faq a:lt('+ind+')').removeClass('open');
		$(this).addClass('open');
		$('.faq').each(function(i,e) {
			if (i != ind) {
				$('+ div',this).slideUp(300,function() { $(this).addClass('hide'); });	
			} else {
				$('+ div',this).slideDown(300,function() { $(this).removeClass('hide'); });
			}
		});
		return false;
	});
	}
}
function quick_setup() {
	if ($('.page_calendar_landing').length > 0 || $('.page_calendar_search_results').length > 0) {
		$('#quick_form select.custom').selectmenu({
			style:'dropdown',
			width:210
		});
	} else {
		$('#quick_form select.custom').selectmenu({
			style:'dropdown',
			width:150
		});		
	}
}
function slideout_setup() {
	$('#calendar_slideout select.custom').selectmenu({
		style:'dropdown',
		width:170
	});
	
	$('a#b_cal,a#b_close').click(function() {
		$('#calendar_slideout').toggleClass('open');
		if ($('#calendar_slideout').hasClass('open')) {
			$('#calendar_search_slideout').animate({'left':'0px'},{duration:300,queue:false});
		} else {
			$('#calendar_search_slideout').animate({'left':'-505px'},{duration:300,queue:false});
		}
		return false;
	});
	
	if ($('.page_home').length > 0) {
		function h_scroll() {
			var t = $(window).scrollTop(),
				y = $('#content').offset().top-65,
				h = $('#calendar_slideout').offset().top+37;
			if (t >= y) {
				$('#calendar_slideout').css({'position':'fixed','top':'65px'});
				if ($('#calendar_slideout').hasClass('open') && $('ul#venue_select-menu').hasClass('ui-selectmenu-open')) {
					$('ul#venue_select-menu').css({'top':h+'px','z-index':'9999'});
				}
			} else {
				$('#calendar_slideout').css({'position':'absolute','top':'0px'});
				$('ul#venue_select-menu').css({'top':(y+102)+'px','z-index':'9999'});
			}
		}
		h_scroll();
		$(window).scroll(function() {
			h_scroll();
		});
	} else {	
		function h_scroll_dd() {
			if ($('#calendar_slideout').hasClass('open') && $('ul#venue_select-menu').hasClass('ui-selectmenu-open')) {
				var t = $(window).scrollTop(),
					y = $('#content').offset().top-65,
					h = $('#calendar_slideout').offset().top+37;
				$('ul#venue_select-menu').css({'top':h+'px','z-index':'9999'});
			}
		}
		h_scroll_dd();
		$(window).scroll(function() {
			h_scroll_dd();
		});
	}
}
function weather() {
	$('#weather').weatherfeed(['USMO0787'],{
		'unit':'f',
		'image':true,
		'highlow':false,
		'wind':false,
		'link':false,
		'showerror':false
	});
}
function clear_text() {
	$('input.text').each(function() {
		var text = $(this).val();
		$(this).click(function() { if ($(this).val() == 'Search') $(this).val(''); });
		$(this).blur(function() {
			if ($(this).val() == '' || $(this).val() == 'Search') {
				$(this).val(text);
			}
		});
	});
}
function mac_pc() {
	if (navigator.appVersion.indexOf("Mac")!=-1) {
		$('body').addClass('mac');
	} else {
		$('body').addClass('pc');
	}
	$.each($.browser, function(i,e) {
		if (i != 'version') {
			$('body').addClass(i);
		} else {
			var v = String(e).split('.');
			$('body').addClass('v'+v[0]);
		}
	});
	if (navigator.platform.indexOf("iPad") != -1) {
		$('body').addClass('iPad');
	}

}
function slideshow_setup() {

	$('.slideshow').each(function() {
		var my_div = $(this);
		add_slideshow(my_div);
	});

	function add_slideshow(el) {
		var current = 0;
		var total = $('.image',el).length;

		$('.count',el).text('image '+(current+1)+' of '+total);
		$('.image',el).fadeTo(0,0);
		$('.image img',el).each(function() {
			$(this).css({'margin-left':-($(this).width()/2)+'px','margin-top':-($(this).height()/2)+'px'});
		});
		$('.image:eq(0)',el).fadeTo(0,1);
		

		el.hover(function() {
			$(this).addClass('hover');
			slide_caption_down();
		},function() {
			$(this).removeClass('hover');
			slide_caption_up();
		});

		function slide_caption_down() {
			if ($('.image:eq('+current+') .image_caption',el).length > 0) {
				$('.image:eq('+current+') .image_caption',el).animate({'margin-top':'0px'},{duration:500,queue:false});
			}
		}
		function slide_caption_up() {
			if ($('.image:eq('+current+') .image_caption',el).length > 0) {
				$('.image:eq('+current+') .image_caption',el).animate({'margin-top':'-435px'},{duration:500,queue:false});
			}
		}

		
		if (total > 1) {
			$('a.direction',el).click(function() {
				clearInterval(ss_timer);
				var dir = ($(this).hasClass('next')) ? 'next' : 'previous';
				fade_slide(dir);
				return false;
			});
			$('.image',el).click(function() {
				clearInterval(ss_timer);
				fade_slide('next');
				return false;
			});
	
			function fade_slide(dir) {
				//slide_caption_up();
				//$('.image',el).eq(current).fadeTo(300,0);
				$('.image',el).eq(current).animate({'opacity':0},{duration:300,queue:false});
				if (dir == 'next') {
					if (current < total-1) {
						current++;
					} else {
						current = 0;
					}
				} else {
					if (current <= 0) {
						current = total-1;
					} else {
						current--;
					}
				}
				//$('.image',el).eq(current).fadeTo(300,1);
				$('.image',el).eq(current).animate({'opacity':1},{duration:300,queue:false});
				//slide_caption_down();
				$('.count',el).text('image '+(current+1)+' of '+total);
				slide_caption_down();
			}

			var ss_timer = setInterval(function() {
				fade_slide('next');
			},5000);

		} else {
			$('a.direction',el).hide();
			$('.image',el).css({'cursor':'default'});
		}

	}
}
