function default_init()
{
	$("a.mailto.dir-rtl").click(function(event){
		event.preventDefault();
		var _email = $(this).attr('href').replace('mailto:', '').toString();
		var email = '';
		for(var i = _email.length-1; i>=0; i--) {
			email += _email.substr(i, 1);
		}
		location.href = "mailto:" + email;
	});
	if($("div#slideshow").length > 0) {
		setInterval("slideSwitch('div#slideshow', 1000)", 5000);
	}
	// Slideshow-Pics Links
	// TODO: Links aus der DB lesen
	$("div#slideshow img#slideshow-pic1").click(function(event){
		document.location.href = "/de/Unternehmen/Profil";
	});
	$("div#slideshow img#slideshow-pic2").click(function(event){
		document.location.href = "/de/Kollektionen/KollektionArtikel";
	});
	$("div#slideshow img#slideshow-pic3").click(function(event){
		document.location.href = "/de/Kollektionen/KollektionArtikel";
	});
	$("div#slideshow img#slideshow-pic4").click(function(event){
		document.location.href = "/de/Kollektionen/Kollektion/4/Linea";
	});
}

function mycore_jumpToContentTop()
{
	if($(window).scrollTop() > $("a#contentmain_top").offset().top) {
		mycore_jumpToAnchor("#contentmain_top");
	}
}

function mycore_jumpToAnchor(anchorname)
{
	location.href = anchorname;
}

function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}

/**
 * opens layer window
 * @param dom_layer
 */
function layer_Open(dom_layer) {
	$("#layerBG").fadeTo(0, 0.8);
	var a = $("#layerBG").width();
	var b = dom_layer.width();
	dom_layer.css('left', (dom_layer.parent().width() - dom_layer.width()) / 2);
	dom_layer.show('fast');
	//dom_layer.find("input:text:visible:first").focus();	// Erstes Input-Element selektieren
	dom_layer.find('div.close').unbind('click');
	dom_layer.find('div.close').click(
		function(event) {
			layer_Close($(event.target).parents('div.layerwindow'));
		}
	);
}

function layer_Close(dom_layer) {
	$("#layerBG").hide();
	dom_layer.hide('fast');
}

/**
 * Mainmenu
 */
var mainmenu_info = {
	activeMainmenuButtonID: null,
	openMainmenuID: null
};
function mainmenu_init()
{
	if(($('ul#menu').find('a.button.active').length == 1)) {
		mainmenu_info.activeMainmenuButtonID = $($('ul#menu').find('a.button.active')[0]).attr('id');
	}
	$("ul#menu li.level0.submenu-enabled a.button").click(onMainmenuClick);
}
function onMainmenuClick(event)
{
	event.preventDefault();
	$("ul#menu ul.level1").hide();
	var akt_mainmenubutton = $(event.currentTarget);
	var submenu = $($(event.currentTarget).parent().find("ul.level1"));
	var b_submenu_show = false;
	if(! mainmenu_info.openMainmenuID) {
		// No open Submenu
		b_submenu_show = true;
	} else {
		if(mainmenu_info.openMainmenuID != mainmenu_info.activeMainmenuButtonID) {
			$("a#"+mainmenu_info.openMainmenuID).removeClass('active');
		}
		b_submenu_show = akt_mainmenubutton.attr('id') != mainmenu_info.openMainmenuID;
	}
	if(b_submenu_show) {
		submenu.show();
		mainmenu_info.openMainmenuID = akt_mainmenubutton.attr('id');
		akt_mainmenubutton.addClass('active');
		if(mainmenu_info.activeMainmenuButtonID) {
			if(mainmenu_info.activeMainmenuButtonID != mainmenu_info.openMainmenuID) {
				$("#"+mainmenu_info.activeMainmenuButtonID).removeClass('active');
			}
		}
	} else {
		mainmenu_info.openMainmenuID = null;
		if(mainmenu_info.activeMainmenuButtonID) {
			$("#"+mainmenu_info.activeMainmenuButtonID).addClass('active');
		}
	}
}

function multicheck_init()
{
	$("select.multi-check").each(function(){
		//multicheck_create(this);
	});
}

function multicheck_create(_select)
{
	var select = $(_select);
	var select_id = select.attr("id");
	var options = [];
	select.find("option").each(function(){
		options.push(
			{
				value: $(this).attr("value"),
				name: $(this).html(),
				selected: $(this).attr("selected")
			}
		);
	});

	var newbox = $('<div id="'+select_id+'" class="multi-check box"></div>');
	var option_nr = 0;
	for(var optionname in options) {
		if(options.hasOwnProperty(optionname)) {
			var option = options[optionname];
			var option_id = select_id+'-check-'+option_nr;
			var checked = option['selected'] == undefined ? '' : 'checked="checked"';
			var multi_element = $(
				'<div class="multi-check-option">' +
				'<input type="checkbox" id="'+option_id+'" '+checked+'>' +
				'<label for="'+option_id+'">'+option['name']+'</label>' +
				'<br clear="all" />' +
				'</div>'
			);
			newbox.append(multi_element);
			option_nr++;
		}
	}
	select.after(newbox);
	var button_id = select_id+'-waehlen';
	var button = $('<input id="'+button_id+'" type="button" value="bitte wählen" />');
	select.after(button);
	button.click(function(event){
		newbox.toggle();
	});

}


function slideSwitch(container_id, animation_duration) {
	var $active = $(container_id+' IMG.active');

	if ( $active.length == 0 ) $active = $(container_id+' IMG:last');

    // use this to pull the images in the order they appear in the markup
	var $next =  $active.next().length ? $active.next()
		: $(container_id+' IMG:first');

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, animation_duration, function() {
			$active.removeClass('active last-active');
		});
}

function galery_click_init(container_class, outfits)
{
	// delegated Event
	$(container_class).on("click", "div.ad-image", null, function(event){
		var bildurl = $(this).find("img").attr("src");
		var x = event.pageX - $(this).offset().left;
		var clickpic_nr = Math.floor(x / $(this).width() * 4);
		var y = event.pageY - $(this).offset().top;
		// Gibt es für das Bild eine Outfitliste?
		for(var bild=0; bild<outfits.length; bild++) {
			if(outfits[bild]) {
				if(outfits[bild]['bildurl'] == bildurl) {
					var konfig_url = "/de/Konfigurator/#!g=" + outfits[bild]['outfits'][clickpic_nr]['geschlecht_key'];
					for(var artikel=0; artikel<outfits[bild]['outfits'][clickpic_nr]['artikel_nummern'].length; artikel++) {
						konfig_url += "&artikel[]=" + outfits[bild]['outfits'][clickpic_nr]['artikel_nummern'][artikel];
					}
					document.location.href = konfig_url;
				}
			}
		}
	});

}
