var NavigationMain = new Class({

    options: {
    },

    initialize: function(eNavigationMain) {
        this.eNavigationMain = eNavigationMain;
		
        this.aItems = new Array();

        var tItems = this.eNavigationMain.getElements('UL.items LI.item');
        for(var i=0; i<tItems.length; i++) 
            this.aItems[i] = new NavigationMainItem(tItems[i], i, this);
		
    }
	
});

var NavigationMainItem = new Class({

    options: {
		index: 0,
		active: false
    },

    initialize: function(eNavigationMainItem, index, cParent) {
        this.options.index = index;
		
        this.eNavigationMainItem = eNavigationMainItem;
		this.eNavigationMainItemSize = this.eNavigationMainItem.getSize();
		this.cParent = cParent;
		
		this.eNavigationMainItem.setStyle('z-index', 10000-index*10);
		
		this.eLinkImgNormal = this.eNavigationMainItem.getElement('DIV.link_img_normal');
        this.eLinkImgOver = this.eNavigationMainItem.getElement('DIV.link_img_over');
		
		this.eSubMenu = this.eNavigationMainItem.getElement('UL.items');
		
		if(this.eLinkImgNormal && this.eLinkImgOver) {
			this.eNavigationMainItem.addEvent('mouseover', this.eventMouseOver.bind(this));
			this.eNavigationMainItem.addEvent('mouseout', this.eventMouseOut.bind(this));
			
			if(this.eNavigationMainItem.get('class').contains('active')) {
				this.options.active = true;
			}
		}
    },
	
    eventMouseOver: function() {
		this.eLinkImgNormal.setStyle('display', 'none');
		this.eLinkImgOver.setStyle('display', 'block');
		
		if(this.eSubMenu) {
			this.eSubMenu.setStyle('display', 'block');
		}
    },

    eventMouseOut: function() {
		this.eLinkImgNormal.setStyle('display', 'block');
		this.eLinkImgOver.setStyle('display', 'none');
		
		if(this.eSubMenu) {
			this.eSubMenu.setStyle('display', 'none');
		}
    }
	
});

var SearchBoxClickClass = new Class ({
	options: {
		default_value: ''
	},
	
	initialize: function (eElement) {
		this.eElement = eElement;
		this.options.default_value = eElement.get('value');
		
		eElement.addEvents({
			'focus': this.eventMouseFocusClick.bind(this),
			'click': this.eventMouseFocusClick.bind(this),
			'blur': this.eventMouseLeaveFocus.bind(this)
		});
	},
	
	eventMouseFocusClick: function() {
		this.eElement.set('value', '');
	},

	eventMouseLeaveFocus: function() {
		if(this.eElement.get('value') == '')
			this.eElement.set('value',this.options.default_value);
	}
});

var Rotation = new Class({
	
	options: {
		active: 0,
		speed: 3000
    },
	
	initialize: function(sItemsElement, speed) {
		this.options.speed = speed;
		this.aItems = new Array();
		
		var tItems = $$(sItemsElement);
		for(var i=0; i<tItems.length; i++) {
			this.aItems[i] = tItems[i];
			if(i>0) this.aItems[i].set('opacity',0);
		}
		
		window.addEvent('load', this.eventLoad.bind(this));
    },
	
	showItem: function() {
		this.aItems[this.options.active].fade('out');
		this.aItems[this.options.active = this.options.active < this.aItems.length - 1 ? this.options.active+1 : 0].fade('in');
	},
	
	eventLoad: function() {
		this.interval = this.showItem.bind(this).periodical(this.options.speed);
	}
	
});

var RotationSteps = new Class({
	
	options: {},
	
	initialize: function(oRotation, eParent) {
		this.oRotation = oRotation;
		this.eParent = eParent;
		
		this.oRotation.showItem = this.showItem.bind(this);
		this.aSteps = new Array();
		
		var tStepsElement = new Element('div', {id: 'steps'});
		tStepsElement.addClass('rotation_steps');
		this.eParent.adopt(tStepsElement);
		for(var i=0; i<this.oRotation.aItems.length; i++) {
			var tStepElement = new Element('div', {id: 'steps' + '_' + 'step' + i});
			tStepElement.addClass('rotation_step');
			tStepElement.addEvent('click', this.eventClick.bind(this, i));
			tStepsElement.adopt(tStepElement);
			
			this.aSteps[i] = tStepElement;
		}
		
		this.aSteps[0].addClass('active');
    },
	
	activateStep: function(index) {
		for(var i=0; i<this.aSteps.length; i++) 
			this.aSteps[i].removeClass('active');
		
		this.aSteps[index].addClass('active');
	},
	
	showItem: function() {
		this.oRotation.aItems[this.oRotation.options.active].fade('out');
		this.oRotation.aItems[this.oRotation.options.active = this.oRotation.options.active < this.oRotation.aItems.length - 1 ? this.oRotation.options.active+1 : 0].fade('in');		
		
		this.activateStep(this.oRotation.options.active);
	},
	
	eventClick: function(index) {
		clearInterval(this.oRotation.interval);
		this.oRotation.interval = this.oRotation.showItem.bind(this.oRotation).periodical(this.oRotation.options.speed);
		
		this.oRotation.aItems[this.oRotation.options.active].fade('out');
		this.oRotation.aItems[this.oRotation.options.active = index].fade('in');
		
		this.activateStep(this.oRotation.options.active);
	}
	
});

window.addEvent('domready', function() {
	
	var tNavigationMain = new NavigationMain($('navigation_main'));
	var tSearchBoxClickClass = new SearchBoxClickClass($('search_box_sword'));
	
	
	var elements = $$('DIV.rotation');
    elements.each(function(element, index) {
		var tRotation = new Rotation(element.getElements('DIV.items DIV.item'), 5000);
		var tRotationSteps = new RotationSteps(tRotation, element);
    });
	
});
