YAHOO.namespace('istore.app');

YAHOO.istore.app.EmailFriend = function(id, config) {
	YAHOO.istore.app.EmailFriend.superclass.constructor.call(this, id, config);
}

YAHOO.lang.extend(YAHOO.istore.app.EmailFriend, YAHOO.istore.app.panel, {
	
	ajaxUrl : '/ajax/catalogue/emailfriend',
	
	ajaxConn : null,
	
	btnCancelClickHandler : function (e, args) {
		this.hide();
	},
	
	formSubmitHandler : function (e, args) {
		this.submit();
	},
	
	submitSuccessHandler : function (o) {
		
		var root = o.responseXML.documentElement;
		var status = root.getElementsByTagName('status')[0].firstChild ? root.getElementsByTagName('status')[0].firstChild.nodeValue : '';
		var opText = root.getElementsByTagName('optext')[0].firstChild ? root.getElementsByTagName('optext')[0].firstChild.nodeValue : '';
		
		// show message
		var msgId = 'emailfriend-msg-submit-' + YAHOO.extension.funclib.JSHash(opText);
		
		// try to find message in registered overlays
		var msg = YAHOO.istore.utils.Cache.find(msgId);
		
		// if msg doesn't exists
		if (!msg) {
			
			var msgType = (status == 'ok' ? 'msg' : 'err');
			
			// create message overlay
			msg = new YAHOO.istore.app.Msg(
				opText,
				{
					name : msgId,
					type : msgType,
					context : YAHOO.util.Dom.getElementsByClassName('btn-submit', 'input', this.element)[0],
					visible : false
				}
			);
			
		}
		
		msg.show();
		
		this.hidePreloader();
		
	},
	
	submitFailureHandler : function (o) {
		
		this.hidePreloader();
		
	},
	
	showPreloader : function () {
		if (this.preloader) {
			
			var me = this;
			
			YAHOO.util.Dom.setStyle(this.preloader, 'opacity', 0);
			YAHOO.util.Dom.setStyle(this.preloader, 'visibility', 'visible');
			
			var anim = new YAHOO.util.Anim(this.preloader, {opacity: {to: 1}}, 0.5);
			anim.onComplete.subscribe(function () {
				me.preloader.src = me.preloader.src; // IE bugfix
			});
			anim.animate();
			
		}
	},
	
	hidePreloader : function () {
		if (this.preloader) {
			new YAHOO.util.Anim(this.preloader, {opacity: {to: 0}}, 0.5).animate();
		}
	},
	
	submit : function () {
		
		// if call in progress
		if (this.ajaxConn && YAHOO.util.Connect.isCallInProgress(this.ajaxConn)) {
			return false;
		}
		
		// getting posted variables
		var type = document.getElementsByName('email-friend-type')[0].value;
		var email = YAHOO.util.Dom.getElementsByClassName('email', 'input', this.element)[0].value;
		
		// if email is not valid
		if (!YAHOO.istore.utils.Validator.isEmail(email)) {
			
			// show error notification
			var msgId = 'emailfriend-msg-invalidemail';
			
			// try to find message in registered overlays
			var msg = YAHOO.istore.utils.Cache.find(msgId);
			
			// if msg doesn't exists
			if (!msg) {
				// create message overlay
				msg = new YAHOO.istore.app.Msg(
					'E-mail format is incorrect',
					{
						name : msgId,
						type : 'err',
						context : YAHOO.util.Dom.getElementsByClassName('email', 'input', this.element)[0],
						visible : false
					}
				);
			}
			
			// show message
			msg.show();
			
			return false;
		}
		
		// ajax callback funtions
		var callback = {scope: this, success: this.submitSuccessHandler, failure: this.submitFailureHandler};
		
		// ajax connect
		this.ajaxConn = YAHOO.util.Connect.asyncRequest('POST', this.ajaxUrl, callback, 'emailfriend_type=' + type + '&email=' + email);
		
		this.showPreloader();
		
	},
	
	init : function (id, config) {
		
		// call superclass (YAHOO.istore.app.panel) constructor 
		YAHOO.istore.app.EmailFriend.superclass.init.call(this, id, config);
		
		// attach onClick listener to the cancel button
		YAHOO.util.Event.addListener(
			YAHOO.util.Dom.getElementsByClassName('btn-cancel', 'input', this.element)[0],
			'click', this.btnCancelClickHandler, [], this
		);
		
		// attach onSubmit listener to the form
		YAHOO.util.Event.addListener(
			this.element.getElementsByTagName('form')[0], 
			'submit', this.formSubmitHandler, [], this
		);
		
		this.preloader = YAHOO.util.Dom.getElementsByClassName('preloader', 'img', this.element)[0];
		
	}
	
});
