function loadswf(id, flashvars) {
 	var height = $('div#' + id).css('height').replace('px', '');
 	var width = $('div#' + id).css('width').replace('px', '');
 	var file = '/media/3/swf/' + id + '.swf';
 	var so = new SWFObject(file, id, width, height, 10, '#FFF');

 	if (typeof flashvars != 'undefined') so.addParam('flashvars', flashvars);
 	
	so.addParam('wmode', 'transparent'); 
	so.write(id);
}

function loadswf2(id, file, width, height, flashvars) {
 	var so = new SWFObject(file, id, width, height, 10, '#FFF');

 	if (typeof flashvars != 'undefined') so.addParam('flashvars', flashvars);
 	
	so.addParam('wmode', 'transparent'); 
	so.write(id);
}

var intervalhome;

function load_home() {
	/* div.banner_horizontal */
	$('div.home div.banner_horizontal ul').each(function () {
		$($(this).children('li')[0]).innerfade({
			animationtype: 'fade',
			speed: 'normal',
			timeout: 8000,
			type: 'sequence',
			containerheight: '166px'
		});
		$($(this).children('li')[1]).innerfade({
			animationtype: 'fade',
			speed: 'normal',
			timeout: 9000,
			type: 'sequence',
			containerheight: '166px'
		});
		$($(this).children('li')[2]).innerfade({
			animationtype: 'fade',
			speed: 'normal',
			timeout: 10000,
			type: 'sequence',
			containerheight: '166px'
		});
	});

	/* div.encuesta_btn */
	$('div.encuesta_btn').click(function () {
		if ($(this).css('margin-left') != '-463px') {
			$('div.home div.info').each(function () {
				$(this).children('div.online').fadeOut();
				$(this).children('div.encuesta').fadeIn('slow');
				
				$('div.online_btn').fadeOut();
				$('div.encuesta_btn').stop().animate({ marginLeft: -463}, 500, function () {
					$('div.online_btn').css('margin-left', '-493px');
					$('div.online_btn').fadeIn('fast');
				})
			});
			
		}
		
		return false;
	});
	
	$('div.online_btn').click(function (data) {
		if ($(this).css('margin-left') != '-463px') {
			$('div.home div.info').each(function () {
				$(this).children('div.encuesta').fadeOut();
				$(this).children('div.online').fadeIn('slow');
				
				$('div.encuesta_btn').fadeOut();
				$('div.online_btn').stop().animate({ marginLeft: -463}, 500, function () {
					$('div.encuesta_btn').css('margin-left', '-493px');
					$('div.encuesta_btn').fadeIn('fast');
				})
			});
		}
		
		return false;
	});

	/* div.comentarios */
	$('div.home div.comentarios div.block').innerfade({
		animationtype: 'fade',
		speed: 'normal',
		timeout: 8000,
		type: 'sequence',
		containerheight: '104px'
	});

    $('.rotate_banner_intermedio').innerfade({
		animationtype: 'fade',
		speed: 'normal',
		timeout: 4000,
		type: 'sequence',
		containerheight: '104px'
	});

	/* div.online */
	$('div.home div.info div.online div.block').each(function () {
		var width_max = $(this).children('div.cant_votos').width();
			
		i = Math.round($(this).children('p.cant').html() * width_max / 100);
		
		if (i > width_max) i = width_max;
		
		$(this).children('div.cant_votos').children('span').width(i);
	});

	/* div.encuesta */
	$('div.home div.info div.encuesta div.estadisticas p').each(function () {
		var valor = $(this).html();
		var name = $(this).attr('class');
		
		name = name.replace(' last', '');
				
		$(this).parent().children('div.barras').children('span.' + name).each(function () {
			var height = valor * $(this).parent().css('height').replace('px', '') / 100;
			$(this).height(height);
			$(this).css('margin-top', $(this).parent().css('height').replace('px', '') - height);
			
			$(this).parent().children('span:last-child').addClass('last');
			var cant = $(this).parent().children('span:not(.last)').length;
			
			if (!$(this).hasClass('last')) {
				$(this).css('margin-right', (80 / cant) - 14);
				$(this).parent().parent().children('p.' + name).css('margin-right', (80 / cant) - 20)
			}
			
		});

		$(this).append('%')		
	});
	
	/* div.social */
	$('div.info div.encuesta a.cerrar_btn').click(function () {
		$(this).parent().parent().children('div.online').fadeIn('slow');
		$(this).parent().fadeOut();
		
		return false;
	});

	/* div.bigbanner */
	$('div.home div.bigbanner').each(function () {
		$(this).children('div.thumb').innerfade({
			animationtype: 'fade',
			speed: 'normal',
			timeout: 8000,
			type: 'sequence',
			containerheight: '257px'
		});
		
		$(this).children('div.botonera').each(function () {
			var cant = $(this).parent().children('div.thumb').children('a').length;
			for (var i = 1; i <= cant; i++) {
				$(this).children('div.block').append('<a href="#" class="' + i + '">' + i + '</a>');
			}
		});
		
		$(this).children('div.botonera').children('div.block').children('a').click(function () {
			var pos = $(this).attr('class') - 1;
			
			$(this).parent().parent().parent().children('div.thumb').children('a').fadeOut();
			$($(this).parent().parent().parent().children('div.thumb').children('a')[pos]).fadeIn();
			
			return false;
		});
	});

	/* div.acordeon */
	$('div.articulos div.acordeon').each(function () {
		$(this).children('div.elem:not(.active)').hide();
		$(this).children('div.active').show();

		$(this).children('h4').children('a').click(function () {
			$(this).parent().parent().children('div.elem').removeClass('active');
			$(this).parent().next('div.elem').addClass('active');
			
			$(this).parent().parent().children('div.elem:not(.active)').slideUp();
			$(this).parent().parent().children('div.active').slideDown();
			
			return false;
		});
	});

	intervalencuesta = setInterval(toggleEncuesta,10000);
}

