/**
 * This is Nux s.r.o. standard library
 *
 * @author Jan Škvařil <jan.skvaril@nux.cz>
 * @copyright Nux s.r.o. (www.nux.cz)
 */

/**
 * Makes email address and the login form
 */
NLoad = function(emailClass, loginId)
{
	this.emailClass = emailClass;
	this.loginId = loginId;

	this.loadEmails();
	this.focusLogin();
	
	return;
}

NLoad.load = function(emailClass, loginId)
{
    var loader = function()
	{
		new NLoad(emailClass, loginId);
	}

	return $(window).ready(loader);
}

NLoad.prototype.loadEmails = function()
{
	var spans = document.getElementsByTagName("span");

	for (var i = 0; i < spans.length; i++)
	    if (spans[i].className.toLowerCase() == this.emailClass)
	        spans[i].style.display = "none";

	return;
}

NLoad.prototype.focusLogin = function()
{
	var login = document.getElementById(this.loginId);
	
	if (login)
	    login.focus();

	return;
}

/**
 * Resizer for the textareas
 */
NAreaResizer = function(element, horizontalClassName, verticalClassName, updateMargin)
{
	this.element = element;
	this.horizontalClassName = horizontalClassName;
	this.verticalClassName = verticalClassName;
	this.updateMargin = updateMargin;
	this.horizontalResizer = document.createElement("div");
	this.verticalResizer = document.createElement("div");
	this.instanceId = this.instances.length;
	this.instances[this.instanceId] = this;
	this.instanceText = "NAreaResizer.prototype.instances[" + this.instanceId + "]";
	this.upMethod = new Function(this.instanceText + ".up()");
	
	eval("this.downMethod = function(eventHandler){NAreaResizer.prototype.instances[" + this.instanceId + "].down(eventHandler);}");
	eval("this.moveMethod = function(eventHandler){NAreaResizer.prototype.instances[" + this.instanceId + "].move(eventHandler);}");

	this.minWidth = 50;
	this.maxWidth = 800;
	this.minHeight = 50;
	this.maxHeight = 450;

    this.prevMouseX = 0;
    this.prevMouseY = 0;

	this.run();
}

NAreaResizer.prototype.instances = new Array();
NAreaResizer.load = function(horizontalClassName, verticalClassName, areaOptions, updateMargin)
{
	var loader = function()
	{
        var textareas = jQuery("textarea");
        var resizer = null;

		for (var i = 0; i < textareas.length; i++)
		{
			resizer = new NAreaResizer(textareas[i], horizontalClassName, verticalClassName, updateMargin);

			if (typeof areaOptions == "object")
			{
				if (areaOptions.minWidth)
					resizer.minWidth = areaOptions.minWidth;

				if (areaOptions.maxWidth)
					resizer.maxWidth = areaOptions.maxWidth;

				if (areaOptions.minHeight)
					resizer.minHeight = areaOptions.minHeight;

				if (areaOptions.maxHeight)
					resizer.maxHeight = areaOptions.maxHeight;
            }
		}

		return;
	}

    return $(window).load(loader);
}

NAreaResizer.prototype.run = function()
{
	$(this.horizontalResizer).attr("class", this.horizontalClassName);
	$(this.horizontalResizer).bind("mousedown", {className: this.horizontalClassName}, this.downMethod);
	$(this.verticalResizer).attr("class", this.verticalClassName);
	$(this.verticalResizer).bind("mousedown", {className: this.verticalClassName}, this.downMethod);

	if (!$(this.horizontalResizer).css("height") || $(this.horizontalResizer).css("height") == "auto")
    	$(this.horizontalResizer).css("height", this.element.offsetHeight + "px");
    	
    if (!$(this.verticalResizer).css("width") || $(this.verticalResizer).css("width") == "auto")
    	$(this.verticalResizer).css("width", this.element.offsetWidth + "px");

    $(this.element).css("width", (this.element.offsetWidth - 6) + "px");
    $(this.element).css("height", this.element.offsetHeight + "px");
    
	if (this.updateMargin)
    	$(this.horizontalResizer).css("margin-left", this.element.offsetWidth + "px");

	if (this.horizontalClassName)
		$(this.element.parentNode).append(this.horizontalResizer);

	if (this.verticalClassName)
		$(this.element.parentNode).append(this.verticalResizer);

	return;
}

