function reDate(obj){
	var date = obj.innerHTML.split('.');
	var r = '';
	r += date[0];
	r += ' ';
	switch (date[1]){
		case '01':
			r += 'января';
		break;
		case '02':
			r += 'февраля';
		break;
		case '03':
			r += 'марта';
		break;
		case '04':
			r += 'апреля';
		break;
		case '05':
			r += 'мая';
		break;
		case '06':
			r += 'июня';
		break;
		case '07':
			r += 'июля';
		break;
		case '08':
			r += 'августа';
		break;
		case '09':
			r += 'сентября';
		break;
		case '10':
			r += 'октября';
		break;
		case '11':
			r += 'ноября';
		break;
		case '12':
			r += 'декабря';
		break;
	}
	r += ' ';
	r += date[2];
	obj.innerHTML = r;
}
$$('.date').each(function(item){
	reDate(item);
});

function doload(a){
	if(!a) return;
	var update = new Element('div', {
		'html': 'Загрузка...',
		'style': 'margin: 10px 0 0 0'
	});
	update.inject(a.getParent('div.content3'));
	var ajax = new Request.HTML({
		'update': update,
		'url': a.href,
		onSuccess: function(){
			a.setStyle('display', 'none');
		}
	});
	ajax.send();
}

$$('a[target="_self"]').each(function(a){
	a.addEvent('click', function(e){
		Event(e).stop();
		doload(a);
	});
});

var ie7 = (navigator.appVersion.indexOf('MSIE 7.0') > 0) ? true : false;

$$('#left .menu ul.list ul.level2').each(function(level2){
	level2.setStyle('display', 'block');
		
	var width = level2.getSize().x;
	level2.getElements('.level3').each(function(level3){
		level3.setStyle('left', width - 16);
	});
	
	level2.setStyle('display', '');
});

if(ie7){
	$$('#left ul.list ul.level2').each(function(ul){
		ul.setStyle('display', 'block');
		var w = 0;
		var w3 = 0;
		ul.getElements('li').each(function(li){
			var w_li = li.getSize().x;
			if(w_li > w) w = w_li;
		});
		
		ul.getElements('li').each(function(li){
			li.getElements('.level3').each(function(ul3){
				ul3.setStyle('display', 'block');
				ul3.getElements('li a').each(function(li3){
					var w_li3 = li3.getSize().x;
					w3 = (w_li3 > w3) ? w_li3 : w3;
				});
				
				ul3.setStyle('display', '');
			});
			
			li.setStyle('width', w);
			
		});
		
		ul.getElements('li .level3 li').each(function(li3){
				//alert(w3);
				li3.setStyle('width', w3);
		});
		ul.setStyle('display', '');
	});
}


var LeftMenu = new Class({
	Implements: Options,

	options: {
		'top_lvl': '',
		'lvl1': "ul.level1 li",
		'lvl2': "ul.level2 li",
		'lvl3': "ul.level3 li"
	},
	
	initialize: function(options){
		this.setOptions(options);
		
		// переменная интервалов
		this.t = new Array();
		
		// наведение 
		$$(
				this.options.top_lvl + ' ' + this.options.lvl1,
				this.options.top_lvl + ' ' + this.options.lvl2
			).each(function(li1, n){
				li1.addEvent('mouseover', function(){
					li1.addClass('hover');
					clearTimeout(this.t[n]);
				}.bind(this))
		}.bind(this));
		
		// mouseout
		$$(
				this.options.top_lvl + ' ' + this.options.lvl1,
				this.options.top_lvl + ' ' + this.options.lvl2
			).each(function(li1, n){
				li1.addEvent('mouseout', function(){
					this.t[n] = setTimeout(function(){
						li1.removeClass('hover');
					}, 15);
				}.bind(this));
		}.bind(this));
	}
});

new LeftMenu({
	top_lvl: '#left .menu',
	lvl1: 'ul.list li'
});

