var NO_OVER = false;
var brand_timeout = null, last_item_timeout = null;
var left_banner;
function __load(){
	var $ = this;
	brand_images = $('brand-images');
	init_brands($);
	init_prd_dropdown($);
	init_news($);
	init_brand_image($);
	if(ie6){
		fix_other_brands($);
		fix_brand_image($);
	}
	init_arrows($);
	init_poll($);
	check_cols_height($);
	init_search($);
	init_tables($);
	init_right_block($);
	init_glass_hover($);
	fix_pager($);
	fix_buttons($);
	set_left_menu($);
	set_arrow_titles({
		prev: 'На100ящий', next: 'Брест-Литовск'
	});
}

var brand_images;
var brand_classes = {
	blue: '',
	white: 'leto',
	orange: 'montik',
	sepia: 'brest',
	brown: 'nax',
	green: 'na100'
};
var brand_names = [
	{type: 'blue', name: 'Савушкин'},
	{type: 'sepia', name: 'Брест-Литовск'},
	{type: 'orange', name: 'Монтик'},
	{type: 'white', name: 'Ласковое лето'},
	{type: 'brown', name: 'На хлебушек'},
	{type: 'green', name: 'На100ящий'}
];
function get_arrow_titles(type){
	var cur_ind;
	brand_names.each(function(obj, ind){
		if(obj.type == type){
			cur_ind = ind;
			throw $break;
		}
	});
	if(cur_ind == 0){
		return {
			prev: brand_names.last().name,
			next: brand_names[1].name
		}
	}else if(cur_ind == brand_names.length - 1){
		return {
			prev: brand_names[cur_ind - 1].name,
			next: brand_names[0].name
		}
	}else{
		return {
			prev: brand_names[cur_ind - 1].name,
			next: brand_names[cur_ind + 1].name		
		}
	}
}
var ie = !window.getComputedStyle && document.getElementById;
var ie6 = ie && navigator.appVersion.indexOf('MSIE 7') == -1;
var brand_items;
var last_hover_item;
function init_brands($){
	var root = $('brands');
	if(!root){
		return false;
	}
	root.items = $C('item', root, 'DIV');
	root.items.each(init_brand_item.bind(root));
	brand_items = root;
}
function init_brand_item(item){
	item.root = this;
	item.type = item.className.split(' ').last();
	var arr_classes = ['png-dd', 'dd-bgr', 'dd-top', 'dd-bottom', 'dd-content'];
	var arr = $C_arr(arr_classes, item, 'DIV');
	arr_classes.each(function(cName, ind){
		item[cName.replace('-', '_')] = arr[ind];
	});
	var pad_val = 21;
	var cur_height = item.dd_top.offsetHeight + item.dd_bottom.offsetHeight;
	var cont_height = item.dd_content.offsetHeight;
	if(cur_height < (cont_height + pad_val * 2)){
		item.dd_bgr.style.height = (cont_height + pad_val * 2) - cur_height + "px";
	}
	item.addClass('item-i');
	item.onmouseover = function(ev){
		if(this.HERE){
			return false;
		}
		this.HERE = true;
		if(last_hover_item){
			last_hover_item.onmouseout(1);
		}
		if(ev != 1){
			brand_timeout = setTimeout(function(){
				if(window.brand_images){
					brand_images.className = brand_classes[this.type];
				}
				this.addClass('over');
				last_hover_item = this;
				set_arrow_titles(get_arrow_titles(this.type));
			}.bind(this), 600);
		}else{
			if(window.brand_images){
				brand_images.className = brand_classes[this.type];
			}
			this.addClass('over');
			last_hover_item = this;		
			set_arrow_titles(get_arrow_titles(this.type));
		}
	};
	item.onmouseout = function(e){
		if(NO_OVER){
			return false;
		}
		var ev = e || window.event;
		if(ev != 1){
			var relTarget = ev.toElement || ev.relatedTarget;
			
			if(this == relTarget || _isChild(relTarget, this) || !relTarget){
				return false;
			}
		}
		if(brand_timeout){
			clearTimeout(brand_timeout);
			brand_timeout = null;
		}
		if(window.brand_images){
			brand_images.className = '';		
		}
		this.HERE = false;
		this.removeClass('over');
	};
	item._img = $C('img-small', item, 'IMG', 1);
	item._img_link = $C('img-link', item, 'A', 1);
	item._img_link.style.width = item._img.width + "px";
	item._img_link.style.height = item._img.height + "px";
	item._img_link.style.left = Math.floor((item._img_link.parentNode.offsetWidth - item._img.width) /  2) + "px";

	item._img.style.top = Math.floor((item.offsetHeight - item._img.height) / 2) + "px";
	if(ie6){
		png_fix(item._img);
		item._temp_div = $C("temp", item, "DIV", 1);
		item._temp_img = $T("img", item._temp_div, 1);
		png_fix(item._temp_img);
		item._temp_div.style.height = item._temp_img.height + "px";
		item._temp_img.style.position = "absolute";
	}

	item.dropdown = $C('png-dd', item, 'DIV', 1);
	item.dd_link = document.createElement("A");
	item.dd_link.href = item._img_link.href;
	item.dd_link.style.display = "block";
	item.dd_link.style.position = "absolute";
	item.dd_link.style.left = "0";
	item.dd_link.style.top = "0";
	item.dd_link.style.width = item.dropdown.offsetWidth + "px";
	item.dd_link.style.height = item.dropdown.offsetHeight + "px";
	item.dd_link.style.zIndex = '100';
	item.dd_link.style.backgroundImage = 'url(' + root + 'public/i/blank.gif)';
	item.dropdown.appendChild(item.dd_link);
}
function init_prd_dropdown($){
	var root = $('all-production') || $('all-production_tm');
	if(!root){
		return false;
	}
	var arr = $C_arr(['button', 'js'], root, 'DIV');
	root.button = arr.shift();
	root._div = arr.shift();
	root.span = $T('SPAN', root, 1);
	root.button.onclick = root.span.onclick = function(){
		if(this._div.hasClass('open')){
			this._div.removeClass('open');
			if(left_banner){
				left_banner.style.display = 'block';
			}
		}else{
			this._div.addClass('open');
//			alert(left_banner.offsetTop);
			if(left_banner){
				if(left_banner.offsetTop < 400){
					left_banner.style.display = 'none';
				}
			}
		}
	}.bind(root);
	root.span.onmouseover = function(){
		this.addClass('s-over');
	};
	root.span.onmouseout = function(){
		this.removeClass('s-over');	
	};
	var left_col = $('left-col');
	if(left_col){
		left_banner = $C('banner', left_col, 'DIV', 1);
	}
}
function init_news($){
	var root = $('right-col');
	if(!root){
		return false;
	}
	root.items = $C('news-item', root, 'DIV');
	root.items.each(function(item){
		item.root = root;
		var arr = $C_arr(['js-state', 'head'], item, 'DIV');
		item.state_div = arr.shift();
		item.head_div = arr.shift();
		if(item.OPEN = item.state_div.hasClass('open')){
			root.here_item = item;
		}
		Object.extend(item, news_item_funcs);
		$A(item.head_div.childNodes).each(function(node){
			if(node.nodeType == 1){
				node.onclick = item.toggle.bind(item);
			}
		});
		if(ie6){
			item.span = $T('SPAN', $C('head', item, 'DIV', 1), 1);
			item.span.onmouseover = function(){
				this.addClass('over');
			};
			item.span.onmouseout = function(){
				this.removeClass('over');
			};
		}
	});
}

