/**
 * @author Holger Sz�sz
 */

var isFlash = false;
var isFullFlash = false;

var FlashResizer = {
	minHeight: 300,
	minWidth: 600,
	maxHeight: null,
	swfMovieRatio: 0.59,
	flashFile: '',
	altImage: '',
	altLink: '',
	altImageWidth: 0,
	altImageHeight: 0,
	altHTML: '',
	swfparams: '',
	wmodetransparent: 1,
	imgBasepath: '',
	imgFile: '',
	scaleFlash: 1,
	align: 'LT',
	headerType: 'headonly',


	init: function() {
	var so;
		if (this.flashFile != '') {
			isFlash = true;

			so = new SWFObject(this.flashFile, 'teaser', '100%', '100%', '8', '#FFFFFF');

			so.addParam('quality', 'high');
			so.addParam('align', this.align);
			so.addParam('allowScriptAccess', 'true');

			if(this.headerType != 'headonly') {
				isFullFlash = true;
				so.addVariable('arrowOffset', this.calcArrowPosition());
			}

			for (var i in this.swfparams) {
				so.addVariable(this.swfparams[i]['name'], this.swfparams[i]['value']);
			}

			if(this.wmodetransparent == 1 || this.flashFile == '') {
				so.addParam('wmode', 'transparent');
			}

//			$('#teaserflash').css({background: 'url(' + this.altImage + ')  center top no-repeat'});

			so.addVariable('basepath', this.imgBasepath);
			so.addVariable('imgfile', this.imgFile);

			if(so.write('teaserflash')) {
				// ok, browser has flash
				$('#content>h2:first').css('display','none');
				$('h1.graphHeader1').remove();

				if(this.wmodetransparent == 0 && this.scaleFlash == 1) {
					$('#main').css('overflow', 'hidden');
				}

				this.resize();
			} else {
				// nonflash variante.
				this.wmodetransparent = 1;

				if(this.headerType == 'headonly') {
					$('#flashHeader').css('display', 'none');
					$('#main_back_top').css('display','');
					$('#main').css('padding-top', '30px');
					$('#maincontent').css('padding-top', '0px');
				} else {
					var lang 	= '';
					var uid		= '';

					for(var i = 0; i < this.swfparams.length; i++) {
						if(this.swfparams[i]['name'] == 'lang') {
							lang = this.swfparams[i]['value'];
						}
						if(this.swfparams[i]['name'] == 'uid') {
							uid = this.swfparams[i]['value'];
						}
					}

					$.post("/fileadmin/uhlsport/php/t3plugin_nonFlash.php", "action=getNonFlashPage&lang="+lang+"&uid="+uid, function(inhalt){
						$('#main').html(inhalt);

						$('#main').css('overflow','hidden');

						$('#noflashShort').html($('#noFlashTextShort').html());
						$('#noflashLong').html($('#noFlashTextLong').html());
						$('#noflashLink').html($('#noFlashLinkText').html());

						if ($.browser.name == 'msie' && $.browser.versionNumber == '6') {
							$('#text-teaser').css({
								'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="/fileadmin/templates/img/shared/teaser_back_shadow.png"',
								'background': 'none'
							});
							$('.footerContainer').css({
								'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="/fileadmin/templates/img/shared/nonFlash_back.png"',
								'background': 'none'
							});
							$('.top').css({
								'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="/fileadmin/templates/img/shared/breadCrump_zu.png"',
								'background': 'none'
							});
						} else {
							$('#text-teaser').css('background', 'url(fileadmin/templates/img/shared/teaser_back_shadow.png)');
						}

						modifikation = 0;
						$('#text-teaser>.text-teaser_item').css('padding-top', '23px');
						$('div#text-teaser').height(65-modifikation);

						var range = 90;

						if($('#text-teaser').length <= 0) {
							range -= 40;
						}
						if($('#breadnav').length <= 0) {
							range -= 30;
							$('.top').css('top','-55px');
						} else {
							$('.top').hide();
							$('#main').css('padding-top','25px');
						}

						if($('#main').css('margin-top') != '') {
							range = eval(range + "+" + $('#main').css('margin-top').replace(/px/, ''));
						}

						$('#main').height($(window).height()-range);
					});
				}
			}
		}
	},

	calcMaxHeight: function() {
		var refWidth 		= 1280;
		var refHeight		= 500;
		var currentWidth	= $('#teaserflash').width();
		var currentHeight	= $(window).attr('outerHeight');
		var maxHeight		= 0;

		if(typeof(altRefHeight)!='undefined') {
            refHeight = altRefHeight;
		}

		maxHeight = ((currentWidth * refHeight) / refWidth) / (currentWidth / currentHeight);

		//if (window.console && window.console.log)
			//console.log('[calcMaxHeight] (('+currentWidth+' * '+refHeight+') / '+refWidth+') / ('+currentWidth+' / '+currentHeight+') = '+maxHeight);

		if(maxHeight > 0) {
			this.maxHeight = maxHeight;
		}

	},

	calcArrowPosition: function() {
		var lngActiveOffset = $('div#nav_main>div.active').offset();
		var lngActiveWidth	= $('div#nav_main>div.active').width();
		var lngArrowOffset	= (lngActiveOffset.left + (lngActiveWidth / 2));

		return lngArrowOffset;
	},

	resize: function() {
        if (!this.flashFile) return;

		if(this.headerType == 'headonly' && this.scaleFlash == 1) {
			this.calcMaxHeight();
		}

		if ($('#main').height() > this.maxHeight) {
			$('#teaserflash').height(this.maxHeight);
		} else {
			$('#teaserflash').height($('#main').height());
		}

		startingHight = $('#teaserflash').height();
	}

};

$(document).ready(function() { FlashResizer.init(); } );
$(window).resize(function()  { FlashResizer.resize(); } );

function setContentMeasures() {

	if(breadIsMoving === true) {
		if (($('#main').css('padding-top').replace(/px/, '')) > 30) {
//			if (window.console && window.console.log)
//				window.console.log('[now moving] teaser: '+$('#teaserflash').height()+' | '+(($('#main').css('padding-top').replace(/px/, '')) - 30)+' | '+($(window).height()-(eval(103+'+'+($('#main').css('padding-top').replace(/px/, ''))))));

			callExternalInterface((($('#main').css('padding-top').replace(/px/, ''))));
			$('#main').height($(window).height()-(eval(103+'+'+($('#main').css('padding-top').replace(/px/, '')))));
		}

		return;
	}

	var higher = (startingHight-$('#text-teaser').height())+(65-modifikation);

	if(higher > startingHight) {
		higher = startingHight;
	}

	if($.browser.name == 'msie' && $.browser.versionNumber == '6') {
		$('#teaserflash>embed').attr('height',higher);
	}
	if (direction == 'down') {
		$('#main').height(higher);
		$('#teaserflash').height($('#main').height());
	} else {
		$('#teaserflash').height(higher);
		$('#main').height($('#teaserflash').height());
	}
}

function callExternalInterface(height) {
	thisMovie("teaser").moveFlash(height);
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
        return document[movieName];
    }
}
