// Overwrite Lightbox, to show iframes either

// Standard width. If you want to customize it link by link, put a &w= and &h= in
// the Link-url that will be noticed
// @todo Double-Loaded iFrame -- why? I don't know, but fix it!

Lightbox.prototype.iframeWidth = 640;
Lightbox.prototype.iframeHeight = 480;

Lightbox.prototype.changeImage = function(imageNum) {
	activeImage = imageNum;	// update global var
	// hide elements during transition
	if(animate){ Element.show('loading');}
	Element.hide('lightboxImage');
	Element.hide('hoverNav');
	if($('lightboxIframeContainer')) Element.hide('lightboxIframeContainer');
	Element.hide('prevLink');
	Element.hide('nextLink');
	Element.hide('imageDataContainer');
	Element.hide('numberDisplay');
	if(imageArray[imageNum][0].match(/\.php$|\.php\?|\.html?$/)) {

		var dimensionSearch = /w=([0-9]*)&a?m?p?;?h=([0-9]*)/;
		var res = dimensionSearch.exec(imageArray[imageNum][0]);
		if(res) {
			this.iframeWidth = parseInt(res[1]);
			var actualwidth = jQuery("#outerImageContainer").width();
			this.iframeHeight = parseInt(res[2]);
			// this.iframeHeight = 240	;
		}
		this.doIFrame(imageNum);
	} else {
		//Old stuff

		imgPreloader = new Image();

		// once image is preloaded, resize image container
		imgPreloader.onload=function(){
			Element.setSrc('lightboxImage', imageArray[activeImage][0]);
			myLightbox.resizeImageContainer(imgPreloader.width, imgPreloader.height);
			imgPreloader.onload=function(){};	//	clear onLoad, IE behaves irratically with animated gifs otherwise
		}
		imgPreloader.src = imageArray[activeImage][0];
	}
}
Lightbox.prototype.doIFrame = function(imageNum) {
	//if there's no Iframe, create one
	// jQuery("#lightboxIframe iframe").remove();
	if(!$('lightboxIframe')) {
		var iframeContainer = document.createElement("div");
		iframeContainer.id = "lightboxIframeContainer";
		iframeContainer.style.display = "none";

		var iframe = document.createElement("iFrame");
		iframe.id = "lightboxIframe";
		iframeContainer.appendChild(iframe);
		iframe.style.width = this.iframeWidth+"px";
		iframe.style.height = this.iframeHeight+"px";
		$('imageContainer').appendChild(iframeContainer);
	}
	this.resizeContainerForIframe();


	//Load the Frame
//	$('lightboxIframe').src = imageArray[imageNum][0];
//	$('lightboxIframe').onload = function() {
//		if(imageArray.length == 1) {
//			new Effect.Appear('lightboxIframe', { duration: resizeDuration, queue: 'end', afterFinish: function(){	myLightbox.updateDetailsForIframe(); } });
//			$('hoverNav').style.display = "none";
//		} else {
//			new Effect.Appear('lightboxIframe', { duration: resizeDuration, queue: 'end', afterFinish: function(){	myLightbox.updateDetails(); } });
//		}
//		Element.hide('loading');
//	}

	if(imageArray.length == 1) {
		new Effect.Appear('lightboxIframeContainer', { duration: resizeDuration, queue: 'end', afterFinish: function(){	myLightbox.updateDetailsForIframe(); $('lightboxIframe').src = imageArray[imageNum][0]; } });
		$('hoverNav').style.display = "none";
	} else {
		new Effect.Appear('lightboxIframeContainer', { duration: resizeDuration, queue: 'end', afterFinish: function(){	myLightbox.updateDetails(); $('lightboxIframe').src = imageArray[imageNum][0];} });
	}
	Element.hide('loading');

}
//Had to copy the whole function becaus of one single line at the end, damm scripting!
Lightbox.prototype.resizeContainerForIframe = function() {
	// get curren width and height
	this.widthCurrent = Element.getWidth('outerImageContainer');
//	this.widthCurrent = jQuery("#outerImageContainer").width();
	this.heightCurrent = Element.getHeight('outerImageContainer');
//	this.heightCurrent = jQuery("#outerImageContainer").height();

	// get new width and height
	var widthNew = (this.iframeWidth  + (borderSize * 2));
	var heightNew = (this.iframeHeight  + (borderSize * 2));

	// scalars based on change from old to new
	this.xScale = ( widthNew / this.widthCurrent) * 100;
	this.yScale = ( heightNew / this.heightCurrent) * 100;

	// calculate size difference between new and old image, and resize if necessary
	wDiff = this.widthCurrent - widthNew;
	hDiff = this.heightCurrent - heightNew;

	if(!( hDiff == 0)){ new Effect.Scale('outerImageContainer', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); }
	if(!( wDiff == 0)){ new Effect.Scale('outerImageContainer', this.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }

	// if new and old image are same size and no scaling transition is necessary,
	// do a quick pause to prevent image flicker.
	if((hDiff == 0) && (wDiff == 0)){
		if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);}
	}

	Element.setHeight('prevLink', this.iframeHeight);
	Element.setHeight('nextLink', this.iframeHeight);
	Element.setWidth( 'imageDataContainer', widthNew);
	lightboxCloseDiv();
}
//Had to copy the whole function becaus of one single line at the end, damm scripting!
Lightbox.prototype.updateDetailsForIframe = function() {

		// if caption is not null
		if(imageArray[activeImage][1]){
			Element.show('caption');
			Element.setInnerHTML( 'caption', imageArray[activeImage][1]);
		}

		// if image is part of set display 'Image x of x'
		if(imageArray.length > 1){
			Element.show('numberDisplay');
			Element.setInnerHTML( 'numberDisplay', "Bild " + eval(activeImage + 1) + " von " + imageArray.length);
		}

		new Effect.Parallel(
			[ new Effect.SlideDown( 'imageDataContainer', { sync: true, duration: resizeDuration, from: 0.0, to: 1.0 }),
			  new Effect.Appear('imageDataContainer', { sync: true, duration: resizeDuration }) ],
			{ duration: resizeDuration, afterFinish: function() {
				// update overlay size and update nav
				var arrayPageSize = getPageSize();
				Element.setHeight('overlay', arrayPageSize[1]);
				}
			}
		);
	lightboxCloseDiv();
	}
	