var saveState = function () {
	
	var green_open = jQuery('.green .open').html() ? '1' : '0';
	var blue_open = jQuery('.blue .open').html() ? '1' : '0';
	
	jQuery.cookie('green_open', green_open, {expires: 10, path: '/'});
	jQuery.cookie('blue_open', blue_open, {expires: 10, path: '/'});
	
}

var news_item_funcs = {
	open: function(){
		if(this.root.here_item){
			this.root.here_item.close();
		}
		this.OPEN = true;
		this.state_div.addClass('open');
		this.root.here_item = this;
	},
	close: function(){
		this.OPEN = false;
		this.state_div.removeClass('open');
		this.root.here_item = null;
	},
	toggle: function(){
		if(this.OPEN){
			this.close();
		}else{
			this.open();
			this.root.items.not(this).close();
		}
		saveState();
		check_cols_height($);
		
	}
};
function init_brand_image($){
	var root = $('brand-image');
	if(!root){
		return false;
	}
	var img = $T('IMG', root, 1);
	img.style.top = Math.floor((root.offsetHeight - img.height) / 2) + "px";

	jQuery('#red #brand-image img').css('top', 0);
}
var test_colors = ['blue', 'pink', 'orange', 'yellow', 'brown', 'green'];
/*************** load ****************/

