var r360bookurl = 'http://www.bruksvallarna.se/boende/boka/';
var r360carturl = 'http://www.ramundberget.se/bruksvallarna/cart.asp';
var r360viewurl = 'http://www.ramundberget.se/bruksvallarna/view.asp';
function addonloadevent(f) {
	var e = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = f;
	} else {
		window.onload = function() {e(); f();}
	}
}
function setCookie(cname, value) {
	document.cookie = escape(cname) + '=' + escape(value) + '; path=/';
}
function getCookie(cname) {
	var i, x, y
	var arrcookies = document.cookie.split(';');
	var cvalue = '';
	for (i=0;i<arrcookies.length;i++) {
		x = arrcookies[i].substr(0,arrcookies[i].indexOf('='));
		y = arrcookies[i].substr(arrcookies[i].indexOf('=') + 1);
		x = x.replace(/^\s+|\s+$/g, '');
		if (x == cname) {
			cvalue = y;
		}
	}
	return unescape(cvalue);
}
var r360SessionID = getCookie('r360SessionID');
if (r360SessionID == '') {
	var d = new Date();
	var t = d.getTime();
	var r = Math.random();
	r360SessionID = 'Core.Bruksvallarna.' + t + r;
	setCookie('r360SessionID', r360SessionID);
}
function getqsvalue(pn) {
	var qsvalue = '';
	var qs = window.location.search.substring(1);
	arrqs = qs.split('&');
	for (i=0;i<arrqs.length;i++) {
		arrnamevalue = arrqs[i].split('=');
		if (arrnamevalue[0] == pn) {
			qsvalue = arrnamevalue[1];
		}
	}
	return unescape(qsvalue);
}
function gethashvalue(pn) {
	var hvalue = '';
	var h = window.location.hash.substring(1);
	arrh = h.split('&');
	for (i=0;i<arrh.length;i++) {
		arrnamevalue = arrh[i].split('=');
		if (arrnamevalue[0] == pn) {
			hvalue = arrnamevalue[1];
		}
	}
	return unescape(hvalue);
}
function r360IntegrationInit() {
	if (document.getElementById('r360container')) {
		var r360obj = new R360.integration({
			parameters: {
				site: 'rmb', lang: 'SWE', compact: 'T', menu: 'F', resvtype: 'L', wp1: '79', wp2: '80', externalsession: r360SessionID
			},
			autoheight: 'true',
			autoparameters: 'true',
			parent: 'r360container',
			onSessionChanged: r360SessionChanged
			}
		);
		var goparam = getqsvalue('go');
		if (goparam != '') {
			addonloadevent(function() {r360obj.go(goparam);});
		}
	}
}
function r360SessionChanged() {
	var d = new Date();
	var t = d.getTime();
	if (document.getElementById('r360cart')) {
		document.getElementById('r360cart').src = r360carturl + '?burl=' + escape(r360bookurl) + '&esid=' + r360SessionID + '&_cac=' + t;
		document.getElementById('r360cart').style.height = '230px';
	}
}
function r360PriceWiewInit() {
	var d = new Date();
	var t = d.getTime();
	var obj = document.getElementById('r360view');
	if (obj) {
		if (obj.tagName.toLowerCase() == 'div') {
			var area = getqsvalue('area');
			var iframe = document.createElement('iframe');
			iframe.id = 'r360view_iframe';
			iframe.src = r360viewurl + '?curl=' + escape(document.location.protocol + '//' + document.location.hostname + document.location.pathname) + '&burl=' + escape(r360bookurl) + '&area=' + area + '&_cac=' + t;
			iframe.width = '200';
			iframe.height = '50';
			iframe.border = '0';
			iframe.frameBorder = '0';
			iframe.marginWidth = '0';
			iframe.marginHeight = '0';
			iframe.scrolling = 'no';
			iframe.style.border = '0';
			iframe.style.position = 'relative';
			obj.appendChild(iframe);
			setTimeout('r360PriceWiewSize();', 200);
		}
	} else {
		var area = getqsvalue('area');
		var iframe = document.getElementById('r360view_iframe');
		if (iframe) {
			if (iframe.tagName.toLowerCase() == 'iframe') {
				iframe.src = r360viewurl + '?curl=' + escape(document.location.protocol + '//' + document.location.hostname + document.location.pathname) + '&burl=' + escape(r360bookurl) + '&area=' + area + '&_cac=' + t;
				iframe.frameBorder = '0';
				iframe.marginWidth = '0';
				iframe.marginHeight = '0';
				iframe.scrolling = 'no';
				iframe.style.border = '0';
				iframe.style.position = 'relative';
				setTimeout('r360PriceWiewSize();', 200);
			}
		}
	}
}
addonloadevent(r360IntegrationInit);
addonloadevent(r360SessionChanged);
addonloadevent(r360PriceWiewInit);


function r360PriceWiewSize() {
	if (document.location.hash != '') {
		var w = gethashvalue('w');
		var h = gethashvalue('h');
		//var h = parseInt(document.location.hash.substring(1));
		var obj = document.getElementById('r360view');
		if (obj) {
			obj.style.width = w + 'px';
			obj.style.height = h + 'px';
		}
		var iframe = document.getElementById('r360view_iframe');
		if (iframe) {
			iframe.style.width = w + 'px';
			iframe.style.height = h + 'px';
		}
	} else {
		setTimeout('r360PriceWiewSize();', 200);
	}
}
