

google.load("jquery", "1");
google.setOnLoadCallback(function(){

	jQuery.noConflict();
	jQuery(document).ready(function(){

		jQuery('#tab-nav li').hover(function(){
			jQuery(this).children().filter('ul').show('fast');
		}, function(){
			jQuery(this).children().filter('ul').hide('fast');
		});

		//Specific processing for the homepage events display
		if(window.location.pathname == '/'){

			if (document.all&&document.getElementById) {
				jQuery('#calendar_event_list table').attr('cellspacing', 5);
				jQuery('#calendar_event_list > table').css('margin-left', '3px');
				jQuery('#tab-nav li').hover(function(){jQuery(this).addClass('over');},function(){jQuery(this).removeClass('over');});
			}


			////////////////////////////////////////////////////////////////////////////
			//Start specific processing for calendar events
			var calendar_id = jQuery('#calendar_event_list table').attr('id');
			var year = parseInt(calendar_id.substr(9,4));
			var month = parseInt(calendar_id.substr(14));

			getMonths(year,month);
			jQuery('td.events div.event_container').hover(function(){
				jQuery(this).children().slideDown('slow');
			}, function(){
				jQuery(this).children().slideUp('slow');
			});



			////////////////////////////////////////////////////////////////////////////
			//Start processing for image treatment
			//use main_image class to contain the index of the image we are viewing
			jQuery('#main_image').attr('class','main_image_0');
			var interval_ref = setInterval("changeFeatureImage()", 8000);
			setUpScrollingImages(interval_ref);
		}
	})
});
/*
 An attempt to add dome sort of time based delay to menu's opening and closing
function menuHide(jQObject)
{
	jQuery(jQObject).children().filter('ul').hide('fast');
}

function menuTimedHide(){
	//alert(test);
	closetimer = window.setTimeout(function(){
		jQuery('#tab-nav li ul').children().filter('ul').hide('fast');
		//jQuery(jQObject).children().filter('ul').hide('fast');
		}, 500);
}
*/
function setUpScrollingImages(interval_ref){

	//////////////////////////////
	//Add scrolling functionality

	//don't scroll if 5 or less images
	if(jQuery('.thumb').length <= 5){
		//jQuery('img.left_image_arrow').hide();
	} else {
		//Set up scrolling images
		jQuery('img.left_image_arrow').click(function(){
			var current_id = jQuery('.images_wrapper').attr('id');
			var current_id = parseInt(current_id.substr(15));
			var next_id = current_id + 1;
			if((next_id + 5) >= jQuery('.thumb').length){
				jQuery('img.left_image_arrow').hide();
			}
			jQuery('.images_wrapper').attr('id','images_wrapper_'+next_id);

			jQuery('.images_wrapper .images').animate({left:'-=88'},1000,'linear',function(){

				});
			jQuery('img.right_image_arrow').show();

			/*var current_id = jQuery('.images_wrapper').attr('id');
			var next_id = parseInt(current_id.substr(15, 1)) + 1;
			jQuery('.image_wrapper').attr('id', 'images_wrapper_'+next_id);*/
		});

		jQuery('img.right_image_arrow').click(function(){
			var current_id = jQuery('.images_wrapper').attr('id');
			var current_id = parseInt(current_id.substr(15));
			var next_id = current_id - 1;
			if(next_id == 0){
				jQuery('img.right_image_arrow').hide();
			}
			jQuery('.images_wrapper').attr('id','images_wrapper_'+next_id);

			jQuery('.images_wrapper .images').animate({left:'+=88'},1000,'linear',function(){

				});
			jQuery('img.left_image_arrow').show();

			/*var current_id = jQuery('.images_wrapper').attr('id');
			var next_id = parseInt(current_id.substr(15, 1)) - 1;
			jQuery('.image_wrapper').attr('id', 'images_wrapper_'+next_id);*/
		});
	}
	//jQuery('img.right_image_arrow').hide();

	///////////////////////////////
	//Add feature image swap out functionality

	jQuery('.scrolling_image').click(function(){

		var id = parseInt(jQuery(this).attr('id').substr(16));

		jQuery.post("/events", {action:"ajax_get_feature_image", product_id:id}, function(data){
			clearInterval(interval_ref);
			jQuery('#main_image').prepend(data);
			jQuery('.image_container').hide();
			jQuery('#new_image_container').fadeIn();
		});

	});
}

