var popupStatus = 0;


function resizeOverlay() {
    $("#overlay").width( $(document).width() );
    $("#overlay").height( $(document).height() );
}
           
function loadPopup(popupSelector){  
    //loads popup only if it is disabled  
    if(popupStatus==0){  
        jQuery("#overlay").css({"opacity": "0.60"});  
        jQuery("#overlay").show(); 
        jQuery(popupSelector).show();  
        popupStatus = 1;  
    }  
}  

function disablePopup(){  
    //disables popup only if it is enabled  
    if(popupStatus==1){  
        jQuery("#overlay").hide();    
        jQuery(".popup").hide();  
        popupStatus = 0;  
    }  
}  

function scrollPopup(){  
    var windowHeight = document.documentElement.clientHeight;  
    var popupHeight = $(".popup").height();  
    
    $("#overlay").css({"height": document.body.clientHeight});
    var scrollTop = $(document).scrollTop();
    var top =  scrollTop + (windowHeight/2-popupHeight/2);
    $(".popup").css({    
        "top": top    
    }); 
}

$(".popup-close").live('click', function(){  
    disablePopup();  
});  
  
$("#overlay").live('click', function(){  
    disablePopup();  
});



$(document).ready(function() {

    // Popup -----------------------------------------------------------------------------------
    $(".popup-open").click(function(){      
        loadPopup("#text-popup");  
    });

    // Anchors -----------------------------------------------------------------------------------
    function filterPath(string) {
  	return string
  	  .replace(/^\//,'')
  	  .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
  	  .replace(/\/$/,'');
    }

    // Open link in new windows -----------------------------------------------------------------------------------
    $("a[href^='http']").click(function(){
      window.open(this.href); 
      return false;
    });

    // Scroll To Page -----------------------------------------------------------------------------------	
    $('a[href*=#]').each(function() {
  	if ( filterPath(location.pathname) == filterPath(this.pathname)
  	&& location.hostname == this.hostname
  	&& this.hash.replace(/#/,'') ) {
  	  var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
  	  var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
  	   if ($target) {
  		 var targetOffset = $target.offset().top;
  		 $(this).click(function() {
  		   $('html, body').animate({scrollTop: targetOffset}, 600);
  		   return false;
  		 });		 
  	  }
  	}
    });

	// dropDown ---------------------------------------------------------------------------------------------------
	$(".dropDown li").click(function(){    
	  $(this).find("ul").slideToggle("slow")
	  $(this).toggleClass("active");
	  $(this).hasClass("active").removeClass("active");
	});

	// TOOLTIP ---------------------------------------------------------------------------------------------------
  $('.tooltip').tipsy({gravity: 'sw', fade: true});

});
