/*** START EZBOX ***/ 
/*
 * Used some code/images from buggy FancyBox 1.2.1 Copyright (c) 2009 Janis Skarnelis
 * This code is (c) 2009 EZWebsiteMonitoring.com
*/

function checkFocus()
{
    try
    {    
        var myid = $("input.focus:last").attr("id");        
        if (myid) { document.getElementById(myid).focus(); }
    } catch (err) {}
    
    $(".aget").click(function()
    {
        $("#savingdiv").html('Loading...');
        
        $.get(this.href,{},function(data)
        {
            $("#ez_ajax object").remove(); // fix for ie8 and object weirdness
            $("#ez_ajax").css({'height':'auto'});
            $("#ez_ajax").empty().append(data);
            checkFocus();
            checkHeight();
        }
        );
    
        return false;
    });
    
    return false;
}

var oh = 0;
var t2 = "";
var doneonce = false;

function checkHeight()
{
    var ez = $("#ez_ajax");
     
    if (ez.length == 0)
    {
        t2 = setTimeout('checkHeight()',100);
        return false;
    }
    
    oh = ez.outerHeight(true);
    
    if (!doneonce)
    {
       oh += 25;
       doneonce = true; 
    }
    
    if (oh >= 600) { oh = 600; } // so it fits on small screens
    
    if ($('#ez_content').height() != oh)
    {
         $('#ez_content').css({'height':oh+'px'});
         $('#ez_outer').css({'height':oh+20+'px'}); 
    }
    
    if (oh == 600)
    {
        ez.css({'height':'600px'});
    }
    else
    {
        ez.css({'height':'auto'});
    }
   
    t2 = setTimeout('checkHeight()',100);
    
    return false;
}

var elem, opts, busy = false, loadingTimer, loadingFrame = 1;
 
$.fn.ezbox = function(settings)
{
    settings = $.extend({}, $.fn.ezbox.defaults, settings);

    function _initialize()
    {
        elem = this;
        opts = settings;

        _start();

        return false;
    };

    function _start()
    {
        if (busy) return;

        opts.itemArray      = [];
        opts.itemCurrent    = 0;
        
        var item = {href: elem.href, title: elem.title};
        opts.itemArray.push( item );

        _change_item();
    };

    function _change_item()
    {
        $("#ez_ajax object").remove(); // fix for ie8 and object weirdness
        
        $("#ez_close, #ez_title").hide();

        var href = opts.itemArray[ opts.itemCurrent ].href;

        $.get(href, function(data)
        {
            $.fn.ezbox.showLoading();
            checkHeight();  
            _set_content( '<div style="overflow-y:auto;" id="ez_ajax">' + data + '</div>', opts.frameWidth, opts.frameHeight );
         
        });
    };

    function _set_content(value, width, height)
    { 
        if (opts.overlayShow) {
            $("#ez_overlay").css('opacity', opts.overlayOpacity).show();
        }
        
        busy = true;

        $("#ez_content").css({
            'top'        : 10+'px',
            'right'        : 10+'px',
            'bottom'    : 0,
            'left'        : 10+'px',
            'width'        : 'auto',
            'height'    : 'auto'
        });

        if ($("#ez_outer").is(":visible") && width == $("#ez_outer").width() && height == $("#ez_outer").height()) {
            $("#ez_content").fadeOut("fast", function() {
                $("#ez_content").empty().append($(value)).fadeIn("fast", function() {
                    _finish();
                });
            });

            return;
        }

        var w = $.fn.ezbox.getViewport();

        var itemLeft    = (width + 36)    > w[0] ? w[2] : (w[2] + Math.round((w[0] - width - 36) / 2));
        var itemTop = w[3] + 100;
                    
        var itemOpts = {
            'left':        itemLeft,
            'top':        itemTop,
            'width':    width+'px'
        };

        if ($("#ez_outer").is(":visible"))
        {
            $("#ez_content").fadeOut("normal", function()
            {
                $("#ez_content").empty();
                $("#ez_outer").css(itemOpts);
                $("#ez_content").append($(value)).fadeIn("fast", function() {
                        _finish();
                });
            });

        } 
        else
        {
            $("#ez_content").hide().empty().append($(value)).show();
            $("#ez_outer").css(itemOpts).fadeIn("fast", function() {
                _finish();
            });
        }   
    };

    function _finish()
    {
        $("#ez_loading").hide();
          
        if (opts.hideOnContentClick)
        {
            $("#ez_wrap, #ez_overlay").bind("click", $.fn.ezbox.close);
        }

        $("#ez_close").bind("click", $.fn.ezbox.close);
        $("#ez_close").show();

        if (opts.itemArray[ opts.itemCurrent ].title !== undefined && opts.itemArray[ opts.itemCurrent ].title.length > 0)
        {
            $('#ez_title div').html(opts.itemArray[ opts.itemCurrent ].title);
            $('#ez_title').show();
        }
        
        checkFocus();

        busy = false;
    };

    return this.unbind('click').click(_initialize);
};

