var numNotiRoll = 1;
function roll(num) {
	ocultarNoticiaHoy(num);
	mostrarNoticiaHoy(num);
	numNotiRoll = (num + 1);
	if(numNotiRoll == 4) {
		numNotiRoll = 0;
	}
	function mostrarNoticiaHoy(id) {
		jQuery('#noticias_hoy .contenido .chvmosimage:eq('+id+')').fadeIn(250);
		jQuery('#noticias_hoy .contenido .chvmosimage:eq('+id+')').removeClass('non-selected');
		jQuery('#noticias_hoy .contenido .chvmosimage:eq('+id+')').addClass('selected');
		jQuery('#noticias_hoy .contenido .elem' + id).removeClass('non-selected');
		jQuery('#noticias_hoy .contenido .elem' + id).addClass('selected');
	}
	function ocultarNoticiaHoy(id) {
		for(i=0; i<5; i++) {
			if(i != id) {
				jQuery('#noticias_hoy .contenido .chvmosimage:eq('+i+')').removeClass('selected');
				jQuery('#noticias_hoy .contenido .chvmosimage:eq('+i+')').addClass('non-selected');
				jQuery('#noticias_hoy .contenido .chvmosimage:eq('+i+')').css('display', 'none');
				jQuery('#noticias_hoy .contenido .elem' + i).removeClass('selected');
				jQuery('#noticias_hoy .contenido .elem' + i).addClass('non-selected');
			}
		}
	}
}
var intervalo;
function temporizador() {
	clearInterval(intervalo);
	intervalo = setInterval("roll(numNotiRoll)", 7000);
}
function stopRolling() {
	clearInterval(intervalo);
}
function rollGaleria(div, num) {
	jQuery('#galer_deporte').css('display', 'none');
	jQuery('#galer_tecnologia').css('display', 'none');
	jQuery('#galer_internacional').css('display', 'none');
	jQuery('#galer_nacional').css('display', 'none');
	jQuery(div).fadeIn(250);
	for(i=0; i<4; i++) {
		jQuery('#foto_galerias .galerias_tab').removeClass('selected_tab');
	}
	jQuery('#foto_galerias .tab' + num).addClass('selected_tab');
}
function rollBuscador(div, num) {
	jQuery('#matinal').css('display', 'none');
	jQuery('#tarde').css('display', 'none');
	jQuery('#central').css('display', 'none');
	jQuery(div).fadeIn(250);
	for(i=0;i<3;i++) {
		jQuery('#buscador_noticias .title_button').removeClass('selected_tab');
	}
	jQuery('#buscador_noticias .button'+num).addClass('selected_tab');
}
function initGalerias() {
	jQuery('#galer_deporte').css('display', 'none');
	jQuery('#galer_tecnologia').css('display', 'none');
	jQuery('#galer_internacional').css('display', 'none');
}
function initScrollables() {
	jQuery("#buscador_noticias div.scrollable").scrollable({size: 6, keyboard: false});
	jQuery("#foto_galerias div.scrollable").scrollable({size: 3, keyboard: false});
	jQuery("#ultimos_cazanoticias_elecciones div.scrollable").scrollable({size: 3, keyboard: false});
}
function mostrarUPI(id) {
	jQuery('#layer_noticia_content').html('<div class=title>' + noticiasUPI[id].titulo + '</div><div class="content">' + noticiasUPI[id].intro + '</div>');
	jQuery('#layer_container').css('display', 'block');
}
function subirCazanoticias() {
	jQuery('#layer_cazanoticias_content').html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="450" id="upload" align="middle"><param name="wmode" value="transparent"><param name="allowScriptAccess" value="sameDomain" /><param name="FlashVars" value="seccion=prensa&terminos=bases_cazanoticias_v2" /><param name="movie" value="http://www.chilevision.cl/utils/uploadV2.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed flashvars="seccion=prensa&terminos=bases_cazanoticias_v2" src="http://www.chilevision.cl/utils/uploadV2.swf" quality="high" bgcolor="#ffffff" width="550" height="450" name="upload" align="middle" allowScriptAccess="sameDomain" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
}
function checkScrollItems() {
	var select = 4;
	if(jQuery('#matinal .scrollable').scrollable().getSize() == 0) {
		jQuery('#buscador_noticias .button0').css('display', 'none');
	}
	else {
		jQuery('#buscador_noticias .button0').css('display', 'block');
		if(select == 4) select = 0;
	}
	if(jQuery('#tarde .scrollable').scrollable().getSize() == 0) {
		jQuery('#buscador_noticias .button1').css('display', 'none');
	}
	else {
		jQuery('#buscador_noticias .button1').css('display', 'block');
		if(select == 4) select = 1;
	}
	if(jQuery('#central .scrollable').scrollable().getSize() == 0) {
		jQuery('#buscador_noticias .button2').css('display', 'none');
	}
	else {
		jQuery('#buscador_noticias .button2').css('display', 'block');
		if(select == 4) select = 2;
	}
	switch(select) {
		case 0:	rollBuscador('#matinal', select);
				break;
		case 1:	rollBuscador('#tarde', select);
				break;
		case 2:	rollBuscador('#central', select);
				break;
	}
}

