//
// mussler-js
//

(function($) {

	$.fn.musslerColorbox = function(params) {
		jQuery("#mussler-flash-movie").css('visibility', 'hidden');
		var myDefaults = {
			initialWidth: 100,
			initialHeight: 100,
			innerWidth: 790,
			innerHeight: 500,
			transition:'elastic',
			speed:300,
			previous: "zur&uuml;ck",
			next: "vorw&auml;rts",
			close: '<img src="'+c5js_root()+'img/dzine/popup-close.png" alt="" border="0">',
			current:'Bild {current} von {total}',
			opacity: 0.8
		};
		var params2 = $.extend({}, myDefaults, params);
		jQuery.fn.colorbox(params2);
	}

})(jQuery);

jQuery(document).bind('cbox_complete', function(){
	jQuery('body').append( jQuery('#cboxClose') );
	var o = jQuery('#cboxContent').offset();
	var w = jQuery('#cboxContent').width();
	jQuery('#cboxClose').css({
		zIndex: 10000,
		top: (o.top-12+1)+'px',
		left: (o.left+w-20-1)+'px'
	});
	jQuery('DIV.popupInnerFixedHeight').jScrollPane();
});

jQuery(document).bind('cbox_cleanup', function(){
	jQuery('#OLDpopupForeLink,#OLDpopupBackLink').remove();
	jQuery('#cboxContent').append( jQuery('#cboxClose') );
	setTimeout('jQuery("#mussler-flash-movie").css("visibility", "visible");', 300);
	//
	if (typeof __uposTIMEOUT != "undefined" && __uposTIMEOUT != -1) {
		clearTimeout(__uposTIMEOUT);
	}
});

/****************************************************************************************************
* TOOLTIP DING
****************************************************************************************************/