NAreaResizer.prototype.down = function(eventHandler)
{
	$(document).bind("mousemove", {className: eventHandler.data.className}, this.moveMethod);
	$(document).mouseup(this.upMethod);

    return;
}

NAreaResizer.prototype.up = function()
{
    $(document).unbind("mousemove", this.moveMethod);
    $(document).unbind("mouseup", this.upMethod);

    this.prevMouseX = 0;
    this.prevMouseY = 0;

    return;
}

NAreaResizer.prototype.move = function(eventHandler)
{
	var mouseX = eventHandler.clientX;
	var mouseY = eventHandler.clientY;

	if (!this.prevMouseX)
	    this.prevMouseX = mouseX;

	if (!this.prevMouseY)
	    this.prevMouseY = mouseY;

	var newMouseX = this.prevMouseX - mouseX;
	var newMouseY = this.prevMouseY - mouseY;

	var actualWidth = window.parseInt($(this.element).css("width")) - newMouseX;
	var actualHeight = window.parseInt($(this.element).css("height")) - newMouseY;

	var verticalResizerWidth = window.parseInt($(this.verticalResizer).css("width")) - newMouseX;
	var horizontalResizerHeight = window.parseInt($(this.horizontalResizer).css("height")) - newMouseY;
	var horizontalResizerMargin = window.parseInt($(this.horizontalResizer).css("margin-left")) - newMouseX;

    this.prevMouseX = mouseX;
    this.prevMouseY = mouseY;

	if (eventHandler.data.className == this.horizontalClassName)
	{
	    if (actualWidth > this.minWidth && actualWidth < this.maxWidth)
	    {
			$(this.element).css("width", actualWidth + "px");
			
			if (this.verticalClassName)
				$(this.verticalResizer).css("width", (verticalResizerWidth) + "px");

			if (this.updateMargin)
			    $(this.horizontalResizer).css("margin-left", horizontalResizerMargin + "px");
        }
    }
	else
	{
	    if (actualHeight > this.minHeight && actualHeight < this.maxHeight)
	    {
			$(this.element).css("height", actualHeight + "px");
			
			if (this.horizontalClassName)
				$(this.horizontalResizer).css("height", horizontalResizerHeight + "px");
		}
	}

	return;
}

/**
 * Seznam map
 */