function png_fix(image){
	image._src = image.src;
	image._w = image.width;
	image._h = image.height;
	image.style.width = image._w + 'px';
	image.style.height = image._h + 'px';
	var name = image.src.substr(image.src.lastIndexOf('\/') + 1);
	image.src = image.src.replace(name, 'blank.gif');
	image.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + image._src + "', sizingMethod='crop')";
}
function fix_other_brands($){
	var root = $("other-brands");
	if(!root){
		return false;
	}
	var images = $T('img', root);
	images.each(png_fix);
}
function fix_brand_image($){
	var root = $('brand-image');
	if(!root){
		return false;
	}
	var img = $T('IMG', root, 1);
	png_fix(img);
}
var a_left, a_right;
function init_arrows($){

	a_left = $('glass-left');
	a_right = $('glass-right');
	a_left.onclick = function(){
		if(brand_items){
			var ind = last_hover_item ? brand_items.items.indexOf(last_hover_item) : 0;
			if(!ind){
				ind = brand_items.items.length - 1;
			}else{
				ind = ind - 1;
			}
			var it = brand_items.items[ind];
			it.onmouseover(1);
			last_hover_item = it;
		}
	};
	a_right.onclick = function(){
		
		if(brand_items){
			var ind = last_hover_item ? brand_items.items.indexOf(last_hover_item) : 0;
			if(ind == brand_items.items.length - 1){
				ind = 0;
			}else{
				ind = ind + 1;
			}
			var it = brand_items.items[ind];
			it.onmouseover(1);
			last_hover_item = it;
		}
	};
	if(ie6){
		a_left.onmouseover = a_right.onmouseover = function(){
			this.addClass("over");
		};
		a_left.onmouseout = a_right.onmouseout = function(){
			this.removeClass("over");
		};
	}
}
function set_arrow_titles(obj){
	if(!a_left){
		return false;
	}
	a_left.title = obj.prev;
	a_right.title = obj.next;
};
function init_poll($){
	var root = $("poll");
	if(!root){
		return false;
	}

	root._form = $T("FORM", root, 1);
	if(!root._form){
		return false;
	}
	root.rad_val_input = document.createElement("INPUT");
	root.rad_val_input.type = "hidden";
	root.rad_val_input.name = "";
	root.rad_val_input.value = "";
	root._form.appendChild(root.rad_val_input);

	root.items = $C("radio", root, "DIV");
	root.items.each(init_radio.bind(root));
	
}
function init_radio(item){
	item.root = this;
	item._round = $C("round", item, "DIV", 1);
	item._label = $C("label", item, "DIV", 1);
	item._input = $T("INPUT", item, 1);
	if(this.rad_val_input.name == ""){
		this.rad_val_input.name = item._input.name;
	}
	item.check = function(){
		if(this.CHECKED){
			return false;
		}
		if(this.root.checked){
			this.root.checked.uncheck();
		}
		this.CHECKED = true;
		this.addClass("selected");
		this.root.rad_val_input.value = this._input.value;
		this.root.checked = this;
	};
	item.uncheck = function(){
		if(!this.CHECKED){
			return false;
		}
		this.removeClass("selected");
		this.CHECKED = false;
		this.root.checked = null;
	};
	item._round.onclick = item._label.onclick = function(){
		this.check();
	}.bind(item);
}
function check_cols_height($){
	var right_col = $("right-col");
	if(!right_col){
		return false;
	}
	var left_col = $("left-col");
	var center_col = $("center-col");
	left_col.style.height = "auto";
	if(right_col.offsetHeight > left_col.offsetHeight && right_col.offsetHeight > center_col.offsetHeight){
		left_col.style.height = right_col.offsetHeight + "px";
	}
}
AttachEvent(window, 'load', __load.bind($$));
function init_search($){
	var root = $("search-form");
	if(!root){
		return false;
	}
	root.OPEN = false;
	root._button = $I("image", root, 1);
	root._form = $T("form", root, 1);
	root._form.onsubmit = function(){
		if(!this.OPEN){
			this.OPEN = true;
			this.addClass("f-open");
			document.onclick = this.document_click.bind(this);
			return false;
		}else{
			return true;
		}

	}.bind(root);
	root.document_click = function(e){
		var ev = e || window.event;
		var target = ev.target || ev.srcElement;
		if(target == this || _isChild(target, this)){
			return true;
		}
		this.OPEN = false;
		this.removeClass("f-open");
		document.onclick = null;
	};
}
function init_tables($){
	var root = $("text-content");
	if(!root){
		return false;
	}
	var tables = $C("tbl",root, "table");
	if(!tables || !tables.length){
		return false;
	}
	tables.each(function(tbl){
		var rows = $T("TR", tbl);
		rows.each(function(row, ind){
			if(!ind){
				throw $continue;
			}
			if(!(ind % 2)){
				row.className = "zebra";
			}
			var cells = $T("TD", row);
			cells.last().className = "last";
		});
	})
}