function redrow_wrap(){
    var last_li = $$('#top_menu ul li').pop().getCoordinates();
    $('wrap').setStyle('min-width', last_li.right + 352);
}

redrow_wrap();


function addWmode(){
    $$('object').each(function(o, key){
		var url = o.getElements('param[name="movie"]')[0];
		url = url.get('value');
		
		if(url.indexOf('youtube.com/v/') < 0) return ;

		var width = o.get('width');
		var height = o.get('height');
		var flashvars = {};
		var param = {
			'wmode': 'opaque',
			'allowFullScreen': 'true'
		};
		var attributes = {};

		var id = 'new_o_' + key;
		var span = new Element('span', {
			'id': id
		});
		span.inject(o, 'before');

		o.dispose();

		swfobject.embedSWF(url, id, width, height, "9.0.0", "", flashvars, param, attributes);
    });
}

if(navigator.userAgent.indexOf("MSIE") < 0) addWmode();


var download_img = new Class({
    Implements: Options,

    options: {
	links: [],
	url: '/download_img.php'
    },

    initialize: function(options){
	this.setOptions(options);

	this.options.links.each(function(a){
	    //a.href = this.options.url + '?url=' + encodeURI(a.href);
	    //a.target = '';
	}.bind(this));
    }
});

var links = new Array();
var i = 0;
$$('#right a').each(function(a){
    if(a.href.indexOf(".jpg") > 0){
	links[i] = a;
	i++;
    }
    return;
});

new download_img({
    'links': links
});

var Tree = new Class({
	Implements: Options,
	
	options: {
		'root': null,
		'autoexpand': null,
		'expanded_class': 'expanded',
		'collapsed_class': 'collapsed',
		'expanded_class_a': 'expanded',
		'collapsed_class_a': 'collapsed'
	},
	
	initialize: function(options){
		this.setOptions(options);
		
		if(!this.options.root) return;
		
		this.init();
	},
	
	init: function(){
		this.options.root.getElements('li').each(function(li){
			var submenu = li.getElement('ul');
			if(submenu){
				this.expand(this.addA(li));
			}
		}.bind(this));
	},
	
	addA: function(li){
		var a = new Element('a', {
			'href': '#',
			'html': '+',
			'class': 'plus',
			'events': {
				'click': function(e){
					Event(e).stop();
					this.expand(e.target);
				}.bind(this)
			}
		});
		a.inject(li, 'top');
		return a;
	},
	
	expand: function(a){
		if(!a) return;
		a.toggleClass('collapse');
		a.getParent('li').getElement('ul').toggleClass('collapse');
		if(a.hasClass('collapse')) {
			a.innerHTML = '+';
		} else {
			a.innerHTML = '&#8211;';
		}
	}
})

var ul = $$('#catalog_tree ul')[0];

// expand opened
if(ul){
	
	var tree = new Tree({
		'root': ul
	});


	var current = ul.getElement('li.current');
	if(current) var parent_li = current.getParent('li');
	if(current && parent_li) tree.expand(parent_li.getElement('a.plus'));

	var collection = new Array();
	$$('#left .menu, #left .h3_block, #left .news').each(function(item, key){
		collection[key] = item;
		//item.setStyle('display', 'none');
	});	
	
	var exit = new Element('li', {
		'html': 'Выход из Магазина',
		'class': 'exit',
		'events': {
			'click': function(e){
				Event(e).stop();
				$('catalog_tree').setStyle('display', 'none');
				$('mag_all_menu').setStyle('display', 'block');
				
				$$('#left .menu ul.list ul.level2').each(function(level2){
					level2.setStyle('display', 'block');
						
					var width = level2.getSize().x;
					level2.getElements('.level3').each(function(level3){
						level3.setStyle('left', width - 16);
					});
					
					level2.setStyle('display', '');
				});
				
				/*collection.each(function(item){
					item.setStyle('display', 'block');
				});*/
			}
		}
	});
	//exit.inject(ul);
}