NMap = function(instanceName, mapId, mapLinkId)
{
	this.instanceName = instanceName;
	this.mapId = mapId;
	this.mapLinkId = mapLinkId;

    this.element = null;
    this.map = null;
    this.coordinates = null;
    this.mark = null;
    this.card = null;
    this.poiLayer = null;
    this.pois = null;
    this.marks = new Array();
    this.lang = 'cs';
    this.cardContent = {obsah: [{title: 'Initiative Media Prague s.r.o.:', cont: 'Chrudimská 2526/2a<br />130 00 Praha 3'}, {cont: ''}]};
    this.cardContent2 = {obsah: [{title: '', cont: 'Stanice metra A &bdquo;Flora&ldquo;.'}, {cont: ''}]};
    this.metro = [
	[ 'Dejvická A', '50°6\'1.248"N', '14°23\'38.843"E' ],
	[ 'Hradcanská A', '50°5\'50.616"N', '14°24\'18.373"E' ],
	[ 'Malostranská A', '50°5\'27.421"N', '14°24\'34.335"E' ],
	[ 'Staromestská A', '50°5\'18.575"N', '14°24\'57.896"E' ],
	[ 'Mustek A/B','50°4\'56.99"N', '14°25\'32.079"E'],
	[ 'Muzeum A/C', '50°4\'47.152"N', '14°25\'46.732"E' ],
	[ 'Námestí Míru A', '50°4\'30.539"N', '14°26\'12.537"E' ],
	[ 'Jirího z Podebrad A', '50°4\'39.169"N', '14°26\'55.999"E' ],
	[ 'Flora A', '50°4\'41.09"N', '14°27\'41.151"E' ],
	[ 'Želivského A', '50°4\'43.258"N', '14°28\'24.933"E' ],
	[ 'Strašnická A', '50°4\'22.248"N', '14°29\'28.968"E' ],
	[ 'Skalka A', '50°4\'5.718"N', '14°30\'31.142"E' ],
	[ 'Depo Hostivar A', '50°4\'35.19"N', '14°30\'57.64"E' ],
    [ 'Cerný Most B', '50°6\'32.125"N', '14°34\'37.931"E' ],
	[ 'Rajská zahrada B', '50°6\'24.305"N', '14°33\'38.699"E' ],
	[ 'Hloubetín B', '50°6\'24.163"N', '14°32\'15.305"E' ],
	[ 'Kolbenova B', '50°6\'36.715"N', '14°30\'54.55"E' ],
	[ 'Vysocanská B', '50°6\'37.512"N', '14°30\'5.876"E' ],
	[ 'Ceskomoravská B', '50°6\'22.596"N', '14°29\'34.056"E' ],
	[ 'Palmovka B', '50°6\'12.443"N', '14°28\'22.004"E' ],
	[ 'Invalidovna B', '50°5\'50.343"N', '14°27\'53.447"E' ],
	[ 'Križíkova B', '50°5\'36.337"N', '14°27\'6.634"E' ],
	[ 'Florenc B/C','50°5\'27.483"N', '14°26\'22.263"E' ],
	[ 'Námestí Republiky B','50°5\'16.574"N', '14°25\'44.512"E' ],
	[ 'Mustek B/A', '50°5\'2.596"N', '14°25\'24.407"E' ],
	[ 'Národní trída B', '50°4\'53.778"N', '14°25\'12.33"E' ],
	[ 'Karlovo námestí B', '50°4\'32.542"N', '14°25\'6.29"E' ],
	[ 'Andel B', '50°4\'17.892"N', '14°24\'13.616"E' ],
	[ 'Smíchovské nádraží B', '50°3\'41.398"N', '14°24\'31.443"E' ],
	[ 'Radlická B', '50°3\'29.701"N', '14°23\'20.3"E' ],
	[ 'Jinonice B', '50°3\'16.133"N', '14°22\'15.977"E' ],
	[ 'Nové Butovice B', '50°3\'2.758"N', '14°21\'9.772"E' ],
	[ 'Hurka B', '50°2\'59.399"N', '14°20\'30.631"E' ],
	[ 'Lužiny B', '50°2\'40.844"N', '14°19\'53.794"E' ],
	[ 'Luka B', '50°2\'43.903"N', '14°19\'18.821"E' ],
	[ 'Stodulky B', '50°2\'48.898"N', '14°18\'30.178"E' ],
	[ 'Zlicín B', '50°3\'14.871"N', '14°17\'26.265"E' ],
	[ 'Ládví C', '50°7\'36.083"N', '14°28\'8.409"E' ],
	[ 'Kobylisy C', '50°7\'27.992"N', '14°27\'21.076"E' ],
	[ 'Nádraží Holešovice C', '50°6\'35.168"N', '14°26\'22.823"E' ],
	[ 'Vltavská C', '50°5\'57.424"N', '14°26\'18.905"E' ],
	[ 'Florenc C/B','50°5\'27.483"N', '14°26\'22.263"E'],
	[ 'Hlavní nádraží C','50°4\'59.362"N', '14°26\'1.617"E' ],
	[ 'Muzeum C/A', '50°4\'47.152"N', '14°25\'46.732"E' ],
	[ 'I. P. Pavlova C', '50°4\'32.548"N', '14°25\'49.219"E' ],
	[ 'Vyšehrad C', '50°3\'46.35"N', '14°25\'50.429"E' ],
	[ 'Pražského povstání C', '50°3\'21.711"N', '14°26\'4.515"E' ],
	[ 'Pankrác C', '50°3\'3.207"N', '14°26\'22.425"E' ],
	[ 'Budejovická C', '50°2\'39.997"N', '14°26\'56.77"E' ],
	[ 'Kacerov C', '50°2\'30.64"N', '14°27\'35.836"E' ],
	[ 'Roztyly C', '50°2\'14.375"N', '14°28\'39.847"E' ],
	[ 'Chodov C', '50°1\'51.114"N', '14°29\'27.087"E' ],
	[ 'Opatov C', '50°1\'41.284"N', '14°30\'30.746"E' ],
	[ 'Háje C', '50°1\'49.634"N', '14°31\'34.681"E' ]
	];

    $(window).load(new Function(this.instanceName + ".register()"));
}

