//lexikon
$(document).ready(function(){
	makeLexikon();
	
	$('a.mainlevelinfo').click(function(){
		if($(this).attr('id')=='active_menuinfo'){
		}else{
			$('#active_menuinfo').attr('id','')
		}
		$('a.activeinfol').removeClass('activeinfol').addClass('mainlevelinfo');
		$(this).addClass('activeinfol').removeClass('mainlevelinfo');
	});
});

function makeLexikon(){
	/*
	$('dt').mouseover(function(){
		$(this).css({textDecoration:'underline'});
	});
	$('dt').mouseout(function(){
		$(this).css({textDecoration:'none'});
	});*/
	$('dt').click(function(){
		$('dt.activl').removeClass('activl');
		$(this).addClass('activl');
		var lcnt = $(this).next();
		$('#gltxt').empty();
		$('#gltxt').append(lcnt.html());	
	});
}

//littleajaxwonderbra	
function littleajaxwondrbra (mnitem, cdiv,callback){
	$(mnitem).click(function(){
		var sourceLnk=jQuery(this);
		var aurl = sourceLnk.attr('href');
		$(cdiv).load(aurl+" "+cdiv+">*",null,function(){
			
			if(callback!=null){
				callback(sourceLnk);
			}
			// track page if ajax reloaded		
			pageTracker._trackPageview();
			
		});
		return false;
	});
}


function loadContactForm(){
	
	$("#thirdbody").load("/cms/index.php?option=com_content&task=view&id=88&Itemid=149  #thirdbody>*",{},function(){
		appendContactJs();
	});
}

function appendContactJs(){
	// attach loaded contact form			
	appendScript('http://www.lieblingsschuh.de/cms/modules/mod_dg_ajaxcontact/js/functionAddEvent.js');
	appendScript('http://www.lieblingsschuh.de/cms/modules/mod_dg_ajaxcontact/js/contact.js');
	appendScript('http://www.lieblingsschuh.de/cms/modules/mod_dg_ajaxcontact/js/xmlHttp.js',function(){
		$(window).unload( EventCache.flush );
		ajaxContact();
	});
}

function appendScript(url,callback,forceIfAdded){
	
	// check if already added and do not add it twice if forceIfAdded is not set to false
	if(typeof forceIfAdded == 'undefined'){
		forceIfAdded=false;
	}
	var breakWanted=false;

	jQuery('head > script').each(function(i,elem){
		if( jQuery(elem).attr('src')==url ){
			if( !forceIfAdded){			
				if(typeof callback !='undefined' &&callback!=null){
					callback();
				}		
				breakWanted=true;		
			}
		}
	});
	if(breakWanted){
		return;
	}
	// add the not yet added scripts

	var head = document.getElementsByTagName("head")[0], done = false;
	var script = document.createElement("script");
	script.src = url;
	script.onload = script.onreadystatechange = function() {
		if (!done && (!this.readyState || this.readyState == "loaded" ||this.readyState == "complete")) {		
			if(typeof callback !='undefined' &&callback!=null){
				callback();
			}
		}
	}
	head.appendChild(script);
}
function iHover(sel,flag){
	$(sel).hover(
		function(){
			$(this).parent().addClass(flag);
		},
		function(){
			$(this).parent().removeClass(flag);
		}
	);
}

function moveTopBy(selector,dy){
	var oTop = jQuery(selector).css('top');
	var top = parseFloat(oTop);
	var newTop = top+dy;
	$(selector).css({'top':newTop+'px'});
}


function animateMenu(){
	if($('#menuv #active_menu').hasClass('mainlevel')){
		var div =$('#menuv').offset().top;
		var item=$('#active_menu').offset().top;
		var dx = item-div;			
		moveTopBy('#menuv',-dx);
		moveTopBy('.hldr',dx);
	}

	var sdiv  = $('.hldr');
	if(sdiv.length<=0){
		return;
	}
	sdiv=sdiv.offset().top; 
	var sitem = sdiv;
	var sdx   =  sitem-sdiv;
	if ($('div.hldr #active_menu').hasClass('sublevel')){
		sitem = $('div.hldr #active_menu').offset().top;
		sdx   =  sitem-sdiv;
	        moveTopBy('.hldr',-sdx);
	}else{		
		moveTopBy('.hldr',-sdx+19);
	}	
}
function changeImg(selector,img){
	jQuery(selector).attr('src',img.srcBg).attr('alt',img.name).css({'width':'607px','height':'444px'});
}
function append(sel,data){
	return jQuery(sel).append(data);
}


function parentHoverClass(sel,clazz){
	return jQuery(sel).bind("mouseover",function(e){
		jQuery(this).parent().addClass(clazz);
	}).bind("mouseout",function(e){
		jQuery(this).parent().removeClass(clazz);
	});
}

function buildGallery(){
	if(typeof galD=='undefined'||galD==null){
		return; // if module deactivated 
	}
	var lastImg;	
	var added=false;
	galD.each(function(idx,img){
		if(!added){// init image
			changeImg('#CHANGEBILD',img);			
			added=true;
		}else{
			// preload others
			append("#preloading",'<img id="preload'+idx+'"src="'+img.srcBg+'" alt="'+img.name+'">');			
		}

		// add thumb
		var htmlContent = '<div id="thumb'+idx+'" class="thumb"> <img id="smImg'+idx+'" class="thumbImg" src="'+img.srcSm+'" alt="'+img.name+'"></div>';
		append("#thumbshow",htmlContent);
		
		parentHoverClass('#smImg'+idx,'thover');
		
		// bind click
		jQuery('#smImg'+idx).bind("click",function(e){
			jQuery('.thumbImg').parent().removeClass('tactive');
			jQuery(this).parent().addClass('tactive');
			changeImg('#CHANGEBILD',img);				
		});

		lastImg=img;
	});
	

	// popuptext available
	if(typeof lastImg!='undefined'||lastImg!=null){
		var visible='thumbdetailVisible';		
		// add details box
		append("#thumbshow",'<div id="thumbdetails" class="thumb"></div>');
		// animate	
		jQuery("#thumbdetails").bind("click",function(){
			var popup =jQuery("#popupText")		
			if(popup.hasClass(visible)||jQuery('#thumbdetails').hasClass("evenClosed")){
				jQuery('#thumbdetails').removeClass("evenClosed");
				return;	// return if already available			
			}
			
			popup.remove();// not available but remove to be sure

			// add new popup
			append("#thumbdetails",'<div id="popupText" class="PopupDescrText" style="width:0px,height:0px"><div style="float:right;margin-right:1px;margin-top:2px;"><img id="closeImage" src="/cms/templates/websein_standard/images/closedetails.png"></div><br>'+lastImg.description+'</div>');
			
			popup=jQuery("#popupText");
	
			popup.fadeIn();
			popup.addClass(visible);
			
			
			jQuery("#thumbdetails").css({border:'0',background:'none',overflow:'hidden'});
			jQuery("#thumbdetails").animate({bottom:'37px',height:'132px',width:'348px'},300);
			
			
			// fade out stuff.
			jQuery("#closeImage").bind('click',function(){
				
				popup.fadeOut();

				jQuery('#thumbdetails').addClass("evenClosed");		
				jQuery("#thumbdetails").animate({bottom:'-33px',height:'50px',width:'50px'},300,function(){			
				jQuery("#thumbdetails").css({border:'1px solid #6D6E71',background:'transparent url(http://www.lieblingsschuh.de/cms/templates/websein_standard/css/../images/detailsbutton.png) no-repeat 0 0',overflow:'hidden'});
				popup.remove();
				});
				
			});

			
				
		});
	}
}

