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

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

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

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

		if(result == true){
			form.formIsValid.value = 1;
			form.submit();
		}
		else{
			//alert('Не заполенны поля, отмеченные *');
		}

		return false;
	}
	
	this.reset = function(form){
		$(form).select('[rel="required"]').invoke('removeClassName', 'empty_field');
	}
}

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

function shopBasketClass(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($('shopbasket_goods') && $('shopbasket_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();
						$('shopbasket_goods').update(result.goods);
						$('shopbasket_word').update(result.word);
						$('shopbasket_price').update(result.price);
					}
					else if(transport.responseText.length){
						alert(transport.responseText);
					}
				},
				onComplete: function(){
					new Effect.Pulsate('shopbasket', {pulses: 2, duration: .8});
				},
				onException: function(a, b){
					alert('UpdateCart error: #' + b.message);
				}
			});
		}
	}
	
	this.changeCount = function(obj){
		$('shop_order_confirm').setOpacity(0.5);
		$('shop_order_recalculate').setOpacity(1.0);
	}
}

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

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));
}

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

Event.observe(document, 'dom:loaded', function(e){
	$$('img[rel="hover"]').each(function(el, i){
	    if (el.tagName.match(/img/i) && el.src != '') {   
	        defsrc = el.src;   
			hovsrc = el.src.substring(0, el.src.length - 4) + '_a' + el.src.substring(el.src.length - 4);
	        (function(defsrc, hovsrc) {   
	            el.onmouseover = function(){
	                if (this.src == defsrc) this.src = hovsrc;
	            }   
	            el.onmouseout = function(){   
	                if (this.src == hovsrc) this.src = defsrc;
	            }   
	        })(defsrc, hovsrc);   
	    }   
	});

	$$('input[rel="clear"]').each(function(el, i){
		if(el.type == 'text' && el.value != ""){
			el.setStyle({color: '#999'});
			var defValue = el.value;
			(function(defValue){
				el.observe('focus', function(e){
					if(this.value == defValue) this.value = "";
					el.setStyle({color: '#000'});
				});
				el.observe('blur', function(e){
					if(this.value == "") this.setStyle({color: '#999'}).value = defValue;
				});
			})(defValue);
		}
	});
});