NMap.prototype.makeMetro = function(zoom)
{
    var coordinates = null;
    var point = null;
    this.pois = {source: '', data: []};
	this.poiLayer = new SZN.MapEngine.MapLayers.PoiLayer('metro', false, 1, this.pois, null);

	if (!zoom)
	    zoom = 10;

    if (this.map.zoomGet() > zoom)
	{
		this.map.addLayer(this.poiLayer);

		for (var i = 0; i < this.metro.length; i++)
		{
			coordinates = this.map.wgsToPP(this.metro[i][1], this.metro[i][2]);
			point = new SZN.Visual.BaseMapIcon(coordinates.x, coordinates.y, 19, 13, 0, 0, 'http://www.nux.cz/modules/ProjectManager/files/metro.gif', '', this.metro[i][0]);
			this.pois.data.push(point);
	 	}
 	}

	return;
}

NMap.prototype.run = function()
{
	this.markClick = null;
	this.element = document.getElementById(this.mapId);

	if (!this.map && this.element && SZN && SZN.isSupported)
	{
		window.eval(this.specialPreFunction);

		if (!this.mouseSet)
			this.mouseSet = 7;

		if (!this.enableSelection)
			this.enableSelection = 1;

		if (!this.zoomSet)
			this.zoomSet = 15;

        this.element.innerHTML = '';
		this.map = new SZN.MapEngine(this.element);
		this.map.mouseSet(this.mouseSet);
		this.map.enableSelection(this.enableSelection);
		this.map.zoomSet(this.zoomSet);

		if (this.center)
		{
			this.coordinates = this.map.wgsToPP(this.center.x, this.center.y);
			this.map.setCenter(this.coordinates.x, this.coordinates.y);
		}
		
		if (this.marks)
		{
			for (var i = 0; i < this.marks.length; i++)
			{
				this.marks[i].mark = this.map.makeMark("company", this.marks[i].title, (i + 1), "mark");

				if (this.marks[i].click)
				{
					eval("this.marks[i].clickFunction = function (eventListener, element, mark) {var cardData = {obsah: [{title: '" + this.marks[i].name + "', cont: '" + this.marks[i].content + "'}, {cont: ''}]}; var crd = new SZN.Visual.BaseCard(" + this.instanceName + ".marks[" + i + "].mark.pos.x, " + this.instanceName + ".marks[" + i + "].mark.pos.y, cardData, null, null, null);	" + this.instanceName + ".map.addCard(crd);}");
					this.marks[i].mark.setAction(window, this.marks[i].clickFunction);
				}

				this.coordinates = this.map.wgsToPP(this.marks[i].x, this.marks[i].y);
				this.map.addMark(this.coordinates.x, this.coordinates.y, this.marks[i].mark);
			}
		}

		if (this.northRuler)
			this.map.setNorthRuler(1);

		if (this.scaleRuler)
			this.map.setScaleRuler(1);

		if (this.controls)
		{
			var layoutBox = this.map.getDefaultLayoutBox()
			var moveControl = new SZN.Visual.MoveControl();
			var move = this.map.addControls(moveControl,layoutBox, 10, 12);
			var zoomControl = new SZN.Visual.ZoomControl('full');
			var pos = this.map.getControlById(move).getSize().height + 17;
			this.map.addControls(zoomControl, layoutBox, 25, pos);
		}

		if (this.showMetro)
			this.map.apiHandler.addApiListener(window, "mapInited", new Function(this.instanceName + ".makeMetro()"));
			
		this.map.init();

		if (this.showCard)
		{
			if (this.showCardIndex < 0 || this.showCardIndex > this.marks.length)
				this.showCardIndex = 0;

			window.setTimeout(this.marks[this.showCardIndex].clickFunction, 0);
		}

		window.eval(this.specialPostFunction);
	}
	
	return;
}

/**
 * Controll class for email, flash etc.
 */
NController = function(instanceName)
{
	this.instanceName = instanceName;
}

