window.addEvent('domready', function() {

	if($$('div.passive a')){
		$$('div.passive a').each(function(a){
			a.addEvent('mouseover', function(e){
				e.stop();
				a.getParent().getParent().addClass('active');
				//a.addClass('active');
			});
			a.addEvent('mouseout', function(e){
				e.stop();
				a.getParent().getParent().removeClass('active')
			});
		});
	}
	
	if($$('div.int_left_block','div.int_right_block')){
		$$('div.int_left_block a','div.int_right_block a').each(function(a){
			a.addEvent('mouseover', function(e){
				e.stop();
				a.getParent().getParent().addClass('active_block');
				//a.addClass('active');
			});
			a.addEvent('mouseout', function(e){
				e.stop();
				a.getParent().getParent().removeClass('active_block')
			});
		});
	}
	
	if($$('div#dop_navigation a')){
		$$('div#dop_navigation a').each(function(a){
			a.addEvent('mouseover', function(e){
				e.stop();
				a.getParent().addClass('active');
				//a.addClass('active');
			});
			a.addEvent('mouseout', function(e){
				e.stop();
				a.getParent().removeClass('active')
			});
		});
	}
	

/* Формы регистрации */
function parse_blocked_form(){
	if($$('input[block]') || $$('option[block]')){
		$$('input[block]','option[block]').each(function(he_block){
			to_block=he_block.get('block');
			value_block=he_block.value;
			if($(to_block)){
				$(to_block).getParent().getParent().setStyle('visibility','hidden');
				
				he_block.getParent().addEvent('change', function(e){
					e.stop();
					if(he_block.getParent().value==value_block) $(to_block).getParent().getParent().setStyle('visibility','hidden');
					else $(to_block).getParent().getParent().setStyle('visibility','visible')
				});
			}		
		});
	}
}

parse_blocked_form();

/* Галереи */
function make_gallery(start_i,gallery4make) {
	var tpl_url_gallery = new Array();
	gallery4make.each(function(image, i){
		tpl_url_gallery[i]=[image];
	});
	Slimbox.open(tpl_url_gallery, start_i, {loop: true});
}

if($('gallery_2008')){
	var gallery_czechia_2008 = new Array();
	for(var i=0, i_length=73; i<i_length; i++) {
		fotos_url='http://trainer.imaton.ru/img/foto2008/'+(i+1)+'.jpg';
		//fotos_url='/content/Image/czechia/2008/'+(i+1)+'.jpg';
		gallery_czechia_2008[i]=fotos_url;
	}
	$('gallery_2008').addEvent('click', function(e){
		e.stop();
		make_gallery(0,gallery_czechia_2008);
		return false;
	});
}
if($('gallery_2009')){
	var gallery_czechia_2009 = new Array();
	for(var i=0, i_length=82; i<i_length; i++) {
		fotos_url='http://trainer.imaton.ru/img/foto2009/'+(i+1)+'.jpg';
		//fotos_url='/content/Image/czechia/2009/'+(i+1)+'.jpg';
		gallery_czechia_2009[i]=fotos_url;
	}
	$('gallery_2009').addEvent('click', function(e){
		e.stop();
		make_gallery(0,gallery_czechia_2009);
		return false;
	});
}

	if($$('img.slim_img')) {
		var img_num=$$('img.slim_img').length;
		var slim_images = new Array();
		$$('img.slim_img').each(function(image,i){
			slim_images[i]=image.getParent().get('href');
			image.addEvent('click', function(e){
				e.stop();
				//alert(i + ' / ' + img_num);
				//Slimbox.open(slim_images, i, {loop: true});
				gallery_tpl_content(i);
				return false;
			});
		 });
	}
	
	function gallery_tpl_content(start_i) {
		var url_gallery = new Array();
		slim_images.each(function(image, i){
			url_gallery[i]=[image];
		});
		Slimbox.open(url_gallery, start_i, {loop: true});
	}


/* Списки */
if($$('div.div_list')){
	$$('div.div_list').each(function(i_list){
		i_list.addEvent('mouseover', function(e){
			e.stop();
			i_list.setStyle('background','#f5f5f5')
		});
		i_list.addEvent('mouseout', function(e){
			e.stop();
			i_list.setStyle('background','none')
		});
		if(i_list.hasClass('list_href')){
			i_list.addEvent('click', function(e){
				e.stop();
				document.location=i_list.getElement('h5').getElement('a').get('href');
			});
		}
	});
}

/* Сортировка таблицы */
if($('sort_me')) {
	var backup_table=new Array();
	var backup_tr=new Array();
	$('sort_me').getElement('tbody').getElements('tr').each(function(tr_i,i){
		backup_tr[i]=tr_i.innerHTML;
		backup_table[i]=new Array();															 
		backup_table[i][0]=tr_i.getChildren()[0].get('sorttable_customkey');
		backup_table[i][1]=i;
	});
	sort_table=backup_table.sort();
	var tr_result='<table width="90%">' + $('sort_me').getElement('thead').innerHTML;

	var today = new Date();
	var today_customkey = today.getFullYear()+(((today.getMonth()-""+1)<10) ? '0':'')+(today.getMonth()-""+1)+((today.getDate()<10) ? '0':'')+today.getDate();
	//var today_customkey="20100416";
	
	$('sort_me').getElement('tbody').getElements('tr').each(function(tr_ii,ii){
		var tr_number=sort_table[ii][1];
		if(($('sort_me').getElement('tbody').getElements('tr')[tr_number].getChildren()[0].get('sorttable_customkey')-"")>=(today_customkey-"")){
			var new_tr=backup_tr[tr_number];
			tr_result=tr_result + '<tr>' + new_tr + '</tr>';
		}
	});
	
	tr_result=tr_result+'</table>';
}

if($('sort_me')) {
	$('sort_me').destroy();
	$('sorted_result').innerHTML=tr_result;
}

/* Обработка корзины и заказов */
if($('span_basket')){
	new Request({
		url: '/cart.php',
		method: 'post',
		onSuccess: function(responseText, responseXML) {
			if(responseText!=""){
				productsIDs=responseText.split(',');
				num_of_products=productsIDs.length;
				HTMLbasket='<small>(заказов: '+num_of_products+')</small>';
				$('span_basket').set('html',HTMLbasket);
				if($$('.add2basket')){
					for(i=0;i<num_of_products;i++){
						tr=eval("$$('tr[productid=" + '"' + productsIDs[i] + '"' + "]')");
						if(tr) {
							HTMLafterLoad='<small>в корзине</small>';
							tr.getElement('td.add2basket').set('html',HTMLafterLoad);
						}
					}
				}
			}
		},			
		data: {
			goodid: '0'					
		}
	}).send();	
}


if($$('.add2basket')){
	$$('.add2basket').each(function(basket_a,i){
		basket_a.getElement('a').addEvent('click', function(e){
				e.stop();
				var local_productID=basket_a.getParent().get('productID');
				var backUP_a=basket_a.get('html');
				new Request({
					url: '/cart.php',
					method: 'post',
					onRequest: function(){
						basket_a.set('html','<img src="/trainer/interface/images/spinner.gif"');
					},
					onSuccess: function(responseText, responseXML) {
						HTMLafterLoad='<small>в корзине</small>';
						basket_a.set('html',HTMLafterLoad);
						HTMLbasket='<small>(заказов: '+responseText+')</small>';
						$('span_basket').set('html',HTMLbasket);
					},
					onFailure: function(responseText, responseXML) {
						alert('Ошибка при добавлении в корзину! Попробуйте, пожалуйста, ещё раз...')
						basket_a.set('html',backUP_a);
						//alert(responseText)
					},					
					data: {
						goodid: local_productID					
					}
				}).send();
				
			});
	});
}

if($$('.delfrombasket')){
	$$('.delfrombasket').each(function(del_a,i){
		del_a.addEvent('click', function(e){
				e.stop();
				var local_productID=del_a.getParent().getParent().get('productID');
				var backUP_a=del_a.getParent().get('html');
				new Request({
					url: '/cart.php',
					method: 'post',
					onRequest: function(){
						del_a.getParent().set('html','<img class="delfrombasket_spinner" src="/trainer/interface/images/spinner.gif"');
						del_spinner=$$('.delfrombasket_spinner');
					},
					onSuccess: function(responseText, responseXML) {
						del_spinner.getParent().getParent().destroy();
						HTMLbasket='<small>(заказов: '+responseText+')</small>';
						$('span_basket').set('html',HTMLbasket);					
					},
					onFailure: function(responseText, responseXML) {
						alert('Ошибка при удалении из корзины! Попробуйте, пожалуйста, ещё раз...')
						del_spinner.getParent().set('html',backUP_a);
						//alert(responseText)
					},					
					data: {
						goodid: local_productID,
						delfromcart: local_productID 
					}
				}).send();
				
			});
	});
}

});






