function f1() {
	var width = $("#popup-iframe").find("img.show").width();
	var height = $("#popup-iframe").find("img.show").height();
	
	if(width>600) {
		$("#popup-iframe").find("img.show").css("width","600px");
		var tempH = (600*height)/width;
		$("#popup-iframe").find("img.show").css("height",tempH+"px");
		width = 600;
		height = tempH;
	}
	
	$("#popup-iframe").css("margin-left",((-width/2))+"px");
	$("#popup-iframe").css("margin-top",((-height/2))+"px");
	$("#popup-closer").css("margin-left",((width/2)-10)+"px");
	$("#popup-closer").css("margin-top",((-height/2)-10)+"px");
	$("#popup-closer").click(function(e){
		e.preventDefault();
		$("#popup-iframe").remove();
		$(this).remove();
		$("#darker").fadeOut("slow",function(){
			$(this).remove();
		});
	});
	$("#popup-closer,img.show").show();
}
		
function setFullHeight(layer) {
	var bodyHeight = $('body').height();
	var windowHeight = $(window).height();
	var h = (bodyHeight > windowHeight ? bodyHeight : windowHeight);
	$(layer).height(h);
}

$(document).ready(function(){


	/*
	 * -- Product Selector
	 */
	
	$("select[name=product_group], select[name=product],select[name=bakt],select[name=gallery]").change(function(e) {
		if(this.selectedIndex==0) return;
		self.location.href = "/"+$(this).val();
	});

	$(".news-list-item").find("img:first").each(function(){
		if($(this).parents(".news-list-item").find("a[href*=.jpg]").length==0) return;
		var temp_href = $(this).parents(".news-list-item").find("a[href*=.jpg]").filter(":first").attr("href");
		if(temp_href.indexOf(".jpg")==-1) return;
		$(this).css("cursor","pointer");
		$(this).click(function(){
			$(this).parents(".news-list-item").find("a[href*=.jpg]").filter(":first").click();
		});
	});
	
	$(".news-list-item").find("a").click(function(e){
		var temp_href = $(this).attr("href");
		if(temp_href.indexOf(".jpg")==-1) return;
				
		e.preventDefault();
		$(document).scrollTop(0);
		$("body").append('<div id="darker" style="position:absolute; top:0px; left:0px; width:100%; background-color:#000;z-index:4000; display:none;"></div>');
		$("#darker").css('opacity',0.6).fadeIn("slow");
		setFullHeight($("#darker"));
		$("body").append('<a href="#" id="popup-closer" style="position:absolute; display:none; top:50%; left:50%; margin-left:320px; margin-top:-285px; z-index:4050;"><img src="uploads/tf/haubis-close-button.png" border="0" alt="Schließen" /></a>');
		$("body").append('<div id="popup-iframe" style="position:absolute; top:50%; left:50%; margin-left:-338px; margin-top:-269px; z-index:4030; background:none;"></div>');
		$("#popup-iframe").append('<img class="show" src="'+temp_href+'" style="cursor:pointer; display:none;" />');
		$("#popup-iframe").find("img.show").each(function(){
			this.onload = f1;	
		});
	});
	
	$("a[href*=.flv]").click(function(e){
		e.preventDefault();
		var href="http://www.haubis.at/"+$(this).attr("href").replace("http://www.haubis.at/","");
		$(document).scrollTop(0);
		$("body").append('<div id="darker" style="position:absolute; top:0px; left:0px; width:100%; background-color:#000;z-index:4000; display:none;"></div>');
		$("#darker").css('opacity',0.6).fadeIn("slow");
		setFullHeight($("#darker"));
		$("body").append('<a href="#" id="popup-closer" style="position:absolute; top:50%; left:50%; margin-left:320px; margin-top:-285px; z-index:4050;"><img src="uploads/tf/haubis-close-button.png" border="0" alt="Schließen" /></a>');
		$("body").append('<div id="popup-iframe" style="position:absolute; top:50%; left:50%; margin-left:-338px; margin-top:-269px; z-index:4030; background:none;"></div>');
		$("#popup-iframe").append('<a href="'+href+'" class="media { width:300, height:400 }"></a>');
		$('a.media').media();
		var width = 400;
		var height = 300;
		$("#popup-iframe").css("margin-left",(-width/2)+"px");
		$("#popup-iframe").css("margin-top",(-height/2)+"px");
		$("#popup-closer").css("margin-left",((width/2)-10)+"px");
		$("#popup-closer").css("margin-top",((-height/2)-10)+"px");
		$("#popup-closer").click(function(e){
			e.preventDefault();
			$("#popup-iframe").remove();
			$(this).remove();
			$("#darker").fadeOut("slow",function(){
				$(this).remove();
			});
		});
	});
	

	$("a[href*=.swf]").click(function(e){
		e.preventDefault();
		var href="http://www.haubis.at/"+$(this).attr("href").replace("http://www.haubis.at/","");
		$(document).scrollTop(0);
		$("body").append('<div id="darker" style="position:absolute; top:0px; left:0px; width:100%; background-color:#000;z-index:4000; display:none;"></div>');
		$("#darker").css('opacity',0.6).fadeIn("slow");
		setFullHeight($("#darker"));
		$("body").append('<a href="#" id="popup-closer" style="position:absolute; top:50%; left:50%; margin-left:320px; margin-top:-285px; z-index:4050;"><img src="uploads/tf/haubis-close-button.png" border="0" alt="Schließen" /></a>');
		$("body").append('<div id="popup-iframe" style="position:absolute; top:50%; left:50%; margin-left:-338px; margin-top:-269px; z-index:4030; background:none;"></div>');
		/*$("#popup-iframe").append('<a href="'+href+'" class="media { width:300, height:400 }"></a>');
		$('a.media').media();*/
		var width = 600;
		var height = 500;
		$("#popup-iframe").append('<div id="popup-swf"></div>');
		$("#popup-iframe").find("#popup-swf").flash({
			src : $(this).attr("href"),
			width : '600px',
			height : '500px',
			wmode : 'transparent'			
		});
		$("#popup-iframe").css("margin-left",(-width/2)+"px");
		$("#popup-iframe").css("margin-top",(-height/2)+"px");
		$("#popup-closer").css("margin-left",((width/2)-10)+"px");
		$("#popup-closer").css("margin-top",((-height/2)-10)+"px");
		$("#popup-closer").click(function(e){
			e.preventDefault();
			$("#popup-iframe").remove();
			$(this).remove();
			$("#darker").fadeOut("slow",function(){
				$(this).remove();
			});
		});
	});

});
