//need this to get code to agree with other libraries
jQuery.noConflict();
(function($) {
	
var cal_events;
var SHC = {};
var event_dates = [];
var calendar_loaded = false;
var check_gallery_loaded;

SHC.labelReplacement = function (match_class) {
	$('label.' + match_class).each(function () {
		$(this).hide();
		$('input#' + $(this).attr("for")).val($(this).text());
	});
	$('input.' + match_class).focus(function () {
		$(this).val() == $('label[for="' + $(this).attr('id') + '"]').text() ? $(this).val('') : '';
	});
	$('input.' + match_class).blur(function () {
		$(this).val() == '' ? $(this).val($('label[for="' + $(this).attr('id') + '"]').text()) : '';
	});
}

SHC.seedDates = function(date) {
	
	for (var i = 0; i < event_dates.length; i++) {
		if (date.getTime() == event_dates[i].date.getTime()) {
			return [true, 'date_with_event'];
		}
	}
	return [false, ''];
}

SHC.dateSelected = function(dateText, inst) {
	var date_html = "";
	var menu_html = "";
	for(cal_event in cal_events)
	{
		if(cal_event == dateText)
		{
			date_html += "<h3 class='event-hd'> What's Happening on " + cal_event + '?</h3>';
			for(var i = 0; i < cal_events[cal_event].length; i++)
			{
				var menu_pattern = /^menus?$/i;
				if(!cal_events[cal_event][i].category.match(menu_pattern))
				{
					date_html += '<div class="event-container"><div class="event-title">' + cal_events[cal_event][i].title + '</div><div class="event-date">' + cal_events[cal_event][i].start_date + ' ' + (cal_events[cal_event][i].start_date != cal_events[cal_event][i].end_date ? '- ' + cal_events[cal_event][i].end_date : '') + '</div><div class="event-time">' + (cal_events[cal_event][i].allday == 0 ? cal_events[cal_event][i].start_time + ' - ' + cal_events[cal_event][i].end_time : 'all day') + '</div><div class="event-description">' + cal_events[cal_event][i].description + '</div></div>';
				}
				else
				{
					menu_html += '<div class="sidebar-food-menu"><div class="event-title">Today\'s Menu</div><div class="event-description"><div class="event-container">' + cal_events[cal_event][i].description + '</div></div></div>';
				}
			}
			date_html += menu_html;
		}
	}
	$.colorbox({html: date_html, width: '450px', opacity: .5});
}

SHC.getDates = function(month, year){
	var date_string;
	month == 'now' || year == 'now' ? date_string = '&&cal_year=now&&cal_month=now' : date_string = '&&cal_year=' + year + '&&cal_month=' + month;
	$('div.calendar').addClass('data-loading');
	$.ajax({
	   type: "GET",
	   url: "/?format=json&&datatype=events" + date_string,
	   dataType: "json",
	   success: function(data)
	   {
		 event_dates.length = 0;
		 event_dates = [];
		 for(var cal_event in data)
		 {
			cal_events = data;
			date_array = cal_event.split("/");
			event_dates.push({date: new Date(date_array[2], date_array[0]-1, date_array[1]), events: 1});
		 }
		 if(calendar_loaded == false)
		 {
			calendar_loaded = true;
			SHC.loadCalendar();
		 }
		 else
		 {
			$('div.calendar').datepicker( "setDate" ,  month + '/1/' + year);
		 }
	   }
	 });
}

SHC.loadCalendar = function (){
	$('div.calendar').datepicker( "refresh" )
	$('div.calendar').removeClass('data-loading').datepicker({
		onSelect: SHC.dateSelected,
		onChangeMonthYear: function(year, month, inst) { 
			SHC.getDates(month,year);
		},
		beforeShowDay: SHC.seedDates
	});
}

SHC.hofNav = function(){
	$('div.hall_of_fame_nav ul li a').each(function () {
		$(this).attr('href',$(this).attr('href').replace(/^.*hall-of-fame\/(.+)\/?/, '#$1'));
	});
	$('div.hall_of_fame_nav ul li a:not(a.year)').click(function () {
		if(!$(this).closest('li').hasClass('active'))
		{
			var link_el = this;
			$('div.hall_of_fame_nav ul li').removeClass('active');
			
			var load_url = $(link_el).attr('href').replace(/#/,'/');
			$('#hall_of_fame_window').fadeTo('slow', 0.5);
			$('#hall_of_fame_window').load(load_url + ' #hall_of_fame_content', function () {
				$('#hall_of_fame_window').fadeTo('fast', 1);
				$(link_el).closest('li').addClass('active');
				
			});
		}
		return false;
	});
}

SHC.enhancedGallery = function() {
	var gallery_images = [];
	var current_gallery = 0;
	var i = 0;
	$('div.gallery').each(function () { 
		if($(this).find('dl').size() > 0)
		{
			gallery_images[i] = {};
			gallery_images[i].container = $(this).closest('div.article');
			gallery_images[i].images = [];
			$(this).find('dl img').each(function () { 
				gallery_images[i].images.push($(this).clone());
			});
			gallery_images[i].images_loaded = 0;
			gallery_images[i].all_loaded = false;
			i++;
		}
	});
	
	
	for(var i = 0; i < gallery_images.length; i++)
	{
		var pic_list = document.createElement('div');
		$(gallery_images[i].images).each(function () {
			var temp_div = document.createElement('div');
			var this_img = $(this).attr('src');
			
			$(pic_list).append('<img src="' + this_img + '" alt="" style="max-width: 570px" />');
			
			//figure out preload solution

			preloadGalleryImages(temp_div, this_img, i);

		});
		$(pic_list).addClass('slideshow');
		var slideshow_wrapper = document.createElement('div');
		$(slideshow_wrapper).addClass('slideshow-wrapper').append(pic_list);
		
		$(gallery_images[i].container).find('div.gallery').after(slideshow_wrapper).remove();
	}

	check_gallery_loaded = setInterval(checkGalleriesLoaded, 1000);

	function checkGalleriesLoaded() {
		var all_loaded = true;
		for(var i = 0; i < gallery_images.length; i++)
		{
			gallery_images[i].all_loaded == false ? all_loaded = false : '';
		}
		if(all_loaded == true)
		{
			clearInterval(check_gallery_loaded);
			SHC.startSlideshows();
		}
	}
	
	function preloadGalleryImages(temp_div, this_img, i)
	{
		if(navigator.appName != 'Microsoft Internet Explorer')
		{
			$(temp_div).load(this_img, function () {
				gallery_images[i].images_loaded++;
				gallery_images[i].images_loaded == gallery_images[i].images.length ? gallery_images[i].all_loaded = true : '';
			});
		}
		else
		{
			$(temp_div).load('<img src="' + this_img + '" />', function () {
				gallery_images[i].images_loaded++;
				gallery_images[i].images_loaded == gallery_images[i].images.length ? gallery_images[i].all_loaded = true : '';
			});
		}
	}

}

SHC.startSlideshows = function() {
	$('div.slideshow').each(function(i) {
		$(this).before('<div class="slideshow-controls" style="display:none"><span><a href="" id="prev_' + i + '" class="prev-btn">Prev</a></span><span><a href="" id="pause_' + i + '" class="pause-btn">Pause</a></span><span style="display: none"><a href="" id="play_' + i + '" class="play-btn">Play</a></span><span><a href="" id="next_' + i + '" class="next-btn">Next</a></span></div>');
		$(this).cycle({
	        fx:     'fade',
	        speed:   400,
	        timeout: 3000,
			next:   '#next_' + i,
			prev:   '#prev_' + i
	    });
		$(this).closest('div.slideshow-wrapper').hover(
		        function() { $(this).closest('div.slideshow-wrapper').find('div.slideshow-controls').fadeIn(); },
		        function() { $(this).closest('div.slideshow-wrapper').find('div.slideshow-controls').fadeOut(); }
		);
		$('#pause_' + i).click(function() { $('div.slideshow:eq(' + i + ')').cycle('pause'); $('#pause_' + i).closest('span').hide(); $('#play_' + i).closest('span').show(); return false; });
		$('#play_' + i).click(function() { $('div.slideshow:eq(' + i + ')').cycle('resume'); $('#play_' + i).closest('span').hide(); $('#pause_' + i).closest('span').show(); return false; });
	});
	$('div.slideshow img').css({'cursor' : 'pointer'}).click(function () {
		var slide_pic = $(this).attr('src').replace(/-[0-9]+x[0-9]+/g,'');
		$.colorbox({ href: slide_pic, opacity: .5, maxWidth: '70%' });
	});
}


SHC.showTestimonials = function() {
	$('a.more-testimonial').live('click', function() {
		if($(this).hasClass('more'))
		{
			$(this).removeClass('more').addClass('less').html('[ &hellip;less]');
			$(this).closest('q').find('span.more-testimonial').hide().slideDown('slow');
		}
		else if($(this).hasClass('less'))
		{
			$(this).removeClass('less').addClass('more').html('[ &hellip;more]');
			$(this).closest('q').find('span.more-testimonial').slideUp('fast');
		}
		return false;
	});
}

SHC.showJobOpenings = function() {
	if($('#open-jobs-widget li').size() > 5)
	{
		$('#open-jobs-widget li').each(function (i) {
			if(i > 4)
			{
				$(this).hide();
			}
		});
		$('#open-jobs-widget').after('<a href="#" class="btn-alt all-open-jobs closed">view all job openings &raquo;</a>');
	}
	
	$('a.all-open-jobs').live('click', function () {
		if($(this).hasClass('closed'))
		{
			$('#open-jobs-widget li').each(function (i) {
				if(i > 4)
				{
					$(this).slideDown('fast');
				}
			});
			$(this).removeClass('closed').addClass('open').html('&laquo; collapse');
		}
		else
		{
			$('#open-jobs-widget li').each(function (i) {
				if(i > 4)
				{
					$(this).slideUp('fast');
				}
			});
			$(this).removeClass('open').addClass('closed').html('view all job openings &raquo;');
		}
		return false;
	});
}

$(function () {
	SHC.labelReplacement('text-input');
	if($('div#fullcalendar').size() == 0)
	{
		SHC.getDates('now','now');
	}
	$('a.event-link').click(function () {
		//$(this).next('div.event-detail').clone().dialog({show: 'fade', position: ['center',100]});
		$.colorbox({html: $(this).next('div.event-detail').html(), title: true, width: '450px'});
		return false;
	});
	$('a[href$=".jpg"],[href$=".png"],[href$=".gif"]').each(function () {
		if($(this).width() > 600)
		{
			var offset = parseInt((600 / $(this).find('img').width()) * $(this).find('img').height());
			$(this).find('img').css({'width' : '600px', 'height' : offset.toString() + 'px'});
		}
	});
	$('a[href$=".jpg"],[href$=".png"],[href$=".gif"]').colorbox({ opacity: .5, maxWidth: '70%' });
	
	$('a[rel^="attachment"]').live('click', function(){
		current_image_link = $(this).find('img').attr('src').replace(/-[0-9]+x[0-9]+/,'');
		$.colorbox({ href: current_image_link, opacity: .5, maxWidth: '70%' });
		return false;
	});
	$('a.flickr-image').each(function () {
		if($(this).width() > 600)
		{
			var offset = parseInt((600 / $(this).find('img').width()) * $(this).find('img').height());
			
			$(this).find('img').css({'width' : '600px', 'height' : offset.toString() + 'px'});
		}
	});
	$('a.flickr-image').click(function () {
		var img_link = $(this).find('img').clone();
		$(img_link).css({'height' : '', 'width' : ''});
		$.colorbox({html : img_link, opacity: .5 });
		return false;
	});
	$('a.comment-link').click(function () { 
		var comment_form = document.createElement('div');
		$(comment_form).load($(this).attr('href').replace(/^(.+)#(.+)$/, '$1 #$2'), function () {
			$(this).prepend('<h2>Leave a Comment</h2>');
			$.colorbox({html : comment_form, width : '500px' });
		});
		return false; 
	});
	SHC.hofNav();
	if($('#guestbook-msg').size() > 0)
	{
		$.colorbox({ html : $('#guestbook-msg'), width : '500px', opacity: .5});
	}
	SHC.enhancedGallery();
	SHC.showTestimonials();
	$('#open-jobs-widget a.colorbox').colorbox({ width: '500px', maxHeight: '500px' });
	$('#open-jobs-listing a.colorbox').colorbox({ width: '500px', maxHeight: '500px' });
	$( "#accordion" ).accordion({autoHeight: false});

	//SHC.showJobOpenings();
});

//need this to get code to agree with other libraries
})(jQuery);

