/**
 * Grabs an array with widgets params , generates iframes 
 * and loads into them widgets.
 * Important : WBCheapFlightsParams and WBExploreParams are arrays.
 * This change is done in order to load multuple widgets on the same page
 * 
 * @author Konstantin Deryabin
 * @date 22 dec. 2009
 */
var WBWidgetLoader = function(){
	this.host = ( typeof( bthost ) == 'undefined' ) ? 'http://beta.travel' : bthost;
	this.baseURI = '';
	this.bloc = null ;
	this.name = 'WBWidgetFrame',
	this.wname = "" ;
	this.calls = { 	WBExplore : '/?page=Widgets&plugins=Explore' , 
						WBCheapFlights : '/?page=Widgets&plugins=CheapFlights' ,
						WBHotels : '/?page=Widgets&plugins=BookingHotels' } ;
	this.options = { 
		container : '',
		width :  null,
		height :  null,
		scrolling :  'no',
		frameborder :  'no',
		allowTransparency :  0 ,
		/*
		 * params are specific to each widget
		 */
		params : {}
	};
	this.setOptions = function( params ){
		for ( name  in params) {
			if (typeof(params[name]) == "object"){ 
				for( _ak in params[name]){
					this.options[name][_ak]  = params[name][_ak] ;
				}
			}else
				this.options[name]  = params[name] ;	
		}		
	};
	this.initialize = function( options ) {
		
		if( options)
			this.setOptions( options );
		if(this.wname && this.calls[this.wname])
			this.baseURI = this.calls[this.wname] ;
		if(!this.options.container)
			return false ;
		// Check bloc
		this.bloc = document.getElementById( this.options.container );
		
		if (!this.bloc) {
			return false;
		}
		// check if link is on page
			if( !this.checkLink()){			
				
				return false;
			}
		
		// Check other params
		if( !this.options.width) 
			this.options.width = 320 ;
		if( !this.options.height) 
			this.options.height = 300 ;
	
		// create call uri
		uri = this.appendQuery( this.host + this.baseURI , this.options.params ) ;	
		// adjustement for widget height and width 
		uri = this.appendQuery( uri , { width : this.options.width , height: this.options.height });
		_parent = this.bloc.parentNode  ;
		frame = this.getElement( 'iframe', { name : this.name ,
												src: uri , 
												scrolling:  this.options.scrolling ? this.options.scrolling : 'no', 
												frameborder: this.options.frameborder ? this.options.frameborder : 1, 
												width: this.options.width +  'px', 
												height: this.options.height +  'px', 
												allowTransparency: this.options.allowTransparency ? this.options.allowTransparency : 0 } );
		//_parent.replaceChild( frame , this.bloc );	
		this.bloc.innerHTML = '';
		this.bloc.appendChild( frame );			
	};
	this.getElement = function( type, params ) {
		var ele = document.createElement( type );
		for( var i in params ) {
			att = document.createAttribute( i );
			att.nodeValue = params[i];
			ele.setAttributeNode( att );
		}
		// Set style without use setAttributeNode for IE compatibility
		ele.style.position = 'relative';
		ele.style.overflow = 'hidden';
		return 	ele ;
	};
	this.checkLink = function(){
		
		for (var i = 0; i < this.bloc.childNodes.length; i++) {
			if (this.bloc.childNodes[i].nodeName == 'A' && this.bloc.childNodes[i].href.match(/beta.travel|whichbudget.com/i)) 
				return true;
		}
		return false ;
	} ;
	this.toQuery = function(params){
		qs = '';
		t = [];
		
		for (key in params) {
			
			if (params[key] == 'object' ){
						
			 
			}else
				t[t.length] = key + '=' + params[key];
		}
		if (t.length > 0) 
			qs = t.join('&');
		return qs;
	};
	this.appendQuery = function(uri, params){
		uri += (uri.indexOf('?') != -1 ? '&' : '?') + this.toQuery(params);
		return uri;
	};
	this.loadExploreWidget = function(){
		if(window.WBExploreParams){
			if( typeof(WBExploreParams) == 'object' && WBExploreParams.length > 0){
				for(i=0;i<WBExploreParams.length;i++){
					var WBExploreLoader = new WBWidgetLoader;
					WBExploreLoader.wname = "WBExplore";
					WBExploreLoader.initialize(WBExploreParams[i]);		
				}
			}
		}
	};
	this.loadCheapFlightsWidget = function(){
		if(window.WBCheapFlightsParams){
			if( typeof( WBCheapFlightsParams) == "object" && WBCheapFlightsParams.length > 0 ){
				for(i=0; i<WBCheapFlightsParams.length;i++){
					var WBCheapFlightsLoader = new WBWidgetLoader();
					WBCheapFlightsLoader.wname = "WBCheapFlights";
					WBCheapFlightsLoader.initialize( WBCheapFlightsParams[i] );
				}
			}		
		}
	};
	this.loadHotelsWidget = function(){
		if(window.WBHotelsParams){
			if( typeof( WBHotelsParams) == "object" && WBHotelsParams.length > 0 ){
				for(i=0; i<WBHotelsParams.length;i++){
					var WBHotelsLoader = new WBWidgetLoader();
					WBHotelsLoader.wname = "WBHotels";
					WBHotelsLoader.initialize( WBHotelsParams[i] );
				}
			}		
		}
	};
};
function WBloadWidgets(){
	WBWLInstance = new WBWidgetLoader();
	WBWLInstance.loadExploreWidget();
	WBWLInstance.loadCheapFlightsWidget();
	WBWLInstance.loadHotelsWidget();	
}
if( window.addEventListener){
	window.addEventListener( "load" , WBloadWidgets, false )
}else if( window.attachEvent ){
	window.attachEvent("onload" , WBloadWidgets )
}
