// init page
window.addEvent('domready', function() {
	initCufon();
	initGallery();
	initSelectTabs();
	hoverForIE6(".select-point", "hover");
});

// hover for IE
function hoverForIE6(_list, _class) {
	var _hoverClass = 'hover';
	if(_class) _hoverClass = _class;
	if(typeof(document.body.style.maxHeight) == 'undefined'){
		$$(_list).each(function(_el, i){
			_el.addEvent('mouseenter', function(){
				_el.addClass(_hoverClass);
			});
			_el.addEvent('mouseleave', function(){
				_el.removeClass(_hoverClass);
			});
		});
	}
}

// slideshow init
function initGallery() {
	$$('div.carousel').each(function(item){
		var g = new fadeGallery(item,{
			activeClass: 'active',
			slides: 'ul.slideset > li',
			btnPrev: 'ul.nav-buttons li.prev',
			btnNext: 'ul.nav-buttons li.next',
			autoSlide: true,
			switchTime: 5000,
			duration : 650
		});
	});
}

// fade gallery module
var fadeGallery = new Class({
	options: {
		pager: 'div.pagerlinks a.switcher',
		slides: 'ul.slideset > li',
		btnPrev: 'a.link-prev',
		btnNext: 'a.link-next',
		activeClass: 'active',
		autoSlide: false,
		switchTime: 3000,
		duration : 500
	},

	// create class
	initialize: function(element, options){
		this.setOptions(options);
		var _this = this;

		this.slideset = element.getElements(this.options.slides);
		this.btnPrev = element.getElement(this.options.btnPrev);
		this.btnNext = element.getElement(this.options.btnNext);
		this.slidesCount = this.slideset.length;
		this.pager = element.getElements(this.options.pager);
		this.autoRotation = this.options.autoSlide;
		this.activeClass = this.options.activeClass;
		this.switchTime = this.options.switchTime;

		// slides
		this.duration = this.options.duration;
		this.currentIndex = 0;
		this.oldIndex = 0;
		this.timer = false;

		// gallery init
		this.slideset.each(function(_slide, _index){
			if(_index != _this.currentIndex) _slide.removeClass(_this.activeClass).setStyles({display:'none',opacity:0});
			else _slide.addClass(_this.activeClass).setStyles({display:'block',opacity:1});
		});

		// init control
		if(this.btnPrev) {
			this.btnPrev.addEvent('click', function(){
				_this.prevSlide();
				return false;
			});
		}
		if(this.btnNext) {
			this.btnNext.addEvent('click', function(){
				_this.nextSlide();
				return false;
			});
		}
		if(this.pager.length) {
			this.pager.each(function(item,ind){
				item.addEvent('click', function(){
					_this.numSlide(ind);
					return false;
				});
			});
		}

		// start autoslide
		if (this.options.autoSlide) {
			this.autoSlide();
		}
	},
	refreshPagination:function(){
		var _this = this;
		if(this.pager.length) {
			this.pager.each(function(item,ind){
				if(ind == _this.currentIndex) item.addClass(_this.activeClass);
				else item.removeClass(_this.activeClass)
			});
		}
	},
	numSlide: function(ind){
		if(this.currentIndex != ind) {
			this.oldIndex = this.currentIndex;
			this.currentIndex = ind;
			this.switchSlide();
		}
	},
	nextSlide: function(){
		this.oldIndex = this.currentIndex;
		if(this.currentIndex < this.slidesCount-1) this.currentIndex++;
		else this.currentIndex = 0;
		this.switchSlide();
	},
	prevSlide: function(){
		this.oldIndex = this.currentIndex;
		this.currentIndex--;
		if (this.currentIndex < 0) this.currentIndex = this.slidesCount-1;
		this.switchSlide();
	},
	switchSlide:function(){
		if ( this.slideset.length > 1 ) {
			var _oldSlide = this.slideset[this.oldIndex].removeClass(this.activeClass).setStyles({opacity:1,display:'block'});
			var _newSlide = this.slideset[this.currentIndex].addClass(this.activeClass).setStyles({opacity:0,display:'block'});
	
			// fade elements
			var fxFadeOut = new Fx.Morph(_oldSlide, {duration: this.duration, onComplete:function(){_oldSlide.setStyles({display:'none'})}});
			var fxFadeIn = new Fx.Morph(_newSlide, {duration: this.duration});
			fxFadeOut.start({opacity:0});
			fxFadeIn.start({opacity:1});
			this.autoSlide();
			this.refreshPagination();
		}
	},
	autoSlide: function(){
		if(this.autoRotation) {
			var _this = this;
			if(this.timer) clearTimeout(this.timer);
			this.timer = setTimeout(function(){_this.nextSlide()}, this.switchTime);
		}
	},
	// add options and events
	Implements : [Options, Events]
});

