/*!
 * reader jQuery Plugin
 * http://klimchuk.com/projects/reader
 *
 * Copyright 2010, Ivan Klimchuk
 *
 * Date: Tue Jul 06 16:30:00 2010 +0200
 */
jQuery.fn.reader = function( options ){
    
	var element = this;
    
    if( options == null )
        options = {};
	
	jQuery( window ).bind("resize", function(){	
        resizeWindow();		
	});
    
    if( options['action'] == 'show' ){        
        
        jQuery( "body" ).css({
            "overflow" : "hidden"
        });
        
        html = analysis( jQuery( element ).html() );
        
        createShadow( options['shadow'] );
        $ ("object").css({
            "display" : "none"
        });
        createWindow( options['window'], html );
                
        createActions();
        
    } else {             
        addActivateButton( element, options );
    }
    
    function resizeWindow(){
    
        minHeight = jQuery( "#reader-actions" ).height() + 80 + 60;
        calcHeight = jQuery( window ).height() - 2*20;
    		
    	if ( calcHeight < minHeight )
    		height = minHeight;
    	else	
    		height = calcHeight;
        	
    	jQuery( "#reader-window-block" ).css({	
    		"height" : height + "px"
    	});
        
    }
    
    function addActivateButton( element, options ){
        
        var buttonOptions = options['button'];    
        
        if ( !buttonOptions ){
            
            buttonOptions = {
                "background"    : null,
                "color"         : null,
                "text"          : null,
                "padding"       : null,
                "margin"        : null,
                "font-size"     : null,
                "font-family"   : null,
                "font-weight"   : null
            };
        } 
            
        var button = '<p style="text-align: right;"><sup id="reader-activate">' + ( buttonOptions['text'] || "reader" ) + '</sup></p>';
        
        var h1 = jQuery( element ).find( "h1" ).get(0);
        
        jQuery( h1 ).after( button );
    
        jQuery("#reader-activate").css({
    
            "background"        : buttonOptions['background']   || "#222222",
            "color"             : buttonOptions['color']        || "#FFFFFF",                           
            "padding"           : buttonOptions['padding']      || "0 4px 1px 4px",
            "margin"            : buttonOptions['margin']       || "0 0 0 10px",
            "font-size"         : buttonOptions['fontSize']    || "12px",
            "font-family"       : buttonOptions['fontFamily']  || "sans-serif",
            "font-weight"       : buttonOptions['fontWeight']  || "normal",
            "cursor"            : "pointer",
            "line-height"       : "0",
            
                "-moz-border-radius"    : "5px",
                "-webkit-border-radius" : "5px",
                "border-radius"         : "5px"
                
        });    
        
        jQuery( "#reader-activate" ).bind( "click", function(){
            
            options['action'] = "show";
            
            $( element ).reader( options );
            
        });
        
    }
    
    function analysis( html ){
        
        regDIV = /<div(.*?)>(.*?)<\/div>/;
        
        regSUP = /<sup(.*)>(.*)<\/sup>/;
        
        regA = /<(\/?)a(.*?)>/;
        
        //html = html.replace( regDIV, "" );
        html = html.replace( regSUP, "" );
        html = html.replace( regA  , "" );
        
        return html;
        
    }
    
    function createShadow( shadowOptions ){
    
    	var html = '<div id="reader-shadow-block"></div>';
    	
    	jQuery(html).appendTo("body");
        
        if ( !shadowOptions ){
            
            shadowOptions = {
                "opacity"   : null
            }
        }
    	
    	jQuery( "#reader-shadow-block" ).css({
    	   
    		"position" 			: "fixed",
    		"top" 				: "0",
    		"left" 				: "0",
    		"width" 			: "100%",
    		"height" 			: "100%",
            "overflow"          : "hidden",
    		"background" 		: "#000000",
    		"opacity" 			: shadowOptions['opacity'] || ".8"
            
    	});
    }

    function createWindow( windowOptions, text ){
    
    	var html = '<div id="reader-window-block"><div id="reader-window-block-inner"></div></div>';
        	
    	jQuery( html ).appendTo( "body" );
        
        if ( !windowOptions ){
            
            windowOptions = {
                
                "width"         : null,
                "color"         : null,
                "fontFamily"    : null,
                "fontSize"      : null
                
            }        
        }
        
        leftOffset = jQuery( window ).width() / 2 - parseInt( windowOptions['width'] || "700px" ) / 2;        
    	
    	jQuery( "#reader-window-block" ).css({
    	   
            "position"              : "fixed",
            "top"                   : "20px",
            "left"                  : leftOffset    + "px",
            "width"                 : windowOptions['width'] || "700px",
            "background"            : "#FFFFFF",            
            "color"                 : "#444",
            "font-size"             : windowOptions['fontSize']     || "16px",
            "font-family"           : windowOptions['fontFamily']   || "Times New Roman",		
            "overflow"              : "auto",
            
                "-webkit-box-shadow"    : "0px 0px 20px #000",
                "-moz-box-shadow"       : "0px 0px 20px #000",
                "box-shadow"            : "0px 0px 20px #000"
                  
    	});
        
        jQuery( "#reader-window-block-inner" ).css({
            
            "padding"       : "50px",
            "text-align"    : "justify"        
            
    	});   
    	
    	jQuery( "#reader-window-block-inner" ).append( text );
        
        jQuery( "#reader-window-block-inner" ).find( "p" ).css({    
            "padding"       : "10px 0"        
        });
        
        resizeWindow();
    }
    
    function createActions(){
        
        var html = '<div id="reader-actions"></div>';
        jQuery( "body" ).append(html);
            
        windowTop       = jQuery( "#reader-window-block" ).offset().top;
        windowLeft      = jQuery( "#reader-window-block" ).offset().left;
        
        actionsTop      = windowTop     + 80;
        actionsLeft     = windowLeft    - 80;
        
        jQuery( "#reader-actions" ).css({
            
            "position"          : "fixed",
            "top"               : actionsTop    + "px",
            "left"              : actionsLeft   + "px",    
            "background"        : "#555",
            "width"             : "80px",        
            "opacity"           : ".8",
            "color"             : "#fff",
            "padding"           : "25px 0",
            
                "-moz-border-radius-topleft" : "20px",
                "-moz-border-radius-bottomleft" : "20px",
                "-webkit-border-topleft-radius": "20px",
                "-webkit-border-bottomleft-radius": "20px",
                "border-top-left-radius": "20px",
                "border-bottom-left-radius": "20px",			
    			"-moz-user-select" : "none",
    			"-webkit-user-select" : "none",
    			"user-select" : "none"
        });
        
        createZoomInAction();
        createZoomOutAction();
        createCloseAction();
        
        setActionStyles();
       
    }

    function destroyActions(){    
        jQuery( "#reader-actions" ).remove();    
    }
    
    function destroyWindow(){
        jQuery( "#reader-window-block" ).remove(); 
    }
    
    function destroyShadow(){
        jQuery( "#reader-shadow-block" ).animate({opacity: .1}, 1000, function(){
        	$ (this).remove();
        	$ ("object").css({
                "display" : "block"
            });
        }); 
        
    }
    
    function createZoomInAction(){
        
        var html = '<span id="reader-action-zoomin">+</span>';  
          
        jQuery( "#reader-actions" ).append(html);
        
        jQuery( "#reader-action-zoomin" ).bind( "click", function(){
            
            currentFontSize = jQuery( "#reader-window-block" ).css( "font-size" );
    
            if ( parseInt( currentFontSize ) < 28 ){
                
                jQuery("#reader-window-block").css({
                    "font-size" : ( parseInt( currentFontSize ) + 1) + "px"
                });
                
            }
        });
    }

    function createZoomOutAction(){
        
        var html = '<span id="reader-action-zoomout">тАУ</span>';
         
        jQuery( "#reader-actions" ).append(html);
        
        jQuery( "#reader-action-zoomout" ).bind( "click", function(){
            
            currentFontSize = jQuery( "#reader-window-block" ).css( "font-size" );    
                
            if ( parseInt(currentFontSize) > 12 ){
                
                jQuery( "#reader-window-block" ).css({
                    "font-size" : ( parseInt( currentFontSize ) - 1) + "px"
                });
                
            }
        });
    }

    function createCloseAction(){
        
        var html = '<span id="reader-action-close">x</span>';
         
        jQuery( "#reader-actions" ).append( html );
        
        jQuery( "#reader-shadow-block" ).bind( "click", function(){
            
            destroyWindow();
            destroyShadow();
            destroyActions();
            
            jQuery( "body" ).css( "overflow", "auto" );
       });
        
        jQuery( "#reader-action-close" ).bind( "click", function(){
            
             destroyWindow();
             destroyShadow();
             destroyActions();
             
             jQuery( "body" ).css( "overflow", "auto" );
        });
        
    }
    
    function setActionStyles(){
        
        var styles = {
            "font-family"   : "Arial",
            "font-size"     : "70px",
            "font-weight"   : "bold",
            "display"       : "block",
            "text-align"    : "center",        
            "cursor"        : "pointer",
        };
        
        jQuery( "#reader-action-zoomin" ).css(styles);
        jQuery( "#reader-action-zoomout" ).css(styles);
        jQuery( "#reader-action-close" ).css(styles);
        
    }
    
    
}
