﻿/**
 * @name: JMessage JQuery
 * @description: lightbox message alert
 * @Author: Marco Patierno
 * @website: www.hotminds.it
 * @email: m.patierno@hotminds.it
 * @version: 1.0 
 * @date: October 05, 2010
 * @released: October 05, 2010 {1.0}
 * @category: Javascript Custom Component
 * @copyright (c) 2010 Marco Patierno (powered by: http://www.hotminds.it/)
 */
 
  var jMessAction;//custom function to will call
  var jMessInStatus = 0;//0 means disabled; 1 means enabled;  
  function loadJMessage(){	
	  if (jMessInStatus==0) {
	    $("#backgroundJMessage").css({
		    "opacity": "0.7"
	    });
	    $("#backgroundJMessage").fadeIn("slow");
	    
	    $("#JMessage").animate({
	      opacity: 100
	    }, 1000, function() {
        $("#JMessage").fadeIn("slow");	
      });            
	    
	    jMessInStatus = 1;
	  }
  }

  function disableJMessage(){
	  if (jMessInStatus==1) {
	    $("#backgroundJMessage").animate({
	      opacity: 0
	    }, 2000, function() {
        $("#backgroundJMessage").hide();
      });

	    $("#JMessage").animate({
	      opacity: 0
	    }, 2000, function() {
        $("#JMessage").hide();
      });
      jMessInStatus = 0;
	  }  
  }

  function centerJMessage(){
	  var windowWidth = document.documentElement.clientWidth;
	  var windowHeight = document.documentElement.clientHeight;
	  var popupHeight = $("#JMessage").height();
	  var popupWidth = $("#JMessage").width();
	  $("#JMessage").css({
		  "position": "absolute",
		  "top": windowHeight/2-popupHeight/2,
		  "left": windowWidth/2-popupWidth/2
	  });
  	
	  $("#backgroundJMessage").css({
		  "height": windowHeight
	  });	
  }  
 
  function scrollcontent() {
    setTimeout(function() {
      var targetOffset = $("#JMessage").offset().top;
      $('html,body').animate({scrollTop: targetOffset}, 1500);
    }, 1000);
  }
  
  function openMessage(url,action){
    $("iframe#JMesContent").attr("src",url);        
    jMessAction = action;
    centerJMessage();
    loadJMessage();
    
    $('iframe#MesContent').load(function() {
      scrollcontent();
    });
    
    return false;
  }
  
  function closeMessage(){   
    disableJMessage();
    
    return false;
  }
  
  $(document).ready(function(){                                    
    var content;
	  $.get("/include/OverlayMessage/overlay_message.html",function(data){
      content = data;
      
      $('body').prepend(content);      
      //Default operation
      $('#JMessage').hide();
      $("#JMessageClose").click(function(){
        disableJMessage();
      });      
      $("#backgroundJMessage").click(function(){
        disableJMessage();
      });
      $(document).keypress(function(e){
        if(e.keyCode==27 && jMessInStatus==1){
	        disableJMessage();
        }
      });
      
      $('a[rel*=external]').click( function() {
        window.open(this.href);
        return false;
      });	      	                      
    });  
  }); 
