/*** tabs ***/


	$(document).ready(function () {

		$('#tabMenu a').click(function () {
		
			$('#tabMenu a span').css('font-weight','');
			$('#tabMenu a.selected').removeClass('selected');

			$(this).addClass('selected')
			$('span',this).css('font-weight','bold');

			var divId = $(this).attr('href').substring(1);

			$('#ultimos, #leidos, #comentados').hide();
			$('#'+divId).show();

			$(this).blur();

			return false;
		});

		/*** subtitulo | mas info */
		if(jQuery.masinfo) {

			jQuery.masinfo(

				$('#noticias table.texto:eq(0) img'),
				$('#noticias table.texto:eq(0) h1.subtitulo')
			);
			
			jQuery.masinfo(

				$('#testmania img:eq(0)'),
				$('#testmania h2.subtitulo')
			);
		}
	});

/*** BoardZona.tv ***/

	nextTaskTv = 0;
	milisencondstv = 4500;
	enableShowNextTv = true;

	function checkTasksTv () {

		if(enableShowNextTv == true || enableShowNextTv == 'true') {
			
			var now_tv = new Date().getTime();
		
			if(now_tv > nextTaskTv) {

				nextTaskTv = new Date().getTime() + milisencondstv;
				showNext();
			}

			setTimeout(checkTasksTv, milisencondstv/2);
		}
	}

	function showNext () {

		var items = $('#principal ul li');

		for(i=0 ; i< items.length; i++) {

			if(parseInt ($(items[i]).css('opacity')) > 0) {

				if($(items[i]).next()[0]) {

					$(items[i]).css('opacity','0.5');
					$(items[i]).next().css('opacity','1');
					loadContent($('div',$(items[i]).next()));

				} else {

					$(items[i]).css('opacity','0.5');
					$(items[0]).css('opacity','1');
					loadContent($('div',items[0]));
				}

				break;
			}
		}
	}

	function img_cargada () {

		var img = $('#otrolado img:eq(0)');

		if(img[0].complete == true) {

			$('#loading').hide();
			setTimeout (showGrande, 50 );
	
		} else {

			setTimeout (img_cargada, 500 );
		}
	}

	function showGrande () {

		$('#otrolado').fadeIn("slow");
	}

	function loadContent(obj) {

		$(obj).blur();

		$('#principal ul li').css('opacity','0.5');
		$($(obj).parent()).css('opacity','1');

		var item = $('div.link a',$(obj).parent());
		var img = item.attr('id');
		var title = $('img',item).attr('title');
		var msg = $('span',item).text();
		var url = item.attr('href');

		$('#otrolado').fadeOut("slow", function () {
			
			$('#loading').show();
			$('#otrolado img:eq(0)').attr('src','http://www.BoardZona.tv/' + img + 'g.jpg');
			$('#otrolado img:eq(0)').attr('title',title);

			$('#otrolado center a').attr('href',url);

			$('#otrolado div#texto a').attr('href',url);
			$('#otrolado div#texto a').attr('title',title);
			$('#otrolado div#texto a.titulo').text(title);
			$('#otrolado div#texto a span').text(msg);
			$($('#otrolado div#texto a span').parent()).attr('title',msg);

			img_cargada();
		});
	}

	$(document).ready(function(){

		$('#principal li:gt(0)').css('opacity','0.5');
		$('#principal li:eq(0)').css('opacity','1');

		$('#principal ul li div').click(function () {

			enableShowNextTv = false;
			loadContent(this);
			
			return false;
		});

		setTimeout(checkTasksTv,3000);
	});

/*** testmania :: slider ***/

	divWidth = 0;
	play = true;
	reverse = 0;

	task = "next";
	nextTask = 0;
	milisenconds = 5000;
	scrollvalue = 290;

	initialmargin = 0;


	function checkTasks () {

		var now = new Date().getTime();

		if(now > nextTask) {

			nextTask = new Date().getTime() + milisenconds;

			switch(task) {

				case "next":
					reverse = 0;
					slider();
					break;

				case "prev":
					reverse = 1;
					slider();
					break;
			}
		}
		setTimeout(checkTasks, milisenconds/2);
	}

	function reset () {

		if(play == 'true' || play == true) {

			var obj = $('#slider > div');
			$(obj).animate({marginLeft:initialmargin},'slow','swing');
			$('#slider > div').css('width','');
		}
	}

	function slider () {

		if(play == 'true' || play == true) {

			var obj = $('#slider > div');
			var itemNum = $('table', obj).length;
			
			var yTable1 = parseInt($('table:eq(0)', obj).offset().top);
			var yTable2 = parseInt($('table:eq('+ (itemNum -1) +')', obj).offset().top);

			var value = scrollvalue;

			if(obj.css('margin-left') != 'auto')
				var current_offset = parseInt(obj.css('margin-left'));
			else
				var current_offset = 0;

			if(reverse == 1) {

				reverse = 0;
				var newMargin = (current_offset + value) + 'px';

				if((obj.offset().left * -1) > 0) {

					$(obj).animate({marginLeft:newMargin},800,'linear');
				}

			} else {

				var newMargin = (current_offset - value) + 'px';

				if(yTable1 < yTable2 || ((current_offset*-1) < (itemNum-1)*scrollvalue)) {

					$(obj).animate({marginLeft:newMargin},800,'linear');

				} else {

					reset();
				}
			}
		}
	}


	$(document).ready(function(){

		divWidth = $('#slider > div').width();

		if($.browser.msie == true || $.browser.msie == 'true') {

			if(parseInt($.browser.version) < 7) {

				$('#slider > div').css('margin-left','-15px')
				initialmargin = -15;

			}
		}

		$('#slider > div table').hover(

			function () {	
				play = false;
				$(this).css('background-color','#111111');
			}, 
			function () {	
				play = true;
				$(this).css('background-color','');
			}
		);

		$('#prev').hover(

			function () {	
				play = false;
			}, 
			function () {	
				play = true;
			}
		);

		$('#prev').click(function () {
			
			if(play == false || play == 'false'){	play = true; }
			
			nextTask = new Date().getTime() + milisenconds;
			reverse = 1;
			slider();
			task = "next";
		});

		$('#next').hover(

			function () {	
				play = false;
			}, 
			function () {	
				play = true;
			}
		);

		$('#next').click(function () {
		
			if(play == false || play == 'false'){	play = true; }
			nextTask = new Date().getTime() + milisenconds;
			reverse = 0;
			slider();
			task = "next";
		});
		
		setTimeout (checkTasks, 3000 );
	});
