// JavaScript Document

var actFeatured = 1;
var featuredTO = "";
var featuredSliderChangesEach = 5; // in seconds
$(document).ready(function(){

	$('#movecont').scrollFollow({speed: 100});

	$('#menu li.off').hover(function(){
		$(this).addClass('on');
	}, function(){
		$(this).removeClass('on');
	});
	
	$('#featured_list a').click(function(event){
		event.preventDefault();
		var clickedid = $(this).attr('id');
		myid = clickedid.split("_");
		mymsg = $(this).find('img').attr('title').split(" ");
		wordsperline = Math.ceil((mymsg.length/2));
		lines = new Array("", "");
		for(i=0, j=0; i<mymsg.length; i++){
			if(i==wordsperline)
				j++;
			lines[j] += mymsg[i]+" ";
		}
		$('#featured_list li img').removeClass('on');
		$('#featured_list li a#'+clickedid+' img').addClass('on');

		var rnd = Math.floor(Math.random()*2);
		title = "";
		for(i=0; i<lines.length; i++)
			title += "<span>"+lines[i]+"</span><br />";
		if(rnd)
			$('#featured_news #featured .feattxt').css('textAlign', 'right');
		else
			$('#featured_news #featured .feattxt').css('textAlign', 'left');
		$('#featured_news #featured .featpic').hide();
		$('#featured_news #featured #fpic_'+myid[1]).show();
		$('#featured_news #featured .feattxt').html(title+'<a href="'+$(this).attr('href')+'"><img src="img/featured/readmore.png" width="110" height="25" /></a>');
		countFeatured = 1;
		$('ul#featured_list li a').each(function(){
			if(clickedid == $(this).attr('id'))
				actFeatured = countFeatured;
			else
				countFeatured++;
		});
	});
	
	if($('div#featured_news').length > 0){
		featuredTO = setTimeout('changeFeatured()', (featuredSliderChangesEach*1000));
	}

	$('div#featured_news').hover(function(){
		clearTimeout(featuredTO);
	}, function(){
		featuredTO = setTimeout('changeFeatured()', (featuredSliderChangesEach*1000));
	});

	$("a[rel^='prettyPhoto']").prettyPhoto({ overlay_gallery: false });

	var newYear = new Date(); 
	//newYear = new Date(2011, (3-1), 24, 15, 0, 0, 0);
	newYear = new Date(2011, (11-1), 10, 14, 30, 0, 0);
	$('#countdown').countdown({until: newYear, format: 'dHM', 
		labels: ['y', 'm', 'w', 'd', 'h', 'm', 's'],
    	layout: '{dn}{dl} {hn}{hl} {mn}{ml}' }); 

	$('ul#newslist_sec li').hover(function(){
		$(this).addClass('on');
	}, function(){
		$(this).removeClass('on');
	});
	
	$('#newslist_sec a').click(function(event){
		event.preventDefault();
	});
	$('#newslist_sec li').click(function(event){
		self.location.href = $(this).find("a").attr('href');
	});

	$('ul#vidlist_sec li').hover(function(){
		$(this).addClass('on');
	}, function(){
		$(this).removeClass('on');
	});
	
	$('#vidlist_sec a').click(function(event){
		event.preventDefault();
	});
	$('#vidlist_sec li').click(function(event){
		self.location.href = $(this).find("a").attr('href');
	});

	$('ul#wplist_sec li').hover(function(){
		$(this).addClass('on');
	}, function(){
		$(this).removeClass('on');
	});

	$('.checkFrm').submit(function(){
		return checkForm($(this).attr('id'));
	});
	
	$('div#sendcontact').click(function(){
		$('#contform').submit();
	});

});

function changeFeatured(){
	var numFeats = 0;
	var featIds = [];
	$('ul#featured_list li a').each(function(){
		featIds[featIds.length] = $(this).attr('id');
		numFeats++;
	});
	if(actFeatured < featIds.length){
		nextFeat = actFeatured+1;
	} else{
		nextFeat = 1;
	}
	nextData = featIds[(nextFeat-1)].split('_');
	actData = featIds[(actFeatured-1)].split('_');

		mymsg = $('a#'+featIds[(nextFeat-1)]+' img').attr('title').split(" ");
		wordsperline = Math.ceil((mymsg.length/2));
		lines = new Array("", "");
		for(i=0, j=0; i<mymsg.length; i++){
			if(i==wordsperline)
				j++;
			lines[j] += mymsg[i]+" ";
		}
		var rnd = Math.floor(Math.random()*2);
		title = "";
		for(i=0; i<lines.length; i++)
			title += "<span>"+lines[i]+"</span><br />";
		if(rnd)
			$('#featured_news #featured .feattxt').css('textAlign', 'right');
		else
			$('#featured_news #featured .feattxt').css('textAlign', 'left');
		$('#featured_news #featured .feattxt').html(title+'<a href="'+$('a#'+featIds[(nextFeat-1)]).attr('href')+'"><img src="img/featured/readmore.png" width="110" height="25" /></a>');

	$('div#fpic_'+nextData[1]).fadeIn(function(){
		$('div#fpic_'+actData[1]).fadeOut();
	});
	
	$('#featured_list li img').removeClass('on');
	$('#featured_list li a#fnews_'+nextData[1]+' img').addClass('on');
	
	actFeatured = nextFeat;
	featuredTO = setTimeout('changeFeatured()', (featuredSliderChangesEach*1000));

}

function checkForm(frmId){
		var frmError = false;
		$('form#'+frmId+' input, form#'+frmId+' textarea, form#'+frmId+' select').each(function(){
			if($(this).attr('required') == "1" && !frmError){
				if(this.tagName == 'INPUT'){
					if($(this).attr('value') == ''){
						$(this).addClass('required');
						alert($(this).attr('title'));
						$(this).focus();
						frmError = true;
					}
					if($(this).attr('reqtype') == 'email' && !frmError){
						if(!check_email($(this).attr('value'))){
							$(this).addClass('required');
							alert("Ingresa una dirección de correo valida");
							$(this).focus();
							frmError = true;
						}
					}
				}
				if(this.tagName == 'SELECT'){
					if($(this).attr('selectedIndex') == 0){
						$(this).addClass('required');
						alert($(this).attr('title'));
						$(this).focus();
						frmError = true;
					}
				}
				if(this.tagName == 'TEXTAREA'){
					if($(this).attr('value') == ''){
						$(this).addClass('required');
						alert($(this).attr('title'));
						$(this).focus();
						frmError = true;
					}
				}
			}
		});
		if(frmError)
			return false;
		else
			return true;
}

function check_email(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false
			}
		}
		return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		return false
	}
	if (len<2) {
		alert(errStr)
		return false
	}
	return true;
}

