
//------------------------------------------------------------------------------

function orderFormClass(url){
	this.url = url || '/?module=shop&media=ajax';
	this.sum = 0.00;

	this.setSum = function(sum){
		this.sum = sum;
	}

	this.change = function(obj, parent){
		if(this.sum < 1500 && parent == 1){
			var index = obj.selectedIndex;
			var delta = index == 1 ? 42 : (index == 2 ? 84 : 0);
			$('user_data_sum').value = (this.sum + delta).toFixed(2);
		}
		this.load(parent, obj.selectedIndex);
	}

	this.load = function(parent, index){
		new Ajax.Request(this.url, {
			method: 'GET',
			parameters: {'action': 'loadblock', 'parent': parent, 'index': index},
			onCreate: function(){
			},
			onSuccess: function(transport){
				if(transport.responseText.isJSON()){
					var response = transport.responseText.evalJSON();
					if(typeof response.removeElements != 'undefined'){
						$A(response.removeElements).each(function(id){
							$(id) === null ? void(0) : $(id).remove();
						});
					}
					if(typeof response.addElements != 'undefined'){
						$('user_form_blocks').insert({'bottom': response.addElements});
					}					
				}
				else if(transport.responseText.length > 0){
					alert(transport.responseText);
				}
			},
			onComplete: function(){
			},
			onException: function(a, b){
				alert('load block error: #' + b.message);
			}
		});
	}

	this.validate = function(form){
		form.formIsValid.value = 1;
		var result = true;

		$(form).select('[rel="required"]').reverse().each(function(el){
			if($(el) != null){
				if($F(el).blank()){
					$(el).addClassName('empty_field').focus();
					form.formIsValid.value = 0;
				}
				else{
					$(el).removeClassName('empty_field');
				}
			}
		});

		if(form.formIsValid.value == 1){
			if((box = $(form).select('[type="checkbox"]')).length == 1 && box[0].checked == false){
				alert('Ви повинні погодитись з правилами.');
			}
			else{
				form.submit();
			}
		}

		return false;
	}
	
	this.reset = function(form){
		$(form).select('[rel="required"]').reverse().invoke('removeClassName', 'empty_field');
		if($('user_form_blocks') != null){
			$('user_form_blocks').descendants().each(Element.remove);
			this.load(0, 1);
		}
	}
}

//------------------------------------------------------------------------------

function shopCartClass(url){
	this.url = url || '/?module=shop&media=ajax';

	this.add = function(gid, count){
		new Ajax.Request(this.url, {
			method: 'GET',
			parameters: {action: 'add', gid: gid, count: count},
			onSuccess: function(transport){
				if(transport.responseText.length > 0){
					alert(transport.responseText);
				}
			},
			onComplete: function(){
				this.updateCart();
			}.bind(this),
			onException: function(a, b){
				alert('AddCart error: #' + b.message);
			}
		});
	}

	this.updateCart = function(){
		if($('shopcart_goods') && $('shopcart_price')){
			new Ajax.Request(this.url, {
				method: 'GET',
				parameters: {action: 'update'},
				onSuccess: function(transport){
					if(transport.responseText.length && transport.responseText.isJSON() == true){
						var result = transport.responseText.evalJSON();
						$('shopcart_goods').update(result.goods);
						$('shopcart_word').update(result.word);
						$('shopcart_price').update(result.price);
					}
					else if(transport.responseText.length){
						alert(transport.responseText);
					}
				},
				onComplete: function(){
					new Effect.Pulsate('shopcart', {pulses: 2, duration: .8});
				},
				onException: function(a, b){
					alert('UpdateCart error: #' + b.message);
				}
			});
		}
	}
}

//------------------------------------------------------------------------------