$.fn.ezbox.getNumeric = function(el, prop)
{
    return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0;
};

$.fn.ezbox.getPosition = function(el)
{
    var pos = el.offset();

    pos.top    += $.fn.ezbox.getNumeric(el, 'paddingTop');
    pos.top    += $.fn.ezbox.getNumeric(el, 'borderTopWidth');

    pos.left += $.fn.ezbox.getNumeric(el, 'paddingLeft');
    pos.left += $.fn.ezbox.getNumeric(el, 'borderLeftWidth');

    return pos;
};

$.fn.ezbox.getViewport = function()
{
    return [$(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop() ];
};

$.fn.ezbox.animateLoading = function()
{
    if (!$("#ez_loading").is(':visible'))
    {
        clearInterval(loadingTimer);
        return;
    }

    $("#ez_loading > div").css('top', (loadingFrame * -40) + 'px');

    loadingFrame = (loadingFrame + 1) % 12;
};

$.fn.ezbox.showLoading = function()
{
    clearInterval(loadingTimer);

    var pos = $.fn.ezbox.getViewport();

    $("#ez_loading").css({'left': ((pos[0] - 40) / 2 + pos[2]), 'top': ((pos[1] - 40) / 2 + pos[3])}).show();
    $("#ez_loading").bind('click', $.fn.ezbox.close);

    loadingTimer = setInterval($.fn.ezbox.animateLoading, 66);
};

$.fn.ezbox.close = function()
{  
    $("#ez_ajax object").remove(); // fix for ie8 and object weirdness
    
    busy = true;

    $("#ez_overlay, #ez_close, #ez_wrap").unbind();
    $("#ez_close, #ez_loading, #ez_title").hide();

    __cleanup = function()
    {
        $("#ez_overlay, #ez_outer").hide();
        
        clearTimeout(t2);
        doneonce = false;
        document.getElementById('ez_ajax').style.height = '';

        busy = false;
    };

    if ($("#ez_outer").is(":visible") !== false)
    {  
        $("#ez_outer").stop(false, true).fadeOut("fast", __cleanup);
    } 
    else
    {
        __cleanup();
    }

    return false;
};

$.fn.ezbox.build = function()
{
    var html = '';

    html += '<div id="ez_overlay"></div>';

    html += '<div id="ez_wrap">';

    html += '<div class="ez_loading" id="ez_loading"><div></div></div>';

    html += '<div id="ez_outer">';

    html += '<div id="ez_inner">';

    html += '<div id="ez_close"></div>';

    html +=  '<div id="ez_bg"><div class="ez_bg ez_bg_n"></div><div class="ez_bg ez_bg_ne"></div><div class="ez_bg ez_bg_e"></div><div class="ez_bg ez_bg_se"></div><div class="ez_bg ez_bg_s"></div><div class="ez_bg ez_bg_sw"></div><div class="ez_bg ez_bg_w"></div><div class="ez_bg ez_bg_nw"></div></div>';

    html += '<div id="ez_content"></div>';

    html +=  '<div id="ez_title"></div>';

    html += '</div>';

    html += '</div>';

    html += '</div>';
    
    $(html).appendTo("body");

    $('<table style="margin:0 auto" cellspacing="0" cellpadding="0" border="0"><tr><td class="ez_title" id="ez_title_left"></td><td class="ez_title" id="ez_title_main"><div></div></td><td class="ez_title" id="ez_title_right"></td></tr></table>').appendTo('#ez_title');
};

$.fn.ezbox.defaults = {
    frameWidth            :    425,
    frameHeight            :    355,
    overlayShow            :    false,
    overlayOpacity        :    0.1,
    hideOnContentClick    :    false,
    itemArray            :    []
};

$(document).ready(function() {
    $.fn.ezbox.build();
});
/*** END EZBOX ***/