/*
 * JSizes - JQuery plugin v0.33
 *
 * Licensed under the revised BSD License.
 * Copyright 2008-2010 Bram Stein
 * All rights reserved.
 */
(function(b){var a=function(c){return parseInt(c,10)||0};b.each(["min","max"],function(d,c){b.fn[c+"Size"]=function(g){var f,e;if(g){if(g.width!==undefined){this.css(c+"-width",g.width)}if(g.height!==undefined){this.css(c+"-height",g.height)}return this}else{f=this.css(c+"-width");e=this.css(c+"-height");return{width:(c==="max"&&(f===undefined||f==="none"||a(f)===-1)&&Number.MAX_VALUE)||a(f),height:(c==="max"&&(e===undefined||e==="none"||a(e)===-1)&&Number.MAX_VALUE)||a(e)}}}});b.fn.isVisible=function(){return this.is(":visible")};b.each(["border","margin","padding"],function(d,c){b.fn[c]=function(e){if(e){if(e.top!==undefined){this.css(c+"-top"+(c==="border"?"-width":""),e.top)}if(e.bottom!==undefined){this.css(c+"-bottom"+(c==="border"?"-width":""),e.bottom)}if(e.left!==undefined){this.css(c+"-left"+(c==="border"?"-width":""),e.left)}if(e.right!==undefined){this.css(c+"-right"+(c==="border"?"-width":""),e.right)}return this}else{return{top:a(this.css(c+"-top"+(c==="border"?"-width":""))),bottom:a(this.css(c+"-bottom"+(c==="border"?"-width":""))),left:a(this.css(c+"-left"+(c==="border"?"-width":""))),right:a(this.css(c+"-right"+(c==="border"?"-width":"")))}}}})})(jQuery);


/**
 * Erstelle Buttons
 *
 * @param selector
 * @param suffix
 */
var createButtons = function(selector, suffix)
{
	if(typeof(suffix) == 'undefined') suffix = '';

	jQuery(selector).each(function(i, el) {
		// Settings
		var inactiveClasses = jQuery(['button' + suffix + '_grey']);

		var $el = jQuery(el);
		var tag = el.tagName.toLowerCase();
		var classes = $el.attr('class');

		var button = jQuery('<div class="' + classes + '"></div>')
			.append('<div class="button' + suffix + '_border_l">&nbsp;</div>')
			.append($el.clone(true))
			.append('<div class="button' + suffix + '_border_r">&nbsp;</div><div class="cleans">&nbsp;</div>')
			.removeClass('buttons' + suffix)
			.addClass('buttons' + suffix + '_wrap');

		var click = function(e) {
			e.preventDefault();
		};

		if(typeof($el.attr('ref')) != 'undefined') {
			click = function(e)
			{
				e.preventDefault();

				is_inactive = false;

				clicked_button = jQuery(e.currentTarget);

				inactiveClasses.each(function(i, inactiveClass) {
					if (clicked_button.hasClass(inactiveClass)) {
						is_inactive = true;
					}

					if (!is_inactive) {
						window.location.href = $el.attr('ref');
					}
				});
			}
		}
		else if(tag == 'a') {
			click = function(e)
			{
				e.preventDefault();

				is_inactive = false;

				clicked_button = jQuery(e.currentTarget);

				inactiveClasses.each(function(i, inactiveClass) {
					if (clicked_button.hasClass(inactiveClass)) {
						is_inactive = true;
					}

					if (!is_inactive) {
						window.location.href = $el.attr('href');
					}
				});
			}
		}
		else if(tag == 'input') {
			click = function(e)
			{
				is_inactive = false;

				clicked_button = jQuery(e.currentTarget);

				inactiveClasses.each(function(i, inactiveClass) {

					if (clicked_button.hasClass(inactiveClass)) {
						is_inactive = true;
					}

					if (is_inactive) {
						e.preventDefault();
					}
				});
			}
		}

		button.click(click);

		$el.replaceWith(button);
	});
}




/**
 * Ausmaß-Attribute eins Html-Elements ermitteln
 *
 * @param $el Das zu untersuchende Element
 * @return Array mit Attributwerten
 */
