var callBack, mainPopup, ab = 0;
(function($) {
	$(document).ready(function(){
		if ($("#menu").size())
		{
			$("#menu > ul > li").each(function(){
				$(this).mouseover(function(){
					$("#menu > ul > li").each(function(){
						if ($(this).attr("id")=="selected")
						{
							$(this).attr("id","no_selected");
						}
					});
					$(this).css("background","url('images/bullett.jpg') 0px 4px no-repeat");
				})
				$(this).mouseout(function(){
					$("#menu > ul > li").each(function(){
						if ($(this).attr("id")=="no_selected")
						{
							$(this).attr("id","selected");
						}
					});
					$(this).css("background","");
				})
			})
		}
	});
	$.substr = function( f_string, f_start, f_length ) {
	    // http://kevin.vanzonneveld.net
	    // +     original by: Martijn Wieringa
	    // +     bugfixed by: T.Wild
	    // +      tweaked by: Onno Marsman
	    // *       example 1: substr('abcdef', 0, -1);
	    // *       returns 1: 'abcde'
	    // *       example 2: substr(2, 0, -6);
	    // *       returns 2: ''
	 
	    f_string += '';
	 
	    if(f_start < 0) {
	        f_start += f_string.length;
	    }
	 
	    if(f_length == undefined) {
	        f_length = f_string.length;
	    } else if(f_length < 0){
	        f_length += f_string.length;
	    } else {
	        f_length += f_start;
	    }
	 
	    if(f_length < f_start) {
	        f_length = f_start;
	    }
	 
	    return f_string.substring(f_start, f_length);
	}
	$.ajaxLoad = function(divId,url,param)
	{
		var TI = new Date();
		if (!$("#"+divId).length)
		{
			$("body").append('<div id="'+divId+'"></div>');
		}
		$.ajax({
			type: "POST",
			url: url,
			data: param,
			contentType: "application/x-www-form-urlencoded;charset=ISO-UTF-8",
			success: function(msg){
				$('#'+divId).html(msg+'<span style="display: none;">'+TI.getTime()+'</span>');
			}
		});
	}
	$.shadow = function(divId,divId2){
		if ($("#shadow").size())
		{
			$("#shadow").remove();
		}
		$("body").append("<div id='shadow'></div>");
		$("#shadow").css("background-color","#000000");
		$("#shadow").css("opacity",0);
		$("#shadow").css("position","absolute");
		$("#shadow").css("width",$("body").width());
		$("#shadow").css("height",$(document).height());
		$("#shadow").css("top",0);
		$("#shadow").css("left",0);
		$("#shadow").css("z-index",2);
		$("#shadow").click(function(){
			$.closePopup(divId);	
			if (divId2!='undefined')
			{
				$.closePopup(divId2);
			}
		});
		$("#shadow").fadeTo(200,0.5);
	};
	$.closePopup = function(divId)
	{
		$("#"+divId).fadeOut(200,function(){
			$("#"+divId).remove();
			$("#shadow").animate({
				opacity: 0
			},200,function(){
				$("#shadow").remove();
			});
		});
		
	}
	$.ajaxLoadPopup = function(divId,url,param)
	{
		var TI = new Date();
		if ($("#"+divId).length)
		{
			$("#"+divId).remove();
		}
		$.shadow(divId);
		$("body").append('<div id="'+divId+'"></div>');
		$("#"+divId).css("top",0);
		$("#"+divId).css("left",0);
		$("#"+divId).css("margin",0);
		$("#"+divId).css("padding",0);
		$("#"+divId).css("position","absolute");
		$("#"+divId).css("z-index",3);
		$.ajax({
			type: "POST",
			url: url,
			data: param,
			success: function(msg){
				$("#"+divId).fadeTo(0,0);
				$("#"+divId).html(msg+'<span style="display: none;">'+TI.getTime()+'</span>');
				$("#"+divId).css("top",($(window).height()-$("#"+divId).height())/2+$(window).scrollTop());
				$("#"+divId).css("left",($("body").width()-$("#"+divId).width())/2);
				$("#"+divId).fadeTo(300,1);
				$("#closePopup").click(function(){
					$.closePopup(divId);
					return false;
				});
			}
		});
	}
	$.ajaxLoadImage = function(divId,url,param)
	{
		var TI = new Date(), loaderBox = 'loader-box', offsetDiv, i;
		mainPopup = divId;
		$.shadow(divId,loaderBox);
		$.ajaxLoaderBox(loaderBox);
		if (!$("#"+divId).length)
		{
			$("body").append('<div id="'+divId+'" class="hidden absolute_div"></div>');
			$('#'+divId).fadeTo(0,0);
		}
		$.ajax({
			type: "POST",
			url: url,
			data: param,
			contentType: "application/x-www-form-urlencoded;charset=ISO-UTF-8",
			success: function(msg){
				$('#'+divId).html(msg+'<span style="display: none;">'+TI.getTime()+'</span>');
				callBack = function()
				{
					$("#"+divId).removeClass("hidden");
					$.centerDiv(divId);
					offsetDiv = $("#"+divId).offset();
					$('#'+loaderBox).animate({
						width: $("#"+divId).width(),
						height: $("#"+divId).height(),
						left: offsetDiv.left,
						top: offsetDiv.top
					}, 500,function(){
						$("#"+divId+" img").fadeTo(0,0);
						$("#"+loaderBox+" img").fadeTo(200,0,function(){
							$("#"+loaderBox).html($("#"+divId).html());
							$("#"+divId).remove();
							$("#"+loaderBox).attr("id",divId);
							$("#closePopup").click(function(){
								$.closePopup(divId);
								return false;
							});
							i = 1;
							$("#"+divId+" img").fadeTo(300,1,function(){
								i++;
							});
						});
					});
				}
				$.preloadImages(divId,"callBack()");
			}
		});
	}
	$.ajaxLoadImageA = function(divId,url,param)
	{
		var TI = new Date(), tempDiv = 'tempDiv';
		$("#"+divId).fadeTo(200,0,function(){
			$("#"+divId).empty();
			$("#"+divId).html('<tr><td align="center"><img src="images/loading.gif" /></td></tr>');
			$("#"+divId).fadeTo(200,1);
			$("body").append('<div id="'+tempDiv+'" class="hidden"></div>');
			$("#"+tempDiv).css("top",0);
			$("#"+tempDiv).css("left",0);
			$("#"+tempDiv).css("margin",0);
			$("#"+tempDiv).css("padding",0);
			$("#"+tempDiv).css("position","absolute");
			$("#"+tempDiv).css("z-index",3);
			$.ajax({
				type: "POST",
				url: url,
				data: param,
				contentType: "application/x-www-form-urlencoded;charset=ISO-UTF-8",
				success: function(msg){
					$('#'+tempDiv).html(msg+'<span style="display: none;">'+TI.getTime()+'</span>');
					callBack = function()
					{
						$("#"+divId+" img[src$='loading.gif']").fadeTo(200,0,function(){
							if (url=='ajax/dogGalleryImage.php')
							{
								$("#"+divId).html($("#"+tempDiv).html());
							}
							else
							{
								$("#"+divId).html($("#"+tempDiv+" > table > tbody").html());
							}
							$("#"+tempDiv).remove();
							$("#closePopup").click(function(){
								$.closePopup(mainPopup);
								return false;
							});
							$("#"+divId+" img").fadeTo(0,0);
							$("#"+divId).removeClass("hidden");
							$("#"+divId+" img").fadeTo(300,1);
						});
					}
					$.preloadImages(tempDiv,"callBack()")
				}
			});
		});
	}
	$.ajaxLoaderBox = function(divId)
	{
		if ($("#"+divId).size())
		{
			$("#"+divId).remove();
		}
		$("body").append('<div id="'+divId+'" class="popup_loader hidden"></div>');
		$("body").append('<div id="loader_img"><img src="images/loading.gif" alt="" style="width: 32px; height: 32px;" /></div>');
		$.centerDiv(divId);
		$.centerDiv("loader_img");
		$("#"+divId).fadeTo(0,0);
		$("#"+divId).fadeTo(100,1);
	}
	$.centerDiv = function(divId)
	{
		$("#"+divId).css("position","absolute");
		$("#"+divId).css("top",0);
		$("#"+divId).css("left",0);
		$("#"+divId).css("z-index",3);
		$("#"+divId).css("top",($(window).height()-$("#"+divId).height())/2+$(window).scrollTop());
		$("#"+divId).css("left",($("body").width()-$("#"+divId).width())/2);
		$("#"+divId).removeClass("hidden");
	}
	$.preloader = function()
	{
		var imagesLoad = true;
		$("body img").each(function(){
			if ($(this).attr("complete")==false)
			{
				imagesLoad = false;
			}
		});
		if (imagesLoad==false)
		{
			setTimeout("$.preloader()",100);
		}
		else
		{
			$("#preloadBox").remove();
			$("#menu_over").remove();
		}
	}
	$.preloadImages = function(divId,callFunction){
		var imagesLoad = true;
		$("#"+divId+" img").each(function(){
			if ($(this).attr("complete")==false)
			{
				imagesLoad = false;
			}
		});
		if (imagesLoad==false)
		{
			setTimeout("$.preloadImages('"+divId+"','"+callFunction+"')",100);
		}
		else
		{
			eval(callFunction);
		}
	}
	$.message = function(msg)
	{
		$.ajaxLoadPopup("message","ajax/message.php","msg="+msg);
	}
	$.questionMessage = function(msg,delId,delName)
	{
		$.ajaxLoadPopup('questionMessage','ajax/questionMessage.php','msg='+msg+'&delId='+delId+'&delName='+delName);
	}
	$.checkSelectedVideo = function(action,kategoriaId)
	{
		var videoId = '';
		if ($(".selected_video").size()==0)
		{
			$.message(5);
		}
		else
		{
			videoId = $(".selected_video > input[type='hidden']").val();
			if (action==1)
			{
				document.location = 'video_modositasa.php?videoId='+videoId+'&kategoriaId='+kategoriaId;
			}
			else
			{
				if (kategoriaId=="0")
				{
					document.location = 'kereso.php?videoIdDel='+videoId;
				}
				else
				{
					document.location = 'videok_modositasa.php?kategoriaId='+kategoriaId+'&videoIdDel='+videoId;
				}
			}
		}
	}
	$.initializeMap = function()
	{
		var latlng = new google.maps.LatLng(47.349857, 22.099943);
		var myOptions = {
		  zoom: 11,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP,
		  mapTypeControl: false
		};	
		var map = new google.maps.Map(document.getElementById("google_map"), myOptions);
		
		/*var companyLogo = new google.maps.MarkerImage('contact/images/map-marker.png',
			new google.maps.Size(90,60),
			new google.maps.Point(0,0),
			new google.maps.Point(30,60)
		);
		var companyShadow = new google.maps.MarkerImage('contact/images/map-shadow.png',
			new google.maps.Size(106,37),
			new google.maps.Point(0,0),
			new google.maps.Point(27,35)
		);*/
		
		var companyMarker = new google.maps.Marker({
		  position: latlng,
		  map: map,
		  title:""
		  /*icon: companyLogo,
		  shadow: companyShadow*/
		});
	}
	function loadImageComplete()
	{
		var offsetDiv;
		$.centerDiv("popupBox");
		offsetDiv = $("#popupBox").offset();
		$('#loadImageBox').animate({
			width: $("#popupBox").width()+20,
			height: $("#popupBox").height()+20,
			left: offsetDiv.left,
			top: offsetDiv.top
		}, 500,function(){
			$("#popupBox").fadeTo(200,1,function(){
				$("#loadImageBox").remove();
				$("#loader_img").remove();
			});
		});
	}
	$.loadImage = function(imagePath)
	{
		$.shadow("popupBox","loadImageBox");
		$.ajaxLoaderBox("loadImageBox");
		$("body").append('<div id="popupBox"><table cellspacing="0" cellpadding="0" border="0"><tr><td align="right"><img src="images/close.jpg" alt="" id="closePopup" /></td></tr><tr><td><img src="'+imagePath+'" alt="" class="popupImage" /></td></tr></table></div>');
		$("#popupBox").fadeTo(0,0);
		$("#closePopup").click(function(){
			$.closePopup("popupBox");
		});
		$.preloadImages("popupBox",'loadImageComplete()');
	}
})(jQuery);