NController.prototype.email = function(coded, linkname, makeLink, sameAddress)
{
	var cipher = 'ZabcXYdeWVfUTghSiRQjklPmONnMoLpqKJrIHstGuFvEwDxCyBz1A234568790';
	var shift = coded.length;
	var link = '';
	var ltr = null;

	for (var i = 0; i<coded.length; i++)
	{
		if (cipher.indexOf(coded.charAt(i)) == -1)
		{
			ltr = coded.charAt(i);
			link += (ltr);
		}
		else
		{
			ltr = (cipher.indexOf(coded.charAt(i)) - shift + cipher.length) % cipher.length;
			link += (cipher.charAt(ltr));
		}
	}

	if (sameAddress)
		linkname = link;

	if (makeLink !== false)
		document.write('<a class="email" href="mailto:' + link + '">' + linkname + '</a>');
	else
		document.write(linkname);

	return;
}

NController.prototype.flashEmbed = function(flashPath, width, height, bgColor, quality, menu, border, transparent, flashVars, loop)
{
	var embedVar = "";
	loop = loop == null ? "true" : loop;
	transparent = transparent == null ? "transparent" : transparent;
	border = border == null ? "false" : border;
	quality = quality == null ? "high" : quality;
	embedVar = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + width + '" height="' + height + '"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="' + flashPath + '" /><param name="flashvars" value="' + flashVars + '" /><param name="quality" value="' + quality + '" /><param name="menu" value="' + menu + '" /><param name="loop" value="' + loop + '" /><param name="bgcolor" value="' + bgColor + '" /><param name="wmode" value="' + transparent + '" /><embed src="' + flashPath + '" flashvars="' + flashVars + '" quality="' + quality + '" menu="' + menu + '" loop="' + loop + '" bgcolor="' + bgColor + '" width="' + width + '" height="' + height + '" wmode="' + transparent + '" name="' + flashPath + '" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	document.write (embedVar);
}

NController.prototype.getOffset = function(what, id)
{
	var el = document.getElementById(id);
	var offset = 0;

	do
	{
	    switch (what)
	    {
			case "left":
			    offset += el.offsetLeft;
			    break;
			case "top":
			    offset += el.offsetTop;
			    break;
		}

		el = el.offsetParent;
	}
	while (el);

	return offset;
}

NController.prototype.getLeft = function(id)
{
    return this.getOffset('left', id);
}

NController.prototype.getTop = function(id)
{
    return this.getOffset('top', id);
}

NController.prototype.getScrollLeft = function()
{
	return document.documentElement.scrollLeft || window.pageXOffset || 0;
}

NController.prototype.getScrollTop = function()
{
	if (document.documentElement.scrollTop)
	    return document.documentElement.scrollTop;
	else if (window.pageYOffset)
	    return window.pageYOffset;
	else
		return 0;
}

NController.prototype.windowWidth = function()
{
	return window.innerWidth || document.documentElement.clientWidth || 0;
}

NController.prototype.windowHeight = function()
{
	return window.innerHeight || document.documentElement.clientHeight || 0;
}

NController.prototype.windowScrollWidth = function()
{
	return document.documentElement.scrollWidth;
}

NController.prototype.windowScrollHeight = function()
{
	return document.documentElement.scrollHeight;
}

/**
 * Functions for eshop module.
 */
NEshop = function(instanceName, formId)
{
    this.instanceName = instanceName;
	this.formId = formId;
	this.message = '';
	this.firstNameError = '';
	this.lastNameError = '';
	this.emailError = '';
	this.wrongEmailError = '';
	this.wrongPhoneError = '';
	this.addressError = '';
	this.cityError = '';
	this.placeError = '';
	this.cashValue = null;
    this.cashText = null;
    
	var trafficElement = document.getElementById("traffic-send");
	var paymentElement = document.getElementById("payment-cashless");
	
	if (trafficElement && !trafficElement.checked)
	{	
		document.getElementById("traffic-send-type").disabled = true;
		//$(document.getElementById("cash-on-delivery")).css("display", "none");
	}
	
	if (paymentElement && !paymentElement.checked)
		document.getElementById("payment-cashless-type").disabled = true;
		
	$("form#" + this.formId + " [name=recalculate]").click(function()
	{										   
		var element = document.getElementById("custinfoForm");
		
		if (element)
            element.onsubmit = "";
	});
		
}

