
var subMenuObj = {
	hover: null,
	visible: null,

	expand: function(obj){
		if(!obj.readAttribute('effect')) obj.writeAttribute('effect', null);
		if(obj.readAttribute('effect') !== null) obj.readAttribute('effect').cancel();
		obj.writeAttribute('effect', new Effect.Appear(obj, {duration: 0.5, from: 0, to: 1, afterFinish: function(){
			obj.writeAttribute('effect', null);
		}}));
	},

	collapse: function(obj){
		if(obj !== null && typeof obj == 'object') obj.hide();
	},

	show: function(e, el){
		this.hover = el;
		if(this.visible == null || this.visible != el){
			if(this.visible != null) this.collapse(this.visible);
			this.visible = el;
			this.expand(this.visible);
		}
	},

	hide: function(e){
		this.hover = null;
		if(this.visible != null){
			(function(){
				if(this.hover == null || this.hover != this.visible){
					this.collapse(this.visible);
					this.visible = null;
				}
			}).bind(this).delay(0.5);
		}
	},

	init: function(){
		$$('a.dropdown').each(function(el, i){
			var block = $(el.readAttribute('rel'));
			var pos = el.down('img').positionedOffset();
			var dim = el.down('img').getDimensions();

			if(typeof block == 'object' && block != null){
				block.setStyle({left: pos.left + 'px', top: (pos.top + dim.height) + 'px'});

				el.observe('mouseover', this.show.bindAsEventListener(this, block));
				el.observe('mouseout', this.hide.bindAsEventListener(this));
				block.observe('mouseenter', this.show.bindAsEventListener(this, block));
				block.observe('mouseleave', this.hide.bindAsEventListener(this));
			}
		}.bind(this));
	}
};

//------------------------------------------------------------------------------

function maillistClass(){
	this.url = '/?module=maillist&media=ajax';

	this.subscribe = function(form){

		new Ajax.Request(this.url, {
			method: 'POST',
			parameters: {action: 'subscribe', name: $F(form.name), email: $F(form.email)},
			onCreate: function(){
				$('maillistMessDiv').update('');
				$('maillistButton').disable();
			},
			onSuccess: function(transport){
				var text = transport.responseText;
				if(text.length > 0 && text.isJSON()){
					alert(text.evalJSON());
				}
				else if(!transport.responseText.blank()){
					$('maillistMessDiv').update(transport.responseText);
				}
			}.bind(this),
			onComplete: function(){
				$('maillistButton').enable();
			},
			onException: function(a, b){
				alert('maillistClass error: #' + b.message);
			}
		});
	}
}

//------------------------------------------------------------------------------

function expressOrderClass(){
	this.url = '/express.php';
	
	this.div = new Element('div', {id: 'expressOrderDiv'});
	
	this.open = function(){
		if(typeof expressOrderDiv == 'object'){
			this.close();
		}

		var	cWidth = 550;
		var cHeight = 350;
		var oLeft = parseInt((document.body.clientWidth - cWidth) / 2);
		var oTop = parseInt((document.body.clientHeight - cHeight) / 2);

		this.div.setStyle({'top': oTop + 'px', 'left': oLeft + 'px', width: cWidth + 'px', height: cHeight + 'px'});

		new Ajax.Request(this.url, {
			method: 'POST',
			parameters: {},
			onSuccess: function(transport){
				this.div.insert({top: transport.responseText}).hide();
			}.bind(this),
			onComplete: function(){
				this.div.show();
			}.bind(this),
			onException: function(a, b){
				alert('express order error: #' + b.message);
			}
		});

		$$('body')[0].insert({top: this.div});
	}

	this.send = function(){
		if($F('order[name]').blank() || $F('order[email]').blank() || $F('order[phone]').blank() || $F('order[text]').blank()){
			alert('Вы должны заполнить все поля.');
			return;
		}

		new Ajax.Request(this.url, {
			method: 'POST',
			parameters: $('expressForm').serialize(),
			onSuccess: function(transport){
				this.div.innerHTML = transport.responseText;
			}.bind(this),
			onComplete: function(){
				this.close();
			}.bind(this),
			onException: function(a, b){
				alert('express order error: #' + b.message);
			}
		});
	}
	
	this.close = function(){
		$('expressOrderDiv').descendants().invoke('remove');
		$('expressOrderDiv').remove();
	}
}

//------------------------------------------------------------------------------

function orderFormClass(url){
	this.url = url || '/?module=shop&media=ajax';

	this.validate = function(flag){
		var form = $('userOrderForm');
		form.formIsValid.value = 0;

		if(flag){
			var result = true;

			$w('user_data_name1 user_data_name2 user_data_phone user_data_address').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){
				form.formIsValid.value = 1;
				form.submit();
			}
		}
		else{
			$w('user_data_name1 user_data_name2 user_data_phone user_data_address').each(function(el){
				$(el).removeClassName('empty_field');
			});
		}

		return false;
	}
}

//------------------------------------------------------------------------------

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.total);
						$('shopbasket_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: 'close'}).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 oLeft = parseInt((document.body.clientWidth - this.img.width) / 2);
		var oTop = parseInt((document.body.clientHeight - this.img.height) / 2);

		this.div.setStyle({'top': oTop + 'px', 'left': oLeft + '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});
		$('zoomImageDiv').show();
	}
	
	this.onclose = function(e){
		$('zoomImageDiv').descendants().invoke('remove');
		$('zoomImageDiv').remove();
		this.img = null;
		this.iddle = true;
	}

	this.close.observe('click', this.onclose.bindAsEventListener(this));
}

//------------------------------------------------------------------------------

Event.observe(window, 'load', function(e){
	var hover = false;
	var els = [];
	var src = [];
	$$('img[rel*="vstar"]').each(function(el, i){
		els[i] = el;
		src[i] = [el.src, el.src.substring(0, el.src.length - 4) + '_a' + el.src.substring(el.src.length - 4)];
		el.observe('mouseover', function(e, i){
			hover = true;
			for(var j = 0; j < els.length; j++){
				els[j].src = j <= i ? src[j][1] : src[j][0];
			}
		}.bindAsEventListener(this, i));
		el.observe('mouseout', function(e){
			hover = false;
			(function(){
				if(hover == false){
					for(var j = 0; j < els.length; j++){
						els[j].src = src[j][0];
					}
				}
			}).delay(.1);
		});
	});
});

//------------------------------------------------------------------------------

Event.observe(window, 'load', 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);   
	    }   
	});
});

//------------------------------------------------------------------------------

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);
		}
	});
});

//------------------------------------------------------------------------------

(function(key, value){
	function hoverImages(){
		var els = document.body.getElementsByTagName("IMG");

		for(var i = 0; i < els.length; i++){
			if(els[i].getAttribute(key) == value){
				var hovsrc = els[i].src.substring(0, els[i].src.length - 4) + '_a' + els[i].src.substring(els[i].src.length - 4);
				var tmp = new Image(); tmp.src = hovsrc;

				(function(el, defsrc, hovsrc){
					el.onmouseover = function(){
		                if (this.src == defsrc) this.src = hovsrc;
					}
					el.onmouseout = function(){
		                if (this.src == hovsrc) this.src = defsrc;
					}
				})(els[i], els[i].src, hovsrc);
			}
		}  	
	}

	if(window.addEventListener){
		window.addEventListener('load', hoverImages, false);
	}
	else if(window.attachEvent){
		window.attachEvent('onload', hoverImages);
	}
	else{
		window['onload'] = hoverImages;
	}
})('rel', 'hover');
