function fbs_click() {
    u = location.href;
    t = document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

function fsFade() {
	var container = $('#headerImages');
	container.find("img:last").fadeOut(500, function() {
		$(this).remove().prependTo('#headerImages').show();
	});
}

function fsDecide(e) {
	if(e.currentTarget.id=='slidePrev') {
		window.clearInterval(fsInterval);
		var container = $('#headerImages');
		container.find("img:first").hide().remove().insertAfter('#headerImages img:last').fadeIn(500);
	}
	if(e.currentTarget.id=='slideNext') {
		window.clearInterval(fsInterval);
		fsFade();
	}
}

function navMarkenCorrection(mID, smID, ssmID) {
	$("#subnavigation li a[href*='/"+mID+"/"+smID+"/'], #subnavigation p a[href*='/"+mID+"/"+smID+"/"+ssmID+"/'], #brandSwitch a[href*='/"+mID+"/'], #adaNavigation a[href*='/"+mID+"/'], #adaNavigation-v a[href*='/"+mID+"/']").each(function() {
		className = $(this).attr('class');
		$(this).attr('id',className+'Active');
	});	
	if($('p.paginationProducts a:first').size()>0) {
		thref = $('p.paginationProducts a:first').attr('href').substr(12);
		$("#subnavigation p a[href*='"+thref+"']").each(function() {
			className = $(this).attr('class');
			$(this).attr('id',className+'Active');
		});
	}
	$("#subnavigation p.we[alt='"+smID+"']	").removeClass('we');
}

$(document).ready( function() {
	//IMG ohne alt Text füllen
	$("img[alt='']").attr('alt','ADA - Polstermöbel, Betten, Sofas, Matratzen, Couch - Hersteller aus Österreich');

	// fadeshow start
	if($('#headerImages img').size()>1) {
		fsInterval = setInterval(fsFade, 3000);
		$('#slidePrev, #slideNext').bind('click', function (e) {
			fsDecide(e);
		});
	} else {
		$('#slidePrev, #slideNext').hide();
	}
	
	$('#brandSwitch a:first').append('&gt;&gt;');

	// leere iBoxen säubern
	$('#meat div.iBox h2:empty').each(function() {
		if($(this).next().text()=='') {
			$(this).next().next().hide();
		}
	});

	// transparent header removen falls leer
	if($('#headerImages h1').text()=='') {
		$('#headerImages h1').remove();
	}

	var dont = 0;
	if (parseInt($.browser.version)==7) {dont = 1; }

	//subnav correction
	$("#subnavigation ol, #brandSwitch ol").css("list-style-type","none").each(function() {
		var num = 1;
		$(this).find('li').each(function() {
			decimal = "0"+num;
			if($(this).find('#nSubActive').size()==1) {
				$(this).prepend("<span class='bold'>"+decimal+"</span>");
			} else {
				$(this).prepend("<span>"+decimal+"</span>");
			}
			if(dont==0) {
				spanHeight = $(this).height()-2;
				$(this).find('span').height(spanHeight);
			}
			num++;
		});
	});
	if(dont==0) {
		$("#subnavigation span.fine").each(function() {
			spanHeight = $(this).parent().height()-2;
			$(this).height(spanHeight);
		});
	}
	
	//iBox Linken
	$("div.iBox p.we:contains('/')").each(function() {
		linkTo = $(this).text();
		$(this).parent().find("h2,p").wrapInner("<a href='"+linkTo+"'></a>");
		//console.log(linkTo);
	});
	
	// sidebar korrektur downloads
	if($('#downloads') && $('#downloads a').size()==0) {
		$('#downloads').remove();
	}
	// sidebar korrektur funktionen
	if($('#function') && $('#function a.entry, #function a.funcOpen').size()==0) {
		$('#function').remove();
	}

	//pagination Products insert
	$('#paginationProducts a').hover(function() {
		$('#paginationProductsDescription').text($(this).attr('title'));
	}, function() {
		$('#paginationProductsDescription').empty();
	});
	
	// plus zeichen bei thumbs
	$('div.galerieBild').each(function() {
		if($(this).find('img').size()==0) {
			$(this).remove();
		} else {
			$(this).find('a').append("<img class='plus' src='/_include/css/img/photoDetail.png' />");
		}
	});
	
	// funktionen overview order korrektur
	var arr = $.makeArray($("div.pbox[spezial]"));
	$("div.pbox[spezial]").remove();
	var firstBox = $("div.pbox[spezial!='spezial']:first");
	$.each(arr, function(i, val) {
		firstBox.before(val);
	});
	
	// star funktionen first place korrektur
	$('#function a img').each(function() {
		var hide = $(this).parent().next().remove();
		var block = $(this).parent().remove();
		$('#function h3').after(hide).after(block);
	});
	
	//funktionen fancybox
	$('#function a').bind('click', function() {
		var icon = $(this).find('img').attr('src');
		var func = $(this).next();

		if ($.browser.msie && $.browser.version!='9.0' && func.find('object').size()>0) {
			embed = func.find('object').remove();
		} else {
			func.find('embed').remove();
		}
		var title = func.find('span:eq(0)').html();
		var leadin = func.find('span:eq(1)').html();
		var img = func.find('span:eq(2)').html();
		var href = $(this).attr('href');
		if(img=='') {
			image = '';
		} else {
			image ='<div id="funcImg">'+img+'</div>';
		}
		
		if(icon=='' || typeof icon=='undefined') {
			iconhtml = '<div id="funcSpacer"></div>';
		} else {
			iconhtml ='<img src="'+icon+'" alt="'+title+'" />';
		}

		$('body').append('<div id="funcBox" style="display:none;"><img id="funcClose" src="/_include/css/img/funcClose.gif" alt="close" />'+image+iconhtml+'<div id="boxText"><p>'+leadin+'</p><p><a href="'+href+'">Detailseite</a></p></div></div>');
		boxW = ($('#funcBox').width()-220)*-1;
		$('#funcBox').css('margin-left', boxW).fadeIn(500);
		
		return false;
	});
	
	$('#funcClose').live('click', function () {
		$('#funcBox').fadeOut(500, function () {$(this).remove();});
	});
	
	// iFooter Korrektur
	var ifooter = $('#iFooter');
	if(ifooter.size()>0) {
		var iText = ifooter.html();
		iText = iText.replace(/<br>/gi, ", ");
		ifooter.html(iText.substr(0,iText.length-2));
	}
	
	// fancybox
	//$('a').fancybox();
	$("a[href$='jpg'], a[href$='JPG'], a[href$='jpeg'], a[href$='JPEG'], a[href$='png'], a[href$='PNG'], a[href$='gif'], a[href$='GIF']").each(function() {
		img1 = $(this).attr('href');
		img2 = $(this).find('img').attr('src');
		
		img1l = img1.lastIndexOf('/')+1;
		img2l = img2.lastIndexOf('/')+1;
		
		c1 = img1.substr(img1l);
		c2 = img2.substr(img2l);
		
		if(c1==c2) {
			$(this).unbind('click').removeAttr('href').removeAttr('rel').removeAttr('title').css('cursor','auto').find('img.plus').remove();
		}
	});
	$("a[href$='jpg'], a[href$='JPG'], a[href$='jpeg'], a[href$='JPEG'], a[href$='png'], a[href$='PNG'], a[href$='gif'], a[href$='GIF']").fancybox();
	
	//focus on adresseuche
	$("#address").focus();
	
	//partner links 1. _blank 2. popup
	$("#partner p.blank a").each(function() {
		$(this).attr('target','_blank');
	});
	$("#partner p.popup a").each(function() {
		$(this).bind('click', function() {			
			var url = $(this).attr('href');
			var conf = $(this).parent().attr('data');
			fenster = window.open(url, "fenster1", conf+",status=no,scrollbars=yes,resizable=yes");
			fenster.focus();
			return false;
		});
	});
	
});