NEshop.prototype.configure = function(firstNameError, lastNameError, emailError, wrongEmailError, wrongPhoneError, addressError, cityError, placeError)
{
    this.element = document.getElementById(this.formId);
 	this.firstNameError = firstNameError;
	this.lastNameError = lastNameError;
	this.emailError = emailError;
	this.wrongEmailError = wrongEmailError;
	this.wrongPhoneError = wrongPhoneError;
	this.addressError = addressError;
	this.cityError = cityError;
	this.placeError = placeError;

    var cashElement = document.getElementById('cash-on-delivery');

	if (cashElement)
	{
        this.cashValue = cashElement.value;
        this.cashText = $(cashElement).text();
        $(cashElement).remove();
	}

	if (document.getElementById("traffic-personal"))
    	$(document.getElementById("traffic-personal")).click(new Function(this.instanceName + ".payPersonal()"));
    	
    if (document.getElementById("traffic-send"))
    	$(document.getElementById("traffic-send")).click(new Function(this.instanceName + ".sendPost()"));
    	
    if (document.getElementById("payment-cash"))
    	$(document.getElementById("payment-cash")).click(new Function(this.instanceName + ".payCash()"));
    	
    if (document.getElementById("payment-cashless"))
    	$(document.getElementById("payment-cashless")).click(new Function(this.instanceName + ".enabledCashlessType()"));
    	
    if (document.getElementById("payment-cashless-type"))
        $(document.getElementById("payment-cashless-type")).click(new Function("document.getElementById('payment-cashless').checked = true"));

	return;
}

NEshop.prototype.validateCustinfo = function()
{
	this.message = '';

	if (this.element)
	{
	    if (!this.message && this.element.firstName.value == "")
			this.message = this.firstNameError;

		if (!this.message && this.element.lastName.value == "")
			this.message = this.lastNameError;

		if (!this.message && this.element.email.value == "")
			this.message = this.emailError;
        else if (!this.message && this.element.email.value.search(/^[a-zA-Z1-9_\.-]+@[a-zA-Z1-9_\.-]+\.[a-zA-Z1-9_\.-]{1,4}/) < 0)
            this.message = this.wrongEmailError;

        if (!this.message && this.element.phone.value && this.element.phone.value.search(/^[+]?[0-9 ]{1,18}$/) < 0)
	    	this.message = this.wrongPhoneError;

		if (!this.message && this.element.address && this.element.address.value == "")
			this.message = this.addressError;

		if (!this.message && this.element.city && this.element.city.value == "")
			this.message = this.cityError;

		if (!this.message && this.element.place && this.element.place.value == "")
			this.message = this.placeError;
    }

	if (this.message)
	{
        window.alert(this.message);
		return false;
	}
	else
		return true;
}

NEshop.prototype.confirmAction = function(message, url)
{
	temp = window.confirm(message);

	if (temp)
		window.location = url;

	return temp;
}

NEshop.prototype.payCash = function()
{
    document.getElementById('traffic-send-type').disabled = true;
    document.getElementById('traffic-personal').checked = true;
    document.getElementById('payment-cash').disabled = false;
    document.getElementById('payment-cashless-type').disabled = true;
    
    return;
}

NEshop.prototype.sendPost = function()
{
    document.getElementById('payment-cash').disabled = true;
    document.getElementById('payment-cashless').checked = true;
    document.getElementById('traffic-send-type').disabled = false;
    
    var cashElement = document.getElementById('payment-cashless-type');
    var cashOptionOld = document.getElementById('cash-on-delivery');
    var cashOption = document.createElement('option');

    if (!cashOptionOld)
    {
	    $(cashOption).text(this.cashText);
	    cashOption.value = this.cashValue;
	    cashOption.id = 'cash-on-delivery';
	    cashElement.appendChild(cashOption);
    }
    
    cashElement.disabled = false;
    
    return;
}

NEshop.prototype.payPersonal = function()
{
    document.getElementById('traffic-send-type').disabled = true;
    document.getElementById('traffic-personal').checked = true;
    document.getElementById('payment-cash').disabled = false;
    document.getElementById('payment-cashless-type').disabled = false;
    
    var cashElement = document.getElementById('cash-on-delivery');

	if (cashElement)
        $(cashElement).remove();

    return;
}

NEshop.prototype.enabledCashlessType = function()
{
    return document.getElementById('payment-cashless-type').disabled = false;
}

/**
 * Class to change a value of input
 * by clicking.
 */
