// JavaScript Document
$(function(){
	jQuery.fn.zoomImage = function(){
		return this.each(function(){
			$(this).click(function(e){
								   
				var theSrc = $(this).parent().attr('href');
				var docHeight= $(document).height();	//Get Document Height
				var p =$(this).parent();		//Parent of thumb
				//console.log(parent.parent());
				while(p[0].nodeName!="DIV"){
					p = p.parent();
				}
				var thumbHeight = p.height();		//get thumb image height
				var thumbWidth = p[0].offsetWidth;		//get thumb image width
				
				animLeft= $(this).offset().left;
				animTop= $(this).offset().top;
				
				var backgroundDiv= '<div id="imgAnimBackgroundDiv" style=" position:absolute; top:0px; left:0px; background: #000; width:100%; height:'+docHeight+'px; z-index:1000; opacity:0.7; -moz-opacity:0.7;  filter: alpha(opacity=70); display:none; ">.</div>';
				$('body').append(backgroundDiv);
				$('#imgAnimBackgroundDiv').fadeIn(300);

				var margin = 8;
				var container ="<div id='imgContainer' align='right' "+
					"style='"+
					"position:absolute; "+
					"width:"+thumbWidth+"px;"+
					"height:"+thumbHeight+"px; "+
					"top:"+animTop+"px; "+
					"left:"+animLeft+"px; "+
					"z-index:1001; display:none;'>"+
					"<div id='inchidePozaWrapper' style='background: url(../images/x_normal.gif) no-repeat center center; height:19px; width:19px; border:3px #fff solid; margin-bottom:-3px; margin-right:-10px; '><a href='#' id='inchidePoza' style='display:block;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></div>"+
					"<div style='border:1px solid #eee; background:#fff url(images/ajax-loader.gif) no-repeat center center ;'><div id='theImg' style='margin:"+margin+"px; overflow:hidden; '></div></div></div>";
				$('body').append(container).fadeIn("fast");
				
				$("<img>").load(function(){
					$("#imgContainer #theImg").append(this);
					var newImg = $("<img src='"+theSrc+"'>").css("display","none").appendTo($("body"));
					var imgHeight = newImg.height();
					var imgWidth =  newImg.width();
					if(imgWidth>$(window).width()){
						imgNewWidth = $(window).width()-100;
						imgHeight = parseInt(imgHeight/(imgWidth/imgNewWidth));
						imgWidth=imgNewWidth;
					}
					$(this).width(thumbWidth);
					$(this).height(thumbHeight);
					//console.log(imgWidth);
					$('#imgContainer').animate({ width: (imgWidth)+"px",
										height: (imgHeight)+"px",
										left:($(window).width()/2 - (imgWidth/2+10) )+"px",
										top:($(window).scrollTop()+50)+"px",
										opacity:"show"},400);
					$('#imgContainer img').animate({ width: (imgWidth)+"px",
										height: (imgHeight)+"px"
										},400);			
				}).attr("src",theSrc).attr("id","theImage");
				
				//CLOSE IMAGE
				$('#imgAnimBackgroundDiv').click(function(){	
					closeImage(thumbWidth, thumbHeight, animTop, animLeft);
				});
				$("#inchidePoza").click(function(){
					$('#inchidePozaWrapper').css("backgroundImage","url(../images/x_over.gif)");
					closeImage(thumbWidth, thumbHeight, animTop, animLeft);
					return false;
				});
				$("#inchidePoza").hover(function(){
					$('#inchidePozaWrapper').css("backgroundImage","url(../images/x_over.gif)");
				},function(){
					$('#inchidePozaWrapper').css("backgroundImage","url(../images/x_normal.gif)");
				});
				
				return false;
			});
		});
	}
	
	function closeImage(thumbWidth, thumbHeight, animTop, animLeft){
			if($('#imgContainer')!=null){
				$('#imgContainer').animate({"width": thumbWidth+"px",
					"height": thumbHeight+"px",
	      			"opacity":"hide",
					"top": animTop+"px",
					"left": animLeft+"px"
				},300,function(){$(this).remove();});
				
				$('#imgAnimBackgroundDiv').fadeOut(200, function(){$(this).remove();} );	
			}
			
	}
	
	//==========PLUGIN DE TRIMIS EMAILURI =========================//
	//=============================================================//
	jQuery.fn.sendMail = function(){
		return this.each(function(){
			$(this).click(function(e){ 
				var theForm =$(this).parent();
				while(theForm[0].nodeName!="FORM")
					theForm=theForm.parent();
				
				var dateForm = theForm.serialize();
				$.ajax({
				  url: 'mail.php',
				  type:'POST',
				  data: dateForm,
				  ajaxSend:function(){
					  $(theForm).fadeOut("fast",function(){
						$(this).css("display","none");
						theForm.parent().append("<div width='"+theForm.parent().width()+"' height='"+theForm.parent().height()+"' id='mailLoad' style='background: url(images/ajax-loader.gif) no-repeat center center; '>&nbsp;</div>");
				});
				  },
				  success: function() {
					$(theForm).html("<strong>Inregistrare facuta cu succes.</strong>");
				  }
				});
				

				return false;
			});
		});
	}
});