$(function() {
	try {
		if (getFlashVersion()) {
			_filterMedia();
		} else {
			_noFlashMedia()
		}
		initLightBox();
		initIframes();
		initGWS();
	} catch (e) {
	}
});

function _noFlashMedia() {
	var ie6=jQuery.browser.msie && jQuery.browser.version < 7;
	$('a.media').each(
			function() {
				var _width = $(this).attr('_width') || 618;
				var _size = $(this).attr('size');
				if (_size == "smallSize") {
					_height = 120;
				}
				if (_size == "middleSize") {
					_height = 244;
				}
				if(ie6)_width+=8;
				var _image = $(this).attr('imgSrc');
				var _alt = $(this).attr('_alt') || "";
				$(this).replaceWith(
						'<img src="' + _image + '" alt="' + _alt + '" width="'
								+ _width + '" height="' + _height + '" />');
			});
}


function _filterMedia() {
	var ie6=jQuery.browser.msie && jQuery.browser.version < 7;
	try {
		$('a.media')
				.each(
						function() {
							var _file = $(this).attr('href');
							var _stretching = "exactfit";
							var _controlbar = ($(this).attr('controlbar') == 'true' || $(
									this).attr('controlbar') == 'bottom') ? "bottom"
									: "none";
							var _autostart = $(this).attr('autostart') == 'true';
							var _screencolor = $(this).attr('_bgcolor') || '#fff';
							var _width = $(this).attr('_width') || 618;
							var _height = $(this).attr('_height') || 368;
							var _size = $(this).attr('size');
							var _image = $(this).attr('imgSrc');

							if (_size == "smallSize") {
								_height = 120;
							}
							if (_size == "middleSize") {
								_height = 244;
							}
							if(ie6)_width+=8;
							var flvVars = {
								file : _file,
								image : _image,
								stretching : _stretching,
								controlbar : _controlbar,
								autostart : _autostart,
								screencolor : _screencolor
							};
							var flvVarsStr = 'file=' + _file + '&image='
									+ _image + '&stretching=' + _stretching
									+ '&controlbar=' + _controlbar
									+ '&autostart=' + _autostart
									+ '&screencolor=' + _screencolor;
							$(this).media( {
								width : _width,
								height : _height,
								flashvars : flvVars,
								attrs : {
									allowscriptaccess : "always",
									allowfullscreen : "true"
								},
								params : {
									allowscriptaccess : "always",
									allowfullscreen : "true",
									flashvars : flvVarsStr
								}
							});
						});
	} catch (e) {
	}
}


function _media(opts) {
	var player = '/Javascript/jquery/1.7.2/plugins/mediaplayer/player.swf';
	var a = [ '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"   name="player" type="application/x-shockwave-flash" width="'
			+ opts.width + '" height="' + opts.height + '"' ];
	if (opts.src)
		a.push(' data="' + opts.src + '" ');
	a.push('>');
	a.push('<param name="movie" value="' + player + '" />');
	a
			.push('<param name="allowfullscreen"  value="' + opts.params['allowfullscreen'] + '" />');
	a
			.push('<param name="allowscriptaccess"  value="' + opts.params['allowscriptaccess'] + '" />');
	a
			.push('<param name="flashvars"  value="' + opts.params['flashvars'] + '" />');
	a
			.push('<embed type="application/x-shockwave-flash" name="player2" id="player2" src="'
					+ player
					+ '" width="'
					+ opts.width
					+ '" height="'
					+ opts.height
					+ '" flashvars="'
					+ opts.params['flashvars']
					+ '" allowfullscreen="true" allowscriptaccess="always" />');
	a.push('</object' + '>');
	return a.join('')
};

function closeIframe() {
	jQuery("#fancy_close").trigger('click');
}

function initLightBox() {
	jQuery('img.lightbox').each(function() {
		try{
		var imm = $(this);
		imm.wrap('<div><a href="' + imm.attr('src') + '" width= ></a></div>');
		imm.parent().lightBox();
		}catch(e){}
	});
}

function initIframes(pdiv) {
	try {
		var p = pdiv || "";
		$(p + " .iframe").each(function() {
			if ($(this).attr('tempHref')) {
				return;
			}
			$(this).click(function(event) {
				event.preventDefault();
				try {
					$(this).trigger('oeffneLayer');
					$("#fancy_frame").show();
				} catch (e) {
				}
				return false;
			});
			var parms={};
			if($(this).attr('_width')) parms.fixWidth=parseInt($(this).attr('_width'));
			if($(this).attr('_height')) parms.fixHeight=parseInt($(this).attr('_height'));
			parms.noScroll=(parms.fixWidth && parms.fixHeight);
			
			$(this).iframebox(parms);

			// Speichere den Wert von href in tempHref, danach entferne href,
				// damit
				// der User den Link nicht in einem neuem Browser-Tab öffnen
				// kann
			var _url=$(this).attr("href");
			var pos=_url.indexOf('highlight=');
			if(pos>0)_url=_url.substring(0,pos);
			if(_url.indexOf("/")==0) _url=("http://"+window.location.hostname+(window.location.port==80?"":(':'+window.location.port))+_url);
				$(this).attr("tempHref", _url);
				
				$(this).attr("href", "javascript:void(0)");
				// Link jetzt anzeigen, da er nur bei deaktiviertem JS nicht
				// sichtbar sein soll
				// $(this).show();
			});
	} catch (e) {
	}
}

function getFlashVersion() {
	try {
		try {
			// avoid fp6 minor version lookup issues
			// see:
			// http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			try {
				axo.AllowScriptAccess = 'always';
			} catch (e) {
				return '6,0,0';
			}
		} catch (e) {
		}
		return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable(
				'$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
		// other browsers
	} catch (e) {
		try {
			if (navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) {
				return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description
						.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
			}
		} catch (e) {
		}
	}
	return null;
}


function initGWS(){
	try{
	$('.gwsInput').each(function(i){
		    var _input=$(this);
		    var _btn=_input.next();
			var _box=_input.wrapAll('<form style="padding:0; margin:0;" name="gwsSearchForm"  id="gwsSearchForm"  onsubmit="doGWSSearch(); return false;" ></form>').parent();
			_box.append(_box.next());
			_box.append('<div id="gwsSearchBox" ></div>');
			$('#gwsSearchBox').load('/servlet/is/search..SearchGWSAjax/');
	});
	}catch(e){}
}

function initGWS(){
	try{
	if($('#gwsBox').length==0 && $('.gwsInput').length>0){	
		    var _box =$('.gwsElement').wrapAll('<form style="padding:0; margin:0;" name="gwsSearchForm"  id="gwsSearchForm"  onsubmit="doGWSSearch(); return false;" ></form>').parent();
		    _box.append('<div id="gwsSearchBox" ></div>');
		    _box.wrap('<div id="gwsBox"></div>');
		    var _input=$('.gwsInput');
		    if(_input.attr('_border')=="true")$('#gwsBox').addClass('showBorder');
			$('#gwsSearchBox').load('/servlet/is/search..SearchGWSAjax/');
	}
	}catch(e){}
}

