jQuery(document).ready(initTuiTB);

function initTuiTB() {
	jQuery('a.tuitb:not(.tuitbfied), a[rel=tuitb]:not(.tuitbfied)').each(function() {
		jQuery(this).data('href', jQuery(this).attr('href')).addClass('tuitbfied').click(function(){
			showTuiTB(jQuery(this));
			jQuery(this).blur();
			return false;
		});
		jQuery(this).attr('href', jQuery(this).attr('href').indexOf('dest') > 0 ? unescape(get('dest', jQuery(this).attr('href'))) : 'javascript:void(0);');
	});
}

function showTuiTB(linkObj) {
	if(typeof(linkObj) == 'string') {
		linkObj = jQuery('a:first').clone().data('href', linkObj);
	}
	linkObj.data('config', {});
	if(linkObj.data('href')) {
		var hrefMainParts = linkObj.data('href').split('#')[0].split('tuitb');
		linkObj.data('dest', hrefMainParts.length > 1 ? hrefMainParts[0].substring(0, hrefMainParts[0].length-1) : hrefMainParts[0]);
		var configRaw = hrefMainParts[1];
		if(configRaw) {
			var configRawSubParts = configRaw.split('&');
			for(var i=0; i<configRawSubParts.length; i++) {
				var configRawSubSubParts = configRawSubParts[i].split('=');
				linkObj.data('config')[configRawSubSubParts[0]] = configRawSubSubParts[1];
			}
		}
	}
	var defaults = {width:1055, height:'auto', top:50, bg:'fff', mode:'iframe'};
	jQuery.extend(defaults, linkObj.data('config'));
	if(linkObj.data('dest').search(/.*\.jpg|gif|png/gi) != -1) { defaults.mode = 'image'; }
	linkObj.data('config', defaults);

	linkObj.data('target', linkObj.attr('target'));
	try {
		var destWindow = linkObj.data('config').mode == 'iframe' && window.top.document ? window.top : window; 
	} catch(e) {
		var destWindow = window;
	}
	linkObj.data('window', jQuery(destWindow));
	linkObj.data('document', jQuery(destWindow.document));
	try {
		if(linkObj.data('config').mode != 'ajax' && linkObj.data('config').width > linkObj.data('window').width()) {
			linkObj.attr({href:linkObj.data('dest'), target:'_blank'}).unbind('click')
			window.open(linkObj.data('dest')).focus();
			return;
		}
	} catch(e) {}
	var container = jQuery('<div>').addClass('tuitbContainer');
	var cover = jQuery('iframe', {frameborder:0, hspace:0, scrolling:linkObj.data('config').scrolling ? 'yes' : 'no'}).addClass('tuitbCover').css({width:linkObj.data('config').width, 'height':linkObj.data('config').height});
	var overlay = jQuery('<div>').addClass('tuitbOverlay').css({backgroundColor:'#' + linkObj.data('config').bg});
	var close = jQuery('<div>').addClass('tuitbClose').append(jQuery('<a>', {title:'schlie&szlig;en', href:'javascript:void(0);'}).append($('<span>', {text:'close'})));
	var content = jQuery('<div>').addClass('tuitbContent').css({width:linkObj.data('config').width, height:linkObj.data('config').height, top:linkObj.data('config').top});
	jQuery('body', linkObj.data('document')).append(container.append(cover, overlay, close, content));
	linkObj.data('scope', container);
	
	if(parseInt(linkObj.data('config').unfix) || !isBrowser()) {
		jQuery([cover, content, close]).each(function () {
			jQuery(this).css({position:'absolute', top:jQuery('html,body', linkObj.data('document')).scrollTop() + linkObj.data('config').top});
		});
		close.css({top:close.offset().top - close.height()})
	} else {
		close.css({top:linkObj.data('config').top - close.height()})
	}
	jQuery([overlay, close]).each(function () {
		jQuery(this).click(function() {
			jQuery(this).parent('.tuitbContainer').remove();
			linkObj.data('window').unbind('resize', resizeOverlay).unbind('scroll', resizeOverlay);
		});
	});
	linkObj.data('window').bind('resize scroll', {linkObj:linkObj}, resizeOverlay);
	switch(linkObj.data('config').mode) {
		case "image":
			var tbImage = new Image(linkObj.data('config').width);
			jQuery(tbImage).attr('src', linkObj.data('dest')).load(function() {
				content.html(tbImage).height(jQuery('.tuitbContent img', linkObj.data('scope')).height());
				resizeOverlay({data:{linkObj:linkObj}});
			});
			break;
		case "ajax":
			jQuery.get(linkObj.data('dest'), function(data){
				jQuery('.tuitbContent', linkObj.data('scope')).html(data);
				resizeOverlay({data:{linkObj:linkObj}});
				if(typeof(window[linkObj.data('config').ajaxCallback]) == 'function') {
					window[linkObj.data('config').ajaxCallback](linkObj);
				}
			});
			break;
		case "iframe":
		default:
			jQuery('.tuitbContent', linkObj.data('scope')).append('<iframe frameborder="0" hspace="0" scrolling="no" src="' + linkObj.data('dest') + '" id="tuitbIframe" name="tuitbIframe" style="width:' + linkObj.data('config').width + 'px; height:' + linkObj.data('config').height + 'px;"></iframe>');
			resizeOverlay({data:{linkObj:linkObj}});
			break;
	}
}

function resizeOverlay(e) {
	if(!isBrowser()) {
		jQuery('.tuitbOverlay', e.data.linkObj.data('scope')).css({position:'absolute', height:jQuery('body', e.data.linkObj.data('document')).height()});
	}
	jQuery('.tuitbCover, .tuitbContent', e.data.linkObj.data('scope')).css({left:Math.max(jQuery('body', e.data.linkObj.data('document')).outerWidth(true)/2-jQuery('.tuitbContent', e.data.linkObj.data('scope')).width()/2, 0)});
	jQuery('.tuitbClose', e.data.linkObj.data('scope')).css({left:jQuery('.tuitbContent', e.data.linkObj.data('scope')).offset().left+jQuery('.tuitbContent', e.data.linkObj.data('scope')).outerWidth()-jQuery('.tuitbClose', e.data.linkObj.data('scope')).width()});
}