var demoSite		= '/http/en.wikipedia.org/80/wiki/Cobrowsing';
var hoveringIds 	= new Array( 'singleDemoIcon', 'singleDemoTitle', 'multiDemoIcon', 'multiDemoTitle', 'logo' );

window.onload = function(){
	attachHovering( hoveringIds );
	
	// if( PUBChecker.isActive() ) {
	// 	var msg = "You seem to have a pop up blocker protecting you against bad pop ups. Since we launch the demos in nicely arranged pop ups, please add this site to your white list or deactivate you pop up blocker temporarly.<br />Thank you.";
	// 	var alertBox = document.getElementById("alert");
	// 	alertBox.innerHTML = msg;
	// 	alertBox.style.display = "block";
	// }
	
};

function attachHovering( ids ) {
	for(i=0; i<ids.length; i++) {
		try {
			document.getElementById( ids[i] ).onmouseover 	= setPointer;
			document.getElementById( ids[i] ).onmouseout 	= setDefault;
		} catch (e){}
	}
}
var setPointer = function() { this.style.cursor = 'pointer'; }
var setDefault = function() { this.style.cursor = 'default'; }

function openSingleDemo() {
	if( startSession() ) {
		var link = getDirectLink();
		if( link ) {
			var scale  = 0.8;
			var offset = (1-scale)/2;
    		var hostSizes = {
								'width'			: Math.floor( (splitter.getWindowW() * scale) ),
								'height'		: Math.floor( (splitter.getWindowH() * scale) ),
								'x'				: Math.floor( (splitter.getLeftWindowX() + (splitter.getWindowW() * offset ) ) ),
								'y'				: Math.floor( (splitter.getLeftWindowY() + (splitter.getWindowH() * offset ) ) )
							};

    		var hostWindow = window.open( demoSite , 'Host', "resizable=yes, location=yes, menubar=yes, scrollbars=yes, status=yes, toolbar=yes, width=" + hostSizes.width + ",height=" + hostSizes.height + ",left=" + hostSizes.x + ",top=" + hostSizes.y );
    		var companionWindow = window.open( link, 'Companion', "resizable=yes, location=yes, menubar=yes, scrollbars=yes, status=yes, toolbar=yes, width=" + splitter.getWindowW() + ",height=" + splitter.getWindowH() + ",left=" + splitter.getRightWindowX() + ",top=" + splitter.getRightWindowY());

			if( PUBChecker.isWindowOpen( hostWindow )==false || PUBChecker.isWindowOpen( companionWindow )==false ) {
				try {
					hostWindow.close();
					companionWindow.close();
				} catch( e ) {}
				var msg = "You seem to have a pop up blocker protecting you against bad pop ups. Since we launch the demos in nicely arranged pop ups, please add this site to your white list or deactivate you pop up blocker temporarly.<br />Thank you.";
				var alertBox = document.getElementById("alert");
				alertBox.innerHTML = msg;
				alertBox.style.display = "block";
			};

		} else {

			var msg = 'Ups, could not get login link';
			var alertBox = document.getElementById("alert");
			alertBox.innerHTML = msg;
			alertBox.style.display = "block";
		}
	} else {
		var msg = 'Ups, could not start session';
		var alertBox = document.getElementById("alert");
		alertBox.innerHTML = msg;
		alertBox.style.display = "block";
	}
}

function openMultiDemo() {
    window.open( demoSite + '?x-unblu-start-session-dialog=true', 'Host', "resizable=yes, location=yes, menubar=yes, scrollbars=yes, status=yes, toolbar=yes, width=" + splitter.getWindowW() + ",height=" + splitter.getWindowH() + ",left=" + splitter.getLeftWindowX() + ",top=" + splitter.getLeftWindowY());
}

var startSession = function() {
	var request = new Ajax();
	request.setRequest();
	request.setMethod('GET');
	request.setUrl('/unblu/rest/startRecorderSession');
	request.setAsync(false);
	if( request.send() == 'ok' ) {
		return true;
	} else {
		return false;
	}
}

var getDirectLink = function() {
	var link		= null;
	var sessionId 	= null;
	var port 		= null;
	
	var request = new Ajax();
	request.setRequest();
	request.setMethod('GET');
	request.setUrl('/unblu/rest/getSessionId');
	request.setAsync(false);
	sessionId = request.send();
	Logger( sessionId );

	if( sessionId == 'not in a session' ) {
		return false;
	}
	
	var uri = document.location;
	link = uri.protocol + "//" + uri.host + "/unblu/player/?join=" + sessionId ;

	return link;
}

var displayError = function ( msg ) {
	alert( msg );
}



/* * INIT * */

var debug = false;
var splitter = new Splitter(false);

