$(function(){

	$('#hearse_pagination a.item').each(function(){
		$(this).click(function(){
			$('#hearse_image_container').css( 'background-image', 'url(' + $(this).attr('_src') + ')' );
			$(this).addClass('on').siblings().removeClass('on');
			return false;
		});
	});
	
	$('#hearse_pagination_previous').click(function(){
		var el = $('#hearse_pagination a.on').prev('.item')
		if (el.length == 0) {
			// loop to last
			var el = $('#hearse_pagination a.item:last');
		}
		el.click();
		return false;
	});

	$('#hearse_pagination_next').click(function(){
		var el = $('#hearse_pagination a.on').next('.item')
		if (el.length == 0) {
			// loop to 1st
			var el = $('#hearse_pagination a.item:first');
		}
		el.click();
		return false;
	});

	/// sticky footer

	$("#header, #content").wrapAll("<div id='wrapper' />");
	$("#wrapper").append("<div id='push' />");

	/// end sticky footer

	$("#home div.latest div.featured a.previous").bind("click", function(){

		var target = $("#home div.latest div.featured div.panels");

		var i = parseInt(target.find("div.panel:visible").attr("class").replace("panel o", ""));

		if (i == 1) i = 3;
		else i = i - 1;

		target.find("div.panel").hide();
		target.find("div.o" + i).fadeIn();

		return false;

	});

	$("#home div.latest div.featured div.panels div.o1 div.thumbnail a").bind("click", function(){

		var target = $(this).parents("div.panel").find("div.modal");

		modal.open(target,{

			"height" : 385,
			"width" : 640
	
		});

		return false;

	});

	$("#home div.latest div.featured div.panels div.o2 div.thumbnail a").bind("click", function(){

		var target = $(this).parents("div.panel").find("div.modal");

		modal.open(target,{

			"height" : 854,
			"width" : 640
	
		});

		return false;

	});

	$("#home div.latest div.featured a.next").bind("click", function(){

		var target = $("#home div.latest div.featured div.panels");

		var i = parseInt(target.find("div.panel:visible").attr("class").replace("panel o", ""));

		if (i == 3) i = 1;
		else i = i + 1;

		target.find("div.panel").hide();
		target.find("div.o" + i).fadeIn();

		return false;

	});

	modal.initialize();

});

///

modal = {}

modal.close = function(){

	$("div.modal").hide();

}

modal.initialize = function(){

	$("div.modal").css({

		"height" : $(window).height(),
		"width" : $(window).width()

	});

	$("div.modal div.window").append("<a href='#' class='close'>Close</a>");

	$("div.modal div.window a.close").bind("click", function(event){

		modal.close();

		return false;

	});

}

modal.open = function(instance, parameters){

	var html;

	$.extend(modal.parameters, parameters);

	instance.find("div.window").css({

		"height" : modal.parameters.height,
		"margin" : ($(window).height() - (modal.parameters.height + 30)) / 2 + "px auto",
		"width" : modal.parameters.width

	});

	instance.show();

}

modal.parameters = {
	
	"height" : 0,
	"width" : 0
}

///
