/* ------------------------------------------------------------------------
	Class: rsSolutionFinderOpener
	Use: Open Window for Solution Finder
	Author: Florian Koenig-Heidinger, WebLab
	Version: 1.0.0

	Example-URL: http://www2.rohde-schwarz.com/live/rs/camos/configurator/product-configurator.php?p1=Product%20Configurator&p2=messtechnik&p3=signalgeneratoren&p4=smu200a
------------------------------------------------------------------------- */

(function($)
{
	$.rsSolutionFinderOpener = {version: '1.0.0'};

	$.fn.rsSolutionFinderOpener = function(settings)
	{
		settings = jQuery.extend(
		{
			cookieName: 'RSConfigurator',
			cookiePath: '/',
			cookieExpires: 1,
			windowName: 'RSConfigurator',
			windowWidth: 620,
			windowHeight: 420,
			windowLeft: Math.floor((screen.width-600)/2),
			windowTop: Math.floor((screen.height-200)/2)
		}, settings);

		$(this).each(function()
		{
			$(this).bind('click',function()
			{
				if (!$.rsSolutionFinderOpener.isOpen())
				{
					$.rsSolutionFinderOpener.openWindow($(this).attr('href'));
				}
				return false;
			}
			);
		});

		$.rsSolutionFinderOpener.isOpen = function()
		{
			// zuerst das Cookie überprüfen
			if ($.cookie(settings.cookieName) == null)
			{ // Cookie ist nicht vorhanden
				return false;
			}
			// Cookie ist vorhanden
			var testWindow = window.open('', settings.windowName);
			if ( testWindow != null )
			{ // konnte ein neues Fenster geöffnet werden?
				if (!testWindow.document.RSConfiguratorApplet)
				{ // wenn das Fenster keinen Konfigurator enthält
					testWindow.close();
					return false;
				}
			}
			return true;
		};

		$.rsSolutionFinderOpener.openWindow = function(windowUrl)
		{
			$.cookie(settings.cookieName, '1', {'expires': settings.cookieExpires, 'path': settings.cookiePath});
			var windowFeatures = '';
			windowFeatures = windowFeatures + 'width=' + settings.windowWidth + ',';
			windowFeatures = windowFeatures + 'height=' + settings.windowHeight + ',';
			windowFeatures = windowFeatures + 'top=' + settings.windowTop + ',';
			windowFeatures = windowFeatures + 'left=' + settings.windowLeft + ',';
			windowFeatures = windowFeatures + ',dependent=true,titlebar=no,menubar=no,locationbar=no,resizable=yes,status=no';
			return window.open(windowUrl, settings.windowName, windowFeatures);
		};
	};
})(jQuery);


function openSolutionFinderWindow(params)
{
	var urlBase = "/live/rs/camos/configurator/product-configurator.php";
	var queryString = "?";
	var j=0;
	for (var i = 0; i < openSolutionFinderWindow.arguments.length; i++)
	{
		j = i + 1;
		queryString = queryString + "&p" + j + "=" + openSolutionFinderWindow.arguments[i];
	}

	if (!jQuery.rsSolutionFinderOpener.isOpen())
	{
		if (openSolutionFinderWindow.arguments[0] == "Product Configurator")
		{
			dcsMultiTrack(
				"DCS.dcsuri", "/click_events/product_configurator/" + openSolutionFinderWindow.arguments[3] + ".html",
				"WT.ti", "Product Configurator: " + openSolutionFinderWindow.arguments[3]
			);
		}
		jQuery.rsSolutionFinderOpener.openWindow(urlBase + queryString);
	}
}
