/*
 * logiforms V3.0 
 * Copyright(c) 2009-2010, logiforms.com Inc.
 * sales@logiforms.com
 * http://www.logiforms.com/copyright/
 * */ 

/**
 * @class logiFormProxy
 * Auto Iframe Resizing and scrolling script
 */ 
logiFormProxy = function(config) {
	this.getDefaults();
	for (var i in this.defaultConfig){
		this[i] = (config[i])?config[i]:this.defaultConfig[i];
	}
	this.lastiframeMsg = '';
	this.win = window;
	this.init();
}; 

logiFormProxy.prototype = {
	
	init: function(){
    	this.setFormPath();
    	this.writeIframe();
    	if (window.postMessage){
    		this.listen4PostMessage();
    	}
    }
    ,
    
    listen4PostMessage: function(){
    	var delegateFn = this.wrapMethod(this.postMessageHandler, this);
    	this.addListener(
    		this.win, 
    		'message', 
    		delegateFn
    	);
    }
    ,
    
    postMessageHandler: function(e) {
		if (e.origin == this.rootformurl.substr(0,this.rootformurl.length-1)) {
			this.handleEvent(e.data);
		}
	},

    
    getDefaults: function(){
    	this.defaultConfig={
    		fid 			: 0,
    		eid				: 0,
    		eid2			: 0,
    		hid				: 0,
    		resizeOnDemand	: true,
    		height			: 500,
    		width			: '100%',
    		rootformurl		: '',
    		scrolling		: 'no',
    		marginWidth		: 0,
    		marginHeight	: 0,
    		frameborder		: 0,
    		hspace			: 2,
    		vspace			: 2,
    		mode			: 'standard',
    		params			: ''
    	}
    }
    ,

    setFormPath: function(){
    	switch(this.mode){
    		
	    	case 'standard':
	    		var queryString = (typeof(lfquerystring) == 'string' && lfquerystring.length > 0)? 
	    		unescape(lfquerystring.replace(/^[^\?]+\??/,'')): 
	    		unescape(location.search.replace(/^[^\?]+\??/,''));
	    		//var baseurl  = (window.location.href.indexOf('?') < 0)?
	    		//window.location.href:
	    		//window.location.href.substring(0,window.location.href.indexOf('?'));
	    		var appendchar = (queryString.indexOf('?') >= 0) ? '&':'?';
	    		var queryString = queryString + appendchar + 'lflocationname='+window.location.href.replace(/&/g,'%26').replace(/=/g,'%3D');
	    		if (this.params && this.params!=''){
	    			queryString += '&' + this.params;
	    		}

	    		this.formpath = this.rootformurl + 'formdata/user_forms/'+this.fid +'_'+this.eid+this.eid2+'/'+this.hid+'/'+queryString;

	    	break;
	    	
	    	case 'respondentupdate':
	    		this.formpath = this.rootformurl + 'external_requesthandler.lf?fid='+this.fid +'&hid='+this.hid+'&formtype=litelogin&requestmode=prompt'+queryString;
	        break;
    	
    	}
    	
    }
    ,
    
    writeIframe: function(){
    	document.write(this.getIframeSrc());
    	if (!window.postMessage){
    		this.listen();
    	};
    }
    ,
    
    listen: function(){
    	var self = this;
    	setInterval(function(){
    		if(location.hash != self.lastiframeMsg){
    			self.lastiframeMsg = location.hash;
    			var n = self.lastiframeMsg.replace('#','');
    			self.handleEvent(n)
    			}
    	}, 200);
    }
    ,
    
    handleEvent: function(data){
    	data = this.parseEventData(data);
    	if (data.height){
    		this.doResize(data.height)
    	}
    	if (data.scrolltop){
    		this.scrollTop();
    	}	
    }
    ,
    
    parseEventData: function(e){
    	try{
    	eval('var r = '+e);
    	}catch(er){
    		var r = {};
    	}
    	return r;
    }
    ,
    
	scrollTop: function(){
		window.scroll(0,0);
	}
	,
	
	doResize:function(height) {
		if(this.resizeOnDemand && height !== undefined){
			document.getElementById('logiform-frame-'+this.hid).height = height
		}
	}
	,
    
    getIframeSrc: function(startblank){
    	var style = (this.frameborder ==0)?'style="width:'+this.width+';border:none"':'style="width:'+this.width+'"';
    	
    	var src = '<iframe id="logiform-frame-'+this.hid+'" ';
    	src+= ' height="'+this.height+'" allowTransparency="true" ';
    	if (this.width != '100%'){
    		src+= ' width="'+this.width+'"';
    	}
    	src+= ' vspace="'+this.vspace+'" hspace="'+this.hspace+'" ';
    	src+= ' marginWidth="'+this.marginWidth+'" marginHeight="'+this.marginHeight+'" ';
    	src+= ' frameborder="'+this.frameborder+'" scrolling="'+this.scrolling+'" ';
    	src+= ' ' + style + '  src="'+((startblank != true)?this.formpath:'') +'"></iframe>';
		return src;
    }
	,
    
    getIframeObj: function(){
    	return  document.getElementById('logiform-frame-'+this.hid);
    }
    ,
    
 	addListener:function( obj, type, fn ) {
		if ( obj.attachEvent ) {
		    obj["e"+type+fn] = fn;
		    obj[type+fn] = function() { obj["e"+type+fn]( window.event ) };
		    obj.attachEvent( "on"+type, obj[type+fn] );
		  } 
		  else{
		    obj.addEventListener( type, fn, false );	
		  }
	},
	
	wrapMethod :function(method, scope) {
		return function() {
			method.apply(scope,arguments);
		}
	}

}