this.musslerTooltip = function(){
	xOffset = 5;
	yOffset = 5;
	jQuery("a.musslerTooltip").hover(function(e){
		this.t = this.title;
		this.title = "";
		jQuery("body").append("<p id='musslerTooltip'>"+ this.t +"</p>");
		jQuery("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
	},
	function(){
		this.title = this.t;
		jQuery("#musslerTooltip").remove();
	});
	jQuery("a.musslerTooltip").mousemove(function(e){
		jQuery("#musslerTooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};

this.markenTooltip = function(){
	xOffset = 15;
	yOffset = -5;
	jQuery("a.markenTooltip").hover(function(e){
		this.t = jQuery('#marke'+jQuery(this).attr('marke')).html();
		jQuery("body").append("<div id='markenTooltip'>"+ this.t +"</div>");
		var x = e.pageX + xOffset;
		if (x+210 > jQuery("body").width())
			x = jQuery("body").width()-210;
		jQuery("#markenTooltip")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(x) + "px")
			.fadeIn("fast");
		jQuery("body").append("<div id='markenTooltipShadow1'></div>");
		jQuery("body").append("<div id='markenTooltipShadow2'></div>");
		jQuery("body").append("<div id='markenTooltipShadow3'></div>");
		jQuery("#markenTooltipShadow3")
			.css("top",(e.pageY - yOffset + jQuery("#markenTooltip").outerHeight()) + "px")
			.css("left",(x +1) + "px")
			.fadeIn("fast");
		jQuery("#markenTooltipShadow2")
			.css("top",(e.pageY - yOffset + 20) + "px")
			.css("left",(x + 188) + "px")
			.css("height",( jQuery("#markenTooltip").outerHeight() - 20 ) + "px")
			.fadeIn("fast");
		jQuery("#markenTooltipShadow1")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(x + 188) + "px")
			.fadeIn("fast");
	},
	function(){
		jQuery("#markenTooltip,#markenTooltipShadow1,#markenTooltipShadow2,#markenTooltipShadow3").remove();
	});
	jQuery("a.markenTooltip").mousemove(function(e){
		var x = e.pageX + xOffset;
		if (x+210 > jQuery("body").width())
			x = jQuery("body").width()-210;
		jQuery("#markenTooltip")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(x+1) + "px");
		jQuery("#markenTooltipShadow3")
			.css("top",(e.pageY - yOffset + jQuery("#markenTooltip").outerHeight()) + "px")
			.css("left",(x) + "px");
		jQuery("#markenTooltipShadow2")
			.css("top",(e.pageY - yOffset + 20) + "px")
			.css("left",(x + 188) + "px")
			.fadeIn("fast");
		jQuery("#markenTooltipShadow1")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(x + 188) + "px")
			.fadeIn("fast");
	});
};

jQuery(document).ready(function(){
	markenTooltip();
});

/****************************************************************************************************
* AJAX HANDLERS
****************************************************************************************************/

function ajaxUrl(action) {
	return c5getLINKPREFIX()+"&c5p=1021&ajaxAction="+action;
}

function musslerOpenGewinnspiel() {
	jQuery.fn.musslerColorbox({href:ajaxUrl('getGewinnspiel')});
}

function musslerOpenGewinnspiel2() {
	jQuery.fn.musslerColorbox({href:ajaxUrl('getGewinnspiel2')});
}

function musslerOpenNewsletteranmeldung() {
	jQuery.fn.musslerColorbox({href:ajaxUrl('getNewsletteranmeldung')});
}

function musslerOpenFrageantwort() {
	jQuery.fn.musslerColorbox({href:ajaxUrl('getFrageantwort')});
}

function musslerOpenProdderwoche() {
	jQuery.fn.musslerColorbox({href:ajaxUrl('getProdderwoche')});
}

function musslerOpenAktuelles() {
	jQuery.fn.musslerColorbox({href:ajaxUrl('getBeautynews')});
}

function musslerOpenSchminkschule() {
	jQuery.fn.musslerColorbox({href:ajaxUrl('getSchminkschule')});
}

function musslerLoadMarken(typ) {
	jQuery.get( ajaxUrl('getMarken&pTyp='+typ), function(data) {
		jQuery("#marken_container").html(data);
	} );
}

function musslerOpenDame(id) {
	jQuery.fn.musslerColorbox({href:ajaxUrl('getDame&dameId='+id), innerWidth: 735, innerHeight: 460});
}

function musslerOpenHobImpressum() {
	jQuery.fn.musslerColorbox({href:ajaxUrl('getHobImpressum')});
}

/****************************************************************************************************
* HELPERS
****************************************************************************************************/

function musslerSendform(f) {
	var action = jQuery(f).attr('action');
	var fields = jQuery(f).serialize();
	jQuery.post(action, fields, function(data) {
		jQuery('#cboxLoadedContent').html(data);
	});
}

function textfeld_hoehe(hoehe) {
	jQuery("#mussler-frage").css('height', (hoehe+40)+"px");
}

function c2e_getAjaxResultCode(data) {
	var r = /^<!--C2E-JSRESULT--([A-Z0-9_-]+)-->/;
	r.exec(data);
	var res = RegExp.$1;
	return res;
}

function musslerInArray(val, arr) {
	for (var i=0;i<arr.length;i++) {
		if (arr[i]==val)
			return true;
	}
	return false;
}

function musslerHobHandleDamen() {
	jQuery("DIV.hob-dame").each(function() {
		// preparations
		var divID = this.id;
		var r = /^dame([0-9]+)$/;
		r.exec(divID);
		var dameId = RegExp.$1;
		eval("var dameMappings = mappings"+dameId+";");
		// determine new state
		var newState = true;
		if (parseInt(std)>0 && !musslerInArray(parseInt(std), dameMappings))
			newState = false;
		if (parseInt(beh)>0 && !musslerInArray(parseInt(beh), dameMappings))
			newState = false;
		if (parseInt(spz)>0 && !musslerInArray(parseInt(spz), dameMappings))
			newState = false;
		if (parseInt(akt)>0 && !musslerInArray(parseInt(akt), dameMappings))
			newState = false;
		// determine act state
		var opacity = jQuery(this).css('opacity');
		var actState = parseInt(opacity)==1;
		// do change?
		if (actState!=newState) {
			jQuery('#'+divID).animate({ opacity: newState?1:0.2 });
			if (newState)
				jQuery('#'+divID).removeClass('hob-dame-inaktiv');
			else
				jQuery('#'+divID).addClass('hob-dame-inaktiv');
		}
	});
}

function musslerInitFlashfilms() {
	// mussler-logo
	jQuery('body').append("<div id=\"musslerFlash-logo\"></div>");
	jQuery('#musslerFlash-logo').flash(c5js_root()+'img/flash/mussler_logo_schmetterling4.swf', { version: '9.0.0', params: { allowfullscreen: false, allowScriptAccess: 'sameDomain' }, attr: { id:'flash001', width:279, height:257 } });
	// sei schön zu dir
	jQuery('body').append("<div id=\"musslerFlash-seischoen\"></div>");
	jQuery('#musslerFlash-seischoen').flash(c5js_root()+'img/flash/mussler_sei_schoen_schmetterling4.swf', { version: '9.0.0', params: { allowfullscreen: false, allowScriptAccess: 'sameDomain' }, attr: { id:'flash002', width:420, height:136 } });
	// frage
	jQuery("#mussler-frage").flash(c5js_root()+'img/flash/frage3.swf', { version: '9.0.0', params: { allowfullscreen: false, allowScriptAccess: 'sameDomain' }, attr: { id:'flash003', width:760, height:168 } });
	// positions
	musslerPositionFlashes();
}

function musslerPositionFlashes() {
	// mussler-logo
	var logoPos = jQuery('#mussler-main-logo').offset();
	jQuery('#musslerFlash-logo').css({
		top: (logoPos.top-25)+"px",
		left: (logoPos.left-55)+"px"
	});
	// sei schön zu dir
	var seisPos = jQuery('#sei-schoen-zu-dir').offset();
	jQuery('#musslerFlash-seischoen').css({
		top: (seisPos.top-26)+"px",
		left: (seisPos.left-70)+"px"
	});
	// again!
	setTimeout("musslerPositionFlashes()", 1000);
}

function musslerInitFlashfilms2() {
	// animation!?
//	jQuery('body').append("<div id=\"musslerFlash-hob-logo\"></div>");
//	jQuery('#musslerFlash-hob-logo').flash(c5js_root()+'img/flash/home_of_beauty_logo_fade.swf', { version: '9.0.0', params: { allowfullscreen: false }, attr: { width:533, height:462 } });
	// mussler-logo
	jQuery('body').append("<div id=\"musslerFlash-navischmetterlinge\" style=\"z-index:999\"></div>");
	setTimeout("jQuery('#musslerFlash-navischmetterlinge').flash(c5js_root()+'img/flash/home_of_beauty_schmetterling2.swf', { version: '9.0.0', params: { allowfullscreen: false, allowScriptAccess: 'sameDomain' }, attr: { id:'flash004', width:300, height:840} });", 2000);
	setTimeout("jQuery('#musslerFlash-navischmetterlinge').css('display', 'block');", 2100);
	// mussler-startfilm @ hob
	jQuery('#hob-startflash').flash(c5js_root()+'img/flash/home_of_beauty_logo_fade2.swf', { version: '9.0.0', params: { allowfullscreen: false, allowScriptAccess: 'sameDomain' }, attr: { id:'flash005', width:533, height:462} });
	setTimeout("musslerSwitchHobDamen()", 7500);
	setTimeout("jQuery('#mussler-main1').css({zIndex:8});", 7000);
	setTimeout("jQuery('#musslerFlash-navischmetterlinge').css({zIndex:1});", 7000);

	// positions
	musslerPositionFlashes2();
}

function musslerSwitchHobDamen() {
	jQuery('#hob-startflash').css('display', 'none');
	jQuery('#hob-damen').fadeIn(3000);
}

function musslerPositionHobNavi(repeat) {
	if (repeat)
		setTimeout('musslerPositionHobNavi(true);', 500);
	var o = jQuery('#hob-navi-fake').offset();
	var h = jQuery('#hob-navi').height();
	jQuery('#hob-navi').css({
		position: 'absolute',
		zIndex: 999,
		top: o.top+'px',
		left: o.left+'px'
	});
	jQuery('#hob-navi-fake').css({
		height: h+'px'
	});
	// mussler-logo
	var logoPos = jQuery('#mussler-low-logo').offset();
	jQuery('#musslerFlash-navischmetterlinge').css({
		top: (logoPos.top-635)+"px",
		left: (logoPos.left+105)+"px"
	});
}

function musslerPositionFlashes2() {
	return; // !!!
	// mussler-logo
	var logoPos = jQuery('#mussler-low-logo').offset();
	jQuery('#musslerFlash-navischmetterlinge').css({
		top: (logoPos.top-635)+"px",
		left: (logoPos.left+105)+"px"
	});
	// again!
	setTimeout("musslerPositionFlashes2()", 1000);
}

function musslerHandleMappingClick(hobClass, hobId) {
	eval("var act = "+hobClass+";");
	jQuery("A."+hobClass).removeClass('hobActive');
	if (act==hobId) {
		eval(""+hobClass+" = '';");
	}
	else {
		jQuery("A.hobActive").removeClass('hobActive');
		std = beh = spz = akt = "";
		eval(""+hobClass+" = hobId;");
		jQuery("A.map"+hobId).addClass('hobActive');
	}
	musslerHobHandleDamen();
}

function musslerSwitchBild(n) {
	jQuery("[id*=produktText_],[id*=produktBild_]").hide();
	jQuery("DIV#produktText_"+n).show();
	jQuery("DIV#produktBild_"+n).show();
}

function mussler_getAjaxResultCode(data) {
	var r = /<!--M2D-JSRESULT--([A-Z0-9_-]+)-->/;
	r.exec(data);
	return RegExp.$1;
}

function mussler_switchLayersJQ(layerToHideName, layerToShowName, duration, overflowParent, repairWidthHeightAfterAnim, callbackWhenDone) {
	// default params
	if (typeof duration == "undefined" || duration==false)
		duration = 300;
	if (typeof callbackWhenDone == "undefined")
		callbackWhenDone = false;
	if (typeof overflowParent == "undefined" || overflowParent==false)
		overflowParent = window;
	else if	(typeof overflowParent == "string")
		overflowParent = document.getElementById(overflowParent);
	if (typeof repairWidthHeightAfterAnim == "undefined")
		repairWidthHeightAfterAnim = true;
	// check+set avtivity
	if (jQuery("#"+layerToHideName).attr("stmShowHideActive")==1 || jQuery("#"+layerToShowName).attr("stmShowHideActive")==1) {
		setTimeout(function () { mussler_switchLayersJQ(layerToHideName, layerToShowName, duration, overflowParent, repairWidthHeightAfterAnim, callbackWhenDone); }, 500);
		return;
	}
	if (jQuery("#"+layerToShowName).css("display")!="none" || jQuery("#"+layerToHideName).css("display")=="none") {
		return false;
	}
	jQuery("#"+layerToHideName).attr("stmShowHideActive", "1");
	jQuery("#"+layerToShowName).attr("stmShowHideActive", "1");
	// change order in DOM
	var copyOfToShow = jQuery("#"+layerToShowName).clone();
	jQuery("#"+layerToShowName).remove()
	copyOfToShow.insertAfter(jQuery("#"+layerToHideName));
	// get all relevant sizes
	var lthWidth = jQuery("#"+layerToHideName).width();
	var lthHeight = Math.max(1, jQuery("#"+layerToHideName).height());
	//var lthPosX = stm_getLeft(document.getElementById(layerToHideName));
	//var lthPosY = stm_getTop(document.getElementById(layerToHideName));
	var posTemp = jQuery('#'+layerToHideName).offset();
	var lthPosX = posTemp.left;
	var lthPosY = posTemp.top;
	if (overflowParent != window) {
		lthPosX = document.getElementById(layerToHideName).offsetLeft;
		lthPosY = document.getElementById(layerToHideName).offsetTop;
	}
	var lthOverflow = jQuery("#"+layerToHideName).css("overflow");
	// prepare lts
	jQuery("#"+layerToShowName).css({
		'width': lthWidth+'px',
		'display': 'block',
		'opacity': 0.1,
		'position': 'absolute',
		'top': lthPosY+'px',
		'left': lthPosX+'px'
	});
	var ltsWidth = jQuery("#"+layerToShowName).width();
	var ltsHeight = Math.max(1, jQuery("#"+layerToShowName).height());
	var ltsOverflow = jQuery("#"+layerToShowName).css("overflow");
	// TODO/CHECK: flackern beim show???
//NOT!	stm_initiateScrollingJQ(layerToShowName, overflowParent);
	// do the anims
	jQuery("#"+layerToHideName).css({
		'height': lthHeight+'px',
		'width': lthWidth+'px',
		'display': 'block',
		'overflow': 'hidden'
	});
	jQuery("#"+layerToShowName).css({
		'width': ltsWidth+'px',
		'height': lthHeight+'px',
		'overflow': 'hidden'
	});
	jQuery("#"+layerToHideName).animate({
		'height':ltsHeight,
		'opacity':0
	}, duration * 0.99, "linear");
	jQuery("#"+layerToShowName).animate({
		'height':ltsHeight,
		'opacity':0.99
	}, duration, "linear", function () {
		jQuery("#"+layerToHideName).css({
			'height': lthHeight+'px',
			'display': 'none',
			'overflow': lthOverflow
		});
		jQuery("#"+layerToShowName).css({
			'position': 'static',
			'display': 'block',
			'overflow': ltsOverflow,
			'opacity': 1.0
		});
		if (repairWidthHeightAfterAnim)
			stm_repairWidthAndHeightJQ(layerToShowName);
		jQuery("#"+layerToHideName).attr("stmShowHideActive", "0");
		jQuery("#"+layerToShowName).attr("stmShowHideActive", "0");
		if (typeof callbackWhenDone == "function") {
			callbackWhenDone();
		}
	});
	return true;
}

function mussler_switchLayersJQ2(layerToHideName, layerToShowName, duration, overflowParent, repairWidthHeightAfterAnim, callbackWhenDone) {
	// default params
	if (typeof duration == "undefined" || duration==false)
		duration = 300;
	if (typeof callbackWhenDone == "undefined")
		callbackWhenDone = false;
	if (typeof overflowParent == "undefined" || overflowParent==false)
		overflowParent = window;
	else if	(typeof overflowParent == "string")
		overflowParent = document.getElementById(overflowParent);
	if (typeof repairWidthHeightAfterAnim == "undefined")
		repairWidthHeightAfterAnim = true;
	// check+set avtivity
	if (jQuery("#"+layerToHideName).attr("stmShowHideActive")==1 || jQuery("#"+layerToShowName).attr("stmShowHideActive")==1) {
		setTimeout(function () { stm_switchLayersJQ(layerToHideName, layerToShowName, duration, overflowParent, repairWidthHeightAfterAnim, callbackWhenDone); }, 500);
		return;
	}
	if (jQuery("#"+layerToShowName).css("display")!="none" || jQuery("#"+layerToHideName).css("display")=="none") {
		return false;
	}
	jQuery("#"+layerToHideName).attr("stmShowHideActive", "1");
	jQuery("#"+layerToShowName).attr("stmShowHideActive", "1");
	// change order in DOM
	var copyOfToShow = jQuery("#"+layerToShowName).clone();
	jQuery("#"+layerToShowName).remove()
	copyOfToShow.insertAfter(jQuery("#"+layerToHideName));
	// get all relevant sizes
	var lthWidth = jQuery("#"+layerToHideName).width();
	var lthHeight = Math.max(1, jQuery("#"+layerToHideName).height());
	//var lthPosX = stm_getLeft(document.getElementById(layerToHideName));
	//var lthPosY = stm_getTop(document.getElementById(layerToHideName));
	var posTemp = jQuery('#'+layerToHideName).position();
	var lthPosX = posTemp.left;
	var lthPosY = posTemp.top;
	if (overflowParent != window) {
		lthPosX = document.getElementById(layerToHideName).offsetLeft;
		lthPosY = document.getElementById(layerToHideName).offsetTop;
	}
	var lthOverflow = jQuery("#"+layerToHideName).css("overflow");
	// prepare lts
	jQuery("#"+layerToShowName).css({
		'width': lthWidth+'px',
		'display': 'block',
		'opacity': 0.1,
		'position': 'absolute',
		'top': lthPosY+'px',
		'left': lthPosX+'px'
	});
	var ltsWidth = jQuery("#"+layerToShowName).width();
	var ltsHeight = Math.max(1, jQuery("#"+layerToShowName).height());
	var ltsOverflow = jQuery("#"+layerToShowName).css("overflow");
	// TODO/CHECK: flackern beim show???
//NOT!	stm_initiateScrollingJQ(layerToShowName, overflowParent);
	// do the anims
	jQuery("#"+layerToHideName).css({
//		'height': lthHeight+'px',
//		'width': lthWidth+'px',
		'display': 'block',
		'overflow': 'hidden'
	});
	jQuery("#"+layerToShowName).css({
//		'width': ltsWidth+'px',
//		'height': lthHeight+'px',
		'overflow': 'hidden'
	});
	jQuery("#"+layerToHideName).animate({
//		'height':ltsHeight,
		'opacity':0
	}, duration * 0.99, "linear");
	jQuery("#"+layerToShowName).animate({
//		'height':ltsHeight,
		'opacity':0.99
	}, duration, "linear", function () {
		jQuery("#"+layerToHideName).css({
//			'height': lthHeight+'px',
			'display': 'none',
			'overflow': lthOverflow
		});
		jQuery("#"+layerToShowName).css({
			'position': 'static',
			'display': 'block',
			'overflow': ltsOverflow,
			'opacity': 1.0
		});
		if (repairWidthHeightAfterAnim)
			stm_repairWidthAndHeightJQ(layerToShowName);
		jQuery("#"+layerToHideName).attr("stmShowHideActive", "0");
		jQuery("#"+layerToShowName).attr("stmShowHideActive", "0");
		if (typeof callbackWhenDone == "function") {
			callbackWhenDone();
		}
	});
	return true;
}

function mussler_enlargeSpezialbehandlungen() {
	jQuery('#spezial1').hide();
	jQuery('#spezial2').show();
	musslerPositionHobNavi(false);
}

function mussler_reduceSpezialbehandlungen() {
	jQuery('#spezial2').hide();
	jQuery('#spezial1').show();
	musslerPositionHobNavi(false);
}

