(function($){
        $.urlParam = function(name){
            try{
                var urlParams = {};
                var e,
                    a = /\+/g,  // Regex for replacing addition symbol with a space
                    r = /([^#])*#(.+)/g,
                    d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
                    q = window.location.search.substring(1);
                var loc = window.location.href.replace(/http(s)?:\/\//, '');
                loc = loc.substring(loc.indexOf('/') + 1);
                return r.exec(loc)[2];
            }catch(ex){
                return null;
            }

        }
	$.fn.loginbox = function(options){
		var defaults = {
			animate : true,
                        className : "lbox"
		};
		var options = $.extend(defaults, options);

                var url_params = $.urlParam();
                if(url_params=="/my/login/index" || url_params=="/my/login"){
                    init(null, url_params);
                }

		return this.each(function(){
			$(this).bind('click', init);
		});
		function init(ev, forceurl){
                        if(ev!=null)
                            ev.preventDefault();
                        try{
                            var closeBut = '<a class="LBox_but_close LBox_close">x</a>';
                            if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
                                $("body","html").css({height: "100%", width: "100%"});
                                $("html").css("overflow","hidden");
                                if (document.getElementById("LBox_HideSelect") === null) {//iframe to hide select elements in ie6
                                        $("body").append("<iframe id='LBox_HideSelect'></iframe><div id='LB_overlay'></div><div id='TB_window'></div>");
                                        $("#LBox_overlay").click(lbox_remove);
                                }
                            }else{//all others
                                    if(document.getElementById("LBox_overlay") == null){
                                            $("body").append("<div id='LBox_overlay'></div><div id='LBox_window'><div id='LBox_title'>"+closeBut+"</div><div id='LBox_content'></div></div>");
                                            $("#LBox_overlay").click(lbox_remove);
                                    }
                            }
                            $(".LBox_close").click(lbox_remove);
                            $("#LBox_overlay").addClass("LBox_overlayBG");//use background and opacity

                            var spinner = "<center><div class='spinner'></div></center>";
                           // if(foreceurl=="undefined"){
                                if($(this).attr("href")){
                                    baseURL =$(this).attr("href").split("#")[1];
                                    title = $(this).attr("title") || "Login box";
                                    method = "GET";
                                    params = [];
                                }else if($(this).attr("type")=="button"){
                                    baseURL = $(this).parents("form").attr("action").split("#")[1];
                                    title = $(this).attr("title") || "Login box";
                                    method = $(this).parents("form").attr("method");
                                    params = $(this).parents("form").serialize();
                                }else if(forceurl!=null){
                                    title = "Your Account";
                                    baseURL = forceurl;
                                    method = "GET";
                                    params = [];
                                }
                            $("#LBox_window #LBox_title").html(closeBut + title);

                            $.ajax({
                                  type: method,
                                  url: baseURL,
                                  data: params,
                                  success: function(data) {
                                     $("#LBox_window #LBox_content").html(data);
                                     $("#LBox_window").slideDown(500);
                                     $("#LBox_window .LBox_close").click(lbox_remove);

                                     $("#LBox_window #LBox_content .lbox").bind('click', init);

                                  },
                                  beforeSend: function(){
                                        $("#LBox_window #LBox_content").html(spinner);
                                  }
                            });
                        }catch(e){
                        }
		};
                function lbox_remove(){
                        $("#LBox_window").slideUp(500,function(){$('#LBox_window,#LBox_overlay,LBox_HideSelect').trigger("unload").unbind().remove();});
                        $("#LBox_load").remove();
                        if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
                                $("body","html").css({height: "auto", width: "auto"});
                                $("html").css("overflow","");
                        }
                        document.onkeydown = "";
                        document.onkeyup = "";
                        return false;

                }
	}
})(jQuery);
