// constructor
function RGOpacityPopup(params) {	
	
	this.useDefaultLayout = typeof(params.useDefaultLayout) == 'undefined' ? true : params.useDefaultLayout;
	this.defaultTemplate = '';
	
	this.defaultWidth = params.width;
	this.defaultHeight = params.height;
	this.backgroundColor = params.backgroundColor || 'white';
	this.border = params.border || '0px';
	this.httpHost = params.httpHost ? params.httpHost : window.location.hostname;
	
	
	
	this.bg = '';
	this.fg = '';	
}

// create layers
RGOpacityPopup.prototype.init = function (params) {
	var width = params.width ? params.width : this.defaultWidth;
	var height = params.height ? params.height : this.defaultHeight;
	
	RGOpacityPopupClose(); // closes old layers if they are open..

	this.bg = document.createElement('DIV');
	this.bg.id = 'opacityBg';
	this.bg.style.position = 'absolute';	
	this.bg.innerHTML = '...';
	this.bg.style.zIndex = 99999996;
	this.bg.style.left = '0px';
	this.bg.style.top = '0px';
	this.bg.style.width = '100%';
	this.bg.style.height = Math.max(jQuery(document).height(), RGGetClientHeight()) + 'px';
	this.bg.style.backgroundColor = 'black';
	this.bg.style.opacity = 0.4;
	this.bg.style.filter = 'alpha(opacity=40)';
	this.bg.onclick = RGOpacityPopupClose;

	this.fg = document.createElement('DIV');
	this.fg.style.position = 'absolute';
	this.fg.id = 'opacityFg';
	this.fg.style.zIndex = 99999997;
	this.fg.style.width = width;
	this.fg.style.height = height;
        this.fg.style.left = RGGetScrollX() + (params.leftMargin ? parseInt(params.leftMargin) : Math.max(Math.round(RGGetClientWidth() / 2 - parseInt(width) / 2), 0)) + 'px';
        this.fg.style.top = RGGetScrollY() + (params.topMargin ? parseInt(params.topMargin) : Math.max(Math.round(RGGetClientHeight() / 2 - parseInt(height) / 2), 0)) + 'px';
	

	window.onkeydown = this.initKeycodes;
	document.onkeydown = this.initKeycodes;
}

// controll popup with keys?
RGOpacityPopup.prototype.initKeycodes = function (e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	
	
	if (keycode == 27) {  // escape
		RGOpacityPopupClose();
	}
}


// show popup and cover backgrund with a opacity-layer
RGOpacityPopup.prototype.show = function (params) {
	
	this.init(params);
	
	var _this = this;
	setTimeout(function() {
		_this.fg.innerHTML = _this.useDefaultLayout ? _this.defaultPopupLayout(params) : params.content;
	}, 1);
	
	document.body.appendChild(this.bg);
	document.body.appendChild(this.fg);
	
	var _this = this;
	
	//setTimeout(function() { _this.fg.style.opacity = 100; _this.fg.style.filter = 'alpha(opacity=100)'; }, 100);
	
	//this.fadePopupAd(this.fg, 0);
}

RGOpacityPopup.prototype.loadingIndicator = function (position) {
	if (!position) position = 'absolute';
	return '<img id="RGOpacityPopupLoadingIndicator" style="position: ' + position + '; left: 49%; top: 40%; margin-top: 20px; margin-bottom: 20px" alt="" src="/groups/rg/images/ajax-loader1-transparent.gif" />'
}

