/**
 *  Version 1.3
 *   @author felipe.delpiccolo@snoopconsulting.es
 *   
 */
(function($) {
    var openedPrintWindow = null;
    var IFRAME_ID_NAME = "print999";
    $.fn.printArea = function(width,height)
        {
    		var ele = $(this);
    		
    		if((width!=undefined)&&(height!=undefined)){
	            if (openedPrintWindow != null){            	
	            	if(!openedPrintWindow.closed){            		
	            		openedPrintWindow.focus();
	            		return;
	            	}
	            }            	
            	var winFeatures = "width="+width+",height="+height;
            	printInWindow(winFeatures, ele);
            }else{
            	printInIframe(ele);            	
            }                                    
        }

    function printInWindow(winFeatures, ele)
    {
    	openedPrintWindow = window.open("","",winFeatures);        	        	
    	openedPrintWindow.document.open();        	
    	openedPrintWindow.document.write("<html>" + getHead() + getBody(ele) + "</html>" );        	
    	openedPrintWindow.document.close();  	
    	openedPrintWindow.print();
    	openedPrintWindow.close();
    	openedPrintWindow = null;
    }
    
    function printInIframe(ele)
    {
    	//Asi funciona en FF2y3 y Chrome
    	$(document).bind("focus",function(event){    		
    		$("#"+IFRAME_ID_NAME).css("display","none");    		
    		$(this).unbind(event);
    	});
    	//Asi funciona en el Resto
    	$(window).bind("focus",function(event){    		
    		$("#"+IFRAME_ID_NAME).css("display","none");    		
    		$(this).unbind(event);
    	});
    	
    	if($("#"+IFRAME_ID_NAME).length == 0){
	    	var i = document.createElement('iframe');
	    	i.src = '';	    	
	        i.id = IFRAME_ID_NAME;
	        i.name= IFRAME_ID_NAME;
	        document.body.appendChild(i);
	        var body = ' <div class="' + $(ele).attr("class") + '">' + $(ele).html() + '</div> </div>';
	        body = fixAlignText(body);
	        window.frames[IFRAME_ID_NAME].document.open();
	        window.frames[IFRAME_ID_NAME].document.write(copyStylesRef() + body);
	        window.frames[IFRAME_ID_NAME].document.close();	        
	        //Focus necesario para 	que funcione en IE (no afecta en el resto)
	        window.frames[IFRAME_ID_NAME].focus();
	        //Se le aplica un retardo a la funcion print debido a que demora un tiempo en actualizarse el iframe despues de cambiar el display.
            setTimeout("window.frames['"+IFRAME_ID_NAME+"'].print()",111);
    	}else{
    		$("#"+IFRAME_ID_NAME).css("display","");
    		var body = ' <div class="' + $(ele).attr("class") + '">' + $(ele).html() + '</div> </div>';
    		body = fixAlignText(body);
    		window.frames[IFRAME_ID_NAME].document.body.innerHTML = copyStylesRef() + body;    		
    		//Focus necesario para 	que funcione en IE (no afecta en el resto)
    		window.frames[IFRAME_ID_NAME].focus();
    		//Se le aplica un retardo a la funcion print debido a que demora un tiempo en actualizarse el iframe despues de cambiar el display.
            setTimeout("window.frames['"+IFRAME_ID_NAME+"'].print()",111);
    	}    	
    	
    }
    
    function copyStylesRef(){
    	var head = "";
    	$(document).find("link")
        .filter(function(){
                return $(this).attr("rel").toLowerCase() == "stylesheet";
            })           
        .each(function(){
                head += '<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" >';
         });
    	return head;
    }
    
    function getHead()
    {
        var head = "<head>";
        head += copyStylesRef();
        head += "</head>";
        return head;
    }

    function getBody( printElement )
    {    	
    	var body = "<body id='printArea'><div style='text-align: left'><br>";
        body += '<div class="' + $(printElement).attr("class") + '">' + $(printElement).html() + '</div>';
        body += "</div></body>";
        //alineacion del texto impreso en IE
        body = fixAlignText(body);        
        return body;
    }
    
    function fixAlignText(text){
    	return text.replace(/advertiseBlockInfo>/gi,"advertiseBlockInfo><BR>");
    }
  
})(jQuery);
