//Inicializar layers principales
function initMainLayers() {
	var layers = $('div.main_layer');
	layers.find('span.close').click(function() {
				$(this.parentNode.parentNode).hide();
			});

	$('#rss_trigger').click(function() { 
		var pos = $(this).offset();
		layers.hide(); 
		$('#rss_layer').css({ top: pos.top+$(this).height(), left: pos.left-170 }).show(); 
		return false; 
	});
	$('a.share').click(function() { 
		var pos = $(this).offset();
		layers.hide(); 
		var left = pos.left;
		if ($(this).hasClass('right')) left=left-114;
		$('#share_layer').css({ top: pos.top+$(this).height(), left: left }).show(); 
		return false; 
	});
}

//Inicializar hover buttons
function initHoverButtons() {
	$('img.hoverButton').hover(function() {
		var current_src = this.getAttribute('src');
		this.setAttribute('src', current_src.replace(/\.(\w+)$/, '_on.$1'));
		
	}, function() {
		var current_src = this.getAttribute('src');
		this.setAttribute('src', current_src.replace(/_on.(\w+)$/, '.$1'));
	});
}

/* -----------------------------------------------------------------------------
	PORTADA
------------------------------------------------------------------------------*/
function initPortada() {
	//var triggers = $('#header ul.menu li').append('<span />');
	var triggers = $('#header ul.menu li');
	var portada = $('#portada');
	var slideshow = $('#slideshow');
	var boxes = portada.find('div.box');
	var currentBox = null;
	
	triggers.find('a').click(function() {
		var t = $(this);
		var img = t.find('img');
		var li = t.parent().parent();

		if (li.hasClass('current')) {
			li.removeClass('current');
			var lastimg = li.find('img');
			lastimg.attr('src', lastimg.attr('off'));
			portada.hide();
			slideshow.show();
			currentBox.hide();
			currentBox = null;
		} else {
			var lastimg = triggers.filter('.current').find('img');
			lastimg.attr('src', lastimg.attr('off'));
			
			triggers.removeClass('current');
			li.addClass('current');
			img.attr('src', img.attr('on'));
			
			if (currentBox) {
				currentBox.slideUp();
			} else {
				portada.show();
				slideshow.hide();
			}
			currentBox = $(this.getAttribute('href').replace(/^[^#]*/,'')+'_box').slideDown();
		}
		return false;
	});
	
	//Inicializar slideshow
	var slideshow = $('#slideshow');
	var images = slideshow.find('img');
	var paginator = slideshow.find('div.pages');
	var box = slideshow.find('div.box');
	var countPages = images.length;
	var currentPage = -1;
	var widthPage = 980;
	
	var paginator_html='<span class="go" rel="back"><img src="'+cms_ruta+'content/css/page-prev.gif" alt="-" /></span>';
	for (var i=0; i<countPages; i++){ 
        paginator_html+='<span class="go" rel="'+(i+1)+'">'+(i+1)+'</span>'; 
    }
	paginator_html+='<span class="go" rel="next"><img src="'+cms_ruta+'content/css/page-next.gif" alt="+" /></span>';
	
	function setPage(page) {
		paginator.find('span').each(function(i) {
			var rel = this.getAttribute('rel');
			switch(rel) {
				case 'back': this.className = (page>1)? 'go':'disabled';  break;
				case 'next': this.className = (page<countPages)? 'go':'disabled';  break;
				default: this.className='go';
			}
		}).get(page).className='current';
		
		box.animate({'left': -(widthPage)*(page-1)}, 750);
		currentPage=page*1;
	}
	
	paginator.html(paginator_html).find('span').click(function() {
		var t = $(this);
		if (t.hasClass('disabled')) return false;
		var page = this.getAttribute('rel');
		if (page=='back') return setPage(currentPage-1);
		if (page=='next') return setPage(currentPage+1);   
		setPage(page);
	});
	setPage(1);
}

/* -----------------------------------------------------------------------------
	SECCIÓN INTERIOR
------------------------------------------------------------------------------*/
function initSeccionInterior() {
	creatSlider('#casos_slider', 'td.caso', 2, 486);
}


/* -----------------------------------------------------------------------------
	CASOS DE ÉXITO
------------------------------------------------------------------------------*/
function initCasosExito() {
	//Slideshow
	var slideshow = $('#casosexito');
	var pages = slideshow.find('td.page');
	var paginator = slideshow.find('div.pages');
	var box = slideshow.find('#slider');
	var countPages = pages.length;
	var currentPage = -1;
	var widthPage = 775;
	
	var count = slideshow.find('b.count');
	slideshow.find('b.total').html(slideshow.find('div.caso').length);
	
	var paginator_html='<span class="go" rel="back"><img src="'+cms_ruta+'content/css/page-prev2.jpg" alt="-" /></span>';
	for (var i=0; i<countPages; i++){ 
        paginator_html+='<span class="go" rel="'+(i+1)+'">'+(i+1)+'</span>'; 
    }
	paginator_html+='<span class="go" rel="next"><img src="'+cms_ruta+'content/css/page-next2.jpg" alt="+" /></span>';
	
	function setPage(page) {
		paginator.find('span').each(function(i) {
			var rel = this.getAttribute('rel');
			switch(rel) {
				case 'back': this.className = (page>1)? 'go':'disabled';  break;
				case 'next': this.className = (page<countPages)? 'go':'disabled';  break;
				default: this.className='go';
			}
		}).get(page).className='current';
		
		count.html( pages.eq(page-1).find('div.caso').length );
		box.animate({'left': -(widthPage)*(page-1)}, 750);
		currentPage=page*1;
	}
	
	paginator.html(paginator_html).find('span').click(function() {
		var t = $(this);
		if (t.hasClass('disabled')) return false;
		var page = this.getAttribute('rel');
		if (page=='back') return setPage(currentPage-1);
		if (page=='next') return setPage(currentPage+1);   
		setPage(page);
	});
	setPage(1);
	
	//Dropdown
	var list = $('#list');
	$('#dropdown a.trigger').click(function() {
		list.show();
		return false;
	});
	list.find('span.close').click(function() {
		list.hide();
		return false;
	});
}


/* -----------------------------------------------------------------------------
	CLIENTES
------------------------------------------------------------------------------*/
function initClientes() {
	//Slideshow
	var slideshow = $('#clientes');
	var pages = slideshow.find('td.page');
	var paginator = slideshow.find('div.pages');
	var box = slideshow.find('#slider');
	var countPages = pages.length;
	var currentPage = -1;
	var widthPage = 490;
	
	var count = slideshow.find('b.count');
	slideshow.find('b.total').html(slideshow.find('td.cliente').length);
	
	var paginator_html='<span class="go" rel="back"><img src="'+cms_ruta+'content/css/page-prev2.jpg" alt="-" /></span>';
	for (var i=0; i<countPages; i++){ 
        paginator_html+='<span class="go" rel="'+(i+1)+'">'+(i+1)+'</span>'; 
    }
	paginator_html+='<span class="go" rel="next"><img src="'+cms_ruta+'content/css/page-next2.jpg" alt="+" /></span>';
	
	function setPage(page) {
		paginator.find('span').each(function(i) {
			var rel = this.getAttribute('rel');
			switch(rel) {
				case 'back': this.className = (page>1)? 'go':'disabled';  break;
				case 'next': this.className = (page<countPages)? 'go':'disabled';  break;
				default: this.className='go';
			}
		}).get(page).className='current';
		
		count.html( pages.eq(page-1).find('td.cliente').length );
		box.animate({'left': -(widthPage)*(page-1)}, 750);
		currentPage=page*1;
	}
	
	paginator.html(paginator_html).find('span').click(function() {
		var t = $(this);
		if (t.hasClass('disabled')) return false;
		var page = this.getAttribute('rel');
		if (page=='back') return setPage(currentPage-1);
		if (page=='next') return setPage(currentPage+1);   
		setPage(page);
	});
	setPage(1);
	
	//Hover images
	$('#slider td.cliente img').hover(function() {
		this.src = this.getAttribute('on');
	}, function() {
		this.src = this.getAttribute('off');
	});
	
	//Dropdown
	var list = $('#list');
	$('#dropdown a.trigger').click(function() {
		list.show();
		return false;
	});
	list.find('span.close').click(function() {
		list.hide();
		return false;
	});
}

/* -----------------------------------------------------------------------------
	TESTIMONIOS
------------------------------------------------------------------------------*/
function initTestimonios() {
	//Slideshow
	var slideshow = $('#testimonios');
	var sliderbox = $('#sliderbox');
	var pages = slideshow.find('td.page');
	var paginator = slideshow.find('div.pages');
	var box = slideshow.find('#slider');
	var countPages = pages.length;
	var currentPage = -1;
	var widthPage = 750;
	
	var heightPage=0;
	pages.each(function() {
		var height = $(this).height();
		if (height>heightPage) heightPage = height;
	});
	sliderbox.height(heightPage);
	
	var count = slideshow.find('b.count');
	slideshow.find('b.total').html(slideshow.find('div.testimonio').length);
	
	var paginator_html='<span class="go" rel="back"><img src="'+cms_ruta+'content/css/page-prev2.jpg" alt="-" /></span>';
	for (var i=0; i<countPages; i++){ 
        paginator_html+='<span class="go" rel="'+(i+1)+'">'+(i+1)+'</span>'; 
    }
	paginator_html+='<span class="go" rel="next"><img src="'+cms_ruta+'content/css/page-next2.jpg" alt="+" /></span>';
	
	function setPage(page) {
		paginator.find('span').each(function(i) {
			var rel = this.getAttribute('rel');
			switch(rel) {
				case 'back': this.className = (page>1)? 'go':'disabled';  break;
				case 'next': this.className = (page<countPages)? 'go':'disabled';  break;
				default: this.className='go';
			}
		}).get(page).className='current';
		
		$('#testimonios div.testimonio a.less').click();
		count.html( pages.eq(page-1).find('div.testimonio').length );
		box.animate({'left': -(widthPage)*(page-1)}, 750);
		currentPage=page*1;
	}
	
	paginator.html(paginator_html).find('span').click(function() {
		var t = $(this);
		if (t.hasClass('disabled')) return false;
		var page = this.getAttribute('rel');
		if (page=='back') return setPage(currentPage-1);
		if (page=='next') return setPage(currentPage+1);   
		setPage(page);
	});
	setPage(1);

	//Ampliar / Reducir
	$('#testimonios div.testimonio a.more').click(function() {
		var parent = this.parentNode.parentNode.parentNode;
		var newHeight = $('div.block2', parent).show().height();
		sliderbox.height(heightPage+newHeight+20);
		$('a.less', parent).show();
		$('img.quote', parent).hide();
		$(this).hide();
		return false;
	});
	$('#testimonios div.testimonio a.less').click(function() {
		var parent = this.parentNode.parentNode.parentNode;
		$('div.block2', parent).hide();
		$('a.more', parent).show();
		$('img.quote', parent).show();
		$(this).hide();
		sliderbox.height(heightPage);
		return false;
	});


	//Dropdown
	var list = $('#list');
	$('#dropdown a.trigger').click(function() {
		list.show();
		return false;
	});
	list.find('span.close').click(function() {
		list.hide();
		return false;
	});
}

/* -----------------------------------------------------------------------------
	FICHA CASO DE ÉXITO
------------------------------------------------------------------------------*/
function initCasoExito() {
	creatSlider('#casos_slider', 'td.caso', 2, 486);
	creatSlider('#caso_images', 'td.page', 1, 465);
	
	//Zoom
	var currentLink;
	var zoom = $('#caso_images div.zoom_icon');
	zoom.click(function() {
		if (currentLink) $(currentLink).click();
	});
	$('#caso_images div.slider a').mouseover(function() {
		currentLink = this;
		zoom.show();
	});
	$('#caso_images').mouseleave(function() {
		currentLink = false;
		zoom.hide();
	});
	
	//Tooltip
	var tooltip = $('#tooltip');
	$('#casos_nav a').hover(function() {
		var pos = $(this).offset();
		tooltip.find('span').text(this.getAttribute('title'));
		tooltip.css({ top: pos.top-43, left: pos.left-tooltip.width()+5 }).show(); 
	}, function() {
		tooltip.hide();
	});
}

/* -----------------------------------------------------------------------------
	FUNCIONES COMUNES
------------------------------------------------------------------------------*/
function creatSlider(selector_container, selector_elem, elems_page, widthPage) {
	var slideshow = $(selector_container);
	var box = slideshow.find('div.slider');
	var elems = slideshow.find(selector_elem);
	var countPages = Math.ceil(elems.length/elems_page);
	var currentPage = -1;
	
	function setPage(page) {
		var back = slideshow.find('div.arrow_left');
		if (page>1) back.show(); else back.hide();
		var next = slideshow.find('div.arrow_right');
		if (page<countPages) next.show(); else next.hide();
		box.animate({'left': -(widthPage)*(page-1)}, 750);
		currentPage=page*1;
	}
	
	slideshow.find('div.arrow > a').click(function() {
		var t = $(this);
		if (t.hasClass('disabled')) return false;
		page = this.getAttribute('rel');
		if (page=='back') return setPage(currentPage-1);
		if (page=='next') return setPage(currentPage+1);   
		return false;
	});
	setPage(1);
}
