/**
 * @author allkom
 */

function showMostViewed(){

	//Oculto
	$('mostEmailed').removeClassName( "menuMostSel" );
	$('divMostEmailed').style.display = "none";
	
	//Muestro
	$('mostViewed').toggleClassName('menuMostSel');
	$('divMostViewed').style.display = "block";
	
	
}

function showMostEmailed(){

	//Oculto
//	if( $('mostViewed').hasClassName( "menuMostSel" ) ){		
		$('mostViewed').removeClassName( "menuMostSel" );
//	}
	$('divMostViewed').style.display = "none";
	
	//Muestro
	$('mostEmailed').toggleClassName('menuMostSel');
	$('divMostEmailed').style.display = "block";
	
	
}

function menuEvents(){
	
	
	$$("li.liMenuPrincipal").each(function(node){
		node.observe('mouseover',function(){
			$$("ul.menuSecundario").each(function(nodeSec){
				nodeSec.style.display = 'none';
			});
			if($('sub'+node.id) != undefined){
				$('sub'+node.id).style.display = 'block';
			}
		})		
	})
	
}


function votePoll(){
	$('pollForm').request({
		onComplete: function(res){
			$('popups').update(res.responseText);
			$('popups').style.width = '391px';			
			showDisable();
			new Effect.Center('popups');
			new Effect.Appear('popups');
		}
	});
	
}

function viewResultsPoll(url){
	new Ajax.Request(url,{
		onComplete: function(res){
			$('popups').style.width = '391px';
			$('popups').update(res.responseText);
			showDisable();
			new Effect.Center('popups');
			new Effect.Appear('popups');
		}
	})
}

function closePollResults(){
	Effect.Fade('popups');
	hideDisable();
}

function commentNotice( ){

	$('frmComment').request({
		onComplete: function(response){
			
			if( response.responseText != 'err' ){
				
				$('comments').update( response.responseText );
			}
			
		}
	})

}

function voteNotice( noticeId, points ){

	//FIXME: HAY QUE ARREGLAR ESTO ES UNA "CABECEADA" (LastMoment)
	new Ajax.Request('/BreakingNews/Votar/' + noticeId + '/' + points, { method : 'get',
		onComplete: function( res ){
			if( res.responseText != 'err' ) {
				datos = res.responseText.evalJSON(true);
				
				$('votos').update( datos.stars );
				$('cantVotes').update( datos.votos + ' votes')
			}
		}
	} );
	
}

function init_top10(){

	if( $('mostViewed') != null ) {
		Event.observe( 'mostViewed', 'click', function(){
			showMostViewed();
		});
	}
	if( $('mostEmailed') != null ) {	
		Event.observe( 'mostEmailed', 'click', function(){
			showMostEmailed();
		});
	}
}

function openMailBox(){
	
	document.getElementById('securecaptchaimg').src = '/Login/SecureImageShow/sid/' + Math.random().toString();
	showDisable();
	Effect.Center('enviarNoticia');
	Effect.Grow('enviarNoticia',{duration:0.1, queue: 'end'});
		
}

function closeMailBox(){
	
	$('enviarNoticia').hide();
	hideDisable();
}

function sendMailBox(){

	$('frmSendMail').request({		
		onComplete: function(response){
			
			alert( response.responseText );
			closeMailBox();
			
		}
	});	
}


