// extend AntiSPAM of TYPO3 Core
function obscureAddMid(a,b) {
	zahl = a*b;
	document.write('&#'+zahl+';');
}
function obscureAddEnd(a,b) {
	zahl = a+b;
	document.write('&#'+zahl+';');
}

/*
jQuery.fn.fadeIn = function(speed, callback) {
	return this.animate({opacity: 'show'}, "750", function() {
		if (jQuery.browser.msie) 
			this.style.removeAttribute('filter'); 
		if (jQuery.isFunction(callback))
		callback(); 
	});
};

jQuery.fn.fadeOut = function(speed, callback) {
	return this.animate({opacity: 'hide'}, "750", function() {
		if (jQuery.browser.msie) 
		this.style.removeAttribute('filter'); 
		if (jQuery.isFunction(callback))
		callback(); 
	});
};

jQuery.fn.fadeTo = function(speed,to,callback) {
	return this.animate({opacity: to}, "750", function() {
		if (to == 1 && jQuery.browser.msie) 
		this.style.removeAttribute('filter'); 
		if (jQuery.isFunction(callback))
		callback(); 
	});
};

*/

jQuery(function($) {

	// open external links in new window //
	$('.link-external').click(function(){     this.target = "_blank";});

	// mailform upload control size = 26
	$('.csc-mailform-field .fileupload').attr("size", "27");

	// fix breadcrumps for ie
	if ($.browser.msie) {
		$('#breadcrumb a').each(function(i){
			var oldtext = $(this).text();
			var newtext = oldtext.replace(" ", "&nbsp;");
			$(this).html(newtext);
		});
	}

	// drop-down navigation for the evil browser
	if ($.browser.msie && (parseInt($.browser.version) == 6)) {
		$('#nav-main li').hover(
			function(){$(this).addClass('hover'); },
			function(){$(this).removeClass('hover'); }
		);
	}

	// keyboard navigation
	$('#nav-main > li > a').focus(function(){
		$(this).next().addClass('focus');
	}).blur(function(){
		$(this).next().removeClass('focus');
	});

	$('#nav-main ul li a').focus(function(){
		$(this).parent().parent().addClass('focus');
	}).blur(function(){
		$(this).parent().parent().removeClass('focus');
	});

	// rounded corners
	$('#m2, .csc-frame-frame1').append('<span></span>');

	// quicklinks hide
	$('div.send').hide();

	// disable job offer application form jobtitle field
	$('#mailformjobtitle').attr("disabled", true);

	// show and hide product solution finder
	$('#psf').hide();
	$('#psfinfo').append('<span></span>');

	if ($.browser.msie) {
		$('#container').prepend('<span class="whitefog"></span>');
		$('#psfswitch').click(bgBlurIE);
		$('#psfclose').click(bgFocusIE);
	} else {
		$('#psfswitch').click(bgBlur);
		$('#psfclose').click(bgFocus);
	}

	// add video overlay behaviour to regular links
	$('.filelinks .flv a').click(function(){
		var filename = this.href.substring(this.href.lastIndexOf("/")+1);
		tx_mmdownloadcenter_pi2_showfilm($(this).attr('href'), filename);
		return false;
	});




// start: add video player on pages with video links
	var videolinks = $('.filelinks .flv a');
	if (videolinks.length > 0) {
		$('#container-2').prepend('<div class="tx-mmdownloadcenter-pi2"><div class="tx_mmdownloadcenter_container"><div id="dlc_playerbox"><div class="tx-mmflvplayer-pi1"><div id="tx_mmflvplayer_pi1"><a href="http://www.adobe.com/go/getflashplayer"><img src="fileadmin/templates/img/noflash_homepage.jpg" alt="Get Flash Player" /></a></div></div></div><div class="dlc_playlistbox"><h3>Film List</h3><ul class="mm_dlc_playlist_cat"></ul><p><a href="javascript:hideplayer()">close</a></p></div></div></div>');
    var flashvars = { flvfile: "", flvname: "", flvtitle: "" };
    var params = {
				loop: "false",
        menu: "false",
        quality: "best",
        swliveconnect: "false",
        bgcolor: "#002745",
        wmode: "transparent"
    };
    swfobject.embedSWF(
      "typo3conf/ext/mm_flvplayer/pi1/mmflvplayer.swf",
      "tx_mmflvplayer_pi1",
      "492", "414", "8.0.0",
      "/fileadmin/templates/flash/expressInstall.swf",
      flashvars, params, false );

		$(videolinks).each(function(i){
			var filename = this.href.substring(this.href.lastIndexOf("/")+1);
			var aTag = "<a onclick=\"tx_mmdownloadcenter_pi2_showfilm('"+$(this).attr('href')+"', '"+filename+"'); return false;\" href=\"#\">"+$(this).text()+"</a>";
			$('.dlc_playlistbox ul.mm_dlc_playlist_cat').append('<li class="file">'+aTag+'</li>');
		});
	}
// end: add video



// start: News flash
	var newsItems = $('#col2 .news-list-container .news-list-item');
	var newsContainer = $('#col2 .news-list-container');
	var heightNewsBox = 0;
	var currentItemIndex = -1;

	// if there is only one news item we need no animation
	if(newsItems.length > 1) {
		// first get the height of the biggest news item ...
		newsItems.each(function(i) {
			var itemHeight = $(this).height();
			$(this).hide();
			heightNewsBox = itemHeight > heightNewsBox? itemHeight : heightNewsBox;
		});
		// ... and assign it to the container element
		newsContainer.css({ height: heightNewsBox+'px', position:'relative', margin: '0 0 .3em 0' });

		// change the positioning of the news items so that they are stacked upon each other
		newsItems.css({ position:'absolute', top:'0px'});

		// to emulate a recursive function we append a helper SPAN element ...
		newsContainer.append('<span class="next-teaser" style="display:none; position:absolute; left:-2000px;"></span>');
		// .. and assign a click handler to it which will do the animation by repetitive "virtual clicks" on our SPAN element
		newsContainer.children('span').click( function() {
			currentItemIndex++;
			currentItemIndex = currentItemIndex >= newsItems.length? 0 : currentItemIndex;
			newsItems.eq(currentItemIndex).fadeIn('slow', function() {
				$(this).animate({top: '0'}, 4000, function(){
					$(this).fadeOut('slow', function() {
						newsContainer.children('span.next-teaser').click();
					});
				});
			});
		});

		// start the animation
		newsContainer.children('span.next-teaser').click();
	}
// end: News flash

});




