var PintClientLogin = 
{
    showing: false,
    
        initForm : function() {
            var userDefault = "john@yourname.com";
            YAHOO.util.Event.addListener('username', 'focus', function(e) {
                var el = document.getElementById('loginForm');
                var oldValue = el.username.value;
                el.username.value = oldValue === userDefault ? "" :  oldValue;
            });
            YAHOO.util.Event.addListener('username', 'blur', function(e) {
                var el = document.getElementById('loginForm');
                var oldValue = el.username.value;
                el.username.value = oldValue === "" ? userDefault : oldValue;
            });

            var formValidator = new PINT.util.form.validator({
                form: "loginForm",
                errorHandler: function(e){
                    YAHOO.util.Dom.get("valid_" + e.name.toLowerCase()).className="invalidbox"; 
                },
                clearHandler: function(e){
                    YAHOO.util.Dom.get("valid_" + e.name.toLowerCase()).className="checkbox"; 
                },
                notificationHandler: function(){}
            });
            formValidator.addRule(new PINT.util.form.rule({
                "element" : "username",
                "rule" : "NOTNULL",
                "message" : "Please enter your Username"
            }));
            formValidator.addRule(new PINT.util.form.rule({
                "element" : "password",
                "rule" : "NOTNULL",
                "message" : "Please enter your Password"
            }));
            formValidator.activate();
        },

	displayClientLogin: function(e, panel)
	{
                panel.show();
                sIFR.replace(newsgothic, {
                   selector : '#clientLogin .hd h2',
                   wmode : 'transparent',
                   customHeight : 24,
                   css : {'.sIFR-root' : {'color' : '#ffffff', 'font-size' : '14px', 'margin-left' : '22px', ratios : [7,1.47,10,1.43,18,1.36,22,1.34,25,1.32,28,1.33,29,1.31,32,1.32,33,1.3,37,1.31,52,1.3,72,1.29,74,1.28,75,1.29,77,1.28,79,1.29,81,1.28,82,1.29,1.28]}}
                });
                //sIFR.replaceElement(named({sSelector:"#clientLogin .hd h2", sFlashSrc:themeRootDirectory + "/flash/newsgothic.swf", sColor:"#ffffff", sWmode:"transparent", sFlashVars:"textalign=right", sFlashVars:"textalign=right"}));
	},

	hideClientLogin: function(panel)
	{
                var anim = new YAHOO.util.Anim("clientLogin_mask", {opacity: {from: 0.65, to:0}}, 0.25);
                anim.onComplete.subscribe(function() {
                     //YAHOO.util.Dom.getElementsByClassName( "sIFR-replaced", null, "clientLogin", function(e) { e.parentNode.removeChild(e); } )
                     PintClientLogin.hidePlaceHolder('clientlogin');
                     
                     var node = document.getElementById('clientLogin_mask');
                     if(node !== null && node !== undefined) {
                         node.parentNode.removeChild(node);
                     }

                     node = document.getElementById('clientLogin_c');
                     if(node !== null && node !== undefined) {
                         node.parentNode.removeChild(node);
                     }

                     PintClientLogin.showing = false;
                     if(PintSearch !== null) { PintSearch.enableSearch(); }
                });
		anim.animate();
	},

        createLoader : function(name) {
            var div = document.createElement('div');
            div.id = name + "loader";
            var span = document.createElement('span');
            var text = document.createTextNode('loading');
            span.appendChild(text);
            div.appendChild(span);
            document.getElementById('footer_inside').appendChild(div);
        },

        createPlaceHolder : function(name, title) {
            var div = document.createElement('div');
            div.id = name + "placeholder";
            var span = document.createElement('span');
            var text = document.createTextNode(title);
            span.appendChild(text);
            div.appendChild(span);
            document.getElementById('footer_inside').appendChild(div);
        },

        removeElement : function(id) {
            var el = document.getElementById(id);
            if(el !== null) {            
                el.parentNode.removeChild(el);
            }
            return el;
        },

        showLoader : function(name) {
            PintClientLogin.createLoader(name);
            YAHOO.util.Dom.setStyle(name + 'link', 'visibility', 'hidden');
            YAHOO.util.Dom.setStyle(name + 'loader', 'visibility', 'visible');
        },

        hideLoader : function(name, text) {
            PintClientLogin.createPlaceHolder(name, text);
            YAHOO.util.Dom.setStyle(name + 'loader', 'visibility', 'hidden');
            YAHOO.util.Dom.setStyle(name + 'placeholder', 'visibility', 'visible');
            YAHOO.util.Dom.setStyle(name + 'link', 'visibility', 'hidden');
        },

        hidePlaceHolder : function(name) {
            YAHOO.util.Dom.setStyle(name + 'placeholder', 'visibility', 'hidden');
            YAHOO.util.Dom.setStyle(name + 'link', 'visibility', 'visible');
        },

	createXHR : function () {
		try { return new XMLHttpRequest (); } catch ( e ) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0");	} catch ( e ) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0");	} catch ( e ) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP");	} catch ( e ) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP");	} catch ( e ) {}

		return null;

	},

        hidePanel : function() {
            PintClientLogin.panel.hide();
        },

    hideOtherPanels : function() {
        if(PintSearch !== null && document.getElementById('search') !== null) {
            PintSearch.hidePanel();
        }
        if(PintSiteMap !== null && document.getElementById('sitemap') !== null) {
            PintSiteMap.hidePanel();
        }
    },

	handleResponse : function ( xhr, e ) {
		if ( xhr.readyState == 4 && xhr.status == 200 ) {
            PintClientLogin.hideLoader('clientlogin', 'Client Login');
		    var response = xhr.responseText;

    		document.getElementById('clientLoginHolder').innerHTML = response;

            PintClientLogin.panel = new YAHOO.widget.Panel("clientLogin", { 
                width:"506px",  
                height: "264px",
                fixedcenter: true, 
                modal: true, 
                close: true,  
                visible: false,
                effect: { effect:YAHOO.widget.ContainerEffect.FADE, duration: 0.25 }
                }
            );
           

            YAHOO.util.Event.addListener('closePanelLink', 'click', function() { PintClientLogin.panel.hide(); });
            PintClientLogin.panel.beforeHideEvent.subscribe(function(){PintClientLogin.hideClientLogin(PintClientLogin.panel);});
            PintClientLogin.panel.render ();

            PintClientLogin.initForm();

            YAHOO.util.Dom.setStyle("clientLogin", 'display', "");
            PintClientLogin.displayClientLogin (e, PintClientLogin.panel);

		}
	},

	init: function(e)
	{
        if (PintClientLogin.showing){ YAHOO.util.Event.preventDefault (e); return;}
        
        PintClientLogin.hideOtherPanels();
        
        if(PintSearch !== null) { PintSearch.disableSearch(); }

        PintClientLogin.showing = true;

		var xhr = PintClientLogin.createXHR ();

		if (xhr) {
            PintClientLogin.showLoader('clientlogin');
			xhr.open ( "POST", PINT.clientLoginPath, true );
			xhr.onreadystatechange = function () { PintClientLogin.handleResponse ( xhr, e ); };
			xhr.send ( null );
			YAHOO.util.Event.preventDefault (e);
		} else {
            return true;    // if no XHR Object, follow the link
        }

	}
};
