var timerID = null;
var notTope=0;
var puedoSubir=false;
var puedoBajar=false;
$(document).ready(function(){
	$(".megamenu").megamenu({'justify':'left'});
	nots = new Array();
	$('.not').live('mouseenter mouseleave click', function(event){
		switch (event.type) {
			case "mouseenter":
				if(!$(this).hasClass("notAct")) $(this).addClass("notOver");
				break;
			case "mouseleave":
				if(!$(this).hasClass("notAct")) $(this).removeClass("notOver");
				break;
			default:
				ponNoticia;
		}
	});
	$.ajax({
		type:"GET",
		url: "/"+idioma+"/noticias/index.xml",
		dataType: "xml",
		cache: false,
		success: function(datos){
			if ($(datos).find("noticia").text()=="") $("#noticiaCont").html("<p>"+str_no+"</p>");
			else{
				var con="";
				var con2="";
				$(datos).find("noticia").slice(0).each(function(i) {
					nots[i]={"id":$(this).attr("id"),"ti":$(this).find("tit").text(),"fe":$(this).attr("fecha"),"fo":$(this).find("foto").find("nombre").text(),"te":$(this).find("texto").text(),"v":$(this).find("v").text()};
				});
			}
		},
		complete:function(){
			var con="";
			for (i=0;i<nots.length;i++){
				con+="<div id='not_"+i+"' class='not'><div class='notA'></div><div class='notB'><p>"+nots[i].fe+"<br />"+nots[i].ti+" <a href='javascript:;'>";
				con+=(nots[i].v=="")?str_mas+"</a></p></div></div>":str_vid+"</a></p></div></div>";
			}
			if (nots.length>0) {
				$("#notListadob").html(con);
				$(".not").bind("click",ponNoticia).first().click();
				if ($("#notListado").height()+2 < $("#notListadob").height()) {
					$("#cajanotnotint .downOff").addClass("downOn").bind("click",notSubir);
					puedoSubir=true;
					notTope=$("#notListado").height()-$("#notListadob").height();
				}
				$("#noticiaCargando").hide();
			}
		},
		error:function(){$("#noticiaCont").html("<p>"+str_no+"</p>");}
	});
});
subiendo=null;
bajando=null;
function notSubir(){
	clearTimeout(subiendo);
	subiendo=setTimeout("notSubir2()",200);
}
function notSubir2(){
	if (($("#notListadob").position().top-160)>=notTope) $("#notListadob").animate({top: "-=80"}, 500);
	else {
		if ($("#notListadob").position().top>notTope) $("#notListadob").animate({top: notTope}, 500);
		if (puedoSubir) {
			$("#cajanotnotint .downOff").removeClass("downOn").unbind("click");
			puedoSubir=false;
		}
	}
	if (!puedoBajar) {
		$("#cajanotnotint .upOff").addClass("upOn").bind("click",notBajar);
		puedoBajar=true;
	}
}
function notBajar(){
	clearTimeout(bajando);
	bajando=setTimeout("notBajar2()",200);
}
function notBajar2(){
	if (($("#notListadob").position().top+159)<0) $("#notListadob").animate({top: "+=80"}, 500);
	else {
		if (($("#notListadob").position().top)<0) $("#notListadob").animate({top: 0}, 500);
		if (puedoBajar) {
			$("#cajanotnotint .upOff").removeClass("upOn").unbind("click");
			puedoBajar=false;
		}
	}
	if (!puedoSubir) {
		$("#cajanotnotint .downOff").addClass("downOn").bind("click",notSubir);
		puedoSubir=true;
	}

}
function ponNoticia(){
	if (!$(this).hasClass("notAct")){
		var con;
		$(".notAct").removeClass("notAct").removeClass("notOver");
		$(this).addClass("notAct");
		var ind=$(this).attr("id").split("_")[1];
		con=(nots[ind].v=="")?"<div id='noticiaFondo'></div><img src='/imgnoticias/m_"+nots[ind].fo+"' alt='"+nots[ind].ti+"' width='500' height='320' />"+
	"<p>"+nots[ind].te+"<a href='/"+idioma+"/noticia.php?id="+nots[ind].id+"'>"+str_lee+"</a></p>":"<object width='500' height='320' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0'><param name='menu' value='false' /><param name='movie' value='videonoticias.swf?videoacargar="+nots[ind].v+"' /><param name='quality' value='high' /><param name='wmode' value='opaque' /><embed width='500' height='320' src='videonoticias.swf?videoacargar="+nots[ind].v+"' quality='high' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' wmode='opaque' menu='false' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object><p>"+nots[ind].te+"<a href='noticia.php?id="+nots[ind].id+"'>"+str_lee+"</a></p>";
	
		if ($("#noticiaCont").position().top==0) $("#noticiaCont").animate({top:"+=320"},500, function(){$(this).html(con).animate({top:"-=320"},500);});
		else $("#noticiaCont").html(con).animate({top:"-=320"},500);
	}
}
