jQuery(document).ready(initTuiTB);

function initTuiTB(context) {
	var context = typeof(context) == 'object' ? context : 'body';
	jQuery('a.tuitb:not(.tuitbfied), a[rel=tuitb]:not(.tuitbfied), area:not(.tuitbfied, .redaktion)', context).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('tuitb');
		linkObj.data('dest', hrefMainParts.length > 1 ? hrefMainParts[0].substring(0, hrefMainParts[0].length-1) : hrefMainParts[0]);
		if(hrefMainParts[1]) {
			var configRaw = hrefMainParts[1].split('#')[0];
			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'};
	if(typeof(window['getTuitbDefaults']) == 'function') {
		var brandDefaults = window['getTuitbDefaults'](linkObj);
		if(typeof(brandDefaults) == 'object') {
			jQuery.extend(defaults, window['getTuitbDefaults'](linkObj));
		}
	}
	if(linkObj.data('config').mode == 'inline') {
		var anchorParts = hrefMainParts[1].split('#');
		if(anchorParts[1]) {
			if(jQuery('#' + anchorParts[1]).length) {
				linkObj.data('dest', '#' + anchorParts[1]);
				defaults.width = defaults.height = 'auto';
			}
		}
	}
	jQuery.extend(defaults, linkObj.data('config'));
	try {
		var jsonConfig = jQuery.parseJSON(linkObj.attr('data-tuitb').replace(/\\/g, ''));
		jQuery.extend(defaults, jsonConfig);
	} catch(e) {}
	linkObj.data('config', defaults);
	linkObj.data('dest', linkObj.data('config').decode ? decodeURIComponent(linkObj.data('dest')) : linkObj.data('dest'));
	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) {}
	if(linkObj.data('dest').search(/.*\.jpg|gif|png/gi) != -1) {
		linkObj.data('config').mode = 'image';
	}
	if(linkObj.data('dest').search(/.*\.flv/gi) != -1 && linkObj.data('dest').search(/flowplayer/) == -1) {
		linkObj.data('config').mode = 'flashvideo';
	}
	var container = jQuery('<div>').addClass('tuitbContainer');
	var cover = jQuery('<iframe>', {frameborder:0, hspace:0, scrolling:'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').addClass(linkObj.data('config').closeclass).append(jQuery('<a>', {href:'javascript:void(0);'}).append(jQuery('<span>', {html:linkObj.data('config').close ? decodeURIComponent(linkObj.data('config').close) : 'schlie&szlig;en'})));
	var content = jQuery('<div>').addClass('tuitbContent').css({
		width:linkObj.data('config').width == 'auto' ? 'auto' : parseInt(linkObj.data('config').width),
		height:linkObj.data('config').height == 'auto' ? 'auto' : parseInt(linkObj.data('config').height),
		top:linkObj.data('config').top
	});
	close.find('a').attr('title', close.text());
	jQuery('body', linkObj.data('document')).append(container.append(cover, overlay, close, content));
	if(linkObj.data('config').width != 'auto' && parseInt(linkObj.data('config').width) != content.width()) {
		content.css({width:(parseInt(linkObj.data('config').width)+content.outerWidth()-content.width())})
	}
	if(linkObj.data('config').height != 'auto' && parseInt(linkObj.data('config').height) != content.height()) {
		content.css({height:(parseInt(linkObj.data('config').height)+content.outerHeight()-content.height())})
	}
	linkObj.data('scope', container);
	if(parseInt(linkObj.data('config').unfix) || !isBrowser()) {
		jQuery([cover, content, close]).each(function () {
			jQuery(this).css({position:'absolute', top:jQuery(linkObj.data('document')).scrollTop() + linkObj.data('config').top});
		});
		close.css({top:close.offset().top - close.outerHeight() + parseInt(linkObj.data('config').closetop ? linkObj.data('config').closetop : 0)});
	} else {
		close.css({top:linkObj.data('config').top - close.outerHeight() + parseInt(linkObj.data('config').closetop ? linkObj.data('config').closetop : 0)});
	}
	jQuery([overlay, close]).each(function () {
		jQuery(this).click(function() {
			if(typeof(window[linkObj.data('config').closeCallback]) == 'function') {
				window[linkObj.data('config').closeCallback](linkObj);
			}
			if(linkObj.data('config').mode == 'inline') {
				jQuery('body', linkObj.data('document')).append(jQuery(linkObj.data('dest')).hide());
			}
			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 'inline':
			jQuery('.tuitbContent', linkObj.data('scope')).html(jQuery(linkObj.data('dest')).show());
			if(typeof(window[linkObj.data('config').inlineCallback]) == 'function') {
				window[linkObj.data('config').inlineCallback](linkObj);
			}
			resizeOverlay({data:{linkObj:linkObj}});
			break;
		case 'flashvideo':
			var flashParts = linkObj.data('dest').split('?');
			var flashSWF = flashParts[0].replace(/\.swf$/, '');
			var flashFLV = get('movie', linkObj.data('dest'));
			AC_FL_RunContent(
				'containerclass', jQuery('.tuitbContent', linkObj.data('scope')),
				'width', linkObj.data('config').width,
				'height', linkObj.data('config').height,
				'src', flashSWF,
				'movie', flashSWF,
				'flashvars', 'file=' + flashFLV + '&autostart=true&icons=false'
			);
			resizeOverlay({data:{linkObj:linkObj}});
			break;
		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="' + (linkObj.data('config').scrolling == 'true' ? 'auto' : '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)
	});
	if(e.data.linkObj.data('config').closefullwidth) {
		var closeStyle = {
			left:jQuery('.tuitbContent', e.data.linkObj.data('scope')).offset().left,
			width:jQuery('.tuitbContent', e.data.linkObj.data('scope'))[isWorst() ? 'outerWidth' : 'width']()
		};
	} else {
		var closeStyle = {
			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')).outerWidth()
		};
	}
	jQuery('.tuitbClose', e.data.linkObj.data('scope')).css(closeStyle);
}
