function _f(str) {
	var a = arguments;
	return str.replace(/\$([0-9])/g, function($0, $1) { return a[$1] });
}

$.fn.wait = function(time, callback){
    return this.animate({ wait: 1 }, time, callback);
}


var fg =
{
	wmax: 90,
	wmin: 0,
	middle: 400,
	zed: 100,
	iboxVert: 100,
	basketCount: 0,

	init: function() {
		fg.lame = $.browser.msie && $.browser.version < 7;
		fg.wmin = parseInt($("#win").css("top"));
		fg.emptyText = $("#counter").text();

	//	$("#main").css("backgroundImage",  "url('img/feature.jpg')");
		
		$("#winup").click(function() {
			if(fg.winVisible())
				return fg.winClose();
			fg.winOpen(fg.basketCount ? "#basket" : "#intro");
		});
		$("#counter").click(function() {
			fg.winOpen(fg.basketCount ? "#basket" : "#intro");
		});
		$("#winclose").click(function() {
			fg.winClose();
		});
		$("#win button").hover(
			function() { $(this).css("backgroundImage", $(this).css("backgroundImage").replace(/_off/, "_on")) },
			function() { $(this).css("backgroundImage", $(this).css("backgroundImage").replace(/_on/, "_off")) }
		);
		$("#weiter").click(function() {
			$("#intro").html($("#intro2").html());
		});
		
		
		$(".bookBox").click(function() {
			var id = $(this).attr("id");
			if(!id.length)
				return
			if(fg.winVisible())
				return;
			var id = id.substr(1);
			fg.lastID = id;
			$(".bookPop").show().find("div").html("<img src='img/ajax_fachmedien.gif' />").load("?data=" + id);
		});
		
		
		$("#popupBuy").click(function() {
			fg.basketAdd(fg.lastID, this);
			
		});
		$("#popupClose").click(function() {
			$(".bookPop").hide();
		});		
		
		
		

		$(".bookBox").hover(
			function() {
				$(this).css("backgroundColor", "#bbb");
				$(this).css("backgroundImage", "url('img/buchBoxBottom2.png')");
				$(this).css("border-top", "5px solid #999");
				$(this).css("border-bottom", "5px solid #bbb");
				$(this).css("cursor", "pointer");
			},
			function() {
				$(this).css("backgroundColor", "#cecece");
				$(this).css("backgroundImage", "url('img/buchBoxBottom.png')");
				$(this).css("border-top", "5px solid #c4c4c4");
				$(this).css("border-bottom", "5px solid #ccc");
			}
		);
		

		
		

			/*
			
			return;
			
			
			
			var p = $("#content").offset();
			var q = $(this).offset();
			
			
			$("#p" + id).show().css({
				left: q.left - p.left + 10,
				top: q.top - p.top
			});
			
			*/


		
		$(window).scroll(function() {
			$("#fixed").css({
				position: "absolute"
			 });
			clearTimeout(fg.scrollTimer);
			$("#fixed").css({top: -50});
			fg.scrollTimer = setTimeout(function() {
				$("#fixed").css({top :$(document).scrollTop()});
			}, 100);
		});
		
		
		
		
		$("#bestellen").click(function() {
			fg.basketGo();
		});
		$("#empf").click(fg.empf);
		$("#senden").click(function() {
			var s = $("#eform form").serialize();
			$("#eform_back").html("senden...").load("?action=mail&" + s);
			return false;
		});
		//fg.winOpen();
	},

	empf: function() {
		$("#eform_back").html("");
		fg.winOpen("#eform");
		return false;
	},

	winVisible: function() {
		return ($("#win").position().top > fg.wmin);
	},

	winOpen: function(sel) {
		if(sel) {
			$("#intro,#eform,#basket").hide();
			$(sel).show();
		}
		if(fg.winVisible())
			return;
		if(fg.lame) {
			$("#win").css("top", fg.wmax);
		} else { 
			$("#win").stop().animate({top: fg.wmax }, 200, "swing");
			$("#win").css("backgroundImage",  "url('img/win.png')");
		}
	},
	
	winClose: function() {
		if(fg.lame) {
			$("#win").css("top", fg.wmin);
		} else { 
			$("#win").stop().animate({top: fg.wmin }, 200, "swing");
			$("#win").css("backgroundImage",  "url('img/winx.png')");
		}
	},

	basketAdd: function(id, btn) {
		if($("#b" + id).length) {
			var o = $(btn).offset();
			o.top -= 50;
			o.zIndex = fg.zed + 1;
			$("#dblwarn").css({
				left:   o.left,
				top:    o.top - 20,
				zIndex: fg.zed + 1
			}).show().wait(2000).fadeOut();
			return;
			var qty = $("#b" + id).find("input");
			qty.val(1 + (parseInt(qty.val()) || 0));
		} else {
			var p = fg.productInfo(id);
			var html = $("#brow").html().replace(/@(\w+)/g, function($0, $1) { return p[$1] || "" });
			$("#bbody").append(_f("<div id='b$1'>$2</div>", id, html));
		}
		$("#bbody img").unbind().click(function() {
			fg.basketRemove($(this).closest("div").attr("id").substr(1));
		});
		fg.basketUpdate();
		fg.counterAnimate();

	},

	basketRemove: function(id) {
		var qty = $("#b" + id).find("input");
		var n = parseInt(qty.val()) || 1;
		if(n > 1)
			qty.val(n - 1);
		else
			$("#b" + id).remove();
		fg.basketUpdate();
	},

	basketUpdate: function() {
		var n = 0;
		$("#bbody input").each(function() { n += parseInt($(this).val() || 0); });
		$("#counter").text(n ? n + " " + (n > 1 ? "Bücher" : "Buch") + " ausgewählt" : fg.emptyText);
		fg.basketCount = n;

		$("#intro,#eform,#basket").hide();
		$("#" + (fg.basketCount ? "basket" : "intro")).show();
	},

	basketGo: function() {
		var url = [];
		$("#bbody > div").each(function() {
			var id = $(this).attr("id").substr(1);
			var qq = parseInt($(this).find("input").val()) || 1;
			url.push("ChangeAction=AddToBasket&ChangeObjectPath=/Shops/Soldan/Products/" + id);
		});
		window.open("http://www.soldan.de/epages/Soldan.sf/?" + url.join("&"));
	},

	counterAnimate: function() {
		$("#counter_ani").text($("#counter").text()).stop().show().fadeOut(1000);
	},

	productInfo: function(id) {
		var o = {}, div = $(".bookPop div");
		o.topic = div.find("h6").text();
		o.author = div.find("h1").text();
		o.title = div.find("h2").html().replace(/<.+?>/g, " ");
		o.netto  = div.find("h3 span").text().replace(/[^0-9,]/g, "") + " &euro;";
		o.brutto = div.find("h3 b").text().replace(/[^0-9,]/g, "") + " &euro;";
		return o;
	}
}

$(fg.init);