function openDDMenu(node) {
	
	var root = $("dd_menu");
	var items = $C("item", root);
	
	items.each(function(item){
	
		item.removeClass('here');
		
	});
	
	node.addClass('here');
	var but = jQuery('#all-production .button')[0];
	if(!but){
		but = jQuery('#all-production_tm .button')[0];
	}
	setTimeout(function(){but.onclick();but.onclick()}, 1);
	
}
function init_glass_hover(){
	var links = $C("glass-link", null, "A");
	if(!links){
		return false;
	}
	var buttons = $C("glass-button", null, "DIV");
	buttons.each(function(b){
		var type = b.id.split("-").last();
		b._link = links.detect(function(link){
			return link.id.split("-").last() == type;
		});
		b._link.onmouseover = function(){
			this.addClass("b-over");
		}.bind(b);
		b._link.onmouseout = function(){
			this.removeClass("b-over");
		}.bind(b);
	});
}

function init_news2($){
	var root = $('right-col');
	if(!root){
		
	}
}
var right_block;
function init_right_block($){
	right_block = $('right-block');
	if(!right_block){
		return false;
	}
	resize_right_block($);
}
function resize_right_block($){
	var h = right_block.offsetHeight + 323;
	var h_root = $("right-col");
	if(ie6){
		h_root.style.height = h + "px";
	}else{
		h_root.style.minHeight = h + "px";	
	}
}
function fix_pager($){
	var root = $('paging');
	if(!root){
		return false;	
	}
	var kids = $A(root.childNodes);
	kids.each(function(kid){
		if(kid.nodeType != 1){
			kid.parentNode.removeChild(kid);
		}
	});
}
window.onresize = function(){
	if(right_block){
		resize_right_block(this);
	}
}.bind($$);
function fix_buttons($){
	var elem = $('text-content');
	if(!elem){
		return false;
	}
	var buttons = $C('btn', elem, 'INPUT');
	if(buttons.length){
		buttons.each(function(but){
			var img_left = $Node(document.createElement('img'));
			img_left.src = root + 'public/i/b-left.gif';
			img_left.className = 'img-left img-but';

			var img_right = $Node(document.createElement('img'));
			img_right.src = root + 'public/i/b-right.gif';
			img_right.className = 'img-right img-but';

			but.parentNode.appendChild(img_right);
			but.parentNode.insertBefore(img_left, but);
			if(window.opera){
				img_right.addClass('opera');
				img_left.addClass('opera');
			}
		});
	}
}

function set_left_menu($){
	return false;
	var root = $('left-menu');
	if(!root){
		return false;
	}
	var heres = $C('here', root, 'DIV');
	heres.each(function(here){
		if(here == heres.last()){
			throw $break;
		}
		var h_div = $C('h-div', here, 'DIV', 1);
		h_div.addClass('h-blue');
	});
}
if(!window.showImage){
	window.showImage = function(){};
}