var dialogbox_active = false;
var dialogbox_stack = new Array();
var dialogbox_position = 150;

jQuery(document).ready(function(){
	/*
		adjust's top position of message box when browser is being scrolled 
		better if default_position is equal to the msgbox style default top position
		anyway you can modify it accordding what your site needs
	*/
	jQuery(window).scroll(function(){
		if ( typeof dialogbox_active == 'object' ) {
			jQuery(dialogbox_active).animate({top:jQuery(window).scrollTop()+dialogbox_position+"px" },{queue: false, duration: 500});
		}
	});
									
	jQuery("#DimBackground").css("height", $(document).height());

	//adjust's height of overlay to fill screen when browser gets resized  
	jQuery(window).bind("resize", function(){
		$("#DimBackground").css("height", $(window).height());
	});	
	
	if ( document.getElementById( 'imgenlarger' )==null ) {
		jQuery('body').append('<div id="imgenlarger" class="dialog" />');
	}
	
	//When the link that triggers the message is clicked, display DimBackground and fadeIn msgbox...
	/*
	$(".show").click(function(){
		dialog_display( 'msgbox' );
	});
	*/
	
});

function img_enlarge( a_enlarger, width, height, imgparam ) {
	var img = dom_element_find( a_enlarger, 'IMG' );
	if ( !width && !height ) {
		width = img.enlargesize[0];
		height = img.enlargesize[1];
	}
	var src = img.src.split('/');
	jQuery( 'img.enlarged' ).removeClass( 'enlarged' );
	if ( !jQuery(img).hasClass( 'enlarged' ) ) jQuery(img).addClass( 'enlarged' );
	
	var param = img_filename_resize( src, width, height, imgparam );
	
	/*
	var param = new Array();
	var erf=false;
	for( i=0;i < src.length; i++ ) {
		if ( !erf && src[i]=='media.php' ) erf=true;
		else if ( erf ) param.push( src[i] );
	}
	param[1]=width+'x'+height;
	*/
	
	var imgenlarger = document.getElementById( 'imgenlarger' );
	imgenlarger.imgcurrent = img;
	imgenlarger.mediaurl = '/media.php/' + param.join('/');
	jQuery.getJSON( '/module.php/general/json/media_info_get', {'file':param[2]}, function( data ) {
		var imgenlarger = document.getElementById( 'imgenlarger' );
		var content='<h1>' + data.media_file_title + '<a class="close" href="javascript:img_enlarger_close()" ><img src="/module.php/general/img/dialog-cross.png" /></a></h1>';
		content+='<img src="'+imgenlarger.mediaurl + '" alt="' + data.media_file_title + '" /><br />';
		content+=data.media_file_description+'<br />';
		
		var images = jQuery('a img.thumb');
		
		imgenlarger.imgbefore=new Array();
		imgenlarger.imgafter=new Array();
		var crossed=false;
		for( i=0; i<images.length; i++ ) {
			if ( !jQuery(images[i]).hasClass('enlarged') ) {
				images[i].enlargesize = jQuery( images[i].parentNode ).attr( 'enlargesize' ).split('x');
				if ( crossed ) {
					if ( imgenlarger.imgafter.length < 3 ) imgenlarger.imgafter.push( images[i] );
					else break;
				} else {
					imgenlarger.imgbefore.push( images[i] );
					if ( imgenlarger.imgbefore.length > 2 ) imgenlarger.imgbefore.shift();
				}
			} else {
				crossed=true;
			}
		}
		// console.log( before, before.length );
		content+='<div>';
		for( var i=0;i < imgenlarger.imgbefore.length;i++ ) {
			param = img_filename_resize( imgenlarger.imgbefore[i].src, 50, '50', 'c' );
			content+='<a href="javascript:void(0)" onclick="img_enlarge(this.parentNode.parentNode.imgbefore['+i+'],0,0)"><img src="/media.php/' + param.join('/')+'" /></a>';
		}
		for( var i=0;i < imgenlarger.imgafter.length;i++ ) {
			param = img_filename_resize( imgenlarger.imgafter[i].src, 50, '50', 'c' );
			content+='<a href="javascript:void(0)" onclick="img_enlarge(this.parentNode.parentNode.imgafter['+i+'],0,0)"><img src="/media.php/' + param.join('/')+'" /></a>';
		}
		content+='</div>';
		content+='<div><a href="javascript:img_enlarger_close()">Schließen</a></div>';
		imgenlarger.innerHTML = content;
	} );

	
	jQuery(imgenlarger).css('width', width );
	jQuery(imgenlarger).css('height', parseInt( height ) + 150 );
	console.log( parseInt( height ) + parseInt( 150 ) );
	jQuery(imgenlarger).css('margin-left', width / -2 );
	if ( typeof( dialogbox_active ) != 'object' || dialogbox_active.id!='imgenlarger' ) {
		dialog_display( 'imgenlarger' );
	}
}

function img_filename_resize( flename, width, height, p ) {
	if ( typeof flename == 'string' ) flename = flename.split('/');
	var param = new Array();
	var erf = false;
	for( var i=0;i < flename.length; i++ ) {
		if ( !erf && flename[i]=='media.php' ) erf=true;
		else if ( erf ) param.push( flename[i] );
	}
	if ( p ) p=','+p;
	param[1]=width+'x'+height+p;
	
	return param;
}

function img_enlarger_close( ) {
	if ( dialogbox_active.id=='imgenlarger' ) {
		jQuery( dialogbox_active.imgcurrent ).removeClass( 'enlarged' );
	}
	dialog_close();
}

function dialog_display( id ) {
	jQuery('#DimBackground').css('display','block');
	if ( dialogbox_active ) dialogbox_stack.push( dialogbox_active );
	dialogbox_active = document.getElementById(id);
	jQuery(dialogbox_active).fadeIn();
	jQuery(dialogbox_active).animate({top:jQuery(window).scrollTop()+dialogbox_position+"px" },{queue: false, duration: 350});
	
	jQuery('#'+id+' a.close').click(function(){
		dialog_close( );
		return false;
	});
}

function dialog_close( ) {
	jQuery(dialogbox_active).fadeOut();
	if ( dialogbox_stack.length==0 ) {
		jQuery("#DimBackground").css({'display':'none'});
		if ( typeof dialogbox_active.displayonclose != 'undefined' ) jQuery(dialogbox_active.displayonclose).css( 'display', 'block' );
		dialogbox_active = false;
		
	} else {
		dialogbox_active = dialogbox_stack.pop();
	}
}

// Ermittelt den äußeren dialog div container.
// Ausgangspunkt ist ein beliebiges DOM -Element innerhalb des dialog containers
function dialog_box_element_determine( innerelement ) {
	do {
		innerelement = innerelement.parentNode;
	} while( innerelement.tagName != 'BODY' && !jQuery( innerelement ).hasClass( 'dialog' ) );
	return innerelement;
}

function wait( ms ) {
	var jetzt = new Date();
	var biswann = jetzt.getTime() + ms;
	while (jetzt.getTime() < biswann) {
		var jetzt = new Date();
	}
}

function dom_element_find( node, nodename, multiple )  {
	if ( typeof node.nodeName!='undefined' ) {
		if ( node.nodeName==nodename ) return node;
		nodes = new Array();
		node = node.childNodes;
	}
	for( i=0;i < node.length; i++ ) {
		if ( node[i].nodeName==nodename ) {
			if ( !multiple ) return node[i];
			nodes.push( node[i] );
		}
	}
	if ( multiple ) return nodes;
}