function parse_url (str, component) {
    // Parse a URL and return its components  
    // 
    // version: 901.2514
    // discuss at: http://phpjs.org/functions/parse_url
    // +      original by: Steven Levithan (http://blog.stevenlevithan.com)
    // + reimplemented by: Brett Zamir
    // %          note: Based on http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
    // %          note: blog post at http://blog.stevenlevithan.com/archives/parseuri
    // %          note: demo at http://stevenlevithan.com/demo/parseuri/js/assets/parseuri.js
    // %          note: Does not replace invaild characters with '_' as in PHP, nor does it return false with
    // %          note: a seriously malformed URL.
    // %          note: Besides function name, is the same as parseUri besides the commented out portion
    // %          note: and the additional section following, as well as our allowing an extra slash after
    // %          note: the scheme/protocol (to allow file:/// as in PHP)
    // *     example 1: parse_url('http://username:password@hostname/path?arg=value#anchor');
    // *     returns 1: {scheme: 'http', host: 'hostname', user: 'username', pass: 'password', path: '/path', query: 'arg=value', fragment: 'anchor'}
    var  o   = {
        strictMode: false,
        key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
        q:   {
            name:   "queryKey",
            parser: /(?:^|&)([^&=]*)=?([^&]*)/g
        },
        parser: {
            strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
            loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-protocol to catch file:/// (should restrict this)
        }
    };
    
    var m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
    uri = {},
    i   = 14;
    while (i--) uri[o.key[i]] = m[i] || "";
    // Uncomment the following to use the original more detailed (non-PHP) script
    /*
        uri[o.q.name] = {};
        uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
        if ($1) uri[o.q.name][$1] = $2;
        });
        return uri;
    */

    switch (component) {
        case 'PHP_URL_SCHEME':
            return uri.protocol;
        case 'PHP_URL_HOST':
            return uri.host;
        case 'PHP_URL_PORT':
            return uri.port;
        case 'PHP_URL_USER':
            return uri.user;
        case 'PHP_URL_PASS':
            return uri.password;
        case 'PHP_URL_PATH':
            return uri.path;
        case 'PHP_URL_QUERY':
            return uri.query;
        case 'PHP_URL_FRAGMENT':
            return uri.anchor;
        default:
            var retArr = {};
            if (uri.protocol !== '') retArr.scheme=uri.protocol;
            if (uri.host !== '') retArr.host=uri.host;
            if (uri.port !== '') retArr.port=uri.port;
            if (uri.user !== '') retArr.user=uri.user;
            if (uri.password !== '') retArr.pass=uri.password;
            if (uri.path !== '') retArr.path=uri.path;
            if (uri.query !== '') retArr.query=uri.query;
            if (uri.anchor !== '') retArr.fragment=uri.anchor;
            return retArr;
    }
}
