var Wait;
var index_gallery;
var galleries = new Hash();


		
var scroll = new Array();
(function($) {

var is_ie = Browser.Engine.trident;
var is_safari = Browser.Engine.webkit;
var Mozilla = Browser.Engine.gecko;

var Forum;




var umiSaveFormData = new Class({
    Implements: [Options, Events],
    options: {
		'error_messages': 'error_messages'
	},
    initialize: function (form){
		var params = Array.link(arguments, {options: Object.type, elements: $defined});
        this.setOptions(params.options || null);
		
		this.form = $(form);
		if(this.form){
  			var _self = this;
			var err = '.'+_self.options.error_messages;
			if(form.getElement(err)) _self.restoreFormData(_self.form);
				_self.saveFormData(_self.form);
		}
	},
	saveFormData: function(form) {
	
		if(!form) return false;
		if(!form.id) {
			alert("You should set id attribute in form tag to save or restore it.");
			return false;
		}

		var cookieData = new Array;	
		for(var i = 0; i < form.elements.length; i++) {
			var input = form.elements[i];
			var inputName = input.name.replace(/([)\\])/g, "\\$1");

			switch(input.type) {
				case "password": break;
				case "text":
				case "textarea": {
					cookieData.push({type: 'T', name: inputName, value: input.value});
					break;
				}
				case "checkbox":
				case "radio": {
					cookieData.push({type: 'C', name: inputName, value: (input.checked ? 1 : 0)});
					break;
				}
				case "select-multiple":
				case "select-one": {
					cookieData.push({type: 'S', name: inputName, value: input.selectedIndex});
					break;
				}
			}
		}
		
		var i, str = "";
		for(var i = 0; i < cookieData.length; i++) {
			var elementData = cookieData[i];
			var value = new String(elementData.value);
			var inputName = new String(elementData.name);
			if(!inputName || !value) continue;
			str += elementData.type + "," + inputName.length + "," + inputName + "," + value.length + "," + value;
		}
		
		document.cookie="frm" + form.id + "=" + escape(str.replace(/([|\\])/g, "\\$1"));
		return true;
	},

	restoreFormData: function(form) {
		if(!form) return false;
		
		if(!form.id) {
			alert("You should set id attribute in form tag to save or restore it.");
			return false;
		}
		var cookieName = "frm" + form.id + "=";
	
		var cookie = new String(unescape(document.cookie));
		var posStart, posEnd;
		if((posStart = cookie.indexOf(cookieName)) == -1) {
			return false;
		}
		
		if((posEnd = cookie.indexOf(";", posStart)) == -1) {
			posEnd = cookie.length;
		}
		
		var data = cookie.substring(posStart + cookieName.length, posEnd);
		var pos = 0, cookieData = new Array;

		while(pos < data.length) {
			var inputName;
			var type = data.substring(pos, pos + 1);
			pos += 2;
			
			var length = parseInt(data.substring(pos, data.indexOf(",", pos)));
			pos = data.indexOf(",", pos) + 1;
			var inputName = data.substring(pos, pos + length);
			pos += length + 1;

			var length = parseInt(data.substring(pos, data.indexOf(",", pos)));
			if(length == 0) {
				pos += 2;
				continue;
			} else {
				pos = data.indexOf(",", pos) + 1;
			}
			
			var value = data.substring(pos, pos + length);
			pos += length;
			
			cookieData.push({type: type, name: inputName, value: value});
		}
		
		for(var i = 0; i < cookieData.length; i++) {
			var elementData = cookieData[i];
			switch(elementData.type) {
				case "T": {
					form.elements[elementData.name].value = elementData.value;
					
					break;
					
				}
				case "C": {
					form.elements[elementData.name].checked = elementData.value ? true : false;
					break;
				}
				case "S": {
					form.elements[elementData.name].selectedIndex = elementData.value;
					
					break;
				}
			}
		}
	}

});





window.addEvent('domready', function(){
	$('data_storage').getElements('.save_data').each(function(item){
		if(item.id){
			var id = item.id;
			item.removeProperty('id');
			item.setProperty('data:id',id);
		}
	});
	

	
	if($('galleryBlckStatic')){
		var li_slider = $$('.li_slider');
	
		
			for(i=0;i<10;i++){
				li_slider.each(function(item){
					item.clone().inject($('slider_list_104'));
				});
			}
	
			
		
		initializeSlider();
		Wait = new Loading();
	}	
		
		$$('.fill').setStyle('opacity',0.7);
	  $$('.fill').addEvents({
		'mouseenter': function(){this.setStyle('opacity',1);},
		'mouseleave':function() {this.setStyle('opacity',0.7);}
      });
	
	$$('.save_data').each(function(item, index){
			if(item.id){
				new umiSaveFormData(item);	
				item.addEvent('submit', function(e){
					if(checkForm(item)){
						new umiSaveFormData(item);	
						
						return true;
					}
					return false;
				});
			}
	});

	$$('.add_to_compare').each(function(item){
			var tr = item.getParent('tr');
			if(item.checked) {
				tr.addClass('hover2');
				//compare_button();
			}
			item.addEvent('click', function(e){
				if(this.checked) this.getParent('tr').addClass('hover2');
				else this.getParent('tr').removeClass('hover2');
				compare_button();
			});
			
			tr.addEvent('mouseenter',function(){
				tr.addClass('hover');
			});
			tr.addEvent('mouseleave',function(){
				tr.removeClass('hover');
			});
		
	});
	
	if($('index_special_list')){
		$('index_special_list').getElements('li').each(function(item){
			var a = item.getElement('p.slider-title a');
			if(a){
				var href = a.href;
				var a = new Element('a',{'href':href});
				var img = item.getElement('img');
				a.wraps(img);
			}
		});
	}
	
	
	//  Popup window
  $$('.call-back').addEvent('click', function() {
    $$('.black, .popup-form').setStyle('display','block');

    return false;
  });
    $$('.close, .black').addEvent('click', function() {
    $$('.black, .popup-form').setStyle('display','none');

    return false;
  });
  
  

  

	
	if($('faq')){
		$$('.trigger a').removeEvents('click');
		
		new Fx.Accordion($$('.trigger'), $$('.a_content'), {
			display: '0',
			alwaysHide: true,
			opacity: false,
			initialDisplayFx: false,
			onActive: function(el){
				el.addClass('l_active');
			},
			onBackground: function(el){
				el.removeClass('l_active');
			}
		});
		new Fx.Accordion($$('.trigger2'), $$('.a_content2'), {
			display: '-1',
			alwaysHide: true,
			opacity: false,
			initialDisplayFx: false,
			onActive: function(el){
				el.addClass('l_active');
			},
			onBackground: function(el){
				el.removeClass('l_active');
			}
		});
		new Fx.Accordion($$('.trigger3'), $$('.a_content3'), {
			display: '-1',
			alwaysHide: true,
			opacity: false,
			initialDisplayFx: false,
			onActive: function(el){
				el.addClass('l_active');
			},
			onBackground: function(el){
				el.removeClass('l_active');
			}
		});
	} 
	
	
	
	
	
		

	
	
	
	
	$('popup_form').setStyle('top',20);
	
	$$('.popup_show').each(function(item){
		item.addEvent('click',function(e){
			e.stop();
			var rel = item.getProperty('rel');
			var title = $(rel).getProperty('data:title');
			if(!title || title == ''){
				var title = $(rel).getElement('form').getProperty('data:title');
			}
			//$('content').setStyle('visibility','hidden');
			var html = $(rel).get('html');
			//$(rel).destroy();
			$('form_title').set('html', title);
			$('popup_content').set('html',html);
			$('callback_black').setStyle('display','block');
			$('popup_form').setStyle('display','block');
		
			if($('popup_content').getElements('.save_data').length > 0) {
				$('popup_content').getElements('.save_data').each(function(item, index){
						var id = item.getProperty('data:id');
						if(id){
							item.setProperty('id',id);
							item.addEvent('submit', function(e){
								//e.stop();
								if(checkForm(item)){
									new umiSaveFormData(item);	
									return true;
								}
								return false;
							});
						}
				});
			}
		});
		
	});
	$('callback_close').addEvent('click',function(e){
		e.stop();
		//$('content').setStyle('visibility','visible');
		$('callback_black').setStyle('display','none');
		$('popup_form').setStyle('display','none');
	});
	
	
	
	$('close_button').addEvent('click',function(e){
		$('all_city').setStyle('z-index','-1');
		$('callback_black').setStyle('display','none');
	});
	
	
	$('all-contacts').addEvent('click',function(e){
		$('all_city').setStyle('z-index',100);
		showAllCity();
	});
	
	
	
	
	var special_list = $('special_list');
	if(special_list){
		special_list.getElements('h2').each(function(item){
			
			if(item.getNext().tagName != 'TABLE') item.setStyle('display','none');
		});
	}
	
	
	$$('#content .wrap .center-img .index-cat ul li').each(function(item){
		var a = item.getElements('a');
		a.addEvents({
			'mouseover': function(){a.addClass('hover')},
			'mouseout': function(){a.removeClass('hover')}
		});
	});
	
	
	
	
	if( $('submit_order')) {$('submit_order').addClass('unactive');active_button();}
	if($('compare_button')) {
		$('compare_button').addClass('unactive');
		$('compare_button').addEvent('click', function(){
			var state = 0;
			$$('.add_to_compare').each(function(item){
				if(item.checked) state++;			
			});
			if(state > 1) {
				$('compare_button').getParent('form').submit();
			}
		});
	}
	
	
	checkInBasket();

	
});




function compare_button(){
	var button = function(butt, state){
	
		var form = butt.getParent('form');

		if(state > 1) {
			butt.addClass('active');
			butt.removeClass('unactive');
		}else {
			butt.addClass('unactive');
			butt.removeClass('active');
		}
	}
	var state = 0;
	
	$$('.add_to_compare').each(function(item){
		if(item.checked) state++;			
	});
	button($('compare_button'), state);
	return state;
}


function active_button(){
	var button = function(butt){
		if($('agree').checked) {
			butt.addClass('active');
			$('agree_error').setStyle('display','none');
			butt.removeClass('unactive');
			butt.removeEvent('click');
			butt.addEvent('click', function(e){
				e.stop();
				var form = this.getParent('form');
				form.action='/eshop/order/chd_orders/'; 
				form.submit();
			});
		}else {
			butt.addClass('unactive');
			butt.removeClass('active');
			butt.removeEvent('click');
		}
	}
	
	var butt = $('submit_order');
	$('agree').addEvent('click', function(e){
		
		
		button(butt);
	});
	
	$('submit_order').addEvent('mouseenter', function(e){
		button(butt);
	});
	$('submit_order').addEvent('click', function(e){
		if($('agree').checked) {
			$('agree_error').setStyle('display','none');
		}else {
			$('agree_error').setStyle('display','block');
		}
	});
	
}




/********************************************
*
* 	FORM CHECK
*
*********************************************/
/*
	CLASS
*/

var InputValidate = new Class ({	
	Implements: [Events, Options],
	options: {
		errorMsg: 'This field is required.'
	
	},
	initialize: function(options) {
		this.setOptions(options);
	},
	test: function(element){
		
		if (element.type == 'select-one' || element.type == 'select')
			return !(element.selectedIndex >= 0 && element.options[element.selectedIndex].value != '');
		else
			return ((element.get('value') == null) || (element.get('value').length == 0));
	},
	isEmpty: function(input){
		return this.test(input);
	},
	getMsg: function(){
		return this.options.errorMsg;
	},
	marked: function(input){
		if(input.getStyle('border-width').toInt() != 4){
			input.setStyle('width', (input.getStyle('width')).toInt() - 8);
			input.tween('border', '4px solid #ff0000');
			input.focus();
		}
	},
	unmarked: function(input){
		if(input.getStyle('border-width').toInt() == 4){
			input.setStyle('border-width', '1px');
			input.setStyle('width', (input.getStyle('width')).toInt() + 8);
			input.setStyle('border-color', '#b5bfc4');
		}
	}
});

/*
	Function
*/
	var isEmpty = new InputValidate({
		errorMsg: 'This field is required.'
	});
	
function checkForm(form){
	

	var inputs = $(form).getElements('input');
	var c = inputs.length;
	
	for(i=0; i<c; i++){
		var input = inputs[i];
		if(input.hasClass('required')){
			if(isEmpty.test(input)) {
				
				isEmpty.marked(input);
				
				return false;
			}
			else{
				isEmpty.unmarked(input);

			}
		}
	}

	return true;
}
/*
	The end of FormCheck
*/


function checkInBasket(){
	if(typeof(inBasket != 'undefined')){
		$$('.in_basket').each(function(item){
			var id = item.getProperty('data:id');
			if(id){
				if(typeof(inBasket[id]) != 'undefined'){
					var span = new Element('span');
					span.inject(item);
					var s = "в корзине " + inBasket[id] + " шт";
					span.set('html', s);
					item.setStyle('display','block');
				}
			}
		});
	}
}



function showAllCity(){
	var show = function(){
		$('all_city').setStyle('z-index',100);
		$('callback_black').setStyle('display','block');
		$('callback_black').addEvent('click', function(e){
			$('close_button').fireEvent('click');
		});
	}
	
	var close = function(){
		$('close_button').fireEvent('click');
	}
	
	//alert($('all_city').getStyle('z-index'));
	if($('all_city').getStyle('z-index') == 100) show();
	else close();
	
}



})(document.id);



var inBasket = new Array();
