﻿var $ = jQuery.noConflict();
var homePageSlideImageWidth = 600;
$(document).ready(function () { 
	
	function themeBehaviours() {
		accordionBehaviour();
	}

	/*** Accordion ***/
	var slidesInt = null;
	function accordionBehaviour() {
		var slidesCount = $(".accordion:last .accordionSlide").length;
		var slideInitialWidth = Math.floor( ($(".accordion").width() - slidesCount) / slidesCount);
		var slideColapsedWidth = Math.floor( ($(".accordion").width() - homePageSlideImageWidth ) / (slidesCount - 1) - 1);
		var time = 4000;
		var slideHovered = false;
		clearInterval(slidesInt);
		
		function startAccordion() {
			$(".accordion:last .accordionSlide").each(function(){
				$(this).css({"width" : slideInitialWidth+"px"});
				$(this).find("img").animate({left: -220}, 0);
			});
			
			$(".accordion:last .accordionSlide").hover(function(){
				slideHovered = true;
				clearInterval(slidesInt);
			},
			function(){
				slideHovered = false;
				setSlidesInterval();
			});
			
			$(".accordion:last .accordionSlide").click(function () { 
				if ($(this).hasClass("currentSlide")) {
					var newLink = $(this).find("a").attr("href");
					SWFAddress.setValue(newLink)
				} else {
					$(".currentSlide").removeClass("currentSlide");
					$(this).prev().addClass("currentSlide");
					nextSlide(slideColapsedWidth);
				}
				return false;
			});		
			
		}
		function setSlidesInterval() {
			slidesInt = setInterval(function() {
				if (slideHovered == false) {
					nextSlide(slideColapsedWidth);
				}
			}, time);
		}
		
		setSlidesInterval();
		
		first = true;
		function nextSlide(slideColapsedWidth) {
			if ($(".currentSlide").length == 0) {
				$(".accordion:last .accordionSlide:last").addClass("currentSlide");
			}
			$current = $(".accordion:last .currentSlide");
			$next = $(".accordion:last .currentSlide").next();
			
			if ($next.attr('class') == undefined) {
				$next = $(".accordion:last .accordionSlide:first");
			}
			
			$current.removeClass("currentSlide");
			$(".accordionSlide").stop().animate({ width: slideColapsedWidth }, {duration:850, easing:"swing"});
			$(".accordionSlide").find(".slideText").animate({opacity:0}, {queue:false, duration:850, easing:"swing"});
			$(".accordionSlide").find("img").animate({left: -220}, {queue:false, duration:850, easing:"swing"});
			
			$next.addClass("currentSlide").stop().animate({ width: homePageSlideImageWidth }, {duration:850, easing:"swing"});
			if (!first) {
				$next.find(".slideText")
					.animate({"left": homePageSlideImageWidth}, {queue:false, duration:0})
					.animate({opacity:0.60, left:40}, {queue:false, duration:850, easing:"swing"});
			} else {
				first = false;
				$next.find(".slideText")
					.animate({opacity:0.60, left:40}, {queue:false, duration:850, easing:"swing"});
			}
			$next.find("img").animate({left: 0}, {queue:false, duration:850, easing:"swing"});
		}
		startAccordion();
	}
	
	themeBehaviours();
	$("#suscripcion").focus(function() {
		if (this.value == "Correo Electrónico") {
			this.value = "";
		}
	});
	$("#suscripcion").blur(function() {
		if (this.value == "") {
			this.value = "Correo Electrónico";
		}
	});
	//Get the height of the first item
	$('#mask').css({'height':$('#panel-1').height()});	
	
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#panel').width(parseInt($('#mask').width() * $('#panel div').length));
	
	//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	$('#panel div').width($('#mask').width());
	
	//Get all the links with rel as panel
	$('a[rel=panel]').click(function () {
	
		//Get the height of the sub-panel
		var panelheight = $($(this).attr('href')).height();
		
		//Set class for the selected item
		$('a[rel=panel]').removeClass('selected');
		$(this).addClass('selected');
		
		//Resize the height
		$('#mask').animate({'height':panelheight},{queue:false, duration:500});			
		
		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		$('#mask').scrollTo($(this).attr('href'), 800);		
		
		//Discard the link default behavior
		return false;
	});
});
