var oneShot = true;
$(function() {
	
	$("#header #logo_box").click(function() {
		window.location.href = HTTP_ROOT;
	});
	
	$("embed").each(function(i, item) {
		var src = $(item).attr("src").replace(/\.\.\//, "");
		$(item).attr("src", HTTP_ROOT + "admin/" + src); 
	});
	
	$("#textcontent a.nagyit").each(function(i, item) {
		var src = $(item).attr("href").replace(/\.\.\//, "");
		$(item).attr("href", HTTP_ROOT + "admin/" + src); 	
	}).lightBox({
		fixedNavigation:true
	});
	
	$("#sendform form").bind("submit", function() {
		if($("#sendform textarea").val() == "Text") {
			$("#sendform textarea").val("");		
		}
		if($("#sendform input:eq(0)").val() == "Name") {
			$("#sendform input:eq(0)").val("");		
		}
		if($("#sendform input:eq(1)").val() == "Telefon") {
			$("#sendform input:eq(1)").val("");		
		}
		if($("#sendform input:eq(2)").val() == "E-mail") {
			$("#sendform input:eq(2)").val("");		
		}
	});
	
	$("#sendform textarea").click(function() {
		if($(this).val() == "Text") {
			$(this).val("");
		}
	}).blur(function() {
		if($(this).val() == "") {
			$(this).val("Text");
		}
	});
	$("#sendform input:eq(0)").click(function() {
		if($(this).val() == "Name") {
			$(this).val("");
		}
	}).blur(function() {
		if($(this).val() == "") {
			$(this).val("Name");
		}
	});
	$("#sendform input:eq(1)").click(function() {
		if($(this).val() == "Telefon") {
			$(this).val("");
		}
	}).blur(function() {
		if($(this).val() == "") {
			$(this).val("Telefon");
		}
	});
	$("#sendform input:eq(2)").click(function() {
		if($(this).val() == "E-mail") {
			$(this).val("");
		}
	}).blur(function() {
		if($(this).val() == "") {
			$(this).val("E-mail");
		}
	});
	
	if($.browser.msie || $.browser.webkit) {
		$("embed").each(function(i, item) {
			var w = $(item).width();
			var h = $(item).height();
			var src = $(item).attr("src");
			$(item).wrap('<div id="flashcontainer_'+i+'" />');
			$(item).remove();
			$("#flashcontainer_" + i).flash({ 
				src: src,
          		width: w,
          		height: h 
          	}, { version: 10 }
    		);
		});	
	}
	
	$("a.date").hover(function() {
		$(this).children().attr("src", "images/datum_click.png");
	}, function() {
		$(this).children().attr("src", "images/datum.png");
	});
	
});

$(window).load(function() {
	
	$("#textcontent img:not(img[alt*='Skype'], .noFrame)").each(function(i, item) {
		
		var src = $(item).attr("src").replace(/\.\.\//, "");
		
		$(item).attr("src", HTTP_ROOT + "admin/" + src);
		if($(item).parent().hasClass("nagyit")) {
			$('<div class="frameborder" id="fb_'+i+'" />').insertBefore($(item).parent("a"));
			$(item).parent("a").wrap("#fb_" + i);
			$('<div class="frameborder-topleft" /><div class="frameborder-topright" />').insertBefore($(item).parent("a"));
			$('<div class="frameborder-bottomleft" /><div class="frameborder-bottomright" />').insertAfter($(item).parent("a"));
		} else {
			$('<div class="frameborder" id="fb_'+i+'" />').insertBefore($(item));
			$(item).wrap("#fb_" + i);
			$('<div class="frameborder-topleft" /><div class="frameborder-topright" />').insertBefore($(item));
			$('<div class="frameborder-bottomleft" /><div class="frameborder-bottomright" />').insertAfter($(item));
		}
		
		$(".frameborder").each(function(j, div) {
			if($(div).children().length == 0) {
				$(div).remove();
			}
		});
		
		/** alignments **/
		if($(item).attr("align") == "left" || $(item).attr("align") == "right") {
			$(".frameborder").eq(i).css("float", $(item).attr("align"));	
		} else if($(item).attr("align").toLowerCase() == "absmiddle" || $(item).attr("align").toLowerCase() == "middle") {
			$(".frameborder").eq(i).css({
				float : "none"/*,
				width : $(item).attr("width") + 15*/
			}).wrapAll('<table width="100%"><tr><td id="center_td_'+i+'" align="center" />');
		} else {
			$(".frameborder").eq(i).css({
				float : "none",
				width : $(item).attr("width") + 15
			});
		}
		/*if(alignmentDiv.css("text-align") != "start" || alignmentDiv.attr("align") != "") {
			if(alignmentDiv.css("text-align") == "center" && alignmentDiv.attr("align") == "") {
				var maxW = alignmentDiv.width() / 2;
				var imgWs = 0;
				alignmentDiv.children(".frameborder").children("img").each(function() {
					imgWs += $(this).width();
				});
				imgWs = imgWs / 2 + 20;
				//console.log(maxW + " " + imgWs);
				alignmentDiv.children(".frameborder").eq(0).css("margin-left", (maxW - imgWs) - (i * 8) + "px");
				$(".frameborder").eq(i).css("float", "left");
			} else if(alignmentDiv.css("text-align") == "left" || alignmentDiv.attr("align") == "left") {
				$(".frameborder").eq(i).css("float", "left");
			} else if(alignmentDiv.css("text-align") == "right" || alignmentDiv.attr("align") == "left") {
				$(".frameborder").eq(i).css("float", "left");	
			}
		}*/
		
	});
	
	setTimeout("shit()", 500);
})

function shit() {
	$(".frameborder").each(function(i, item) {
		$(item).css("width", ($(item).children("img").attr("width") + 15) + "px");
	});	
	oneShot = false;
}
