/**
*  Custom plugins to call a dialogbox with an ajax content in it.
*  ------------------------------------------------
**/

(function($) {
    var $$;

     $$ = $.fn.dialogBox = function(options){
        var opts = $.extend({}, $.fn.dialogBox.defaults, options);
        // Invoke $block (with a copy of the merged html options) for each element.
        return $(this).each(function(){
            var $this = $(this);
            $(this).data("opts", opts);

            $(opts.overlay).click(function(){
                $$.close();
            });

            $(opts.closeBtn).click(function(){
                $$.close();
            });
        });
    }

    /**
    *  Prepare the dialog box
    *  ------------------------------------------------
    *  @param dialog_title      the name of the box
    *  @param dialog_address    the ajax call that need to be loaded
    **/
    $$.loadDialog = function(dialog_title, widget_address){

        $($.fn.dialogBox.defaults.body).html("");

        $$.changePosition(dialog_title);

        $$.open();
        $$.showLoading();

        $($.fn.dialogBox.defaults.title).html(dialog_title);
        $($.fn.dialogBox.defaults.body).load(widget_address, function(){
             $$.showLoading();
             $$.hideLoading();
        });
    }

    /**
    *  Append the result into the dialogbox
    *  ------------------------------------------------
    *  @param dialog_title      the name of the box
    *  @param body              the result of the ajax request
    **/
    $$.loadDialogBody = function(dialog_title, body){

       $$.changePosition(dialog_title);

        $$.showLoading();

        $($.fn.dialogBox.defaults.title).html(dialog_title);
        $($.fn.dialogBox.defaults.body).html(body);
        $$.hideLoading();
    }

    /**
    *  Show the loading for waiting
    *  ------------------------------------------------
    **/
    $$.showLoading = function(){
        $($.fn.dialogBox.defaults.loading).width($("#dialog_body").width());
        $($.fn.dialogBox.defaults.loading).fadeIn("fast");
    }

    /**
    *  Remove the loading when ajax call is finish
    *  ------------------------------------------------
    **/
    $$.hideLoading = function(){
          $($.fn.dialogBox.defaults.loading).fadeOut("slow");
    }

    /**
    *  Adapt the position of the dialogbox
    *  ------------------------------------------------
    **/
    $$.changePosition = function(dialog_title){
        var scrollY = 0;
        if(document.documentElement && document.documentElement.scrollTop ) scrollY = document.documentElement.scrollTop;
        else if(document.body && document.body.scrollTop ) scrollY = document.body.scrollTop;
        else if( window.pageYOffset ) scrollY = window.pageYOffset;
        else if( window.scrollY ) scrollY = window.scrollY;
        if(dialog_title=="Request her for a scene")
        	scrollY = scrollY + ((screen.height/2) - 247);
        else
        	scrollY = scrollY + ((screen.height/2) - 307);
        $($.fn.dialogBox.defaults.positionWrapper).css("top", (scrollY));
    }


    /**
    *  Close the dialogbox
    *  ------------------------------------------------
    **/
    $$.close = function(){
        $($.fn.dialogBox.defaults.dialogbox).css({opacity: 1, display: 'block'}).animate({opacity: 0}, function() {$(this).css({display: 'none'});});
    }

    /**
    *  Open the dialogbox
    *  ------------------------------------------------
    **/
    $$.open = function(){
        $($.fn.dialogBox.defaults.dialogbox).css({opacity: 0, display: 'block'}).animate({opacity: 1});
    }

    /**
    *  Custom config that can be overwritten onload
    *  ------------------------------------------------
    **/
    $.fn.dialogBox.defaults = {
        dialogbox   : '#dialog_container',
        positionWrapper   : '#dialog_box_container',
        closeBtn    : '#dialog_box_close',
        title       : '#dialog_title',
        body        : "#dialog_body",
        overlay     : "#site_overlay",
        loading     : ".dialog_loading"
    }

})(jQuery);

/*
jQuery(document).ready(function()
{
    jQuery("#dialog_container").dialogBox();

     jQuery(".call_dialog_box").each(function()
     {
         jQuery(this).data("dialog_url", jQuery(this).attr("href")).attr("href", 'javascript:;');
     });


     jQuery(".call_dialog_box").click(function()
     {
        var title = jQuery(this).attr("dialog_title");
        var url = jQuery(this).attr("url");

        jQuery("#dialog_container").dialogBox.loadDialog(title, url);
        return false;
     });
});
*/