//document.location.reload();
var ScentOfLoveOnlineTestProxy = function()
{
	if (self.location.href.match(/#ScentOfLoveOnlineTestHash.*$/))
	{
		var hrefMatches = self.location.href.match(/#(ScentOfLoveOnlineTestHash)+:action:(.*):parameter:(.*)$/);
		var action = hrefMatches[2];
		var parameter = hrefMatches[3];
		eval('self.opener.ScentOfLoveOnlineTestLoader.testWindow.' + action + '(' + unescape(parameter) + ');');
		return;
//		self.opener.location.replace(parameter);
	}
}

ScentOfLoveOnlineTestProxy();

var ScentOfLoveOnlineTestLoader = function(timeout)
{
	ScentOfLoveOnlineTestLoader.sourceDirectory = ScentOfLoveOnlineTestLoader.detectSourceDirectory();
	if (typeof jQuery == 'undefined')
	{
		ScentOfLoveOnlineTestLoader.getScript(
			ScentOfLoveOnlineTestLoader.sourceDirectory + 'jquery-1.4.4.js',
			function ()
			{
				if (typeof jQuery != 'undefined') {
					jQuery.noConflict();
				}
				ScentOfLoveOnlineTestLoader.getScript(
					ScentOfLoveOnlineTestLoader.sourceDirectory + "colorbox/jquery.colorbox.js",
					function () {ScentOfLoveOnlineTestLoader.init();},
					timeout
				);
			},
			timeout
		);
	}
	else
	{
		ScentOfLoveOnlineTestLoader.getScript(
			ScentOfLoveOnlineTestLoader.sourceDirectory + "colorbox/jquery.colorbox.js",
			function () {ScentOfLoveOnlineTestLoader.init();},
			timeout
		);
	}
	ScentOfLoveOnlineTestLoader.getLink(ScentOfLoveOnlineTestLoader.sourceDirectory + 'colorbox/colorbox.css');
}

ScentOfLoveOnlineTestLoader.testWindow = {};

ScentOfLoveOnlineTestLoader.includeStates = {};

ScentOfLoveOnlineTestLoader.getScript = function(filename, callback, timeout)
{
	var fileref = document.createElement('script');
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", filename);
	ScentOfLoveOnlineTestLoader.includeStates[filename] = false;
	/* for mozilla */
	fileref.onload = function (){
		ScentOfLoveOnlineTestLoader.includeStates[filename] = true;
		if (typeof callback == 'function')
		{
			callback.call(fileref);
		}
	};
	/* for IE */
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		fileref.onreadystatechange = function()
		{
			if (this.readyState == 'complete' || this.readyState == 'loaded')
			{
				ScentOfLoveOnlineTestLoader.includeStates[filename] = true;
				if (typeof callback == 'function')
				{
					callback.call(fileref);
				}
			}
		}
	}
	if (typeof fileref!="undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref);

	return ScentOfLoveOnlineTestLoader.tryReady(0, filename, timeout);
};

ScentOfLoveOnlineTestLoader.detectSourceDirectory = function (){
	var scripts_array = document.getElementsByTagName("script");
	for (var k in scripts_array)
	{
		var v = scripts_array[k];
		if (v.src != null && v.src != 'undefined' && v.src.match(/scentoflove_freeonlinetest_loader\.js(\?.*)?$/))
		{
			var path = v.src.replace(/scentoflove_freeonlinetest_loader\.js(\?.*)?$/,'');
			return path;
		}
	}
	return '';
};

ScentOfLoveOnlineTestLoader.getLink = function(filename)
{
	var fileref = document.createElement('link');
	fileref.setAttribute('rel', 'stylesheet');
	fileref.setAttribute('type', 'text/css');
	fileref.setAttribute('media', 'screen');
	fileref.setAttribute('href', filename);
	ScentOfLoveOnlineTestLoader.includeStates[filename] = false;
	fileref.onload = function (){ScentOfLoveOnlineTestLoader.includeStates[filename] = true;};
	if (typeof fileref!="undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref);
};

ScentOfLoveOnlineTestLoader.messageListener = function(event)
{
//	alert('event.origin: ' + event.origin + ';\nevent.source: ' + event.source + ';\nevent.data: ' + event.data + ';');
	eval(event.data);
};

ScentOfLoveOnlineTestLoader.initColorBox = function()
{
	(function($){
			var publicMethodName = 'ScentOfLoveOnlineTest',
			publicMethod;

			publicMethod = $.fn[publicMethodName] = $[publicMethodName] = function(){};

			publicMethod.show = function(anhor) {
				$.ScentOfLoveColorbox({
					iframe:true,
					innerWidth:762,
					innerHeight:573,
					height:573,
					width:762,
					title: ' ',
					href:anhor,
					overlayClose: false,
					onOpen:function(){
						$('#ScentOfLoveCboxWrapper').append($('#ScentOfLoveCboxClose'));
					},
					onComplete:function()
					{
						$('#ScentOfLoveCboxTopCenter').height(8);
						$('#ScentOfLoveCboxMiddleLeft').width(8);
						$('#ScentOfLoveCboxMiddleRight').width(8);
						$('#ScentOfLoveCboxBottomCenter').height(8);
						$('#ScentOfLoveColorbox').css('padding-bottom', 16);
						$('#ScentOfLoveColorbox').css('padding-right', 16);
						$('#ScentOfLoveColorbox').css('width', 746);
						$('#ScentOfLoveColorbox').css('height', 557);
						$('#ScentOfLoveCboxTopCenter').css('width', 746);
						$('#ScentOfLoveCboxMiddleLeft').css('height', 557);
						$('#ScentOfLoveCboxContent').css('width', 746);
						$('#ScentOfLoveCboxContent').css('height', 557);
						$('#ScentOfLoveCboxLoadedContent').css('width', 746);
						$('#ScentOfLoveCboxLoadedContent').css('height', 557);
						$('#ScentOfLoveCboxLoadingOverlay').css('height', 557);
						$('#ScentOfLoveCboxLoadingGraphic').css('height', 557);
						$('#ScentOfLoveCboxMiddleRight').css('height', 557);
						$('#ScentOfLoveCboxBottomCenter').css('width', 746);
						$('#ScentOfLoveCboxOverlay').css('position', 'absolute');
						$('#ScentOfLoveCboxOverlay').css('height', $(document).height());
					},
					onClosed: function()
					{
//							$('#ScentOfLoveCboxOverlay').css('position', 'fixed');
					}
				});

				return false;

			};

			publicMethod.hide = function(){
				$.ScentOfLoveColorbox.close();
			};
		}
	)
	(jQuery);
};

ScentOfLoveOnlineTestLoader.initListener = function()
{
	if (typeof window.addEventListener != 'undefined') {
		window.addEventListener('message', ScentOfLoveOnlineTestLoader.messageListener, false);
	} else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onmessage', ScentOfLoveOnlineTestLoader.messageListener);
	}
};

ScentOfLoveOnlineTestLoader.init = function()
{
	ScentOfLoveOnlineTestLoader.initColorBox();
	ScentOfLoveOnlineTestLoader.initListener();
};

ScentOfLoveOnlineTestLoader.tryReady = function(time_elapsed, filename, timeout)
{
	/* Continually polls for jQuery library. */
	if (ScentOfLoveOnlineTestLoader.includeStates[filename] == false) {
		if (timeout != false && timeout != 'undefined' && timeout != null && time_elapsed > timeout) {
			//alert("Timed out while loading script: " + filename);
			return false;
		}
		setTimeout("ScentOfLoveOnlineTestLoader.tryReady(" + (time_elapsed + 200) + ", '" + filename + "', " + timeout + ");", time_elapsed);
	} else {
		return true;
	}
};

ScentOfLoveOnlineTestLoader.testWindow.showTest = function(anhor, mode) {
	if (mode == 'popup' || typeof jQuery == 'undefined' || typeof jQuery.ScentOfLoveColorbox == 'undefined')
	{
		var coordX = window.screen.width/2 - 762/2;
		var coordY = window.screen.height/2 - 572/2;
		ScentOfLoveOnlineTestLoader.testWindow.window = window.open(
			anhor + '&openerLocation=' + self.location.href,
			'ScentOfLoveFREEOnlineTest',
			'resizable=0,status=0,menubar=0,toolbar=0,scrollbars=0,location=0,directories=0,width=762,height=572,screenX='+coordX+',screenY='+coordY+',left='+coordX+',top='+coordY+''
		);
		ScentOfLoveOnlineTestLoader.testWindow.window.focus();
	}
	else
	{
		jQuery.ScentOfLoveOnlineTest.show(anhor);
	}
}

ScentOfLoveOnlineTestLoader.testWindow.hideTest = function(mode)
{
	if (mode == 'popup' || typeof jQuery == 'undefined')
	{
		if (typeof ScentOfLoveOnlineTestLoader.testWindow.window != 'undefined')
		{
			ScentOfLoveOnlineTestLoader.testWindow.window.close();
		}
	}
	else
	{
		jQuery.ScentOfLoveOnlineTest.hide();
	}
};

ScentOfLoveOnlineTestLoader.testWindow.redirectTestUser = function(url, mode)
{
	ScentOfLoveOnlineTestLoader.testWindow.hideTest(mode);
	document.location.replace(url);
}

ScentOfLoveOnlineTestLoader.testWindow.loginTestUser = function(login_url, em, pass, redirect_url, mode)
{
	ScentOfLoveOnlineTestLoader.testWindow.hideTest(mode);
	
	jQuery.post(
		login_url,
		{
			email: em,
			password: pass,
			submitted:'login'
		},
		function(data){
		}
	);
	setTimeout("ScentOfLoveOnlineTestLoader.testWindow.redirectTestUser('" + redirect_url + "', '" + mode + "');", 200);
}

ScentOfLoveOnlineTestLoader(false);

ScentOfLoveShowOnlineTest = function(anhor, mode) {
	if (navigator.cookieEnabled)
	{
		ScentOfLoveOnlineTestLoader.testWindow.showTest(anhor, mode);
		return false;
	}
	else
	{
		alert('Cookies in your browser are disabled. Please, enable cookies to provide correct working of the Test.');
		return false;
	}
};

ScentOfLoveHideOnlineTest=function(mode){
	ScentOfLoveOnlineTestLoader.testWindow.hideTest(mode);
	return false;
};

ScentOfLoveRedirectOnlineTest=function(url, mode){
	ScentOfLoveOnlineTestLoader.testWindow.redirectTestUser(url, mode);
	return false;
};

ScentOfLoveLoginOnlineTest=function(login_url, em, pass, redirect_url, mode){
	ScentOfLoveOnlineTestLoader.testWindow.loginTestUser(login_url, em, pass, redirect_url, mode);
	return false;
};
