// adeoreisen gmbh - anfragen manager receiver v1.0 

/**
 *	amMeta() - anfragen manager Meta
 *	
 *	opens the receiver window and populate it with informations found in the
 *	html head meta informations.
 */  
function amMeta(){
	var d = document;
	var m = d.getElementsByTagName('head')[0].getElementsByTagName('meta');
	var r = new Receiver();
	for(var i in m){
		if(m[i].name && m[i].content){
			r[m[i].name] = typeof(r[m[i].name]) == 'number'?parseInt(m[i].content):m[i].content;
		}
	}
	r.open();
	return false;
}

/**
 *	Receiver - object
 *	
 * used to create an receiver request opened in an extra window   
 */ 
function Receiver() {
	// receiver
	this.version = '1.1';
	this.url = 'http://anfrage.adeoworld.de/Receiver/';
	this.charset = '';
	
	// popup window
	this.window = {
		'name':'adeoanfrage',
		'width':540,
		'height':610,
		'location':false,
		'menubar':false,
		'status':true,
		'toolbar':false,
		'scrollbars':true,
		'resizable':false
	}
	//	params	
	this.departure_date = '';
	this.affiliate_url = '';
	this.days = 0;
	this.hotel = '';
	this.image_url = '';
	this.nights = 0;
	this.offer_url = '';
	this.partner_id = 0;
	this.persons = 0;
	this.destination = '';
	this.rooms = 0;
}


/**
 *	populate - function
 *	
 *	populate the receiver parameters with automatic or default values
 */   
Receiver.prototype.populate = function(){
	// type validation
	var receiver = new Receiver();
	for(var i in receiver){
		if(typeof(this[i]) != typeof(receiver[i])){
			//console.log('error',i,this[i],'is not type of',typeof(receiver[i]),'- resetted');
			this[i] = receiver[i];
		}
	}
	delete(receiver);
	
	// populate charset if not set
	if(!this.charset.length){
		var metas = document.getElementsByTagName('meta');
		for(var i in metas){
			if(metas[i].httpEquiv == 'Content-Type' && metas[i].content.search('charset') !== false && !this.charset.length)
				this.charset = metas[i].content.substring(metas[i].content.lastIndexOf('=')+1);
		}
	}

	// populate destination if not set
	if(!this.destination.length){
		var tds = document.getElementsByTagName('td');
		for(var i in tds){
			if(tds[i].className == 'bold' && !this.destination.length)
				this.destination = tds[i].innerHTML;
		}
	}

	// populate image_url if not set
	if(!this.image_url.length){
		var imgs = document.getElementsByTagName('img');
		for(var i in imgs){
			if(parseInt(imgs[i].width) == 470 && !this.image_url.length)
				this.image_url = imgs[i].src;
		}
	}
	
	// populate offer_url if not set
	if(!this.offer_url.length){
		this.offer_url = document.URL;
	}
	
	// populate departure_date if not set
	/*if(!this.departure_date.length){
		var tomorrow = new Date();
		tomorrow.setTime(tomorrow.getTime()+(1000*3600*24));
		this.departure_date = tomorrow.getDate()+'.'+(tomorrow.getMonth()+1)+'.'+tomorrow.getFullYear();
	}*/	

	// todo automatization	
	// 	this.days = 0;
	// 	this.nights = 0;
	// 	this.hotel = '';
	// 	this.partner_id = 0;
}


/**
 *	open - function
 *	
 *	opens the receiver in an extra window.
 */   
Receiver.prototype.open = function(){
	this.populate();
	// define get parameters
	var get = new Array();
	
	// string values
	get.push('z='+encodeURIComponent(this.destination));
	get.push('ab='+encodeURIComponent(this.departure_date));
	get.push('hotel='+encodeURIComponent(this.hotel));
 	get.push('v='+encodeURIComponent(this.version));
 	get.push('charset='+encodeURIComponent(this.charset));
 	get.push('partner_id='+encodeURIComponent(this.partner_id));

	// integer values 
	get.push('days='+encodeURIComponent(parseInt(this.days)));
	get.push('nights='+encodeURIComponent(parseInt(this.nights)));
	get.push('pers='+encodeURIComponent(parseInt(this.persons)));
	get.push('anzzimmer='+encodeURIComponent(parseInt(this.rooms)));
	// url values
	get.push('image_url='+encodeURIComponent(encodeURI(this.image_url)));
	get.push('affiliate_url='+encodeURIComponent(encodeURI(this.affiliate_url)));
	get.push('offer_url='+encodeURIComponent(encodeURI(this.offer_url)));
	var url = this.url + '?' + get.join('&');
	//console.log(url);
	
	// window arguments
	var args = new Array(
							"width="+parseInt(this.window.width),
							"height="+parseInt(this.window.height),
							"location="+(this.window.location?'yes':'no'),
							"menubar="+(this.window.menubar?'yes':'no'),
							"status="+(this.window.status?'yes':'no'),
							"toolbar="+(this.window.toolbar?'yes':'no'),
							"scrollbars="+(this.window.scrollbars?'yes':'no'),
							"resizable="+(this.window.resizable?'yes':'no')
						);
	
	// open window
	return window.open(url,this.window.name,args.join(', '));
}


