// WinterShines 2010-2011 
// Scott                          
// scott@deezine.ca               
// December 2010

// HELPER FUNCTIONS
function fieldclear(field)      { if (field.defaultValue == field.value) { field.value = '';        } }
function fieldrestore(field)    { if (field.value == "")    { field.value = field.defaultValue;     } }
var IE       = /*@cc_on!@*/false;
var url      = "http://"+window.location.hostname+"/wintershines.php";
$.ajaxSetup({ url: url, type: 'post'});
$(document).ready( function() {
	$.validator.addMethod("eventName", function(value) {
		return value != "event name";
	}, "please tell us what the name of the event is");

	$.validator.addMethod("eventHost", function(value) {
		return value != "event host (company)";
	}, "please tell us who is hosting the event");

	$.validator.addMethod("Location", function(value) {
		return value != "event location";
	}, "please tell us where the event is being held");

	$.validator.addMethod("description", function(value) {
		return value != "event description";
	}, "please tell us a little about the event");

	$.validator.addMethod("eventCost", function(value) {
		return value != "event admission";
	}, "please tell us if your event is free or costs money");

	$.validator.addMethod("hostEmail", function(value) {
		return value != "host email";
	}, "please tell us us what your email is");

	 // validate the comment form when it is submitted
	$("#newEvent").validate({
		errorLabelContainer: $("#error"), 
		rules: {
			EventName: {
				eventName: true, 
				required: true
			},
			EventHost: {
				eventHost: true,
				required: true
			},
			Location: {
				Location: true,
				required: true
			},
			description: {
				description: true,
				required: true
			},
			cost: {
				eventCost: true,
				required: true
			},
			HostEmail: {
				required: true,
				hostEmail: true
			}
		},
		messages: {
			description: "please tell us a little about your event"
		}
	});    
});
function isDefaultValue(id) {
	if ( $(id).attr("defaultValue") == $(id).attr("value") ) {
		return true;
	} else {
		return false;
	} 
}
function goLeft() {
	$('#homeWrapper').scrollTo( '#left', 800, {axis:'x', onAfter:function() {
		$('#buttonAdd').fadeOut('fast', function() {
			$('#buttonAdd').unbind('click').click(function(event) { goRight(); return false; });                      
			$('#buttonAdd').html("&nbsp;&nbsp;&nbsp;&nbsp;add event details").fadeIn('fast');  
		});
		$('#buttonAddIcon').fadeOut('fast', function() {
			$('#buttonAddIcon').addClass('submitTextPlus');                      
			$('#buttonAddIcon').removeClass('submitTextMinus');
			$('#buttonAddIcon').html("+").fadeIn('fast');
		});      
	} });
}
function goRight() {
	$('#homeWrapper').scrollTo( '#right', 800, {axis:'x', onAfter:function() {
		$('#buttonAdd').fadeOut('fast', function() {
			$('#buttonAdd').unbind('click').click(function(event) { goLeft(); return false; });                     
			$('#buttonAdd').html("&nbsp;&nbsp;&nbsp;&nbsp;back to calendar").fadeIn('fast');  
		}); 
		$('#buttonAddIcon').fadeOut('fast', function() { 
			$('#buttonAddIcon').addClass('submitTextMinus');                     
			$('#buttonAddIcon').removeClass('submitTextPlus');
			$('#buttonAddIcon').html("-").fadeIn('fast');
		});   
	} });
}
function sendToDatabase() {
	$("#newEvent input").each( function() {
		if ( $(this).attr("type") != "hidden" ) {
			if ( $(this).val() == $(this).attr("defaultValue") ) {
				$(this).val("");
			}              
		}
	});
	var dataString = $('#newEvent').serialize();
	$.ajax({  
		data: dataString, 
		success: function(text) {
			if ( text == "added") {
				$("#message").html("1 more event added");
				goLeft();                 
			} else {
				$("#message").html(text);
			}
 
		},
		error: function(text) {
			$("#message").html("your event was not added.  please email <a href='mailto:scott@deezine.ca'>scott</a> and tell him.");
		}  
	});  
	return false; 
}
function validate() {
	$("#newEvent").validate();
	if ($("#newEvent").valid() == true) {
		sendToDatabase();
	} else {
		if(IE){
			$("#error").html("please make sure all fields are filled!")
		}
	}
}
$("#single input:text").click( function() {
	if ( $(this).val() == $(this).attr("defaultValue") ) {
		$(this).val("");
	}
}).blur( function() {
	if ( $(this).val() == "" ) {
		$(this).val($(this).attr("defaultValue"));
	}
});

// 2012
var defaultMoreInfoText = "More Info";
$(document).ready( function() {
	setInterval("advance()",slideshowDelay);
	$("#gallery").hover( function() {
		isMouseOverSlideShow = true;    
	}, function() {
		isMouseOverSlideShow = false;
	});
	$("#main3 > .dayRow > a").click( function (e) {
		e.preventDefault();
		$.scrollTo($(this).attr("href"), "fast", {
			axis : 'y'
		});
	});   
	$('.day2012:not(.past) ol.events').each( function() {
		var id = $(this).attr("id");
		loadEvents(id);
	});
	$('.day2012.past').click( function() {
		$(this).removeClass('past').css({'marginTop':'0px','marginBottom':'0px'});
		var ol = $(this).children("ol");
		var id = ol.attr("id");
		loadEvents(id);
		$(this).animate({'marginTop':'20px','marginBottom':'20px'},"fast");
		$(this).unbind("click");
	});    
	$(".listings2012 .open").click( function() {
		if( $(this).hasClass("active") ) {
			$(this).next().slideUp("fast");
			$(this).text(defaultMoreInfoText);
			$(this).removeClass("active");
		} else {
			$(this).addClass("active");
			$(this).next().slideDown("fast");
			$(this).text("X");        
		}
	});         
});
var isMouseOverSlideShow = false;
var slideshowDelay       = 4000;
var slideTransitionSpeed = 600;  
function moreEventInfo(elem) {
	if( elem.hasClass("active") ) {
		elem.next().slideUp("fast");
		elem.text(defaultMoreInfoText);
		elem.removeClass("active");
	} else {
		elem.addClass("active");
		elem.next().slideDown("fast");
		elem.text("X");        
	}
}
function loadEvents (id) {
	var data = 'mode=loadEvent&eid=' + id;
	$("ol#"+id).hide().html("<li>Loading</li>").fadeIn(200);
	var xhr  = $.get(
		 url  = 'http://potashcorpwintershines.ca/ajax'
		,data = data
		,success = function(data) {
			$("ol#"+xhr.id).hide().html(data).fadeIn(400);
			$("ol#"+xhr.id+" .open").click( function() {
				moreEventInfo($(this));
			});
		}     
	);
	xhr.id = id;
}        
function advance() {
	try {
		var numberOfSlides = $("#gallery").children.size();
	} catch (e) {
		// not on index page
	}
	if ( false === isMouseOverSlideShow && numberOfSlides > 1 ) {
		var current = $("#gallery li:visible");
		if ( $("#gallery li:visible + li").length == 0 ) {
			var next = $("#gallery li:first");
		} else {
			var next = $("#gallery li:visible + li");
		}
		current.fadeOut(slideTransitionSpeed, function() {
			$(this).css('position','absolute');
		});
		next.fadeIn(slideTransitionSpeed, function() {
			$(this).css('position','relative');
		});
	} 
	return;   
}
