var EasyWays = {
	init: function(){
		this.tablesMakeup();
		if ($('root')) {this.menu.init($('root'));}
		this.roundConners();
		if($$('#middle #breadcrumb').size()!=0){this.verticalAlignMiddile(21);};
		if(typeof(specialOffers)!="undefined"){
			EasyWays.carusel.init(specialOffers.container,specialOffers.count);
		}
		if($('gallery')){ EasyWays.gallery.init(); }
	},
	verticalAlignMiddile: function(padding){
		var containerHeight  = $('middle').getHeight()-padding;
		var breadcrumbHeight  = $('breadcrumb').getHeight();
		var menu = $('menu');
		var companyLogo = $('company-logo');
		var menuHeight  = menu.getHeight();
		var companyLogoHeight = companyLogo.getHeight();
		if( companyLogoHeight == containerHeight ){
			var margin = (containerHeight - menuHeight - breadcrumbHeight)/2;
			menu.setStyle({marginTop    : margin + 'px',marginBottom : margin + 'px'});
		}else{
			var margin = (containerHeight - companyLogoHeight)/2;
			companyLogo.setStyle({marginTop    : margin + 'px',marginBottom : margin+ ' px'});
		}
	},
	roundConners: function(){
		var blocks = $$('.rounded');		
		if(blocks.size()!=0){
			blocks.each(function(item){
				if(!item.hasClassName('no-top'))    new Insertion.Top(item.up(),'<div class="top"><b class="b6"></b><b class="b5"><em></em></b><b class="b4"><em></em></b><b class="b3"><em></em></b><b class="b2"></b><b class="b1"><em></em></b></div>');
				if(!item.hasClassName('no-bottom')) new Insertion.Bottom(item.up(),'<div class="bottom"><b class="b1"><em></em></b><b class="b2"></b><b class="b3"><em></em></b><b class="b4"><em></em></b><b class="b5"><em></em></b><b class="b6"></b></div>');
			});
		}
	},
	tablesMakeup: function(){		
		$$('table').each(function(table){if(table.readAttribute('border')>0){table.addClassName('makeup');}});
	}
}
EasyWays.menu = {
	lineCount : 1,
	lines     : [], /// [height:number,items:[]]
	container : {
		width   : null,
		element : null
	},
	timer:null,
	curNode: null,
	menuBlock : new Template('<div class="mline gray" id="mline#{lineNum}" style="z-index:#{layout}"><div class="rounded"><ul class="level1" id="line#{lineNum}"></ul><div class="cleaner"><!-- --></div></div></div>'),  
	init : function(container){
		this.container.width   = container.getWidth();
		this.container.element = container;
		this.createLines();
		this.makeupLines();
		this.lines.each(function(line){
			line.items.each(function(item){
				this.initSubs(item);
			},this);
		},this);
	},
	initSubs: function (item){
		item.childElements().each(function(node){
			if (node.tagName.toLowerCase() == "ul") {				
				item.onmouseover = function(){EasyWays.menu.subOpen(node);}
				item.onmouseout = function(){EasyWays.menu.subClose(node);}
				this.initSubs(node);
			}else if(node.tagName.toLowerCase() == "li"){this.initSubs(node);}			
		},this);		
	},
	createLines : function(){
		var lineWidth  = 0;
		var lineHeight = 0;
		var startIndex = 0;
		var line = {items:[],width:0,height:0};
		this.container.element.childElements().each(function(item,index){
			var itemWidth = item.getWidth();
			if((itemWidth)>266){
				itemWidth = 233;
				item.setStyle({width:'233px'});
			}
			line.width = line.width + itemWidth;
			if (line.width > this.container.width) {
				this.lines.push(line);
				line = {items:[],width:itemWidth,height:0};
				this.lineCount++;
			}
			var itemHeight = item.getHeight();
			if(itemHeight>line.height) line.height=itemHeight;
			line.items.push(item);
		},this);
		this.lines.push(line);		
	},
	makeupLines : function(){
		this.lines.each(function(line){
			line.items.each(function(item){
				var margin = (line.height - item.getHeight())/2;
				if (margin > 0) {
					item.setStyle({
						paddingTop: margin + 'px',
						paddingBottom: margin + 'px'
					});
				}	
			});
		});
		this.lines.each(function(line,index){
			var lineNum = index+1;
			var layout = 50-index;
			if (lineNum > 1) {
				new Insertion.Bottom($('menu'), this.menuBlock.evaluate({lineNum: lineNum,layout:layout}));
				line.items.each(function(item){$('line' + lineNum).insert(item);
				}, this);
			}			
		},this);
	},
	subOpen : function(sub){
		var offset = sub.up().positionedOffset();
		if(sub.hasClassName("level2")){
			sub.setStyle({
				left:0,
				top: sub.up().getHeight() + 'px',
				display : 'block'			 
			});
		}else{			
			sub.setStyle({
				top  : 0 - 1 - sub.up().getHeight() + 'px',
				left : '201px',
				display : 'block'
			});
		}
		return true;		
	},
	subClose : function(sub){sub.setStyle({display:'none'});}
}
EasyWays.gallery = {
	current: 0,
	images : null,
    init: function(){
        this.images = $$('.thumb img');
        this.images.each(function(item,index){
        	item.observe('click', function(){
        		EasyWays.gallery.show(this,index);
        	});
        },this)
        $$('#big_foto .nav_ff a').each(function(item,index){
        	item.observe('click', function(){
        		EasyWays.gallery.next();
        	});
        });
        $$('#big_foto .nav_rew a').each(function(item,index){
        	item.observe('click', function(){
        		EasyWays.gallery.prew();
        	});
        });
        this.show(this.images[this.current], this.current);
	},
    show: function(img,index){
        this.current = index;
        image = $("view_foto");
        image.readAttribute('src');
        image_path_parts = image.readAttribute('src').split('/');
        image_path_parts.pop();
        image_path = image_path_parts.join("/") + '/' + $(img).readAttribute('src').split('/').pop();
        image.writeAttribute({ src: image_path });
        $$('#big_foto .nav_ff').each(function(item,index){ item.show(); });
    	$$('#big_foto .nav_rew').each(function(item,index){ item.show(); });
        if( this.current == 0) {
    		$$('#big_foto .nav_rew').each(function(item,index){ item.hide(); });
    	}
    	if( this.current + 1 > this.images.size() - 1 ){
    		$$('#big_foto .nav_ff').each( function(item,index){ item.hide(); } );
    	}
    },
    next: function(){
    	this.current ++;
    	this.show(this.images[this.current], this.current);
    },
    prew: function(){
    	this.current = this.current - 1;
    	this.show(this.images[this.current], this.current);
    }
};

