/**
 * Benötigte CSS Datei: rimondo_overlay.css
 * Aufruf Funktion:
 *      - rimondo_overlay.show( 'dein HTML Code' ); // Öffnet das Overlay mit dem übergebenen HTML Code
 *      - rimondo_overlay.hide( ); // versteckt das Overlay wieder
 *      - rimondo_overlay.reposition( ); // Positioniert das overlay neu, wird automatisch gemacht!
 *      - rimondo_overlay.resize( pixelbreite ); // setzt die Breite für das Overlay fest, breite ist immer notwendig!
 * Eventhandling:
 *      - rimondo_overlay.add_event( 'onshow', function(){ code } ); // Beim anzeigen
 *      - rimondo_overlay.add_event( 'onhide', function(){ code } ); // Beim wieder beenden / unsichtbar machen
 * @author: Steffen Grabaum
 */

var rimondo_overlay = new function () {
    this.events = new Array();
    this.jdom_distance = null;
    this.jdom_bg = null;
    this.jdom_container = null;
    this.jdom_content = null;
    var global = this;
    
    this.add_event = function( evt_type, fn ) {
        global.events.push( { 'event_type': evt_type, 'fn': fn } );
    }

    this.raise_events = function( evt_type ) {
        $.each( global.events, function( idx, obj ) {
            if ( evt_type == obj.event_type && obj.fn ) {
                obj.fn();
            }
        } );
    }

    this.delete_event = function(evt_type) {
        $.each( global.events, function( idx, obj ) {
            if ( evt_type == obj.event_type ) {
                obj.fn = "";
            }
        } );
    }

    this.reposition = function(){
        global.jdom_container.css('margin-top', $(document).scrollTop() + ( ($(window).height() / 2) - (global.jdom_container.height() / 2) ) );
        global.jdom_bg.height($(document).height());
    }

    this._prepare = function(contenthtml) {
        global.jdom_content.html( contenthtml );
    }

    this._showPreapared = function() {
        global.reposition();
        $(".rimondo_overlay_container").css('visibility', 'hidden').hide();
        $(".rimondo_overlay").fadeIn('fast', function(){
            $(".rimondo_overlay_container").css('visibility', 'visible');
            global.reposition();

            $(".rimondo_overlay_container").fadeIn('fast', function(){
                global.raise_events('onshow');
            } );
        });
        global.reposition();
    }

    this.show = function( contenthtml, after_fade, before_fade ){
        global.init();
        global.jdom_content.html( contenthtml );
        if ( before_fade && typeof before_fade == 'function' ) {
            before_fade();
        }
        global.raise_events('onbeforeshow');
        global.reposition();
        $(".rimondo_overlay_container").css('visibility', 'hidden').hide();
        $(".rimondo_overlay").fadeIn('fast', function(){
            $(".rimondo_overlay_container").css('visibility', 'visible');
            global.reposition();

            $(".rimondo_overlay_container").fadeIn('fast', function(){
                if ( after_fade && typeof after_fade == 'function' ) {
                    after_fade();
                }
                global.raise_events('onshow');
            } );
        });
        global.reposition();
    }
    
    this.showConfirmDialog = function( sText, sLabelButtonYes, sLabelButtonNo, fn_yes, fn_no, sPathPicYes, $sPathPicNo ){
    	if ( sPathPicYes == null ) {
    		sPathPicYes = "/static/layout/button/rimondo_button_white.png";
    	}
    	
    	if ( $sPathPicNo == null ) {
    		$sPathPicNo = "/static/layout/button/button_zuruecksetzen_white.png";
    	}
    	
    	var sYesButton = '<button name="confirm_yes" id="confirm_yes" type="button" style="margin-right: 10px;"><span style="background: url(&quot;' + sPathPicYes + '&quot;) no-repeat scroll right 0px transparent;"><em style="background: url(&quot;' + sPathPicYes + '&quot;) no-repeat scroll left 0px transparent;">' + sLabelButtonYes + '</em></span></button>';
    	var sNoButton = '<button name="confirm_no" id="confirm_no" type="button"><span style="background: url(&quot;' + $sPathPicNo + '&quot;) no-repeat scroll right 0px transparent;"><em style="background: url(&quot;' + $sPathPicNo + '&quot;) no-repeat scroll left 0px transparent;">' +  sLabelButtonNo + '</em></span></button>';
    	
    	var sContent = '<div class="content_confirm_box"><div style="margin-bottom: 10px;">' + sText + '</div><div class="confirm">' + sYesButton + sNoButton + '</div></div>';
    	
    	if ( typeof( fn_yes ) == 'function' ) {
    		$("#confirm_yes").live('click', fn_yes);
    	}
    	if ( typeof( fn_no ) == 'function' ) {
    		$("#confirm_no").live('click', fn_no);
    	}
        $('.rimondo_overlay_close').hide();
    	global.show( sContent );
    }

    this.hide = function( fn ){
        $(".rimondo_overlay").fadeOut('fast', function(){
            global.raise_events('onhide');
            $('.rimondo_overlay_close').show();
            if ( fn && typeof fn == 'function' ) {
                fn();
            }
        });
    }

    this.resize = function( new_width ){
        global.init();
        $(".rimondo_overlay .rimondo_overlay_container").width( new_width );
    }

    this.init = function(){
        if ( rimondo_overlay.jdom_container === null ) {
            $('body').append('<div class="rimondo_overlay">    <div class="rimondo_overlay_trans">    </div>    <div class="rimondo_overlay_distance">    </div><div class="clear"></div>    <div class="rimondo_overlay_container">        <div class="rimondo_overlay_border pos_left">        </div>        <div class="rimondo_overlay_border pos_right">        </div>        <div class="rimondo_overlay_border pos_top">        </div>        <div class="rimondo_overlay_border pos_bottom">        </div>        <div class="rimondo_overlay_border pos_left wline">        </div>        <div class="rimondo_overlay_border pos_right wline">        </div>        <div class="rimondo_overlay_border pos_top wline">        </div>        <div class="rimondo_overlay_border pos_bottom wline">        </div>        <div class="rimondo_overlay_corner pos_top pos_left">        </div>        <div class="rimondo_overlay_corner pos_top pos_right corner_right_top">        </div>        <div class="rimondo_overlay_corner pos_bottom pos_left">        </div>        <div class="rimondo_overlay_corner pos_bottom pos_right">        </div>        <div class="rimondo_overlay_content">        </div>        <div class="rimondo_overlay_close">        </div>    </div></div>');
            rimondo_overlay.jdom_distance = $(".rimondo_overlay .rimondo_overlay_distance");
            rimondo_overlay.jdom_container = $(".rimondo_overlay .rimondo_overlay_container");
            rimondo_overlay.jdom_content = $(".rimondo_overlay .rimondo_overlay_content");
            rimondo_overlay.jdom_bg = $(".rimondo_overlay .rimondo_overlay_trans");
            $(window).scroll( rimondo_overlay.reposition );
            $(window).resize( rimondo_overlay.reposition );
            $(".rimondo_overlay_close").click( rimondo_overlay.hide ).attr('title', txt_close);
        }
    }
}


$(document).ready(function(){
    rimondo_overlay.init();
});




