var flashListener = new Object();

flashListener.defaultEmailInput = "Email address";
flashListener.defaultPasswordInput = "password";
flashListener.defaultPasswordType = "text";
flashListener.actionPasswordType = "password";

flashListener.defaultText = " ";
flashListener.nullText = "";

flashListener.login = function() {
	EventBridge.dispatchEvent({type:'loginCall',data:{username: document.getElementById('email_input').value, password: document.getElementById('password_input').value}});
	
	//flash code
	//Bridge.getInstance().addBridgeListener("loginCall", loginEvent, [flash object id]);
	
    //private function loginEvent(evt:*):void {
	//	login(evt.data.username, evt.data.password);
	//}
}



flashListener.loginLoad = function() { 
	//document.getElementById('email_input').value = flashListener.defaultEmailInput;
	//document.getElementById('password_input').value = flashListener.defaultPasswordInput;
	
	//document.getElementById('password_input').onfocus = flashListener.passwordFocus;
	//document.getElementById('email_input').onfocus = flashListener.emailFocus;
	//document.getElementById('email_input').onblur = flashListener.emailBlur;
}

flashListener.emailFocus = function(evt) {
	var curObj = document.getElementById('email_input');
	if (curObj.value == flashListener.defaultEmailInput) {
		setTimeout("document.getElementById('email_input').value = flashListener.defaultText",2); 
		setTimeout("document.getElementById('email_input').select()",3); 
		setTimeout("document.getElementById('email_input').value = flashListener.nullText",4); 
	}
}

flashListener.emailBlur = function(evt) {
	var curObj = document.getElementById('email_input');
	if (curObj.value == "") {
		curObj.value = flashListener.defaultEmailInput;
	}
	else {
		EventBridge.dispatchEvent({type:'emailBlur',data:{username: document.getElementById('email_input').value, password: document.getElementById('password_input').value}});
		
		//flash code
		//Bridge.getInstance().addBridgeListener("emailBlur", emailBlurEvent, [flash object id]);
		
	    //private function emailBlurEvent(evt:*):void {
		//	if (isValidEmail(evt.data.username) {
		//		[show login button];
		//	}
		//}
	}
}

flashListener.passwordFocus = function(evt) {
	var curObj = document.getElementById('password_input');
	if (curObj.value == flashListener.defaultPasswordInput) {
		document.getElementById('password_input').value = flashListener.nullText;
		curObj = changeInputType(curObj, flashListener.actionPasswordType);
		
		setTimeout("document.getElementById('password_input').value = flashListener.defaultText",2); 
		setTimeout("document.getElementById('password_input').select()",3); 
		setTimeout("document.getElementById('password_input').value = flashListener.nullText",4); 
	}
}

flashListener.passwordBlur = function(evt) {
	var curObj = document.getElementById('password_input');
	if (curObj.value == "") {
		curObj.value = flashListener.defaultPasswordInput;
		curObj = changeInputType(curObj, flashListener.defaultPasswordType);
	}
}

function changeInputType(oldObject, oType) {
	var newObject = document.createElement('input');
	newObject.type = oType;
	if(oldObject.size) newObject.size = oldObject.size;
	if(oldObject.style.width) newObject.style.width = oldObject.style.width;
	if(oldObject.style.height) newObject.style.height = oldObject.style.height;
	if(oldObject.style.border) newObject.style.border = oldObject.style.border;
	if(oldObject.style.backgroundColor) newObject.style.backgroundColor = oldObject.style.backgroundColor;
	if(oldObject.style.fontSize) newObject.style.fontSize = oldObject.style.fontSize;
	if(oldObject.style.fontWeight) newObject.style.fontWeight = oldObject.style.fontWeight;
	if(oldObject.style.color) newObject.style.color = oldObject.style.color;
  
	if(oldObject.value) newObject.value = oldObject.value;
	if(oldObject.name) newObject.name = oldObject.name;
	if(oldObject.id) newObject.id = oldObject.id;
	if(oldObject.className) newObject.className = oldObject.className;
	oldObject.parentNode.replaceChild(newObject,oldObject);
  
	document.getElementById('password_input').onfocus = flashListener.passwordFocus; 
	document.getElementById('password_input').onblur = flashListener.passwordBlur; 
  
	return newObject;
}

flashListener.showLogin = function(evt) {
	document.getElementById('email_input').style.visibility='visible';
	document.getElementById('password_input').style.visibility='visible';
}

flashListener.getLoginInfo = function(evt) {
	EventBridge.dispatchEvent({type:'loginInfo',data:{username: document.getElementById('email_input').value, password: document.getElementById('password_input').value}});
	
	//flash code
	//Bridge.getInstance().addBridgeListener("loginInfo", loginEvent, [flash object id]);
	
    //private function loginEvent(evt:*):void {
	//	login(evt.data.username, evt.data.password);
	//}

}

EventBridge.addListener("show_login", flashListener, "showLogin");
EventBridge.addListener("get_login_info", flashListener, "getLoginInfo");

//flash code
//on load
//Bridge.getInstance().dispatchEvent(new BridgeEvent("show_login"));