$(document).ready(function() {
  var windowHeight = $(window).height();
  $('#content').css('height', function() {
  return Math.ceil(windowHeight - 118) + "px";
  })
  // slide img centering
  $('#slide img').css('left', function() {
  return Math.ceil(-(1680 - $('#content').width()) / 2) + "px";
  })
});

$(document).ready(function(){
	
	$("#novosti #content-box p.link").click(function(){
		centerPopup();
		loadPopup();
	});
				
	$("#backgroundPopup").click(function(){
		disablePopup();
	});	
	
	$("#popupNews a.close").click(function(){
		disablePopup();
	});
	//Escape
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

/* CONTACT POPUP */
var popupStatus = 0;

function loadPopup(){
	if(popupStatus==0){
		$('embed, select, object').css({'visibility':'hidden'});
		$("#backgroundPopup").css({
			"opacity": "0.8"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupNews").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupNews").fadeOut("slow");
		popupStatus = 0;
	}
}


function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupNews").height();
	var popupWidth = $("#popupNews").width();
	var popupNewsTop = windowHeight/2-popupHeight/2;

	if (popupNewsTop <= 0) { popupNewsTop = 5; }
	
	$("#popupNews").css({
		"position": "absolute",
		"top": popupNewsTop,
		"left": windowWidth/2-popupWidth/2
	});
	
	// IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}



$(window).resize(function() {
  var windowHeight = $(window).height();
  $('#content').css('height', function() {
  return Math.ceil(windowHeight - 118) + "px";
  })
  // slide img centering
  $('#slide img').css('left', function() {
  return Math.ceil(-(1680 - $('#content').width()) / 2) + "px";
  })
});
  
  
$(document).ready(function() {
    if ($("#slide img").length%2!=0){
        $("#slide").append( $("#slide").html()  );
    }
    $("#slide img:even").addClass("even");
    $("#slide img:odd").addClass("odd");

    $("#slide img").each(function(i){
        var order = $("#slide img").length-i;
        $(this).css("z-index",order);
    });
    slideImg($("#slide img:first"));
});


function slideImg(el){
  var slideHeight = $('#slide').height();
  var slideAmount = parseFloat(el.height())-slideHeight;
  if( el.is(".even") ){
      el.animate({bottom: -slideAmount+"px"},5000,function(){
          $(this).fadeOut("slow",function(){
              $(this).css("bottom","0");
          });
          showNext($(this));
      });
  } else {
      el.animate({top: -slideAmount+"px"},5000,function(){
          $(this).fadeOut("slow",function(){
              $(this).css("top","0");
          });
          showNext($(this));
      });
  }  
}

function showNext(el){
  if( el.next().is("img") ){
      slideImg( el.next() );
  } else { 
      $("#slide img:first").fadeIn("slow",function(){
          $("#slide img").show();
          slideImg($(this));    
      });
  }
}


