/**--------------------------------------------------
Site/Kiln Base Classes -- ver .01_20100328
©2010 Remington Reed. All Rights Reserved
--------------------------------------------------**/

(function($) {
	$.skjs = {
		log : function(msg) { 
			(window.console) ? (console.log) ? console.log(msg) : console.debug(msg) : alert(msg);
		},
		
		msg : function(msg, opts) {
			var conf = {
				container: "div#sk_message",
				type: "sk_error",
				wait: 6000,
				fade: 500
			};
			if (opts) $.extend(conf, opts);		     
			
			var dateObj = new Date;
			var newMsgID = "msg_" + parseInt(dateObj.getTime());
			
			var newMsg = $("<div></div>").attr("id", newMsgID).addClass(conf.type).html(msg).appendTo(conf.container);
			if (conf.wait) {
				setTimeout('$("#' + newMsgID + '").fadeOut(' + conf.fade + ')', conf.wait);
			}
			newMsg.appendTo(conf.container);
		}
		
	};
	
	$.fn.skjsLightbox = function() {
		function setLightbox(lightboxContainer, lightboxBackground, lightboxImg){
			var winT = $(window).scrollTop();
			var winL = $(window).scrollLeft();
			var winW = $(window).width();
			var winH = $(window).height();
			var imgT = (winH / 2) - (lightboxImg.height() / 2);
			var imgL = (winW / 2) - (lightboxImg.width() / 2);

			lightboxContainer.css({ top: winT, left: winL, width: winW, height: winH });
			lightboxBackground.css({ top: 0, left: 0, width: winW, height: winH });
			lightboxImg.css({ top: imgT, left: imgL, visibility: "visible" });
		};
		
		return this.each(function(){
			$(this).click(function() {
				$("<div><div></div><img /></div>")
					.attr("id", "lightbox_container")
					.css({
						position: "absolute",
						top: $(window).scrollTop(),
						left: $(window).scrollLeft(),
						width: "100%",
						height: "100%",
						'z-index': "1010"
					})
					.click(function() {
						$(window).unbind('.lightboxEvents');
						$(this).remove();
					})
					.find("div")
						.css({
							position: "absolute",
							top: "0",
							left: "0",
							width: "100%",
							height: "100%",
							background: "#000",
							'z-index': "1000"
						})
						.fadeTo(0, .8)
					.end()
					.find("img")
						.attr({
							id: "lightbox_img",
							src: $(this).parent().attr("href")
						})
						.css({
							position: "absolute",
							border: "12px solid #efefef",
							visibility: "hidden",
							'z-index': "1000"
						})
						.fadeTo(0, 1)
						.load(function() {
							var lightboxContainer = $(this).parent();
							var lightboxBackground = $(this).prev();
							var lightboxImg = $(this);
							
							setLightbox(lightboxContainer, lightboxBackground, lightboxImg);
							
							$(window).bind('scroll.lightboxEvents resize.lightboxEvents', function() {
								setLightbox(lightboxContainer, lightboxBackground, lightboxImg);
							});
						})
					.end()
					.appendTo("body");
					return false;
			});
		});
	};
})(jQuery);
