/*
	Core & Window section
*/
Browser.Engine.ie6 = (function(){
	if( Browser.Engine.trident && Browser.Engine.version<=4)
		return true;
	else
		return false;
})();
var Engine = Browser.Engine;




/*
	Element section
*/
Element.implement({

	// method for adding corners FOR BOXES
	releaseCorners: function(){
		if ( Engine.trident || Engine.presto) {
			new Element('div', {
				'class': 'fix-corners fix-corners-tl',
				'html': '&nbsp;'
			}).inject(this, 'top');
			new Element('div', {
				'class': 'fix-corners fix-corners-tr',
				'html': '&nbsp;'
			}).inject(this, 'top');
			new Element('div', {
				'class': 'fix-corners fix-corners-bl',
				'html': '&nbsp;'
			}).inject(this, 'top');
			new Element('div', {
				'class': 'fix-corners fix-corners-br',
				'html': '&nbsp;'
			}).inject(this, 'top');
		}
		return this;
	},

	// this function makes accordeon with 'dl' tag structure
	releaseAccordeon: function(){
		if (this.get('tag') === 'dl' ) {
			this.getElements('dt').addEvents({
				'click': function(){
					$$(this, this.getNext()).toggleClass('opened');
					return this;
				},
				'mousedown': function(){
					return false;
				}
			});
		}
		return this;
	},

	// this function makes tabulator with 'dl' structure
	releaseTabulator: function(){
		if (this.get('tag') === 'dl' ) {
			this.getElements('dt').addEvents({
				'mouseover': function(){
					return this.addClass('hover');
				},
				'mouseout': function(){
					return this.removeClass('hover');
				},
				'click': function(){
					this.getParent('dl').getElement('dt.this').removeClass('this');
					this.addClass('this');

					this.getParent('dl').getElements('dd').addClass('hidden');
					this.getNext().removeClass('hidden');
					return this;
				},
				'mousedown': function(){
					return false;
				}
			});
		}
		return this;
	}
});




/*
	Arterium Teaser
*/
var Teaser = new Class({

	// constructor
	initialize: function(wrapper, prevButton, nextButton){

		// init properties
		this.items = wrapper.getElements('img').length;
    if (this.items) {
      this.step = wrapper.getElements('img')[0].getSize().x + 64;
      this.cursor = 1;
      this.fx = new Fx.Tween(wrapper, {
        fps: 60,
        unit: 'px',
        duration: 300,
        link: 'chain',
        transition: Fx.Transitions.Circ.easeOut
      });

      // init button events
      try {
        prevButton.addEvent('click', this.prev.bind(this));
        nextButton.addEvent('click', this.next.bind(this));
      } catch(e) {
        return this;
      }
    }

    return this;
	},

	// method slides ribbon to previous position
	prev: function(){
		if (this.cursor > 1) {
			this.cursor--;
			this.fx.start('margin-left', -(this.cursor-1)*this.step);
		}
		return this;
	},

	// method slides ribbon to next position
	next: function(){
		if (this.cursor < (this.items-1) ) {
			this.cursor++;
			this.fx.start('margin-left', -(this.cursor-1)*this.step);
		}
		return this;
	}
});

/*
	Arterium Calendar
*/
var Calendar = new Class({

	// constructor
	initialize: function(links, container, path){
		// AJAX object
		/*
		xhr = new Request.HTML({
			url:path,
			update:container[0]
		});*/
		// 'click' handler for months links
		links.addEvent('click', function(){
			var loading = new Element('div', {id: 'loading'});
			links.removeClass('this');
			this.addClass('this');
			loading.inject(container[0]);
			var xhr = new Request.HTML({url:path,update:container[0]}).send(this.get('href'));
			return false;
		});
		return this;
	}
});


function showContacts(id) {
	$$('#contacts > div').addClass('hidden');
	$$('#contacts > div[title='+id+']').removeClass('hidden');
}


