/*--------------------------------------------------------------------------
 *  YOJA-WEB.COM : module popup
 *  Copyright (c) Jamal Chaqouri, Yohann Fontaine, of yoja team (http://www.yoja-web.com)
 *
 *  module popup is a yoja-web.com work.
 *  For details, see the yoja web site: http://www.yoja-web.com
/*--------------------------------------------------------------------------*/

var POPUP_WINDOW_CLASS = ".yojaPopup";
var POPUP_OVERLAY_CLASS = "yojaOverlay";

/*************************************/
/*  Init des fenetres modal et popup */
/*************************************/

function loadPopupWindows() {
	
 	$j(POPUP_WINDOW_CLASS).each(function(i){
		var popupId = this.id;								 
		var triggerClass = "."+popupId+"Trigger";
		var url = $j(triggerClass).attr("href");
		
	 var cssBegin = {
		display:"block",
        opacity: 0
      }
	  
	  var cssEnd = {
		display:"none",
        opacity: 1
      }
		
			 // open function
		var open = function(hash){
		  hash.w.show();
		  $j("ul.slide", hash.w).removeClass("slide").wslide({
               									width:795,
                								height: 560,
                								fade: true,
												autolink: false
       											 });
		 };
		
			// close fonction
	var close = function(hash) { 
			 hash.w.hide();
			 if(hash.o) hash.o.remove();
	};		
		
		var parameters = {
		overlayClass: POPUP_OVERLAY_CLASS,
		trigger: triggerClass,
		onShow: open
		}
	
		
		if(!url || url=="#"){
			$j(this).jqm(parameters); 
		} else {
			$j(this).jqm(Object.extend(parameters,{ajax: url})); 
		}
	});

};

function openPopupWindow(){
	
	var popupStyle = arguments[1].style;
	var popupWindow = $j("#"+arguments[0]+":first");

	
	switch(popupStyle){
		
		case "custom":
		break;
		case "alert":
		
		$j("<div class='jqmAlertWindow'><div class='jqmAlertTitle clearfix'><h1>"+popupWindow.attr('title')+"</h1><a href='#' class='jqmClose'><em>Close</em></a></div>")
		
		
		popupWindow.addClass("jqmAlert");
		var htmlContent = popupWindow.html();
		popupWindow.html("");
		popupWindow.append("<div class='jqmAlertWindow'><div class='jqmAlertTitle clearfix'><h1>"+popupWindow.attr('title')+"</h1><a href='#' class='jqmClose'><em>Close</em></a></div>");
		popupWindow.append("<div class='jqmAlertContent'>"+htmlContent+"</div></div>");
		
		break;
		
		default:
			popupWindow.addClass("jqmWindow");
			popupWindow.prepend("<img src='http://yoja-web.com/commons/scripts/modules/popup/images/x.gif' class='jqmClose'/>");
		 break; 
	}
	
	
	  var cssBegin = {
		display:"block",
        opacity: 0
      }
	  
	  var cssEnd = {
		display:"none",
        opacity: 1
      }
	

	 // open function
	var open = arguments[1].openFunction || function(hash){
		  hash.w.css(cssBegin).animate({				   	
				opacity: 1 
		},500 ); 
	 };
      
	// close fonction
	var close = arguments[1].closeFunction || function(hash) { 
		hash.w.hide();
		if(hash.o) hash.o.remove();
	};		
	
	
	if(arguments.length>1){
		$j.extend(arguments[1], {onShow:open, onHide : close});
		popupWindow.jqm(arguments[1]).jqmShow();
	} else {
		popupWindow.jqm({onShow: open, onHide : close}).jqmShow();
	}


}


DocumentReadyExecuter.addTraitement(loadPopupWindows);

