(function($) {
    $.fn.yallaWhiteBox = function(tpage) {
        if (tpage.length < 1) {
            var tpage = '/ajax.php';
        }
        $('a[rel*=yallaWhiteBox]').unbind('click').click(function() {
            try {
                var winHeight = $(this).attr('rel').match(/height:([0-9]+)/)[1];
            } catch (err) {
                winHeight = 400;
            }
            
            try {
                var aType = $(this).attr('rel').match(/type:([a-z]+)/)[1];
            } catch (err) {
                aType = 'html';
            }
            
            try {
                var aCallback = eval($(this).attr('rel').match(/callback:([\$\.\(\)a-zA-Z]+)/)[1]);
            } catch (err) {
                aCallback = '';
            }
            
            try {
                var remember = eval($(this).attr('rel').match(/remember:([a-z]+)/)[1]);
            } catch (err) {
                remember = false;
            }
            
            try {
                $().yallaWhiteBox.open({
                    page: tpage, 
                    params: $(this).attr('href'),
                    height: parseInt(winHeight),
                    type: aType,
                    callback: aCallback,
                    remember: remember
                });
            } catch (err) {
                alert (err);
            }
            
            return false;
        });
        
        $(document).keyup(function(event) {
            if (event.keyCode == 27) {
                if ($('#yalla-white-box').length > 0) {
                    $().yallaWhiteBox.close();
                }
            }
        });
    };
    $.fn.yallaWhiteBox.open = function(opt) {
        $.fn.yallaWhiteBox.defaults = jQuery.extend({
            page: '',
            params: 'action=0',
            type: 'html',
            callback: '',
            height: 400,
            remember: false
        },opt);
        
        if ($.fn.yallaWhiteBox.defaults.remember) {
            window.location.hash = '#'+$.fn.yallaWhiteBox.defaults.params;
        }
        
        if ($('#yalla-white-box').length < 1 && $.fn.yallaWhiteBox.defaults.type != 'script') {
            var boxBG = $(jQuery('<div id="yalla-white-box-bg" class="yalla-white-box-bg"><div class="yalla-white-box-space">&nbsp;</div></div>').css({opacity: '0.7', height: ($.fn.yallaWhiteBox.defaults.height+20), top: ($(document).scrollTop()+100)+"px"}).hide());
            var box = $(jQuery('<div id="yalla-white-box" class="yalla-white-box">&nbsp;</div>').css({height: $.fn.yallaWhiteBox.defaults.height, top: ($(document).scrollTop()+111)+"px"}).hide());
            var boxLoader = $(jQuery('<div id="yalla-white-box-loader" class="yalla-white-box yalla-white-box-loader">&nbsp;</div>').css({opacity: '0.7', height: $.fn.yallaWhiteBox.defaults.height, top: ($(document).scrollTop()+111)+"px"}).hide());
            $('body').append(boxBG).append(box).append(boxLoader);
            $('#yalla-white-box-bg').fadeIn(400);
            $('#yalla-white-box').fadeIn(400,function () {
                $('#yalla-white-box-loader').show();
                $.post($.fn.yallaWhiteBox.defaults.page,'params='+encodeURIComponent($.fn.yallaWhiteBox.defaults.params),function(data) {
                    $('#yalla-white-box-loader').hide();
                    if ($.fn.yallaWhiteBox.defaults.type != 'script') {
                        $('#yalla-white-box').html(data);
                    }
                    if ($.fn.yallaWhiteBox.defaults.callback != '') {
                        $.fn.yallaWhiteBox.defaults.callback();
                    }
                },$.fn.yallaWhiteBox.defaults.type);
            });
        } else {
            $('#yalla-white-box-loader').show();
            if ($('#yalla-white-box').length < 1) {
                $('#network-ajax-loader').show();
            }
            
            if ($.fn.yallaWhiteBox.defaults.type == 'script') {
                $.post($.fn.yallaWhiteBox.defaults.page,'params='+encodeURIComponent($.fn.yallaWhiteBox.defaults.params),function(data) {
                    //$('#yalla-white-box-loader').hide();
                    $('#network-ajax-loader').hide();
                },'script');
            } else {
                $.post($.fn.yallaWhiteBox.defaults.page,'params='+encodeURIComponent($.fn.yallaWhiteBox.defaults.params),function(data) {
                    $('#yalla-white-box').html(data);
                    if ($.fn.yallaWhiteBox.defaults.callback != '') {
                        $.fn.yallaWhiteBox.defaults.callback();
                    }
                    $('#yalla-white-box-loader').hide();
                    $('#network-ajax-loader').hide();
                },'html');
            }
        }
        
        $.fn.yallaWhiteBox.close = function () {
            $('#yalla-white-box').fadeOut(400);
            $('#yalla-white-box-loader').fadeOut(400);
            $('#yalla-white-box-bg').fadeOut(400,function() {
                $('#yalla-white-box').remove();
                $('#yalla-white-box-bg').remove();
                $('#yalla-white-box-loader').remove();
            });
        };
    };
})(jQuery);