/*
	MNN
*/
var Mnn = new Class({

	// constructor
	initialize: function(input, button, list, result) {
		var _this_ = this;

    if (input && button && list && result) {
      this.input = input;
      this.button = button;
      this.list = list;
      this.result = result;

      this.search_request = new Request({
        method: 'post',
        charset: 'utf-8',
        link: 'cancel',
        url: _this_.input.get('action'),
        onSuccess: function(html) {
          _this_.list.empty().set('html', html);

          add_tips(_this_.list.getElements('li.dfn'));

          _this_.list.getElements('a').addEvent('click', function() {
            _this_.result.empty();
            _this_.mnn_request.options.data = 'mnn=' + this.get('mnn_id');
            _this_.mnn_request.send();
            $('result_loader').setStyle('display', 'block');
            $('mnn-input').focus();
            $('left_col').setStyle('min-height', 0);
            return false;
          });
          $('search_loader').setStyle('display', 'none');
          return this;
        }
      });

      this.mnn_request = new Request({
        method: 'post',
        charset: 'utf-8',
        link: 'cancel',
        url: _this_.input.get('action'),
        onSuccess: function(html) {
          _this_.result.empty().set('html', html);
          add_tips(_this_.result.getElements('li.dfn'));
          $('result_loader').setStyle('display', 'none');
          set_size();
          return this;
        }
      });
	  
      var set_size = function(){
        var size_product = $('mnn-product').getSize().y + 11
        var size_analog = $('mnn-analogs').getSize().y
        var size_left =  $('left_col').getSize().y
        if($('mnn-analogs')){
          if(size_analog + size_product > size_left){
            $('left_col').setStyle('min-height', size_analog + size_product)
          }else{
            $('mnn-analogs').setStyle('min-height', size_left - size_product)
          }
        }
      }
	  
      var add_tips = function(elements) {
        _this_.tips = new Tips(elements, {
          'className': 'mnn-tip',
          'offset': {'x': 0, 'y': 20},
          'fixed': false,
          'windowPadding': {'x': 0, 'y': 0}
        });
      };

      var search = function() {
        _this_.list.empty();
        _this_.result.empty();

        if (_this_.tips) {
          _this_.tips.hide();
        }

        if (_this_.input.value.length >= _this_.input.get('minlength')) {
          _this_.search_request.options.data = 'search=' + _this_.input.value;
          _this_.search_request.send();
    		  $('search_loader').setStyle('display', 'block')
        }
		
        $('left_col').setStyle('min-height', 0)
        return false;
      };

      this.input.addEvent('keyup', search);
      this.button.addEvent('click', search);
    }

    return this;
	}
});

/*
	Init JS
*/
window.addEvent('domready', function(){

	// faq accordion
	try {
		$$('#faq-index dl dt span').addEvent('click', function(){
			this.getParent('dt').getNext().toggleClass('hidden');
			return false;
		});
	} catch(e){}
	
	// calendar accordion

	try {
		$$('.calendar-accordion dl dt').addEvent('click', function(){
			
			$$('.calendar-accordion dl dd').addClass('x-none');	
			if(this.get('status') != 'open'){
				$$('.calendar-accordion dl dt').set('status','close')
				this.set('status','open');
				this.getNext().removeClass('x-none');
			}else{
				this.set('status','close');
				if(!this.getNext().hasClass('x-none')){
					this.getNext().addClass('x-none');
				}
			}
			return false;
		});
	} catch(e){}

	// milkbox init
	try {
		new Milkbox({
			overlayOpacity: 0.8
		});
	} catch(e){}



	// initialize teaser
	new Teaser($('teaser-ribbon'), $('teaser-prev'), $('teaser-next'));

	// release corners for boxes
	$$('blockquote').releaseCorners();

	// release accordeons
	$$('dl.accordeon').releaseAccordeon();

	// release tabulators
	$$('dl.tabulator').releaseTabulator();

	// product search box dropper
	/*if ($('search-box')) {
		$('search-box').getElement('a').addEvent('click', function(){
			try {
				this.getParent('form').toggleClass('open');
				if ($('advanced')){
					$('advanced').destroy();
				}else{
					var advanced = new Element('input', {
						'type': 'hidden',
						'name': 'advanced',
						'value': 'true',
						'id': 'advanced'
					});
					advanced.inject(this.getParent('form'),'top');					
				}
			} catch(e) {}
			return false;
		});
	}*/

	// embed FLASH movies from '<img alt="flash" src="/flash/[...].swf" />' tag into parent element
	$$('img.swiff').each(function(o){
		if(Browser.Plugins.Flash.version && o.get('alt')) {
			if (o.get('width') && o.get('width') > 0) width = o.get('width');
				else width = '100%';
			if (o.get('height') && o.get('height') > 0) height = o.get('height');
				else height = '100%';

			// let's embed it 
			o.movie = new Swiff(o.get('alt'), {
				'width': width,
				'height': height,
				'container': o.getParent(),
				'params': {
					'wMode': 'transparent',
					'allowfullscreen': 'always'
				}
			});
		} else if (o.get('longdesc')) {
			o.setStyle('cursor', 'pointer')
			 .addEvent('click', function(){
				window.location.href = o.get('longdesc');
			 });
		};
		return this;
	});


	// initialize mnn
  new Mnn($('mnn-input'), $('mnn-button'), $('mnn-list'), $('mnn-result'));

	return this;
});