/* ----------------------- Functions */


function bgBlur() {
	$('#psf').fadeIn('slow');
	$('#container-2').fadeTo('fast', 0.15);
	$('#m2').fadeTo('fast', 0, function(){ $(this).find('ul').hide(); });
 	$('#container-1 .col-1-blue').hide();
	$('#container-1 .psf_header').hide();
 	$('#container-1 #col2').addClass("psfopen");
	$('#col2').fadeTo('fast', 0.15);
	return false;
}

function bgFocus(){
	$('#psf').fadeOut('slow');
	$('#m2').find('ul').show();
	$('#container-2, #m2').fadeTo('fast', 1);
	$('#container-1 .col-1-blue').show();
	$('#container-1 .psf_header').show();
	$('#container-1 #col2').removeClass("psfopen");
	$('#col2').fadeTo('fast', 1);
	return false;
}

function bgBlurIE() {
	$('#psf').slideDown('fast');
	$('#m2 > ul').slideUp('fast', function(){$('#m2 > span').hide();});
	$('#container-1 .col-1-blue').hide();
	$('#container-1 .psf_header').hide();
	$('.whitefog').addClass("fogvisible");
	$('#container-1 #col2').addClass("psfopen");
	return false;
}

function bgFocusIE() {
	$('#psf').slideUp('fast');
	$('#m2 > ul').slideDown('fast', function(){ $('#m2 > span').show();});
	$('#container-1 .col-1-blue').show();
	$('#container-1 .psf_header').show();
	$('.whitefog').removeClass("fogvisible");
	$('#container-1 #col2').removeClass("psfopen");
	return false;
}