// select tabs
function initSelectTabs() {
	var _indexAttr = 'orderindex';
	var _activeClass = 'active';

	$$('div.select-point').each(function(obj){
		var _holder = obj;
		var _opener = _holder.getElement('span.has-child');
		var _drop = _holder.getElement('ul');
		var _links = _drop.getElements('li');

		// init opener
		_drop.setStyles({display:'none'});
		_links.each(function(link,ind){
			if(link.hasClass(_activeClass)) {
				link.removeClass(_activeClass);
				switchTab(link);
				//_opener.grab(link);
				_opener.set('html', '');
				var liAux = link.clone().cloneEvents( link );
				liAux.addEvent('click',function(){
					_drop.setStyles({display:(_drop.offsetWidth ? 'none' : 'block')});					
					return false;
				});
				_opener.grab(liAux)
				//Cufon.replace(liAux);
			}
			var attr = {}; attr[_indexAttr] = ind;
			link.set(attr);
		});

		// select behaviour
		function toggleDrop(state) {
			if(state) { 
				_drop.setStyles({display:'block'});
			} else {
				_drop.setStyles({display:'none'});
			}
		}
		
		function switchTab(link) {
			// hide other
			_links.each(function(link,ind){
				var cur = link.getElement('a').get('href');
				var box = $$(cur)[0];
				if(box) box.setStyles({display:'none'});
			});
			// show current
			var _target = link.getElement('a').get('href');
			var box = $$(_target)[0];
			if(box) box.setStyles({display:'block'});
		}
		
		function sortDropItems() {
			var _items = _drop.getChildren('li');
			_items.sort(function(a,b){
				if(parseInt(a.get(_indexAttr)) < parseInt(b.get(_indexAttr))) return -1;
				else return 1;
			})
			_items.each(function(obj){
				_drop.grab(obj);
			});
		}		
		function switchLink(link) {
			//saca el seleccionado y lo añade a la lista
			//_drop.grab(_opener.getChildren('li')[0]);
			
			//mueve el clickado en el seleccionado
			_opener.set('html', '');
			var liAux  = rebuildLink(link);
			_opener.grab(liAux);
			//Cufon.replace('span[class=has-child]');

			//Oculta los elementos de menu (todos los li que no estan en el span)
			toggleDrop(false);
			
			//Los ordena por orderindex
			sortDropItems();
		}

		function rebuildLink( link ) {
			var clone = link.clone();
			/*var numberArray = clone.getElement("span[class=num]").getElements("cufontext").get('html');;
			var number='';
			for ( var i = 0; i < numberArray.length; i++ ){
				number += numberArray[i];
			}
			*/
			var number=clone.getElement("span[class=num]").get('html');
			var spanNum = new Element('span', {'class':'num'}).set('html', number);
			
			/*
			var textArray = clone.getElement("span[class=text]").getElements("cufontext").get('html');
			var text='';
			for ( var i = 0; i < textArray.length; i++ ){
				text += textArray[i];
			}
			*/
			var text = clone.getElement("span[class=text]").get('html');
			var spanText = new Element('span', {'class':'text'}).set('html', text);
			
			spanNum.replaces( clone.getElement("span[class=num]") );
			spanText.replaces( clone.getElement("span[class=text]") );
			
			clone.addEvent('click',function(){
				_drop.setStyles({display:(_drop.offsetWidth ? 'none' : 'block')});					
				return false;
			});

			return clone;
		}
		
		// links behavior
		_links.each(function(link){
			link.addEvent('click',function(){
				// drop toggle behavior
				if(link.parentNode.match('span')) {
					_drop.setStyles({display:(_drop.offsetWidth ? 'none' : 'block')});					
				}
				// switch tab behavior
				else {
					switchLink(link);
					switchTab(link);
				}
				return false;
			});
		});
	});
}