NDynamicInput = function(instanceName, id, className, startValue)
{
	this.instanceName = instanceName;
	this.element = id;
	this.startValue = startValue;
	this.className = className;

	$(window).load(new Function(this.instanceName + ".register()"));
}

NDynamicInput.prototype.register = function()
{
    this.element = document.getElementById(this.element);

	if (this.element)
	{
	    this.startValue = (this.startValue) ? this.startValue : this.element.value;
        $(this.element).focus(new Function(this.instanceName + ".hide()"));
		$(this.element).blur(new Function(this.instanceName + ".show()"));
	}

	return;
}

NDynamicInput.prototype.hide = function()
{
	if (this.element.value == this.startValue)
	{
	    this.element.className = this.className;
        this.element.value = "";
	}

	return;
}

NDynamicInput.prototype.show = function()
{
    if (this.element.value == "")
	{
	    this.element.className = "";
        this.element.value = this.startValue;
	}

	return;
}

NDynamicInput.prototype.submit = function()
{
    if (this.element.value == this.startValue)
    {
	    this.element.focus();
	    return false;
	}
	else
	    return true;
}

/**
 * Class to make diferent contents
 * in the one box.
 */
NBookmarks = function(instanceName, className, menuClass, menuActiveClass)
{
	this.instanceName = instanceName;
	this.menuClass = menuClass;
	this.className = className;
	this.menuActiveClass = menuActiveClass;
	this.menus = new Array();
	this.items = new Array();
    this.containers = new Array();
    this.bookmarks = new Array();

	$(window).load(new Function(this.instanceName + ".register()"));
}

NBookmarks.prototype.register = function()
{
	this.menus = $("." + this.menuClass);
	this.containers = $("." + this.className);

	for (var i = 0; i < this.menus.length; i++)
	    this.defaults(i);

	return;
}

NBookmarks.prototype.defaults = function(indent)
{
    var toShow = 0;
    var bookmarks = new Array();
    
    for (var i = 0; i < this.containers[indent].childNodes.length; i++)
        if (this.containers[indent].childNodes[i].nodeName.toLowerCase() == "div")
            bookmarks[bookmarks.length] = this.containers[indent].childNodes[i];
    
    this.menus[indent].style.display = "block";
	this.items[indent] = this.menus[indent].getElementsByTagName("li");
	this.bookmarks[indent] = bookmarks;

	for (var i = 0; i < this.items[indent].length; i++)
	{
	    $(this.items[indent][i]).click(new Function(this.instanceName + ".show(" + indent + " , " + i + ")"));

		if (jQuery.browser.opera)
		    this.items[indent][i].getElementsByTagName("a")[0].onclick = "return false";
		else
            this.items[indent][i].getElementsByTagName("a")[0].href = "javascript:" + this.instanceName + ".nothing()";

        if (this.items[indent][i].className == this.menuActiveClass)
            toShow = i;
	}

	this.show(indent, toShow);

	return;
}

NBookmarks.prototype.show = function(indent, menuIndex)
{
    this.hide(indent);

	if (this.items[indent][menuIndex])
	    this.items[indent][menuIndex].className = this.menuActiveClass;

	if (this.bookmarks[indent][menuIndex])
	    this.bookmarks[indent][menuIndex].style.display = "block";

	return false;
}

NBookmarks.prototype.hide = function(indent)
{
	for (var i = 0; i < this.items[indent].length; i++)
	    this.items[indent][i].className = "";

    for (i = 0; i < this.bookmarks[indent].length; i++)
	    this.bookmarks[indent][i].style.display = "none";

	return false;
}

NBookmarks.prototype.nothing = function()
{
	return;
}

/**
 * Form handlers.
 */
NForm = function(instanceName, noEffect)
{
	this.instanceName = instanceName;
	this.noEffect = noEffect;
	this.inputIds = new Array();
	this.blockIds = new Array();
	this.styleTypes = new Array();
	this.styles = new Array();
	this.offset = 0;
	
	$(window).load(new Function(this.instanceName + ".load()"));
}

NForm.prototype.handle = function(inputId, blockId, styleType, styleValues)
{
    if (!styleValues && !styleType)
	    styleValues = ["block", "none"];

	if (!styleType)
	    styleType = "display";

	this.offset = this.inputIds.length;
	this.inputIds[this.offset] = inputId;
	this.blockIds[this.offset] = blockId;
	this.styleTypes[this.offset] = styleType;
	this.styles[this.offset] = styleValues;

	return $(window).load(new Function(this.instanceName + ".addHandle(" + this.offset + ")"));
}

