var scriptSelf = document.getElementsByTagName( 'script' );
var scriptArgs = scriptSelf[ scriptSelf.length -1 ].getAttribute( 'src' ).split( '?' ).pop();

new ( Class.create( {
	'initialize' : function() {
		if( !document ) return;
		document.observe( 'dom:loaded' , this.initializeApplication.bindAsEventListener( this ) );
	}
	, 'initializeApplication' : function() {
		if( scriptArgs.indexOf( 'loadImage=loadImage' ) !== -1 ) {
			this.loadImage();
		}
		this.messages = _LANG_COMMON;
		this.attachEvents();

		var modeSelectors = $A( document.getElementsByName( 'modeSelectCheck' ) );
		if( modeSelectors.size() > 0 ) {
			modeSelectors.each( function( E ) { if( E.checked ) this.mode = E.value; } , this );
		}
		else {
			this.mode = 'user';
		}
		this.setMode();
		$( 'loginUserName' ).focus();
	}
	, 'loadImage' : function() {
		var introCont = $( 'introCont' );
		if( !introCont ) return;

		var introImageType = ( introCont.readAttribute( 'imageType' ) || 'I' );
		try {
			if( introImageType == 'I' ) {
				if( Prototype.Browser.IE ) {
					var introImage = new Image();
					introImage.onload = function() {
						var cont = document.getElementById( 'introCont' );
						var type = cont.getAttribute( 'type' );
						var wLimit = -1;
						var hLimit = -1;
						switch( type ) {
							case 'A' : wLimit = 651; break;
							case 'B' : hLimit = 240; break;
							case 'C' : wLimit = 492; break;
						}
						if( wLimit > 0 && introImage.width > wLimit ) {
							var nY = parseInt( Math.floor( ( wLimit * introImage.height ) / introImage.width ) );
							introImage.width = wLimit;
							introImage.height = nY;
						}
						if( hLimit > 0 && introImage.height > hLimit ) {
							var nX = parseInt( Math.floor( ( hLimit * introImage.width ) / introImage.height ) );
							introImage.height = hLimit;
							introImage.width = nX;
						}
						cont.innerHTML = '';
						cont.appendChild( introImage );
					};
					introImage.src = '/images/?type=intro';
				}
				else {
					var introImage = new Element( 'img' , { 'id' : 'introImage' , 'src' : '/images/?type=intro' , 'alt' : 'Intro Image' } )
						.observe( 'load' , function( ev ) {
							var cont = $( 'introCont' );
							var img = Event.element( ev );
							var type = cont.readAttribute( 'type' );
							var wLimit = -1;
							var hLimit = -1;

							switch( type ) {
								case 'A' : wLimit = 651; break;
								case 'B' : hLimit = 240; break;
								case 'C' : wLimit = 492; break;
							}

							if( wLimit > 0 && img.width > wLimit ) {
								var nY = parseInt( Math.floor( ( wLimit * img.height ) / img.width ) );
								img.writeAttribute( { 'width' : wLimit , 'height' : nY } );
							}
							if( hLimit > 0 && img.height > hLimit ) {
								var nX = parseInt( Math.floor( ( hLimit * img.width ) / img.height ) );
								img.writeAttribute( { 'width' : nX , 'height' : hLimit } );
							}
							cont.update( img );
						} );
				}
			}
			else {
				var type = introCont.readAttribute( 'type' );
				var width = 0 , height = 0;
				switch( type ) {
					case 'A' : width = 651; height = 199; break;
					case 'B' : width = 360; height = 231; break;
					case 'C' : width = 515; height = 157; break;
				}
				if( Prototype.Browser.IE ) {
					introCont.update( '<object'
						+ ' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="' + width + '" height="' + height + '">'
						+ '<param name="movie" value="/images/?type=intro">'
						+ '</object>' );
				}
				else {
					introCont
						.update( new Element( 'embed'
							, { 'id' : 'logoMovie' , 'src' : '/images/?type=intro' , 'width' : width + 'px' , 'height' : height + 'px'  } ) );
				}
			}
		} catch( err ) {
			throw 'Intro Loading Error';
		}
	}
	, 'attachEvents' : function() {
		$$( '#languageSelect .langChangeButton' ).each( function( Btn ) {
			Btn.observe( 'click' , function( ev ) {
				if( !ev || !ev.element ) return;
				this.setCookie(
					'webmailLoginLang'
					, ev.element().readAttribute( 'lang' )
					, new Date( ( new Date ).getTime() + 60*60*24*30 ) // 30 day
				);
				location.reload( true );
			}.bindAsEventListener( this ) );
		} , this );

		var modeUser = $( 'loginModeSelectUser' );
		var modeAdmin = $( 'loginModeSelectAdmin' );
		var modeUserLabel = $( 'loginModeSelectUserLabel' );
		var modeAdminLabel = $( 'loginModeSelectAdminLabel' );

		if( modeUser ) modeUser.observe( 'click' , this.setMode.bindAsEventListener( this , 'user' ) );
		if( modeAdmin ) modeAdmin.observe( 'click' , this.setMode.bindAsEventListener( this , 'admin' ) );

		$( 'loginBox' ).observe( 'submit' , this.submitForm.bindAsEventListener( this ) );

		if( Prototype.Browser.IE ) {
			if( modeUserLabel ) modeUserLabel.observe( 'click' , this.setMode.bindAsEventListener( this , 'user' ) );
			if( modeAdminLabel ) modeAdminLabel.observe( 'click' , this.setMode.bindAsEventListener( this , 'admin' ) );
		}
	}
	, 'submitForm' : function( ev ) {
		if( typeof ev == 'object' ) {
			ev.stop();
		}

		var F = $( 'loginBox' );
		var emailId = $( 'loginUserName' );
		var passwd = $( 'loginUserPassword' );
		var host = $( 'loginUserHost' ).value;

		if( !emailId.value ) return emailId.focus( alert( this.messages[ 'NO_ID_INPUT' ] ) );
		if( !passwd.value ) return passwd.focus( alert( this.messages[ 'NO_PASSWD_INPUT' ] ) );

		if( this.mode == 'user' ) {
			$( 'emailAddress' ).value = emailId.value + host;
		}
		else {
			$( 'emailAddress' ).value = emailId.value;
			if( $( 'saveEmail' ).checked ) this.setCookie( 'webmailAdminId' , $( 'emailAddress' ).value );
		}

		var cK = this.mode == 'user' ? 'webmailEmailId' : 'webmailAdminId';
		var cV = '' , expire = null;
		if( $( 'saveEmail' ).checked ) {
			( expire = new Date ).setFullYear( expire.getFullYear() + 10 );
			cV = $( 'emailAddress' ).value;
		}
		else {
			expire = new Date;
			cV = '';
		}
		this.setCookie( cK , cV , expire );

		F.submit();
	}
	, 'setMode' : function( ev , mode ) {
		var modeChanged = false;
		if( mode ) {
			if( this.mode && ( this.mode != mode ) ) modeChanged = true;
			this.mode = [ 'user' , 'admin' ].indexOf( mode ) !== -1 ? mode : 'user';
		}

		var modeUserLabel = $( 'loginModeSelectUserLabel' );
		var modeAdminLabel = $( 'loginModeSelectAdminLabel' );
		if( !modeAdminLabel ) this.mode = 'user';

		var email = $( 'emailAddress' );
		if( !email.value || modeChanged ) {
			var stored = '';
			if( this.mode == 'user' ) {
				stored = this.getCookie( 'webmailEmailId' );
				if( stored ) {
					email.value = stored;
					if( stored.indexOf( '@' ) < 0 ) stored += $( 'loginUserHost' ).value;
				}
			}
			else {
				stored = this.getCookie( 'webmailAdminId' );
				email.value = stored;
			}
			$( 'saveEmail' ).checked = stored ? true : false;
		}

		$( 'loginUserName' ).value = $( 'emailAddress' ).value.split( '@' ).shift();
		$( 'loginUserNameLabel' ).update( this.mode == 'user' ? this.messages[ 'EMAIL_ADDR' ] : this.messages[ 'EMAIL_ID' ] );
		$( 'loginUserHost' )[ this.mode == 'user' ? 'show' : 'hide' ]();
		$( 'saveEmailLabel' ).update( this.mode == 'user' ? this.messages[ 'SAVE_EMAIL_ADDR' ] : this.messages[ 'SAVE_EMAIL_ID' ] );

		// tab control
		if( modeUserLabel ) modeUserLabel
			.removeClassName( this.mode == 'admin' ? 'off' : 'on' )
			.addClassName( this.mode == 'admin' ? 'on' : 'off' );
		if( modeAdminLabel ) modeAdminLabel
			.removeClassName( this.mode == 'user' ? 'off' : 'on' )
			.addClassName( this.mode == 'user' ? 'on' : 'off' );

		$A( document.getElementsByName( 'modeSelectCheck' ) ).each( function( C ) {
			C.checked = ( this.mode == C.value ? true : false );
		} , this );

		// radio
		if( this.mode == 'user' ) $( 'loginModeSelectUser' ).checked = true;
		if( this.mode == 'admin' ) $( 'loginModeSelectAdmin' ).checked = true;

		// form variable
		$( 'loginType' ).value = this.mode;
	}
	, 'setCookie' : function( key , value , expire ) {
		document.cookie = key + "=" + escape( value ) + ( expire ? '; expires=' + expire.toGMTString() : '' );
	}

	, 'getCookie' : function( key ) {
		var start = 0, end = 0;

		start = document.cookie.indexOf( '; ' + key + '=' );
		if( start == -1 ) {
			if( ( start = document.cookie.indexOf( key + '=' ) ) == -1 ) return '';
			start = key.length +1;
		}
		else {
			start = start +2 + key.length +1;
		}
		end = document.cookie.indexOf( ';' , start );
		if( end == -1 ) end = document.cookie.length;

		return unescape( document.cookie.substring( start , end ) );
	}
} ) );