// Global settings for future AJAX calls
$(function(){
	$.ajaxSetup({
		type: "POST",
		dataType: "html",
		cache: false
	});
	
	// add tooltip
	$("a.tooltiplink").tooltip();
	
	$("#scroller-new").scrollItem();
	
});


// This is a general function to submit a form using ajax.
/*
function ajaxSubmitForm(f)
{
	$.ajax({
		url: "/"+f+"-form.inc.php",
		data: $("#"+f+"form").serialize(),
		success: function(html){
			$("#"+f+"formcont").html(html);
		}
	});
	
	return false;
}
*/

// this one has some special features to allow for multiple sitess
function ajaxSubmitForm(f)
{
	// This line pulls the site var from a hidden input
	var site = $("#site").val();
	$.ajax({
		url: "/"+site+"-"+f+"-form.inc.php",
		data: $("#"+f+"form").serialize(),
		success: function(html){
			$("#"+f+"formcont").html(html);
		}
	});
	
	return false;
}

// update a cont
function updateCont(site, cont, id)
{
	$.ajax({
		url: "/"+site+"-"+cont+".inc.php",
		data: ({site : site, cont: cont, id: id}),
		success: function(html){
			$("#"+cont+"cont").html(html);
		}
	});
	
	return false;
}

// class search
function updateClassSchedule()
{
	var site = $("#site").val();
	$.ajax({
		type: "GET",
		url: "/"+site+"-classschedule.inc.php",
		data: $("#classscheduleform").serialize(),
		success: function(html){
			$("#classschedulecont").html(html);
		}
	});
	
	return false;
}

(function($) {  
	$.fn.scrollItem = function(options) {
		var defaults = {  }
		var options = $.extend(defaults, options);
		return this.each(function() {
			var limit = $("#scrollmove > table").size(),
				delay = 5000,
				timeoutDelay = 5000,
				current = -1,
				next = 0,
				isForced = false,
				isPaused = false,
				isAfterForced = false,
				rotate,
				timeout,
				timeoutStatus,
				currentSlide,
				nextSlide;
			
			function createTimer(){
				rotate = setInterval(autoSwapPhoto,delay);
			}
			
			function stopTimer(){
					clearInterval(rotate);
					if(typeof(timeout) !== 'undefined'){
						clearTimeout(timeout);
					}
			}
			
			function delayTimer(){
				stopTimer();
				if(!isPaused){
					timeout = setTimeout(createTimer, timeoutDelay);
				}
			}
			
			function forceSwapPhoto(event){
				delayTimer();
				var obj = $(event.currentTarget)
				if(obj.attr("id") === "scrollnext"){
					swapPhoto("up");	
				} else if(obj.attr("id") === "scrollprev"){
					swapPhoto("down");	
				}
				return false;
			}
			
			function autoSwapPhoto() {
				swapPhoto("up");
			}
						
			
			function toggleTimer(event){
				if(isPaused){
					createTimer();
					isPaused = false;	
				} else {
					stopTimer();
					isPaused = true;
				}
				$(event.currentTarget).blur();
				return false;
			}

			function swapPhoto(dir) {
				var postContainer = $("#scrollmove > tbody > tr");
				var allPosts = $("#scrollmove > tbody > tr > td");
				
				if(dir === "up"){
					$("#scrollmove").animate({"left":"-330px"}, function(){
						$(allPosts[0]).appendTo(postContainer);
						$("#scrollmove").css("left",0);
					});
				} else if(dir === "down"){
					$(allPosts).last().prependTo(postContainer);
					$("#scrollmove").css("left","-330px");
					$("#scrollmove").animate({"left":0});
				}
				
				if(isForced) {
					$("#rotate_list a").unbind("click");
					$("#rotate_list a").bind("click", forceSwapPhoto);	
					isForced = false;
					isAfterForced = true;
				}
			}
			
			createTimer();
			$("#scroller-new").css("overflow","hidden");
			$("#scrollprev").click(forceSwapPhoto);
			$("#scrollnext").click(forceSwapPhoto);
		});
	}
})(jQuery);