var getSizeFromElement = function($el)
{
	var $el_clone = $el.clone();
	$el.before($el_clone);
	$el_clone.css({
		'height': '',
		'position': 'absolute',
		'visibitility': 'hidden'
	});

	var size = {
		width: $el_clone.width(),
		height: $el_clone.height(),
		padding: $el_clone.padding(),
		margin: $el_clone.margin(),
		border: $el_clone.border(),
		innerWidth: $el_clone.innerWidth(),
		innerHeight: $el_clone.innerHeight(),
		outerWidth: $el_clone.outerWidth(),
		outerHeight: $el_clone.outerHeight(),
		outerMarginWidth: $el_clone.outerWidth(true),
		outerMarginHeight: $el_clone.outerHeight(true)
	};

	$el_clone.remove();

	return size;
}

var sameHeightElements = function()
{
	var groups = {};
	$('[class*=she_]').each(function(i, el) {
		var $el = $(el);
		var she_class = $el.attr('class').match(/(^|\s)(she_[a-zA-Z0-9\_]+)(\s|$)/);

		groups[she_class[2]] = {
			'el': (typeof(groups[she_class[2]]) == 'undefined') ? [$el] : $.merge(groups[she_class[2]].el, [$el]),
			'height': (typeof(groups[she_class[2]]) == 'undefined') ? $el.height() :
						($el.height() > groups[she_class[2]].height) ? $el.height()	: groups[she_class[2]].height
		};

		var x = 1;
	});

	for(var key in groups)
	{
		$(groups[key].el).each(function(i, el) {
			$(el).css('height', groups[key].height);
		});
	}

}