EasyWays.carusel = {
    timerId: null,
    step: null,
    scrolledItems: 0,
    itemsCount: null,
    autoScrollNapr: 'left',
    itemWidth: 288,
    position: null,
    type: null,
    next: false,
    previous: false,
    line: null,
    nextButton: null,
    previousButton: null,
    init: function(el, count){
        this.itemsCount = count;
        this.element = $(el);
        this.conteiner = this.element.down('.container');
        Event.observe(window, 'resize', function(){
            EasyWays.carusel.itemsToScroll();
        });
        this.line = this.conteiner.down('ul');
        this.nextButton = this.element.down('.next').down('.button');
        this.previousButton = this.element.down('.previous').down('.button');
        this.itemsToScroll();
        this.nextEvents();
        this.timerId = setTimeout(function(){
            EasyWays.carusel.autoScroll();
        }, 5000);
    },
    nextEvents: function(){
        this.next = true;
        this.nextButton.writeAttribute({
            className: 'button_enabled'
        });
        this.nextButton.observe('click', function(){
            clearTimeout(EasyWays.carusel.timerId);
            EasyWays.carusel.scroll('left');
        });
        this.nextButton.observe('mouseover', function(){
            this.writeAttribute({
                className: 'button_active'
            });
        });
        this.nextButton.observe('mouseout', function(){
            this.writeAttribute({
                className: 'button_enabled'
            });
        });
    },
    unsetNextEvents: function(){
        this.next = false;
        this.nextButton.writeAttribute({
            className: 'button'
        });
        this.nextButton.stopObserving('click');
        this.nextButton.stopObserving('mouseover');
        this.nextButton.stopObserving('mouseout');
    },
    previousEvents: function(){
        this.previous = true;
        this.previousButton.writeAttribute({
            className: 'button_enabled'
        });
        
        this.previousButton.observe('click', function(){
            clearTimeout(EasyWays.carusel.timerId);
            EasyWays.carusel.scroll('right');
        });
        this.previousButton.observe('mouseover', function(){
            this.writeAttribute({
                className: 'button_active'
            });
        });
        this.previousButton.observe('mouseout', function(){
            this.writeAttribute({
                className: 'button_enabled'
            });
        });
    },
    unsetPreviousEvents: function(){
        this.previous = false;
        this.previousButton.writeAttribute({
            className: 'button'
        });
        this.previousButton.stopObserving('click');
        this.previousButton.stopObserving('mouseover');
        this.previousButton.stopObserving('mouseout');
    },
    
    scroll: function(napr){
        switch (napr) {
            case 'left':
                scrollItem = this.step;
                itemsLeft = this.itemsCount - this.scrolledItems - scrollItem;
                if (itemsLeft < scrollItem) {
                    scrollItem = itemsLeft;
                    this.unsetNextEvents();
                    this.autoScrollNapr = 'right';
                }
                this.position -= this.itemWidth * scrollItem;
                this.scrolledItems += scrollItem;
                if (!this.previous) {
                    this.previousEvents();
                }
                break;
            case 'right':
                scrollItem = this.step;
                itemsLeft = this.scrolledItems;
                if (itemsLeft < scrollItem) {
                    scrollItem = itemsLeft;
                    this.unsetPreviousEvents();
                    this.autoScrollNapr = 'left';
                }
                this.position += this.itemWidth * scrollItem;
                this.scrolledItems -= scrollItem;
                if (!this.next) {
                    this.nextEvents();
                }
                break;
        }
        this.line.morph('margin-left:' + this.position + 'px');
    },
    itemsToScroll: function(){
        this.step = parseInt(this.conteiner.getWidth() / this.itemWidth);
    },
    autoScroll: function(){
        EasyWays.carusel.scroll(this.autoScrollNapr);
        this.timerId = setTimeout(function(){
            EasyWays.carusel.autoScroll();
        }, 5000);
    }
};

EasyWays.init();

panorams = Class.create();
panorams.prototype = {
	delay: 0.04,containers:[],
	initialize:function (){$$('.panoram').each(function(item,index){var img = $$('#'+$(item).identify() + ' img')[0];this.containers.push({'el':item,'width':item.getWidth()-2,'height':item.getHeight()-2,'image':img,'imageWidth':img.getWidth(),'imageHeight':img.getHeight(),'position':0,'scrollTo':'left'});},this);this.startAnimation();},
	startAnimation :function(){this.containers.each(function(item){var parent = this;new PeriodicalExecuter(function(pe) {parent.animation(item);}, this.delay);},this);},
	animation: function(panoram){if( panoram.scrollTo=='right' && panoram.position + panoram.width < panoram.imageWidth ){panoram.position++;}else{panoram.scrollTo='left';} if(panoram.scrollTo=='left' && panoram.position > 0){panoram.position--;}else{panoram.scrollTo='right';}$(panoram.image).style.marginLeft = -panoram.position+'px';}
}
var panorams = new panorams();