/*!
	Slimframe v0.1 - The ultimate lightweight calcbox for jQuery
	(c) 2010 CHO/CA www.atikon.com
*/

if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
	jQuery(function($) {
		$("a[rel^='lightframe']").slimboxframe({/* Put custom options here */}, $(this).attr("href"));
		$("a[rel^='lightframe']").click(function() {return false;});
	});
}

(function($) {
	// Global variables, accessible to Slimbox only
	var win = $(window), options, images, activeImage = -1, activeURL, prevImage, nextImage, compatibleOverlay, middle, centerWidth, centerHeight, ie6 = !window.XMLHttpRequest,
		operaFix = window.opera && (document.compatMode == "CSS1Compat") && ($.browser.version >= 9.3), documentElement = document.documentElement,
	// DOM elements
	overlay, center, image, sizer;
	/*
		Initialization
	*/
	$(function() {
		// Append the Lightframe HTML code at the bottom of the document
		$("body").append($([overlay = $('<div id="lfOverlay" />')[0], center = $('<div id="lfCenter" />')[0]]));
		image = $('<div id="lfImage" />').appendTo(center).append(sizer = $('<div style="position: relative;" />'))[0];
		$(center).append($([$('<div id="lfCorner_lt" />')[0], $('<div id="lfCorner_rt" />')[0], $('<div id="lfCorner_lb" />')[0], $('<div id="lfCorner_rb" />')[0], $('<div id="lfLeft" />')[0], $('<div id="lfTop" />')[0], $('<div id="lfRight" />')[0], $('<div id="lfBottom" />')[0]]));
		$(overlay).hide();	
	});
	
  /*
		API
	*/
	$.slimboxframe = function(_images, _options) {
		options = $.extend({
			loop: false,				// Allows to navigate between first and last images
			overlayOpacity: 0.75,			// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
			overlayFadeDuration: 500		// Duration of the overlay fade-in and fade-out animations (in milliseconds)
			}, _options);

		middle = win.scrollTop() + ((operaFix ? documentElement.clientHeight : win.height()) / 2);
		centerWidth = $(center).width();
		centerHeight = $(center).height();
		$("html").css({overflow: "hidden"});
    $(overlay).css("opacity", options.overlayOpacity).fadeIn(options.overlayFadeDuration);
                if(centerWidth==0) {centerWidth=750}
		$(center).css({top: Math.floor(Math.max(0, middle - (centerHeight / 2))), width: centerWidth, height: centerHeight, marginLeft: -centerWidth/2}).fadeIn();  			
    compatibleOverlay = ie6 || (overlay.currentStyle && (overlay.currentStyle.position != "fixed"));
		if (compatibleOverlay) overlay.style.position = "absolute"; 
		images = _images;
		options.loop = options.loop && (images.length > 1);	    
		return setIframe(images);
	};

	$.fn.slimboxframe = function(_options, link) {
	var linkMapper, linksFilter;
		linkMapper = linkMapper || function(el) {
			return [el.href, el.title];
		};

		linksFilter = linksFilter || function() {
			return true;
		};

		var links = this;

		return links.unbind("click").click(function() {
			// Build the list of images that will be displayed
			var link = this, startIndex = 0, filteredLinks, i = 0, length;
			filteredLinks = $.grep(links, function(el, i) {
				return linksFilter.call(link, el, i);
			});

			// We cannot use jQuery.map() because it flattens the returned array
			for (length = filteredLinks.length; i < length; ++i) {
				if (filteredLinks[i] == link) startIndex = i;
				filteredLinks[i] = linkMapper(filteredLinks[i], i);
			}
			return $.slimboxframe(link, _options);
		});
	};


/*--- Internal functions ---*/
	function setIframe(link) {
    $(sizer).hide();
    $(image).addClass("lbLoading");
		$(image).append($('<iframe id="lbframe" frameborder="0" src="'+ link +'" />'));
		$(image).append($('<div id="lfCloseFrame" />"').append('<a id="lfCloseFrameLink" href="#" />'));
		$(center).width($("#lbframe").width());
		$(center).height($(image).height());
    $("#lbframe").load(function () {$(image).removeClass("lbLoading");});
		placeIframe();
		
    if (center.offsetWidth != centerWidth) {
			$(center).css({width: centerWidth, marginLeft: -centerWidth/2});
		}
		
		$(window).resize(function() {
			placeIframe();
		});
		
		$(overlay).click(function() {
			$(center).hide();
			$("#lbframe").remove();
			$("#lfCloseFrame").remove();
			$(center).css({"max-height": "100%"});
			$("#lbframe").css({"max-height": "100%"});
			$(overlay).stop().fadeOut(options.overlayFadeDuration);
			$("html").css({overflow: "auto"});
		});
		$("#lfCloseFrame").click(function () {$(overlay).click();});
	}

	function placeIframe() {
		middle = win.scrollTop() + ((operaFix ? documentElement.clientHeight : win.height()) / 2);
		if ($(window).height() < $(center).height()+50) {
			$(center).css({"max-height": $(window).height() - $("#lfBottom").height() - $("#lfTop").height() - 20});
			$("#lbframe").css({"max-height": $(window).height()-$("#lfCloseFrameLink").height() - $("#lfBottom").height() - $("#lfTop").height() - 32});
		} else {
			$(center).css({"max-height": "100%"});
//			$("#lbframe").css({"max-height": "100%"});
			$("#lbframe").css({"height": "550px"});
		}	
		centerWidth = image.offsetWidth;
		centerHeight = center.offsetHeight;
		var top = Math.max(0, middle - (centerHeight / 2));
		$(center).css({top: top});
	}
})(jQuery);