var lb_img_arr = function(selector) {
	var image_arr = [];

	$(selector).each(function(i, el) {
		var bg_img = $(el).css('background-image').replace(/url\(\"?(.*?)\"?\)/, '$1').replace(/^(.*?)(\.[a-zA-Z]+)$/, '$1_l$2');
		var img_title = $(el).attr('title');

		if((bg_img != 'undefined' || bg_img != '')
			&& (img_title != 'undefined')) {

			image_arr.push([bg_img, img_title]);
		}
	});

	return image_arr;
}

/**
 * Formfield overtext function
 * @param {Object} selector
 * @param {Object} defvalue
 */
function populateElement(selector, defvalue, color) {
	var password_fields = [];
	var fields = [];
	if(typeof(color) == 'undefined') {
		color = ['#333', '#888'];
	}

	// alle Elemente des Selectors ermitteln
	// und für den IE Passwort-Felder durch normale Text-Inputs ersetzen
	$(selector).each(function(i) {
		if($.trim(this.value) == "") {
			fields[i] = $(this);
	
			if(this.type == 'password') {

				if($.browser.msie) {
					fields[i] = $('<input type="text" value="' + defvalue + '" class="' + $(this).attr('class') + '" style="' + $(this).attr('style') + '" name="' + $(this).attr('name') + '" id="' + $(this).attr('id') + '" ref="password" />');
//					password_fields[i] = fields[i];
					$(this).replaceWith(fields[i]);
				}
				else {
					$(this).attr('ref', 'password');
					password_fields[i] = $(this);
					this.type = 'text';
				}
			}

			this.value = defvalue;
		}
	});


	// Events den Elementen zuweisen
	$(selector).focus(function() {
//	$(selector).live('focus', function() {

		if(this.value == defvalue) {
			var name = $(this).attr('name');

			if($(this).attr('ref') == 'password') {

				// wenn IE, dann Text-Input durch Passwort-Input ersetzen
				if($.browser.msie) {
					$(this).replaceWith('<input type="password" value="" class="' + $(this).attr('class') + '" style="' + $(this).attr('style') + '" name="' + name + '" id="' + $(this).attr('id') + '" ref="password" />');
//					setTimeout(function() {$('[name=' + name + ']').focus();}, 0);
					setTimeout(function() {$(selector).focus();}, 0);
				}
				else {
					this.type = 'password';
				}
			}
			this.value = "";
//			$('[name=' + name + ']').css('color', '#333');
			$(this).css('color', color[0]);

		}

	});

	$(selector).blur(function() {
		if($.trim(this.value) == "") {
			var name = $(this).attr('name');

			if(this.type == 'password') {
				// wenn IE, dann Passwort-Input durch Text-Input ersetzen
				if($.browser.msie) {
					$(this).replaceWith('<input type="text" value="' + defvalue + '" class="' + $(this).attr('class') + '" style="' + $(this).attr('style') + '" name="' + name + '" id="' + $(this).attr('id') + '" ref="password" />');
				}
				else {
					this.type = 'text';
				}
			}
			this.value = defvalue;
//			$('[name=' + name + ']').css('color', '#888');
			$(this).css('color', color[1]);
		}
	});

	$(selector).parents('form').submit(function() {
		if($(selector).attr('value') == defvalue) {
			return false;
		}
	});
}

// DomReady START ==============================================================
jQuery(document).ready(function() {
	var $ = jQuery;

	sameHeightElements();

	//==============================
	// Buttons
	//==============================
	createButtons('.buttons');
	createButtons('.buttonss', 's');
	//==============================

	//=====================

	// Suchfeld 'Suche'-Text einfügen
	populateElement('input[id=sform]', 'Suche');
	populateElement('input[name="search_exp"][class=ffield]', 'Suche');
	populateElement('input[name="search_exp"][class=ffieldxl]', 'Ihr Suchbegriff', ['#333', '#aaa']);

	// Login 'Benutzername'-Text einfügen
	populateElement('input[name="username"]', 'Benutzername');

	// Login 'Passwort'-Text einfügen
	populateElement('input[name="password"]', 'Passwort');

	//=====================


	//=====================
	// Area Nav
	if(jQuery('#area_nav').length == 1) {
		var sub_menu = jQuery('#menu .menu_active .submenu li').clone();
		jQuery('#area_nav').html(sub_menu);
	}
	//=====================

	
	//=====================
	// Gallery
	//=====================
	var shots = {};

	$('.shot, .shots').each(function(i, sshot){
		var $sshot = $(sshot);

		var rel = ($sshot.attr('rel') != '') ? $sshot.attr('rel') : 0;
		var title = ($sshot.attr('ref') != '') ? $sshot.attr('ref') : $sshot.attr('title');

		if(typeof(shots[rel]) == 'undefined') {
			shots[rel] = {
				l: -1,
				shots: []
			};
		}

		$sshot.data('index', shots[rel]['l'] + 1);

		if(sshot.tagName == 'A') {
			$sshot.data('type', 'a');
			shots[rel]['shots'][shots[rel]['l'] + 1] = {
				href: $sshot.attr('href'),
				title: title
			};

			shots[rel]['l']++;
		}
		else if(sshot.tagName == 'IMG') {
			$sshot.data('type', 'img');
			shots[rel]['shots'][shots[rel]['l'] + 1] = {
				href: $sshot.attr('src'),
				title: title
			};
			shots[rel]['l']++;
		}
		else {
			$sshot.data('type', 'el');
			shots[rel]['shots'][shots[rel]['l'] + 1] = {
				href: $sshot.css('background-image').replace(/url\((.*?)\)/, '$1').replace(/("|')/g, '').replace(/(\.jpg|\.png|\.gif)/, '_l$1'),
				title: title
			};
			shots[rel]['l']++;
		}
	});

	$('.shot, .shots').click(function(e) {
		e.preventDefault();
		
		var rel = 0;

		if($(this).attr('rel') != '') {
			rel = $(this).attr('rel');
		}

		$.fancybox(shots[rel]['shots'], {
			padding:0,
			index: $(this).data('index'),
			transitionIn:	'elastic',
			transitionOut:	'elastic',
			easingIn:		'swing',
			easingOut:		'swing',
			speedIn:		200,
			speedOut:		200,
			orig:			$(this),
			titlePosition:	'over',
			overlayColor:	'#222',
			overlayOpacity:	0.35,
			offset: {
				x: 0,
				y: -100
			}
		});
	});
	//=====================


	// Topbox Teaser box
	$('#teaser_box').children().hover(function() {
		var $this = $(this);

		if($this.hasClass('no_anim')) {
			return true;
		}

		$this.clearQueue();
		$this.children('.tb_more').clearQueue();

		$this.animate({
			backgroundPosition: "0 -20px"
		});

		$this.children('.tb_more').animate({
			top: $this.height() - 30
		});
	}, function() {
		var $this = $(this);

		if($this.hasClass('no_anim')) {
			return true;
		}

		$this.clearQueue();
		$this.children('.tb_more').clearQueue();

		$this.animate({
			backgroundPosition: "0 0"
		});

		$this.children('.tb_more').animate({
			top: $this.height()
		});
	});

	// Filter-Selects
	$("select").not('[multiple]').each(function(i, select) {
		if($(select).hasClass('nosb')) {
//		if($(select).hasClass('nosb') || ($.browser.msie && $(select).hasClass('noiesb'))) {
			return;
		}
		// #form_bland: Select ist durch parent display:none; und dadurch wird die Größe nicht richtig berechnet
		if($(select).attr('id') != 'form_bland' && !$(select).prev().hasClass('selectbox')) {
			// nicht ausführen, wenn onChange als Parameter gesetzt
			if(typeof($(select).attr('onchange')) == 'undefined' || typeof($(select).attr('onchange')) == 'object' || !($(select).attr('onchange') != '')) {
				$(select).sb({
					ddCtx: 'self',
					animDuration: 150
				});
			}
		}
	});

//	$('[class^=glossar_]').click(function(e) {
//		e.preventDefault();
//		var gloss_el = $(this);
//		var glossar_id = gloss_el.attr('class').match(/glossar_(\d+)/);
//
//		$.ajax({
//			url: '/app/glossar/glossar.php',
//			type: 'post',
//			data: 'id=' + glossar_id[1],
//			success: function(r) {
//				if(gloss_el.find('div.glossar').length <= 0) {
//					gloss_el.append(r);
//				}
//				else {
//					gloss_el.find('div.glossar').replaceWith(r);
//				}
//			}
//		})
//
//		return false;
//	})


	//==============================
	// Audioplayer
	//==============================
	if($.browser.msie && $.browser.version == '6.0') {
		$('.audioplayer').each(function(){
			$(this).remove();
		});

	}
	else {
		$('.audioplayer').click(function(){

			var $this = jQuery(this);

			$this.unbind('click');

			var id = $this.attr('id').replace('audioplayer','');
			var filename = $this.attr('ref');
			var streamParam = 'audiotex';

			generateSWFObject(id, 'audioplayer', filename, streamParam);
			$this.addClass('player');

			setTimeout(function() {startPlayer(id, $this)},100);

		});

	}
	//==============================

	$('.l200').hover(function() {
		var sid = $(this).children('input').attr('id').replace('s', '');
		$('#spi').attr('src', $('#spi').attr('src').replace(/sp\d+s/, 'sp' + sid + 's'));
	}, function() {
		
	});

	$('#spd').mouseleave(function() {
		$('#spi').attr('src', $('#spi').attr('src').replace(/sp\d+s/, 'sp0s'));
	});
}); // DomReady End ------------------------------------------------------------




/**
 * @author Alexander Farkas
 * v. 1.21
 */
(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}

	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};

	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}

	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');

			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}

			start = toArray(start);

			fx.start = [start[0],start[2]];

			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];

			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);


