window.i3.popup.displayLoader = function(message, options)
{
    if (!options)
        options = {};
    
    return i3.popup.displayPopup({
        'message': "<div class='loaderMessage'><img src='/_ui/media/johncrane_media/ajax-loader.gif'> <p>"+(message ? message : "Please wait... adding to cart")+"</p></div>",
        'title': false,
        'class': "loader",
        'dialogClass': 'loadingPopup',
        options: options
    });
}
window.i3.popup.displayAdvancedPopup = function(url, params, options, callback)
{
     if (!params)
        params = {};

     if (!options)
        options = {};
        
     if (options.loader)
        i3.popup.displayLoader();
        
     if(!options.data_type)
        options.data_type = 'html';        

     if (options.onbeforeopen)
         options.onbeforeopen();
         
     if (options.open)
        options.onopen = options.open;
        
    if (options.close)
        options.onclose = options.close;
        
    if (options.onbeforeclose && typeof(options.onbeforeclose) != 'function')
        options.onbeforeclose = i3.std.string.toFunction(options.onbeforeclose);
        
    if (options.onclose && typeof(options.onclose) != 'function')
        options.onclose = i3.std.string.toFunction(options.onclose);
        
    if (options.onopen && typeof(options.onopen) != 'function')
        options.onopen = i3.std.string.toFunction(options.onopen);
        
    if (options.onaftersubmit && typeof(option.onaftersubmit) != 'function')
        options.onaftersubmit = i3.std.string.toFunction(options.onaftersubmit);
        
    if (i3.popup.url[url])
        return false;
        
    i3.popup.url[url] = true;
        
     params.popup = true;  
     $.ajax({
         url: url,
         type: 'GET',
         dataType: options.data_type,
         data: i3.std.array.httpBuildQuery(params),
         success: function(response)
         {
             i3.popup.url[url] = false;
             
             if (options.single)
                 while (i3.popup.array.length > 0)
                    $(i3.popup.array.pop()).dialog('close');
             
             if ($('.popup.loader').length > 0)
                $('.popup.loader').dialog('destroy');
             
             var html = null;
             var scripts = null;
             response = i3.std.string.stripW(response);
             
             if(options.data_type == 'json')
                html = response.html;
             else
             {
                 if (response.length > 0)
                     switch(response)
                     {
                         case 'permission_denied': 
                         case 'session_timeout':
                         default: 
                            scripts = $(response).filter("script");
                            response = $("<jQfix/>"+response+"<jQfix/>").filter(":not(script, jQfix)");
                            
                            scripts = $(scripts).add($(response).find('script').remove());
                            
                            html = $('<div/>').html(response);
                     }
                 else
                    html = $('<div/>').html("No response");
             }
                
             var id = options.id ? options.id : "popup-"+i3.popup.iterator;
             var title = i3.std.def(options.title, i3.popup.defaults.title) || $(html).find('h1:first, h2:first').remove().html() || '';
             
             // fix for IE action on form bug
             var action = [];
             $(html).find("form").each(function()
             {
                 action.push($(this).attr('action'));
                 $(this).removeAttr('action');
             })
             
             var popup = i3.popup.array[i3.popup.iterator] = $("<div id='"+id+"' class='popup"+(options['class'] ? " "+options['class'] : "")+"'>"+$(html).html()+"</div>'").dialog({
                autoOpen: true,
                show: 'drop',
                closeText: 'x',
                hide: 'drop',
                modal: i3.std.def(options.modal, true),
                title:  options.title == false || options.title == 'false' || title.length == 0 ? null : title,
                resizable: options.title == false || options.title == "false" || title.length == 0 ? false : true,
                width:  i3.std.def(options.w, options.width, 'auto'),
                height: i3.std.def(options.h, options.height, 'auto'),
                beforeclose: function()
                {
                  if (options.onbeforeclose)
                    options.onbeforeclose();
                },
                close: function()
                {
                    if ($('#'+id).dialog)
                    {
                        if (window.tinyMCE)
                        $('#'+id).find('.tinyEditor').each(function()
                        {
                            tinyMCE.execCommand('mceFocus', true, $(this).attr('id'));
                            tinyMCE.execCommand('mceRemoveControl', true, $(this).attr('id'));
                        });
                        
                        $('#'+id+' *').remove();
                        $('#'+id).dialog('destroy').remove();

                        if (options.onclose)
                            options.onclose();
                    }
                },
                open: function()
                {                    
                    // to make sure that the popup is open
                    window.setTimeout(function()
                    {
                        i3.std.globalInherit(id);
                        
                        $(document.body).unbind("click.i3").bind("click.i3", function(ev)
                        {
                            if ($(ev.target).parents('#'+id+", .ui-widget, [id^=fancybox]").length == 0 && !$(ev.target).is("[id^=fancybox]") && $(ev.target).parents('body').length != 0)
                                $('#'+id).dialog('close');
                        });
                        
                        $("#"+id).find("form").each(function()
                        {
                            $(this).attr('action', action.pop());
                            if (options.validate != false && options.validate != 'false')
                            {
                                $(this).append("<input type='hidden' value='true' name='popup'/>");
                                $(this).bind("submit.i3", function(ev)
                                {
                                    if ($(this).find('[name=popup]').length > 0)
                                    {
                                        ev.preventDefault();
                                        window.i3.popup.ajaxValidate(this, options.type);
                                    }
                                });
                                
                                if (options.onsubmit)
                                    $(this).data('onaftersubmit', options.onsubmit);
                            }
                        });
                        
                        if (scripts && scripts.length > 0)
                            $('#'+id).append(scripts);
                    
                        if (options.onopen)
                            options.onopen();
                    }, 1);
                    //alert($(this).parent().attr('style'));
                    if($(this).parent().css('filter')!='none'){
                    	$(this).parent().css('filter','none');
                    }
                    
                }
            });
                            
            if (options.title == false || options.title == "false" || title.length == 0)
            {
                 $('#'+id).parents('.ui-dialog').addClass('no-title');
                 $('#'+id).siblings('.ui-dialog-titlebar').css('display','none');
            }
                        
            if (options['class'])
                $(popup).parents('.ui-dialog').addClass(options['class']);
             
            if(options.callback)
                options.callback();
                
            if(callback)
                callback();
                
            i3.popup.iterator++;
         }
    });
}
