var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	 $("#slide1").hide();
	 $(".rb").hide();
	 $(".rb2").hide();
	 
	$("#nag2").click(function () {
    if ($(".rb").is(":hidden")) {
      $(".rb").slideDown();
	  if ($(".rb2").is(":visible")) {
      $(".rb2").slideUp();
    } 
    } else {
      $(".rb").slideUp();
    }
  });
  
	$("#nag1").click(function () {
    if ($(".rb2").is(":hidden")) {
      $(".rb2").slideDown();
	  if ($(".rb").is(":visible")) {
      $(".rb").slideUp();
    } 
    } else {
      $(".rb2").slideUp();
    }
  });
	$("#button").click(function () {
    if ($("#slide1").is(":hidden")) {
      $("#slide1").slideDown();
    } else {
      $("#slide1").slideUp();
    }
  });
	
	$(".button1").click(function(){
		var id = $(this).attr('id');//alert(id);
		//centering with css
		centerPopup();
		//load popup
		$("#popupContact").load('/'+id, function() {
		loadPopup();
});
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").live('click', function() {
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
// Get all the thumbnail
		$('div.thumbnail-item').mouseenter(function(e) {
			x = e.pageX - $(this).offset().left;
			y = e.pageY - $(this).offset().top;
			$(this).css('z-index','15')
			.children("div.tooltip")
			.css({'top': y + 10,'left': x + 20,'display':'block'});
			
		}).mousemove(function(e) {
			
			// Calculate the position of the image tooltip			
			x = e.pageX - $(this).offset().left;
			y = e.pageY - $(this).offset().top;
			
			// This line causes the tooltip will follow the mouse pointer
			$(this).children("div.tooltip").css({'top': y + 10,'left': x + 20});
			
		}).mouseleave(function() {
			
			// Reset the z-index and hide the image tooltip 
			$(this).css('z-index','1')
			.children("div.tooltip")
			.animate({"opacity": "hide"}, "fast");
		});
});