//==============================
// Flashplayer
//==============================
function generateSWFObject(id, playername, filename, streamParams, options)
{
	if(!options) {
		options = {
			swf: 'musicplayer.swf',
			width: 68,
			height: 19
		};
	}
	if(typeof(streamParams) == 'undefined') {
		streamParams = '';
	} else {
		streamParams = '&streamParams='+streamParams;
	}

	if(typeof(filename) == 'undefined') {
		return;
	}

	var audioplayer = new SWFObject("/img/cont/" + options.swf, "swf"+playername+id, options.width, options.height, "8", "");
	audioplayer.addParam("quality", "high");
	audioplayer.addParam("FlashVars", "file="+filename+streamParams);
	audioplayer.addParam("swliveconnect", "true");
	audioplayer.addParam("allowScriptAccess", "always");
	audioplayer.addParam("wmode", "transparent");
	audioplayer.addParam("name", "i"+id);
	audioplayer.write(playername+id);

	jQuery('#swf' + playername + id).css('visibility', 'visible');
}

function getFlashMovieObject(movieName)
{
	if(document.embeds[movieName]){
		return document.embeds[movieName];
	}
	if(window.document[movieName]){
		return window.document[movieName];
	}
	if(window[movieName]){
		return window[movieName];
	}
	if(document[movieName]){
		return document[movieName];
	}
	return false;
}


function DoCommand (flashID)
{
	var player = jQuery('.player');

	player.each(function(){

		var swfName = jQuery(this).attr('id');
		var movie = getFlashMovieObject('swf'+swfName);

		movie.SetVariable('_level0.send_id',flashID);
	});
 }

function startPlayer(id, player_div){
	var movie = getFlashMovieObject('swfaudioplayer'+id);
	movie.playSound();
}

//==============================
