//functie pentru schimbarea imaginii de background a unui element
function swap(element,over,out) {
				$(element).hover(
						function() {//over
							$(this).css('backgroundImage','url('+over+')');
						},
						function() {//out
							$(this).css('backgroundImage','url('+out+')');
							//alert('out');					
						}
					);//end $(element).hover				
			}

//functie care schimba adresa unui element
function swap_images(element,over,out) {
	$(element).hover(
			function() {//over
				$(this).attr('src',over);
				$("#second_menu_bar").show();
				$("#second_menu_bar").hover(
						function() {
							//alert('da');
						},
						function() {
							//$(element).hide();
						}
						
					);
				
				//alert('over');
			},
			function() {//out
				//if($("#second_menu_bar").attr('visible') == false) 
				{
					$(this).attr('src',out);
					$("#second_menu_bar").hide();
				}
				//alert('out');					
			}
		);//end $(element).hover				
}

function show_all() {
	$("body").append('<div id="lightbox_background"></div>');
	$("body").append('<div id="lightbox_image"></div>');
	$("body").append('<div id="lightbox_close"></div>');
	
	$("#lightbox_image").hide();
	$("#lightbox_close").hide();
	//$("#lightbox_background").hide();
}

//functie care incarca o imagine
function lightbox_image() {
	
	$("img").click(
		function() {
			var rel = $(this).attr('rel');
			//alert(rel);
			
			if(rel == 'my_lightbox') {
				show_all();
				
				
				var src=$(this).attr('src');
				position_image(src);
				/*
				var img = new Image();
				img.src = src;	
				$("#lightbox_image").html('<img src='+src+' width="'+img.width+'px" height="'+img.height+'px">');
				*/
				hide_all();
			}
				
		}
	);
	
	//position_image();
}

function position_image(src) {
	var width = $(window).width();
	var height = $(window).height();
	
	var frame_width = 5/6*width;
	var frame_height = 8/9*height;
	
	$("#lightbox_image").css('width',frame_width);
	$("#lightbox_image").css('height',frame_height);
	
	
	//var src=$(this).attr(img_src);
	var img = new Image();
	img.src = src;	
	
	
	var mx = $("#lightbox_image").width();
	var my = $("#lightbox_image").height();		
	var poz_x = (width-mx)/2;
	var poz_y = (height-my)/2-10;
	
	
	
	$("#lightbox_image").css('left',poz_x);
	$("#lightbox_image").css('top',poz_y);
	
	
	var img_width = img.width;
	var img_height = img.height;
	
	if(img_width > frame_width || img_height > frame_height) {
		var rap_x = img_width/frame_width;
		var rap_y = img_height/frame_height;
		
		var rap = rap_x;
		if(rap_y > rap)
			rap = rap_y;
		
		//rap = Math.ceil(rap);
		
		img_width = img_width/rap;
		img_height = img_height/rap;
		}
	
	$("#lightbox_image").html('');
	$("#lightbox_image").html('<br><img src='+src+' width="'+(img_width)+'px" height="'+(img_height)+'px">');
	$("#lightbox_image").fadeIn(1000);
	
	$("#lightbox_background").fadeIn(1000);
		
	$("#lightbox_close").css('top',poz_y);
	$("#lightbox_close").css('left',poz_x+mx-20-$("#close_button").width());
	$("#lightbox_close").html('<b>X</b>');
	$("#lightbox_close").fadeIn(500);
	
	hide_all();
}

//la apasarea background-ului si a butonului de inchidere se ascund formularele
function hide_all() {
	$("#lightbox_close").click(hide);//la apasarea butonului de inchidere
	$("#lightbox_background").click(hide);//la apasarea backgroundului	
}

function hide() {
	$("#lightbox_image").hide('fast');
	$("#lightbox_close").hide();
	$("#lightbox_background").hide('fast');
	$("#lightbox_image").html('');
	
	$("#lightbox_image").remove();
	$("#lightbox_close").remove();
	$("#lightbox_background").remove();
}