// create default layout for popup.
// Dependent on trimpath templating system
RGOpacityPopup.prototype.defaultPopupLayout = function (params) {
	
	// fetch template
	if (!this.defaultTemplate) { // we don't want the template to be downloaded each result update.		
		var template = RGGetFileXMLHTTP('http://' + this.httpHost + '/groups/rg/jstemplates/common/popup/default.html');
		this.defaultTemplate = TrimPath.parseTemplate(template);
	}

	var bottomWidth = (params.width || this.defaultWidth);
	bottomWidth = (bottomWidth.substring(0, (bottomWidth.length-2)) - 18);

	var height = params.height;
	
	if (height) {
		var cleanHeight = height.substring(0, (height.length-2));
		height = cleanHeight;
	
		if (RGGetClientHeight() < height) {
			height = (RGGetClientHeight() - 100);
			if (height < 200) height = 200;
		}
	
		height += 'px';
	}
	
	var width = (params.width || this.defaultWidth);

	var mainDivStyleAttr = 
		'border: ' + (params.border || this.border) + '; ' + 
		'background-color: ' + (params.backgroundColor || this.backgroundColor) + '; ' + 
		'width: ' + width + '; ' + 
		'height: ' + height + ';' +
		'border-bottom: 2px solid #E2E2E2;';
		
	var content = '';
	
	if (params.iframeUrl) {
		content =
			this.loadingIndicator() +
			this.createIframe(params.iframeUrl, params.width, height, params.iFrameStyle);
	}
	else {
		content = params.content;
	}

	var html =
	this.defaultTemplate.process({
		title: params.title ? params.title : '',
		mainDivStyleAttr: mainDivStyleAttr,
		content: content,
		width: params.width || this.defaultWidth,
		bottomWidth: bottomWidth,
		bottomContent: params.bottomContent ? params.bottomContent : ''
	});
	
	return html;
}

RGOpacityPopup.prototype.redirectAssist = function (params) {
	
	// only for reseguiden.se at the moment
	if (params.site != 'reseguiden') return;
	
	var url =   '/cgi-bin/rg/redirect_assist.cgi?' +
                'site=' + params.site +
                '&section=' + params.section +
                '&company=' + (params.company ? params.company : '') +
                '&company_id=' + params.companyId +
                '&city_id_dest=' + (params.destCityId ? params.destCityId : '') +
                '&city_code_dest=' + (params.destCityCode ? params.destCityCode : '') +
		'&area_id_dest=' + (params.destAreaId ? params.destAreaId : '') +
		'&region_id_dest=' + (params.destRegionId ? params.destRegionId : '') +
		'&country_id_dest=' + (params.destCountryId ? params.destCountryId : '') +
		'&country_code_dest=' + (params.destCountryCode ? params.destCountryCode : '') +
                '&booking_uri=' + encodeURIComponent(params.bookingURI);
	
	this.show({
		title: 'Välkommen tillbaka till Reseguiden',
		content: this.loadingIndicator('relative'),
		width: params.width || '480px',
		topMargin: '50px'
	});       
	
	jQuery.get(url, function (data) {
		setTimeout(function() {
		    jQuery("#RGOpacityPopupContent").html(data);
		}, 1);
	});		
}


// creates code for iframe, arguemts; url, width, height and additional style
RGOpacityPopup.prototype.createIframe = function (url, width, height, style) {
	if (url.match(/\?/)) url += '&opacity_popup=1';
	else url += '?opacity_popup=1';
	
	var iframe = '<iframe id="RGOPacityPopupIframeId" src="' + url + '" style="width: ' + (width || '100%') + '; height: ' + height + '; ' + style + '" frameborder="0" scrolling="auto"></iframe>';
	return iframe;
}


// fades the layer in by calling itself and increasing opacitynumber
RGOpacityPopup.prototype.fadePopupAd = function (div, n) {
	var _this = this;
	div.style.opacity = n/100;
	div.style.filter = 'alpha(opacity=' + n + ')';
	if (n < 100) setTimeout(function() { _this.fadePopupAd(div, n + 20) }, 100);
}

// closes if open
function RGOpacityPopupClose() {
	try	{ document.getElementById('opacityBg').parentNode.removeChild(document.getElementById('opacityBg')) } catch (e) { void(0) };
	try	{ document.getElementById('opacityFg').parentNode.removeChild(document.getElementById('opacityFg')) } catch (e) { void(0) };
}