// set the list selector
var setSelector = "#sortable";
// set the cookie name
var setCookieName = "chvPrensaListOrder";
// set the cookie expiry time (days):
var setCookieExpiry = 30;

function initOrden() {
	// here, we allow the user to sort the items
	jQuery(setSelector).sortable({
		axis: "y",
		cursor: "move",
		update: function() { getOrder(); }
	});

	// here, we reload the saved order
	restoreOrder();
}
 
// function that writes the list order to a cookie
function getOrder() {
	// save custom order to cookie
	jQuery.cookie(setCookieName, jQuery(setSelector).sortable("toArray"), { expires: setCookieExpiry, path: "/" });
}

// function that restores the list order from a cookie
function restoreOrder() {
	var list = jQuery(setSelector);
	if (list == null) return

	// fetch the cookie value (saved order)
	var cookie = jQuery.cookie(setCookieName);
	if (!cookie) return;

	// make array from saved order
	var IDs = cookie.split(",");

	// fetch current order
	var items = list.sortable("toArray");

	// make array from current order
	var rebuild = new Array();
	for (var v=0, len=items.length; v<len; v++){
		rebuild[items[v]] = items[v];
	}

	for (var i = 0, n = IDs.length; i < n; i++) {
		// item id from saved order
		var itemID = IDs[i];
		if (itemID in rebuild) {
			// select item id from current order
			var item = rebuild[itemID];
			// select the item according to current order
			var child = jQuery("ul.ui-sortable").children("#" + item);
			// select the item according to the saved order
			var savedOrd = jQuery("ul.ui-sortable").children("#" + itemID);
			// remove all the items
			child.remove();
			// add the items in turn according to saved order
			// we need to filter here since the "ui-sortable"
			// class is applied to all ul elements and we
			// only want the very first!  You can modify this
			// to support multiple lists - not tested!
			jQuery("ul.ui-sortable").filter(":first").append(savedOrd);
		}
	}
}
function alinearPaginacion() {
	var numElementos = jQuery('.paginacion').children().size();
	var x = jQuery('.paginacion .pagina:first').width();
	var y = jQuery('.paginacion .pagina:last').width();
	var factor = (x+y)/2+16;
	jQuery('.paginacion').css('width', numElementos*factor+'px');
}
jQuery(document).ready(function() {
	roll(0);
	temporizador();
	rollGaleria('#galer_nacional', 2);
	initScrollables();
	checkScrollItems();
	initOrden();
	alinearPaginacion();
	//jQuery('.extra_content #marquee_content').marquee();
	jQuery("#extra a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#noticias_en_linea a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#ultimos_nacional a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#ultimos_internacional a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#ultimos_deportes a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#ultimos_policial a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#ultimos_politica a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#ultimos_economia a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#ultimos_tiempo_libre a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#ultimos_reportajes a[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
	jQuery("#subir_cazanoticias[rel]").overlay({
		expose: {
			color: '#333',
			loadSpeed: 200,
			opacity: 0.9
		}
	});
});