/**
 *
 *
 * TODO:
 *   - copy content styles to popup
 *   - links attributes to set popup position and dimensions <a href="..." class="popup" width="300px" height="200px" ...>
 **/

jQuery.fn.popuplink = popupWindow;

function title_header (capa, titulo){
  //alert(site);
	var codigo = "<table class='barra_titulo'><tr><td width='90%' id='popupTitle'>&nbsp;"+titulo+
               "</td><td width='17' align='right'><a href='#' class='closePopupButton'>"+
               "<img src='"+site+"/img/icono_cerrar.gif' border='0' width='17' height='17'></a><br></td></tr></table>";
	return codigo;
}

function popupWindow( options,parametros ){

    return this.each(function( ) {
        var settings = {
          popupDivId : "popupDiv",
          contentBlockId : "content", // ID of the content to be extracted from the result page
          titulo: "Nueva Ventana"
        };
        if( options ){ jQuery.extend( settings, options ); }
        
        $( this ).click( function(){
	 $.ajax({
   		type: "POST",
   		url: settings.urlPage,
   		data: parametros,
		evalScripts:true,
		update:$("#"+settings.popupDivId),
   		success: function( data ){
                // change the content ID wo it will not match any possible content ID within the current page
                var sNewContentId = "cococontent";
                var contentRE = 'id="'+settings.contentBlockId+'"'; // TODO: catch id="kaka" but also id='kaka' !!!!
                data = data.replace(contentRE, 'id="'+sNewContentId+'"');

                // create a hidden div and add the received HTML:
                var tempHiddenDiv = $("#tempHiddenDiv");
                if( tempHiddenDiv.length == 0 ){
                    $( document.body ).append("<div id='tempHiddenDiv' style='display:none'></div>");
                    tempHiddenDiv = $("#tempHiddenDiv");
                }
                tempHiddenDiv.html( data );
		var contenido="";
	
		try {
          		contenido=tempHiddenDiv.find( "#"+sNewContentId ).html();
		}
		catch(e) { contenido=data; }

		tempHiddenDiv.html("");
		tempHiddenDiv.remove();
    
                // create the popup div, with a "close" button:
                var popupDiv = $("#"+settings.popupDivId);
                if( popupDiv.length == 0 ){
                    $( document.body ).append("<div id='"+settings.popupDivId+"'></div>");
                    popupDiv = $("#"+settings.popupDivId);
                    //popupDiv.append("<input type='button' value='close' class='closePopupButton'/>");
                    popupDiv.append(title_header (settings.popupDivId, settings.titulo));
                    popupDiv.append("<div class='popupContent' >hola</div>");
                    //popupDiv.find(".closePopupButton").click( function(){ $("#"+settings.popupDivId).hide(); } ).end();
                    
                    popupDiv.find(".closePopupButton").click( function(){ close_window( { popupDivId : settings.popupDivId } ) } ).end();
                }
								
                // put the "content" element from the hidden div inside the popup div and show it:
                popupDiv.find(".popupContent").html(contenido).end();
								show_window({ancho: settings.ancho,alto: settings.alto,popupDivId: settings.popupDivId,arriba:settings.arriba,
														 izquierda:settings.izquierda});
               }} );
            return false; // return false, so original link will  not be used!!
        } );    
    });
}
function apaga(popup) {
	$("#"+popup).animate({width:"100%",height:200,left:0,top:0}, "slow",null);
}
function recargar() {
	document.location.reload();
}
function close_window (options){
  var settings = { popupDivId : "popupDiv" };
  if( options ){ jQuery.extend( settings, options ); }
  
	var popupDiv = $("#"+settings.popupDivId);
        if( popupDiv.length != 0 ){
					popupDiv.find(".popupContent").html("").end();
					popupDiv.fadeOut("slow",apaga(settings.popupDivId));		
					popupDiv.remove();
        }
}
function show_window(options) {
  var settings = {
    popupDivId : "popupDiv",
    ancho:50,
    alto:50,
    arriba:-1,
    izquierda:-1
  };	
	if( options ){ jQuery.extend( settings, options ); }
	
	var ancho_act = 0;
	var alto_act = 0;
	if (document.all) {
		ancho_act = document.body.offsetWidth;
		alto_act = top.document.body.offsetHeight;
	} else {
		ancho_act = window.innerWidth;
		alto_act = top.window.innerHeight;
	}
	
	var izq = settings.izquierda;
	if(izq==-1) izq = (ancho_act/2) - (settings.ancho / 2);
	if(izq<0) izq=0;
	
	var arr = settings.arriba;
	if(arr==-1) arr = (alto_act/2) - (settings.alto / 2);
	if(arr<0) arr=0;
	
	var popupDiv = $("#"+settings.popupDivId);
	//popupDiv.show();
	arr=150;
	popupDiv.animate({width:settings.ancho,height:settings.alto,left:izq,top:arr}, "slow", function(){popupDiv.bgiframe();});
	
}

function showPopup( options,parametros ){
        var settings = {
          popupDivId : "popupDiv",
          contentBlockId : "content", // ID of the content to be extracted from the result page
          titulo: "Nueva Ventana",
          ancho:50,
          alto:50,
          urlPage: "#"
        };
        if( options ){ jQuery.extend( settings, options ); }

	 $.ajax({
   		type: "POST",
   		url: settings.urlPage,
   		data: parametros,
		evalScripts:true,
		update:$("#"+settings.popupDivId),
   		success: function( data ){
                // change the content ID wo it will not match any possible content ID within the current page
                var sNewContentId = "cococontent";
                var contentRE = 'id="'+settings.contentBlockId+'"'; // TODO: catch id="kaka" but also id='kaka' !!!!
                data = data.replace(contentRE, 'id="'+sNewContentId+'"');

                // create a hidden div and add the received HTML:
                var tempHiddenDiv = $("#tempHiddenDiv");
                if( tempHiddenDiv.length == 0 ){
                    $( document.body ).append("<div id='tempHiddenDiv' style='display:none'></div>");
                    tempHiddenDiv = $("#tempHiddenDiv");
                }
                tempHiddenDiv.html( data );
		var contenido="";
		
		try {
          		contenido=tempHiddenDiv.find( "#"+sNewContentId ).html();
		}
		catch(e) { contenido=data; }

		tempHiddenDiv.html("");
		tempHiddenDiv.remove();

                // create the popup div, with a "close" button:
                var popupDiv = $("#"+settings.popupDivId);
                if( popupDiv.length == 0 ){
                    $( document.body ).append("<div id='"+settings.popupDivId+"'></div>");
                    popupDiv = $("#"+settings.popupDivId);
                    //popupDiv.append("<input type='button' value='close' class='closePopupButton'/>");
                    popupDiv.append(title_header (settings.popupDivId, settings.titulo));
                    popupDiv.append("<div class='popupContent' >hola</div>");
                    //popupDiv.find(".closePopupButton").click( function(){ $("#"+settings.popupDivId).hide(); } ).end();
                    
                    popupDiv.find(".closePopupButton").click( function(){ close_window( { popupDivId : settings.popupDivId } ) } ).end();
                }
								
                // put the "content" element from the hidden div inside the popup div and show it:
                popupDiv.find(".popupContent").html(contenido).end();
								show_window({ancho: settings.ancho,alto: settings.alto,popupDivId: settings.popupDivId,arriba:settings.arriba,
														 izquierda:settings.izquierda});
                myOnLoad();
               }} );
            return false; // return false, so original link will  not be used!!
}