NForm.prototype.addHandle = function(offset)
{
	var id = this.inputIds[offset];
    var inputElement = document.getElementById(id);
	var changeFunction = new Function(this.instanceName + ".changeInput(" + offset + ")");

	if (inputElement)
	{
		if (inputElement.type == "checkbox" || inputElement.type == "radio")
		{
		    $(inputElement).change(changeFunction);
		    $(inputElement).click(changeFunction);
		}
		
		if (inputElement.type == "radio")
		{
			$("input[type=radio][name=" + inputElement.name + "]").change(changeFunction);
			$("input[type=radio][name=" + inputElement.name + "]").click(changeFunction);
        }
	}
	
	return;
}

NForm.prototype.changeInput = function(offset, noEffect)
{
    var inputElement = document.getElementById(this.inputIds[offset]);
    var blockElement = document.getElementById(this.blockIds[offset]);
    var otherChecked = false;
    
    if (inputElement && blockElement)
    {
        for (var i = 0; i < this.inputIds.length; i++)
        {
			if (this.blockIds[i] == this.blockIds[offset] && this.styleTypes[i] == this.styleTypes[offset] && offset != i)
			{
			    if (this.styles[offset] && document.getElementById(this.inputIds[i]).checked)
			        otherChecked = true;
			}
		}
    
        if (!this.styles[offset])
            eval("blockElement." + this.styleTypes[offset] + " = inputElement.checked ? true : false");
		else if (inputElement.type == "checkbox" || inputElement.type == "radio")
		{
		    if (!noEffect && !this.noEffect && this.styleTypes[offset] == "display" && this.styles[offset][0] == "block" && this.styles[offset][1] == "none")
			{
				if (inputElement.checked || otherChecked)
				    $(blockElement).show("slow");
				else
				    $(blockElement).hide("slow");
			}
		    else
				$(blockElement).css(this.styleTypes[offset], this.styles[offset][((inputElement.checked || otherChecked) ? 0 : 1)]);
        }
	}
        
	return;
}

NForm.prototype.load = function()
{
	for (var i = 0; i < this.inputIds.length; i++)
		this.changeInput(i, true);

	return;
}

/**
 * Collapse of blocks.
 */
NCollapse = function(instanceName)
{
	this.instanceName = instanceName;

	$(window).load(new Function(this.instanceName + ".load()"));
}

NCollapse.prototype.load = function(inputId, blockId, styleType, styleValues)
{
    var elements = jQuery(".expand");
    
    for (var i = 0; i < elements.length; i++)
        if (!/nohide/.test(elements[i].className))
        	$(elements[i]).css("display", "none");

	return;
}

NCollapse.prototype.expand = function(indent)
{
	var elements = jQuery(".expand");
	var block = null;

	for (var i = 0; i < elements.length; i++)
	    if (indent == i + 1)
	        block = elements[i];
	
	if (block)
	{
		if ($(block).css("display") == "block")
		    $(block).hide("slow");
		else
		    $(block).show("slow");
	}
	
	return false;
}

/**
 * Anti-spam controller
 */
NAntiSpamController = function(instanceName)
{
    this.instanceName = instanceName;
    this.ids = new Array();
    this.inputIds = new Array();;
    this.words = new Array();;

	$(window).ready(new Function(this.instanceName + ".run()"));
}

NAntiSpamController.prototype.add = function(id, inputId, word)
{
    this.ids.push(id);
    this.inputIds.push(inputId);
    this.words.push(word);
}

NAntiSpamController.prototype.run = function()
{
	for (var i = 0; i < this.ids.length; i++)
	{
	    $("#" + this.ids[i]).css("display", "none");
		$("#" + this.inputIds[i]).attr("value", this.words[i]);
	}
}

var collapse = new NCollapse("collapse");
var controller = new NController("controller");
var spamController = new NAntiSpamController("spamController");
NLoad.load("nojsemail", "username");
NAreaResizer.load("", "vertical-resizer", {minWidth: 50, maxWidth: 768, minHeight: 50, maxHeight: 600}, true);