var intervalencuesta;
var posencuesta = 0;

function toggleEncuesta() {
	posencuesta = !posencuesta;
	if (posencuesta) $('div.encuesta_btn').trigger('click');
	else $('div.online_btn').trigger('click');
}

function loadOndemand(file)
{
	var flashvar = "&file=" + file + "&autoplay=true&preview=images/preview.jpg";
	if ($("div#player_ondemand")[0] != null) loadswf("/media/2/swf/ondemand.swf", "player_ondemand", 450, 467, "#000", flashvar);
}

function load_verano() {
	$('div.premios div.premio2').click(function () {
		$('div.premio2_detalle').fadeIn();

		return false;
	});

	$('div.premios div.premio2').mouseout(function () {
		$('div.premio2_detalle').fadeOut();

		return false;
	});

}

function load_fiesta() {
	$('div.galeria img').mouseover(function () {
		$('div.galeria div.item').css('z-index', '1000');
		$(this).parent().parent().css('z-index', '9000');
	});
	
	if (typeof $('div.player_swf')[0] != 'undefined') 
		loadswf2('player_swf', '/media/3/swf/ondemand.swf', 340, 212, '&autoplay=false&file=/media/3/swf/backstage.flv&preview=/media/3/fiesta/player_preview.jpg');

    $("div.chilevision a:first").overlay({
        expose: '#fff',
        top: '5%',
        left:  	"center",
		onBeforeLoad: function() {

			// grab wrapper element inside content
			var wrap = this.getContent().find(".contentWrap");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}
	});

	$("div.backstage a").overlay({
		target: '#div_enviar',
		expose: '#fff',
		closeOnClick: true
	}).gallery({
		disabledClass: 'inactive',
		template: '<strong>${title}</strong> <span>${index} de ${total}</span>'
	});
	  
	$("div.galeria a").overlay({
		target: '#div_enviar',
		expose: '#fff',
		closeOnClick: true
	}).gallery({
		disabledClass: 'inactive',
		template: '<strong>${title}</strong> <span>${index} de ${total}</span>'
	});
	
	$("div.lacuarta a").overlay({
		target: '#div_enviar',
		expose: '#fff',
		closeOnClick: true
	}).gallery({
		disabledClass: 'inactive',
		template: '<strong>${title}</strong> <span>${index} de ${total}</span>'
	});
	$("div.publimetro a").overlay({
		target: '#div_enviar',
		expose: '#fff',
		closeOnClick: true
	}).gallery({
		disabledClass: 'inactive',
		template: '<strong>${title}</strong> <span>${index} de ${total}</span>'
	});
	  
	$('a.backstage').click(function () {
		$('div.backstage a:first').trigger('click');
		return false;
	});
	$('a.fotos').click(function () {
		$('div.galeria a:first').trigger('click');
		return false;
	});
	$('a.prensa').click(function () {
		$('div.menu div.info2').toggle();
		return false;
	});
	$('div.menu a.lacuarta_btn').click(function () {
		$('div.lacuarta a:first').trigger('click');
		return false;
	});
	$('div.menu a.chilevision_btn').click(function () {
		$('div.chilevision a:first').trigger('click');

		return false;
	});
	$('div.menu a.publimetro_btn').click(function () {
		$('div.publimetro a:first').trigger('click');

		return false;
	});
	
}

function load_social() {
	$('div.social a.facebook_btn').click(function () {
		$(this).parent().children('div.box_facebook').each(function () {
			$(this).fadeIn();
			$(this).children('a.cerrar_btn').click(function () {
				$(this).parent().fadeOut();
				
				return false;
			});
		});		
		
		return false;
	});
}

function load_encuesta() {
	var i = 0;
	$('div.encuesta div.item').each(function () {
		if (i++ % 2) $(this).addClass('right');
	});
}

$(document).ready(function () {
	if (typeof $('div.home')[0] != 'undefined') load_home();
	if (typeof $('div.fiesta:not(.verano)')[0] != 'undefined') load_fiesta();
	if (typeof $('div.verano')[0] != 'undefined') load_verano();
	if (typeof $('div.social')[0] != 'undefined') load_social();
	if (typeof $('div.encuesta')[0] != 'undefined') load_encuesta();
	//if (typeof $('div#navidad2009')[0] != 'undefined') loadswf('navidad2009');
	if (typeof $('div#enamorados')[0] != 'undefined') loadswf('enamorados');
});
