/**
 * Generate a id for the current tab
 */
function generateTabID() {
	if (typeof (Storage) !== "undefined") {
		if (window.location.href.indexOf("contextkey=") != -1) {
			sessionStorage.xistabid = window.location.href.substring(window.location.href.indexOf("?contextkey") + 12, window.location.href.indexOf("?contextkey") + 12 + 36);
			window.history.pushState(null, null, window.location.href.substring(0, window.location.href.indexOf("?contextkey")));
		}
		if (window.location.href.indexOf(";jsessionid=") != -1) {
			if ((window.location.href.indexOf("/public/") == -1) && (window.location.href.indexOf("/login.xhtml") == -1)) {
				if (sessionStorage.xistabid) {
					setTimeout(function() {
						message_broadcast_tabid('checktabid', sessionStorage.getItem('xistabid'));
					}, 100);
				} else {
					sessionStorage.xistabid = guid();
				}
				if (window.location.href.indexOf("?xistabid=" + sessionStorage.xistabid) == -1) {
					window.location.href = window.location.href.replace("index.xhtml", "home.xhtml") + "?xistabid=" + sessionStorage.xistabid;
				}
			}
			window.history.pushState(null, null, window.location.href.substring(0, window.location.href.indexOf(";jsessionid=")));
		}
	} else {
		// Browse do not have SessionStorage
	}
}

/**
 * Generate a random guid number
 * 
 * @returns {String}
 */
function guid() {
	function s4() {
		return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
	}
	return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}

var origin = guid();

function message_broadcast_tabid(command, id) {
	localStorage.setItem('messagetabid', JSON.stringify({
		'command' : command,
		'id' : id,
		'origin' : origin
	}));
	localStorage.removeItem('messagetabid');
}

function message_receive_tabid(ev) {
	var message = "";
	try {
		if (ev.originalEvent.key != 'messagetabid')
			return;
		if (ev.originalEvent.newValue == null || ev.originalEvent.newValue == '')
			return;
		message = JSON.parse(ev.originalEvent.newValue);
	} catch (e) {
	}

	if (!message)
		return;

	if (message.origin == origin)
		return;

	if (message.command == 'checktabid') {
		if (sessionStorage.getItem('xistabid') == message.id) {
			setTimeout(function() {
				message_broadcast_tabid('invalidatetabid', sessionStorage.getItem('xistabid'));
			}, 10);
		}
		return;
	}

	if (message.command == 'invalidatetabid') {
		if (sessionStorage.getItem('xistabid') == message.id) {
			debugger;
			sessionStorage.removeItem('xistabid');
			window.location.href = window.location.href;
		}
		return;
	}
}

window.onpopstate = function(e) {
	generateTabID();
};

generateTabID();

/*
 * increase and decrease image size
 */
function img_increase(uuid, w, h){
    document.getElementById(uuid).style.height=h+"px";
    document.getElementById(uuid).style.width=w+"px";
} 

function img_decrease(uuid, w, h){
   document.getElementById(uuid).style.height=h+"px";
   document.getElementById(uuid).style.width=w+"px";
}

function load_image(url){
	
}