var lang = "bg";
$(document).ready(function() {
	if(document.location.href.toString().match(/\/en/)) lang = "en";

	if($("#q").val() == "") {
		$("#q")
			.val("Търсене")
			.bind("focus", function () { 
				if($(this).val() == "Търсене") $(this).val("");
			})
			.bind("blur", function () { 
				if($(this).val() == "") $(this).val("Търсене");
			});
	}

	var tto = false;
	$("#pageflip").hover(function() {
		if(tto) clearTimeout(tto);
		$("#pageflip img , .msg_block").stop()
			.animate({
				width: '307px', 
				height: '319px'
			}, 500); 
		} , function() {
		$("#pageflip img").stop() 
			.animate({
				width: '50px', 
				height: '52px'
			}, 220);
		$(".msg_block").stop() 
			.animate({
				width: '50px', 
				height: '50px'
			}, 200);
	});
	if(document.location.pathname.toString().length < 5) {
		$("#pageflip").mouseover();
		tto = setTimeout(function () { $("#pageflip").mouseout() }, 5000);
	}


	if($.cluetip) {
		$("abbr").cluetip({splitTitle: '|', dropShadow: false }); 
	}

	$("#mail").bind("click", function (e) {
		$(this).blur();
		e.preventDefault();
		var d = $(document);
		$("body")
			.append("<div id='bblend' style='display:none;' /><div id='bform' style='display:none;'></div>")
			.children("#bblend")
				.css({
					"zIndex" : 30000,
					"position":"absolute",
					"left":"0",
					"top":"0",
					"height":d.height(),
					"width":d.width(),
					"background":"black",
					"opacity":"0.3"
				}).show().end()
			.children("#bform")
				.css({
					"zIndex" : 30001,
					"position":"fixed",
					"left":"50%",
					"top":"150px",
					"height":"270px",
					"width":"500px",
					"marginLeft":"-250px",
					"background":"white",
					"border":"5px solid silver"
				})
				.show()
				.load("/contact.php?lang=" + (document.location.pathname.toString().match(/\/en/) ? "en" : "bg") );
	});

	$("#page_content .hide").hide().each(function () {
		var i = this.id.toString().replace("hide_",""),
			a = $("[href='#" + this.id.toString() + "']"),
			b;
		if(!a.length) {
			b = $("<a class='more' href='#hide_" + i + "'>още<span class='hs_button'>*</span></a>");
			$(this).before(b);
		}

		a.css({ "color":"black", "font-weight" : "bold", "textDecoration":"none"}).append("<span class='hs_button'>*</span>");

		a.click(function (e) {
			var et = $(e.currentTarget).blur(),
				ii = parseInt($(e.currentTarget).attr("href").replace("#hide_",""),10);
			et.toggleClass("selected");
			$("#hide_" + ii).toggle();
				e.preventDefault();
				return false;
		});
		if(b) {
			b.click(function (e) {
				var et = $(e.currentTarget).blur(),
					ii = parseInt($(e.currentTarget).attr("href").replace("#hide_",""),10);
				et.toggleClass("selected");
				$("#hide_" + ii).toggle();
				e.preventDefault();
				return false;
			});
		}
	});

	var ii = parseInt(document.location.hash.toString().replace("#",""), 10); 
	if(ii) { $("[href='#" + ii + "']").click(); }

	var collections = [];
	$(".stampit_block").each(function () {
		var t = $(this),
			n = t.next(), 
			p = t.prev(),
			h1, h2, i;
		if(!n.is(".stampit_block")) {
			n.css("clear", "both");
		}

		if(p.is(".stampit_block")) {
			i = p.attr("rel");
		}
		else {
			collections.push($());
			i = collections.length - 1;
		}
		collections[i] = collections[i].add(t);
		t.attr("rel", i);
	});
	$.each(collections, function (i, val) {
		if(val.length < 2) { return true; }
		var h = 0, w = val.parent().width(), diff;

		val.width( Math.floor( (w + 20)/val.length - 63) );
		val.each(function () {
			h = Math.max($(this).height(), h);
		});
		val.each(function () {
			// alert(h - $(this).height());
			diff = (h - $(this).height());
			//alert(diff);
			if($(this).find(".price").length) {
				$(this).find(".price").css("marginTop", (30 + diff));
			}
			else { 
				$(this).height(h - 25);
			}
		});
		val.eq(-1).css("marginRight","0");
	});
});

function eml(person, domain) {
	document.write('<'+'a class="eml" h'+'ref="ma'+'ilto'+':'+person+'@'+domain+'">'+person+'@'+domain+'</a>');
}