var agrandado = 0;
function agrandarLetra(){

	if( agrandado < 6 ) {
		
		//Titulo
		$$('span.tituloNoticiaAmpliada').each( function( s ){
			medida = Number( s.getStyle( 'fontSize').substr( 0, s.getStyle( 'fontSize').length - 2 ) ) + 3;
			s.setStyle({
			  fontSize: medida + 'px'
			});
		} );
		
		//Volanta
		$$('span.bajadaNoticiaAmpliada').each( function( s ){
			medida = Number( s.getStyle( 'fontSize').substr( 0, s.getStyle( 'fontSize').length - 2 ) ) + 3;
			s.setStyle({
			  fontSize: medida + 'px'
			});
		} );
		
		//Autor
		$$('span.textoBy').each( function( s ){
			medida = Number( s.getStyle( 'fontSize').substr( 0, s.getStyle( 'fontSize').length - 2 ) ) + 3;
			s.setStyle({
			  fontSize: medida + 'px'
			});
		} );
		
		//Texto
		medida = Number( $('textoNoticia').getStyle( 'fontSize').substr( 0, $('textoNoticia').getStyle( 'fontSize').length - 2 ) ) + 3;
		$('textoNoticia').setStyle({
		  fontSize: medida + 'px'
		});

/*		
		//Copete
		medida = Number( $('copeteNoticia').getStyle( 'fontSize').substr( 0, $('copeteNoticia').getStyle( 'fontSize').length - 2 ) ) + 3;
		$('copeteNoticia').setStyle({
		  fontSize: medida + 'px'
		});		
*/

		agrandado++;
	}
}

function achicarLetra(){

	if( agrandado > -6 ) {

		//Titulo
		$$('span.tituloNoticiaAmpliada').each( function( s ){
			medida = Number( s.getStyle( 'fontSize').substr( 0, s.getStyle( 'fontSize').length - 2 ) ) - 3;
			s.setStyle({
			  fontSize: medida + 'px'
			});
		} );
		
		//volanta
		$$('span.bajadaNoticiaAmpliada').each( function( s ){
			medida = Number( s.getStyle( 'fontSize').substr( 0, s.getStyle( 'fontSize').length - 2 ) ) - 3;
			s.setStyle({
			  fontSize: medida + 'px'
			});
		} );
		
		//Autores
		$$('span.textoBy').each( function( s ){
			medida = Number( s.getStyle( 'fontSize').substr( 0, s.getStyle( 'fontSize').length - 2 ) ) - 3;
			s.setStyle({
			  fontSize: medida + 'px'
			});
		} );
		
		//Texto
		medida = Number( $('textoNoticia').getStyle( 'fontSize').substr( 0, $('textoNoticia').getStyle( 'fontSize').length - 2 ) ) - 3;
		$('textoNoticia').setStyle({
		  fontSize: medida + 'px'
		});

/*		
		//Copete
		medida = Number( $('copeteNoticia').getStyle( 'fontSize').substr( 0, $('copeteNoticia').getStyle( 'fontSize').length - 2 ) ) - 3;
		$('copeteNoticia').setStyle({
		  fontSize: medida + 'px'
		});
*/
	
		agrandado--;
	}
}

function openVentanaImprimir( id , archive, fecha) {
  //mitad de la pantalla
  vLeft = (screen.width-300)/2;
  vTop = (screen.height-500)/2;
  if(!archive)
	  window.open( '/BreakingNews/ViewPrint/' + id , 'pop', "location=0,status=0,scrollbars=1,width=680,height=500,directories=0,menubar=0,resizable=0,left=" + vLeft + ",top=" + vTop  );
  else
	  window.open( "/Archive/" + fecha + '/ViewPrint/' + id, 'pop', "location=0,status=0,scrollbars=1,width=680,height=500,directories=0,menubar=0,resizable=0,left=" + vLeft + ",top=" + vTop  );
}

function multimediaShow( type ){
	
	$('Videos').hide();
	$('Images').hide();
	$('Sounds').hide();
	
	$(type).show();
  		
}

function changeVideo( q ){

	//$( 'FlowPlayer' + videoCounter ).hide();
	
	if( q == 'next' ){
		videoCounter++;
	} else {
		videoCounter--;
	}

	//chequeo que sean valores entre los posibles
	if( videoCounter > videoCant - 1 ) {
		videoCounter = 0;	
	} else {
		if( videoCounter < 0 ) {
			videoCounter = videoCant - 1;
		}
	}

	//Muestro ese player
	//$('FlowPlayer' + videoCounter ).show();
	//$('divPlayer').update( $('FlowPlayer' + videoCounter ).innerHTML );
	$('divPlayer').update( '');
}