function changeFeatureImage(){

	//get next image id
	var current_id = jQuery('#main_image').attr('class');
	var current_id = parseInt(current_id.substr(11));
	var current_image_id = 'image_container_'+current_id;
	current_image_id = '#'+current_image_id;

	if(jQuery(current_image_id).hasClass('next')){

		var next_id = current_id + 1;
		var next_image_id = 'image_container_'+next_id;

		//check that image exists
		next_image = document.getElementById(next_image_id);
		if(next_image == null){
			next_id = 0;
			next_image_id = 'image_container_0';
			//if only one image exists in list of images, don't change
			if(current_id == 0){
				return;
			}
		}



	} else if(jQuery(current_image_id).hasClass('random')){

		//get number of imgs to cycle through
		var count = jQuery('div.image_container').length;
		if(count > 1){
			var next_id = Math.floor(Math.random() * count);

			if(next_id == current_id){
				if(next_id == count - 1)
					next_id = 0;
				else
					next_id++;
			}

			next_image_id = 'image_container_'+next_id;

		} else {
			return;
		}
	} else {
		return;
	}


	next_image_id = '#'+next_image_id;

	//get current z-index
	var z_index = jQuery(current_image_id).css('z-index');

	//place lower z-index in current div and img
	jQuery(current_image_id).css('z-index',jQuery(next_image_id).css('z-index'));
	jQuery(current_image_id+' img').css('z-index',jQuery(next_image_id).css('z-index'));

	//place higher z-index in image to show
	jQuery(next_image_id).css('z-index', z_index);
	jQuery(next_image_id+' img').css('z-index', z_index);

	jQuery(next_image_id).fadeIn('slow');
	jQuery(current_image_id).hide();

	//update main_image class
	jQuery('#main_image').attr('class', 'main_image_'+next_id);
}

function getMonths(year, month){

	jQuery('.left_arrow').remove();
	jQuery('.right_arrow').remove();

	var next_month;
	var next_year;
	var prev_month;
	var prev_year;
	if(month < 10)
		var year_month = year.toString() + '0' + month.toString();
	else
		var year_month = year.toString() + month.toString();
	var table_id;
	var date = new Date();
	var todays_month = date.getMonth() + 1;
	var todays_year = date.getFullYear();

	if(todays_month < 10)
		var todays_year_month = todays_year.toString() + '0' + todays_month.toString();
	else
		var todays_year_month = todays_year.toString() + todays_month.toString();


	//var todays_year_month =

	if(month == 12){
		next_month = 1;
		next_year = parseInt(year) + 1;
		prev_month = 11;
		prev_year = year;

	} else if (month == 1) {
		next_month = 2;
		next_year = year;
		prev_month = 12;
		prev_year = parseInt(year) - 1;
	} else {
		next_month = parseInt(month) + 1;
		next_year = year;
		prev_month = parseInt(month) - 1;
		prev_year = year;
	}

	if(month < 10)
		table_id = 'calendar_'+year+'-0'+month;
	else
		table_id = 'calendar_'+year+'-'+month;

	var table = document.getElementById(table_id);
	var month_created = false;

	if(table == null){
		var month_created = true;
		jQuery.post("/events", {action:"ajax_get_events", event_year:year, event_month:month}, function(data){
			jQuery('table.current_event_list').css({'z-index':'10000'});
			jQuery('#calendar_event_list').prepend(data);
			jQuery('#'+table_id+' td.events div.event_container').hover(function(){
				jQuery(this).children().slideDown('slow');
			}, function(){
				jQuery(this).children().slideUp('slow');
			});

			if (document.all&&document.getElementById) {
				jQuery('#calendar_event_list table').attr('cellspacing', 5);
				jQuery('#calendar_event_list > table').css('margin-left', '3px');
				jQuery('#tab-nav li').hover(function(){jQuery(this).addClass('over');},function(){jQuery(this).removeClass('over');});
			}
		});
	}

	if(todays_year_month < year_month){
		jQuery.post("/events", {action:"ajax_get_short_events", event_year:year, event_month:month, product_list_limit:6}, function(data){
			jQuery('table.short_event_list').replaceWith(data);
		});
	}else {
		jQuery.post("/events", {action:"ajax_get_short_events", product_list_limit:6}, function(data){
			jQuery('table.short_event_list').replaceWith(data);
		});
	}


	jQuery('#calendar_event_list').append('<img src="/siteimages/right_arrow.jpg" alt="right_arrow.jpg" class="right_arrow" />');
	jQuery('#calendar_event_list').append('<img src="/siteimages/left_arrow.jpg" alt="left_arrow.jpg" class="left_arrow" />');

	jQuery('.right_arrow').click(function(){
		if(month == 12){
			next_month = 1;
			next_year = year + 1;
		} else {
			next_month = parseInt(month) + 1;
			next_year = year;
		}
		if(next_month < 10)
			next_table_id = 'calendar_'+next_year+'-0'+next_month;
		else
			next_table_id = 'calendar_'+next_year+'-'+next_month;

		jQuery('#'+table_id).hide();
		getMonths(next_year,next_month);
		jQuery('#'+next_table_id).show();
	});


	jQuery('.left_arrow').click(function(){
		if (month == 1) {
			prev_month = 12;
			prev_year = year - 1;
		} else {
			prev_month = month - 1;
			prev_year = year;
		}
		if(prev_month < 10)
			prev_table_id = 'calendar_'+prev_year+'-0'+prev_month;
		else
			prev_table_id = 'calendar_'+prev_year+'-'+prev_month;

		jQuery('#'+table_id).hide();
		getMonths(prev_year,prev_month);
		jQuery('#'+prev_table_id).show();
	});
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (
			anchor.getAttribute("href") && (
			anchor.getAttribute("rel") == "external" ||
			anchor.getAttribute("rel") == "external nofollow" ||
			anchor.getAttribute("rel") == "nofollow external" )
			)
		anchor.target = "_blank";
	}
}