function zoomImageClass(){
	this.iddle = true;
	this.div = new Element('div', {id: 'zoomImageDiv'});
	this.close = new Element('img', {src: '/images/close.gif', width: 16, height: 16, title: 'Закрити'}).addClassName('close');
	this.img = null
	
	this.zoom = function(src){
		if(this.iddle == false) return;
		this.iddle = false;

		this.img = new Element('img');
		this.img.observe('load', this.onload.bindAsEventListener(this));
		this.img.observe('click', this.onclose.bindAsEventListener(this));
		this.img.src = src;
	}
	
	this.onload = function(e){
		var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
		var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft;

		var cTop = scrollTop + (document.viewport.getHeight() - this.img.height) / 2;
		var cLeft = scrollLeft + (document.viewport.getWidth() - this.img.width) / 2;

		this.div.setStyle({'top': cTop + 'px', 'left': cLeft + 'px', width: this.img.width + 'px', height: this.img.height + 'px'});
		this.div.insert({top: this.img}).insert({top: this.close}).hide();

		$$('body')[0].insert({top: this.div});
		
		new Effect.Appear('zoomImageDiv', {
			duration: 0.5
		});
	}
	
	this.onclose = function(e){
		new Effect.Fade('zoomImageDiv', {
			duration: 0.5,
			afterFinish: function(){
				$('zoomImageDiv').descendants().invoke('remove');
				$('zoomImageDiv').remove();
				this.img = null;
				this.iddle = true;
			}.bind(this)
		});
	}

	this.close.observe('click', this.onclose.bindAsEventListener(this));
}

//------------------------------------------------------------------------------

function descriptionSwitcherClass(){
	this.free = true;

	this.buttons_block = 'goods_description_switch';
	this.containers = 'goods_description_short goods_description_full';

	this.toggle = function(obj, active_id){
		var buttons = $(this.buttons_block).getElementsByClassName('switcher');
		$A(buttons).invoke('removeClassName', 'active');
		obj.addClassName('active');
		
		$w(this.containers).each(Element.hide);
		$(active_id).show();
		
		return false;
	}
	
	this.group = function(obj){
		var id = "catalogue_groupset_" + obj.rel;
		$(id).toggle();

		return false;
	}
}

//------------------------------------------------------------------------------

function slideMenuClass(containerId){
	this.action = null;
	this.active = null;
	this.container = null;
	
	this.init = function(containerId){
		this.container = containerId ? $(containerId) : $('main_menu');

		var topmenu = this.container.select('ul:first').first();
		var nodes = topmenu.select('ul').invoke('up');

		nodes.each(function(node, i){
			var sub = node.select('ul:first').first();

			node.dimensions = {w: node.offsetWidth, h: node.offsetHeight, subw: sub.getWidth(), subh: sub.getHeight()};
			node.istop = typeof node.up().up('ul') == 'object' ? false : true;
			sub.setStyle({top: node.istop ? node.dimensions.h + "px" : 0});

			node.observe('mouseenter', this.expand.bindAsEventListener(this, node));
			node.observe('mouseleave', this.collapse.bindAsEventListener(this, node));
		}.bind(this));

		topmenu.select('ul').invoke('setStyle', {display: 'none', visibility: 'visible'});
	};
	
	this.expand = function(e, node){
		var target = node.select('ul:first').first();
		var left = node.istop ? 0 : node.dimensions.w;
		target.setStyle({left: (left - 2) + "px", width: node.dimensions.subw + 'px'});
		new Effect.BlindDown(target, {duration: .2});
		//target.show();
	};	
	
	this.collapse = function(e, node){
		var target = node.select('ul:first').first();
		new Effect.BlindUp(target, {duration: .1});
		//target.hide();
	};
	
	this.show = function(el){
	}
}

//------------------------------------------------------------------------------

function searchQueryReplace(id){
	var aInput = document.getElementById(id);   
    if (typeof aInput == 'object' && aInput.type == 'text' && aInput.value != ''){   
        var defvalue = aInput.value;   
        (function(defvalue) {   
            aInput.onfocus = function(){   
                if (this.value == defvalue) this.value = '';   
            }   
            aInput.onblur = function(){   
                if (this.value == '') this.value = defvalue;   
            }   
        })(defvalue);   
    }   
}

function openWindow(url, w, h)
	{
	var x	= parseInt((screen.width - w) / 2);
	var y	= parseInt((screen.height - h) / 2);
	var str	= "width=" + w + ", height=" + h + ", left=" + x + ", top=" + y + ", scrollbars=1";
	window.open(url, "newWindow", str);
	}