// cufon init
function initCufon() {
	
	//Cufon.replace('.select-point .num', { fontFamily: 'VAGRounded-Bold', hover: true });
	//Cufon.replace('.select-point .text', { fontFamily: 'VAGRounded-Light', hover: true });
	//Cufon.replace('.has-child .num', { fontFamily: 'VAGRounded-Bold', hover: true });
	//Cufon.replace('.has-child .text', { fontFamily: 'VAGRounded-Light', hover: true });
	
	Cufon.replace('.ad-comment a strong', { fontFamily: 'VAGRounded-Bold', hover: true });
	Cufon.replace('.datail-page .upper h2', { fontFamily: 'VAGRounded-Black'});
	Cufon.replace('.promo-items h3', { fontFamily: 'VAGRounded-Black'});
	Cufon.replace('.map-block p', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.make-route-block h2 span', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.promo-block p', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.promo-items h2', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.user-bar ul a strong', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.promo-block .heading strong', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.promo-block h3', { fontFamily: 'VAGRounded-Black'});
	Cufon.replace('.intro', { fontFamily: 'VAGRounded-Light'});
	Cufon.replace('.promo-block h2', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.principal-block h3', { fontFamily: 'VAGRounded-Thin'});
	//Cufon.replace('.promo-items p', { fontFamily: 'VAGRounded-Thin'});
	Cufon.replace('.comments-block h3', { fontFamily: 'VAGRounded-Thin'});	
	Cufon.replace('.datail-page .map-created .creator', { fontFamily: 'VAGRounded-Thin', hover: true });
	Cufon.replace('.page-footer .feeds-link span', { fontFamily: 'VAGRounded-Light', hover: true });
	Cufon.replace('.page-footer .feeds-link strong', { fontFamily: 'VAGRounded-Bold', hover: true });
	Cufon.replace('.page-footer .nav a', { fontFamily: 'VAGRounded-Light', hover: true });
	Cufon.replace('.page-footer .nav .name-title', { fontFamily: 'VAGRounded-Bold'});
	//Cufon.replace('.principal-block .desctiption', { fontFamily: 'VAGRounded-Thin'});
	Cufon.replace('.principal-block .desctiption strong', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.carousel .short-description', { fontFamily: 'VAGRounded-Black'});
	Cufon.replace('.carousel .short-description strong', { fontFamily: 'VAGRounded-Bold'});
	
	Cufon.replace('.ad-comment-font strong', { fontFamily: 'VAGRounded-Bold'});
	//Cufon.replace('.general-little', { fontFamily: 'VAGRounded-Thin'});
	Cufon.replace('.general-little strong', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.general h3', { fontFamily: 'VAGRounded-Thin'});
	Cufon.replace('.profileToolsContent h1', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.profile_map_title', { fontFamily: 'VAGRounded-Black'});
	Cufon.replace('.profile_map_date', { fontFamily: 'VAGRounded-Light'});
	//Cufon.replace('.profile_map_desc', { fontFamily: 'VAGRounded-Thin'});
	Cufon.replace('.profile_footer_btn', { fontFamily: 'VAGRounded-Thin'});
	
	Cufon.replace('.share h1', { fontFamily: 'VAGRounded-Bold'});
	
	Cufon.replace('.route h1', { fontFamily: 'VAGRounded-Bold'});
	//Cufon.replace('.route p', { fontFamily: 'VAGRounded-Thin'});
	Cufon.replace('.route h3', { fontFamily: 'VAGRounded-Light'});
	Cufon.replace('#map_canvas_tools label', { fontFamily: 'VAGRounded-Thin'});
	
	Cufon.replace('.global h2', { fontFamily: 'VAGRounded-Bold'});
	
	Cufon.replace('.button-gray', { fontFamily: 'VAGRounded-Bold', textTransform: 'uppercase'});
	Cufon.replace('.button-gray a', { fontFamily: 'VAGRounded-Bold', hover: true});

	Cufon.replace('.gray-small', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.gray-small a', { fontFamily: 'VAGRounded-Bold', hover: true});

	Cufon.replace('.gray-medium', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.gray-medium a', { fontFamily: 'VAGRounded-Bold', hover: true});
	
	Cufon.replace('.gray-large', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.gray-large a', { fontFamily: 'VAGRounded-Bold', hover: true});

	Cufon.replace('.pink-medium', { fontFamily: 'VAGRounded-Bold'});
	Cufon.replace('.pink-medium a', { fontFamily: 'VAGRounded-Bold', hover: true});
	
	Cufon.replace('.table-form td', { fontFamily: 'VAGRounded-Thin', hover: true });
	
	Cufon.replace('.padded a', { fontFamily: 'VAGRounded-Bold', hover: true });
	//Fancy Upload
	Cufon.replace('.overall-title', { fontFamily: 'VAGRounded-Thin', hover: true });
	
	Cufon.replace('.route strong', { fontFamily: 'VAGRounded-Bold', hover: true });
	
}
