/*FOOTER_DECO*/
$(document).ready(function() {
	var $c = $('#footerDeco');
	var data = [['#1E1C1A', 08], ['#FE9039', 11], ['#FDE099', 15], ['#C7E6DB', 17], ['#A4C4D8', 20], ['#2F6A84', 29]];
	for (var i =0; i< data.length; i++){
		var fID = 'f' + i;
		var f = '<div id="' + fID + '"></div>';
		$c.append(f);
		$(('#' + fID)).css({ 'width': (data[i][1] + '%'), 'height': '100%', 'background-color': data[i][0], 'float': 'left', 'overflow': 'hidden' });
	}
});
/*TOP MENU*/
$(document).ready(function() {
	var loc = location.href;
		$('#menuTop a').each(function() { 
			var myName = $(this).attr("href");
			myName = myName.replace('../', '');
			if(loc.indexOf(myName) > -1) { 
				$(this).addClass('tm_highlight');
			}
		});
});		
/*NEWS*/
$(document).ready(function() {
	$.get(fe_ruta + 'assets/data/xml/news/news.xml', function(d){
		var  tot = 10; var cur = 0;
		var $feeds = $(d).find('item');
		var t = $feeds.length >= tot ? tot : ($feeds.length * 1);
		var $c = $('#news'); var $nav = $('#news_nav');
		$feeds.each(function(index) {
			if(index < t) {
				var d = new String($(this).find('description').text());
				var da = new String($(this).find('pubDate').text());
				var date = new String(da.substr((da.indexOf(',') + 2), 11));
				var desc = d.substr((d.indexOf(':') + 2), (d.length - 1));
				var l = ''; var s;
				if(desc.indexOf('http://') > -1) {
					s = desc.indexOf('http://');
					l = desc.indexOf(' ', s) > -1 ? desc.substr(s, desc.indexOf(' ', s)) : desc.substr(s, (desc.length - 1));
				}else if (desc.indexOf('www.') > -1){
					s = desc.indexOf('www.');
					l = desc.indexOf(' ', s) > -1 ? desc.substr(s, desc.indexOf(' ', s)) : desc.substr(s, (desc.length - 1));
				}	
				if(l != '') { desc = desc.replace(l, ('<a class="new_link" href="' + l + '" target="_blank">[Ver m&aacute;s.]</a>')); }
				var not = "<div class='new'><p><span class='date_new'>" + date + "</span> " + desc + "</p></div>";
				$nav.before(not);
				/*news_nav*/
				var go_class = index == 0 ? "go_to_new_highlight" : "go_to_new";
				var go_but = "<a class='" + go_class + "' href='" + index + "'>[" + (index + 1) + "]</a>";
				$nav.append(go_but);
			}
		});
		/*news_nav*/
		$nav.append("<a class='go_to_new' href='" + fe_ruta + "noticias/'>[...m&aacute;s]</a>");
		$c.find('div:eq(' + cur + ')').css('color', '#454545');
		/*get heights*/
		var news_heights = []; $c.find('div').each(function(index){ if(index < t) { news_heights.push(($(this).height() + 5)); } });
		var th = 0; for(var i = 0; i < news_heights.length; i++){ th += news_heights[i]; };
		/*timer*/
		$.timer(4000, function(timer) {
			/*on/off*/
			$c.find('div:eq(' + cur + ')').css('color', '#999999');
			$nav.find('a:eq(' + cur + ')').removeClass('go_to_new_highlight');
			$nav.find('a:eq(' + cur + ')').addClass('go_to_new');
			cur ++; if(cur == t) { cur = 0; }
			$c.find('div:eq(' + cur + ')').css('color', '#454545');
			$nav.find('a:eq(' + cur + ')').removeClass('go_to_new');
			$nav.find('a:eq(' + cur + ')').addClass('go_to_new_highlight');
			/*move*/
			var nt = 0; var at = 0; var i;
			for(i = 0; i < cur; i++){ nt += news_heights[i]; } nt *= -1;
			if(cur >= Math.round(t / 2)) { for(i = (t -1); i >= cur; i--) { at += news_heights[i]; } }	
			$c.find('div:lt(' + t + ')').each(function(index){
				var $this = $(this);
				if(cur < Math.round(t / 2)) {
					if(index >= Math.round(t / 2)){
						if(cur == 0){ $this.animate({ 'top': ((th * -1) + "px") }, 1500, "easeOutQuad", function() { $this.css('top', 0); }); }
						else{ $this.animate({ 'top': (nt + "px") }, 1500, "easeOutQuad"); }
					}else{ $this.animate({ 'top': (nt + "px") }, 1500, "easeOutQuad" ); }
				}else{
					if(index < Math.round(t / 2)){
						if(cur == Math.round(t / 2)){ $this.animate({ 'top': (nt + "px") }, 1500, "easeOutQuad", function() { $this.css('top', (at + "px")); }); }
						else{ $this.animate({ 'top': (at + "px") }, 1500, "easeOutQuad" ); }
					}else{ $this.animate({ 'top': (nt + "px") }, 1500, "easeOutQuad" ); }
				}
			});
		});
		/*go_to_new click*/
		$nav.find('a:lt(' + t + ')').click(function(){
			/*on/off*/
			$c.find('div:eq(' + cur + ')').css('color', '#999999');
			$nav.find('a:eq(' + cur + ')').removeClass('go_to_new_highlight');
			$nav.find('a:eq(' + cur + ')').addClass('go_to_new');
			cur = $nav.find('a:lt(' + t + ')').index(this);
			$c.find('div:eq(' + cur + ')').css('color', '#454545');
			$nav.find('a:eq(' + cur + ')').removeClass('go_to_new');
			$nav.find('a:eq(' + cur + ')').addClass('go_to_new_highlight');
			/*move*/
			var nt = 0; var at = 0; var i;
			for(i = 0; i < cur; i++){ nt += news_heights[i]; } nt *= -1;
			if(cur >= Math.round(t / 2)) { for(i = (t -1); i >= cur; i--) { at += news_heights[i]; } }	
			$c.find('div:lt(' + t + ')').each(function(index){
				var $this = $(this);
				if(cur < Math.round(t / 2)) {
					if(index >= Math.round(t / 2)){
						if(cur == 0){ $this.animate({ 'top': ((th * -1) + "px") }, 1500, "easeOutQuad", function() { $this.css('top', 0); }); }
						else{ $this.animate({ 'top': (nt + "px") }, 1500, "easeOutQuad" ); }
					}else{ $this.animate({ 'top': (nt + "px") }, 1500, "easeOutQuad" ); }
				}else{
					if(index < Math.round(t / 2)){
						if(cur == Math.round(t / 2)){ $this.animate({ 'top': (nt + "px") }, 1500, "easeOutQuad", function() { $this.css('top', (at + "px")); }); }
						else{ $this.animate({ 'top': (at + "px") }, 1500, "easeOutQuad" ); }
					}else{ $this.animate({ 'top': (nt + "px") }, 1500, "easeOutQuad" ); }
				}
			});
			return false;
		});
	});
});
/*LINKS*/
$(document).ready(function() {
	var $link = $('.link'); var off = 6.667;
	var w = $link.css('width'); w = w.substr(0, w.lastIndexOf('px')) * 1;
	$link.each(function(index) {
		$(this).css('left', ((index * (w + off)) + 'px'));
	});
});
/*LINKS_EXTERNOS*/
var r_LEIMG = fe_ruta + 'assets/images/linksExternos/';
$(document).ready(function() {
	var cur = 2; var tot;
	var $adsImg = $('#ads img'); var $adsLink = $('#ads>a');
	$.get(fe_ruta + 'assets/data/xml/linksExternos/linksExternos.xml', function(d){ 
		var $links = $(d).find('link'); tot = $links.length;
		var data = []; 
		$links.each(function() { data.push([$(this).find('src').text(), $(this).find('href').text()]); });
		$adsLink.each(function(index) { $(this).attr('href', data[index][1]); });
		$adsImg.each(function(index) { $(this).attr('src', (r_LEIMG + data[index][0])); $(this).hide().fadeIn('slow'); });
		$.timer(5000, function(timer) {
			if(cur == (tot - 3)) { cur = 0; }
			$adsLink.each(function(index) { $(this).attr('href', data[(index + cur)][1]); });
			$adsImg.each(function(index) { $(this).attr('src', (r_LEIMG + data[index + cur][0])); $(this).hide().fadeIn('slow'); });
			cur ++; 
		});
	});
});
/*HEIGHT FIX*/
$(document).ready(function() {
	var h = $('#mainLeft').height() - 210;
	$('#billboard').css('height', (h + 'px'));
});