function showFirstVideo(){

	//$('divPlayer').update( $('FlowPlayer0').innerHTML );
	
	$('divPlayer').update( 'asd' );

}

function toggleChildren(event){
	var id = event.target.value;
	
	$$(".hijo_"+id).each(
	function(n){
		if(event.target.checked == true){
			n.checked = true;
		}else{
			n.checked = false;
		}
	}
	); 
}

function ResetFrmContact(){
	
	$('ContactDestination_id').selectedIndex = 0;
	$('name').value = "";
	$('mail').value = "";
	$('phone').value = "";
	$('comment').value = "";
	
}

function SendFrmContact(){
	
	//Chequeo algunos campos...
	if( $('ContactDestination_id').selectedIndex == 0 ) {
		alert( 'Select the destination' );
		return;
	}
	
	if( $('name').value == "" ) {
		alert( 'Insert your name' );
		return;
	}
	
	if( $('mail').value == "" ) {
		alert( 'Insert the e-mail' );
		return;
	}
	
	if( !validarEmail($('mail').value)) {
		alert( 'The e-mail is incorrect' );
		return;
	}
	
	if( $('phone').value == "" ) {
		alert( 'Insert your phone number' );
		return;
	}
	
	if( $('comment').value == "" ) {
		alert( 'Insert your comment' );
		return;
	}
	
	//Submiteo el form
	$('frmContact').request( {		
		onComplete: function(response){
			if( response.responseText != 'OK' ){
				alert( response.responseText );
			}else{
				$('frmContact').reset();
				alert('Thank you for your consult.');
			}	
		}
	});	
	
}

function showSubscriptions( type ) {
	
	//Cambio la pestañita seleccionada
	if( ! $(type + 'Subscription').hasClassName('menuAdvertisingSel') ) {
		$('printedSubscription').toggleClassName('menuAdvertisingSel');
		$('on-lineSubscription').toggleClassName('menuAdvertisingSel');	
		//Mensaje mientras espera...
		$('tablaSubscripciones').update( '<span class="fdoCeldaCuadroContenido">Loading, please wait</span>' );
		//Hago el Ajax para traerme ese view
		new Ajax.Request('/Subscription/getTable/' + type, {
			method : 'get',
			onComplete: function( res ){
			$('tablaSubscripciones').update( res.responseText );
			type = type.substr(0,1).toUpperCase() + type.substr(1,type.length);
			$('titulosNombrePrinted').update( 'Subscribe to ' + type + ' Edition' );
		}
		} );
	}
}

function ChangeTabs_MyBAHerald( type ) {

	//Cambio la pestañita seleccionada
	if( ! $(type + 'tab').hasClassName('menuAdvertisingSel') ) {
		$('detailstab').toggleClassName('menuAdvertisingSel');
		$('subscriptionstab').toggleClassName('menuAdvertisingSel');	

		if( $(type).innerHTML != '' ){
			$('tablaSubscripciones').update( $(type).innerHTML );
		} else {
			//Mensaje mientras espera...
			$('tablaSubscripciones').update( '<span class="fdoCeldaCuadroContenido">Loading, please wait</span>' );
			//Hago el Ajax para traerme ese view
			new Ajax.Request('/MyBAHerald/get'+type, {
				method : 'get',
				onComplete: function( res ){
					$('tablaSubscripciones').update( res.responseText );
					$(type).update(res.responseText);
				}
			} );
		}
	}
}

function getRadioVal(r) {
  var str = '';
  for(i=0;i<r.length;i++) {
    if(r[i].checked){
    	return r[i].value
    	break;
    	}
  }
}


function renewSubscription(){
	susc = $('subscription_type_id').value;
	
	if(susc == '0'){
		alert('Select any Subscription');
	}else{
		
		new Ajax.Request('/MyBAHerald/Renew/'+susc,{
			onComplete: function(res){
				datos = res.responseText.evalJSON(true);
				$('MONTO').value = datos.monto;
				$('NROOPERACION').value = datos.nrooperacion;
				$('formDecidir').submit();
			}
		})
	}
}