function psfclose2() {
		$('#psf').fadeOut('slow');
		$('#m2').find('ul').show();
		$('#container-2, #m2').fadeTo('fast', 1);
		$('#container-1 .col-1-blue').show();
		$('#container-1 .psf_header').show();
		$('#container-1 #col2').removeClass("psfopen");
		$('#col2').fadeTo('fast', 1);
		return false;
}


function showplayer () {
	if ($('#content > .tx-mmdownloadcenter-pi2')[0] == null) {
		$('.tx-mmdownloadcenter-pi2').prependTo('#content').show();
		$('#container').prepend('<span class="whitefog fogvisible"></span>');
		$('#nav-main .l2').hide();
	}
}

function hideplayer () {
	$('#content > .tx-mmdownloadcenter-pi2').prependTo('#container-2').hide();
	$('#container .whitefog').remove();
	$('#nav-main .l2').show();
}

function movie_refresh() {
	$('#tx_mmflvplayer_pi1').show();
}

function tx_mmdownloadcenter_pi2_showfilm(file, filename, filetitle){
	if(file && filename){
	  	$('#tx_mmflvplayer_pi1').empty();
		showplayer();
		var attributes = false;
		var flashvars = {
		flvfile: "http://www.oystar.iwk.de/" + file,
		flvname: filename,
		flvtitle: filetitle
		};
		var params = {
		loop: "false",
		menu: "false",
		quality: "best",
		swliveconnect: "false",
		bgcolor: "#002745",
		wmode: "transparent"
		};
		swfobject.embedSWF(
		"typo3conf/ext/mm_flvplayer/pi1/mmflvplayer.swf",
		"tx_mmflvplayer_pi1",
		"492", "414", "8.0.0",
		"/fileadmin/templates/flash/expressInstall.swf",
		flashvars, params, attributes
		);
		movie_refresh();
	}
}

function toggle_playlist(id, save) {
   ul = id;
   ul_element = document.getElementById(ul);
   if(ul_element) {
       if(ul_element.className == 'closed'){ ul_element.className = "opened"; }
       else{ ul_element.className = "closed"; }
   }
   if(save == true){ save_playlist(); }
}

function save_playlist() {
   var save = "";
   for(var i = 0; i < document.getElementsByTagName('ul').length; i++){
      if((document.getElementsByTagName('ul')[i].className == "opened" ||
      document.getElementsByTagName('ul')[i].className == "closed") &&
      document.getElementsByTagName('ul')[i].id != 'mm_dlc_playlist_cat')
         save = save + document.getElementsByTagName('ul')[i].id
         + "=" + document.getElementsByTagName('ul')[i].className
         + ",";
   }
   if(save.lastIndexOf(",") > 0){ save = save.substring(0, save.lastIndexOf(",")); }
   window.name = save;
}

function load_playlist() {
   var items = window.name.split(",");
   if(items.length > 0) {
      for(var i = 0; i < items.length; i++) {
         id_value = items[i].split("=");
         if(id_value.length == 2) {
            id = id_value[0];
            value = id_value[1];
            document.getElementById(id).className = value;
         }
      }
   }
}

var activeMovie = "";
function setactiveMovie(id) {
    if(activeMovie!=""){
    	document.getElementById(activeMovie).className = "pl_no";
    }
    document.getElementById(id).className = "pl_act";
    activeMovie = id;
}

var activeCategory = "";
function setactiveCategory(id) {
    if(activeCategory!="" && activeCategory==id){
    	document.getElementById(activeCategory).className = "closed";
    	activeCategory = "";
    }
    else if(activeCategory!="" && activeCategory!=id){
    	document.getElementById(activeCategory).className = "closed";
    	document.getElementById(id).className = "opened";
    	activeCategory = id;
    }
    else{
    	document.getElementById(id).className = "opened";
    	activeCategory = id;
    }
}
