$(document).ready(initAll);

function initAll(){
	sifrH2();
	initProjects();
	if( $('body.page_about').length>0 ){ initAbout(); }
	if( $('#resumeForm').length>0 ){ initResume(); }
	
	
	$('li').mouseenter(liEnter).mouseleave( liLeave );
	Timers = new Object();
	function liEnter(){
		var li = $(this);
		var index = $('li').index(li);
		
		clearTimeout(Timers['timer'+index]);
		li.addClass('hover');
	}
	function liLeave(){
		var li = $(this);
		var index = $('li').index(li);
		
		Timers['timer'+index] = setTimeout(removeClass,200);
		function removeClass(){
			li.removeClass('hover');
		}
	}
}

//========== PROJECTS ============
//================================
function initProjects(){
	// Set globals
	projectsAct = 0;
	projectsTotal = 0;
	projectsViewAll = false;
	
	// Set links
	$('#projects_menu a').click(projectsClick);
	enableLinks();
	
	// Check URL for selected project
	var url = window.location.href;
	var urlAnchorPos = url.indexOf('#');
	if(urlAnchorPos>-1){
		var urlAnchor = url.substring(urlAnchorPos);
		projectsGet(0,urlAnchor);
	}
}

//::::: TOOLS ::::://
function enableLinks(){
	$('#btnPrev').click( function(){projectsShow(projectsAct-1);return false} );
	$('#btnNext').click( function(){projectsShow(projectsAct+1);return false} );
	$('#btnAll').click(projectsAll);
}
function disableLinks(){
	$('#btnPrev,#btnNext,#btnAll').unbind().click( function(){return false;} );
}

//::::: SHOW PROJECTS ::::://
function projectsClick(){
	// Get and open selected project
	var project=$(this).attr('href');
	projectsGet(1000,project);
	
	return false;
}
function projectsGet(speed,projectString){
	// Default value
	if( typeof(projectString) == 'undefined' ) projectString='';
	
	// Show loader
	$('#content .loading').fadeTo(0,0.75);
	
	// Get all projects
	var cat = '';
	if(location.search!=""){
		var variables = location.search.substr(1).split("&");
		for(var i=0; i<variables.length; i++){
			var variable = variables[i].split("=");
			if( variable[0]=='cat' ){
				cat = variable[1];
				break;
			}
		}
	}
	if(cat!='animacion' && cat!='web' && cat!='advergames' && cat!='multimedia' && cat!='impresos') cat = '';
	projects_url = 'chiva_site_files/ajax/get_projects.php';
	projects_url = ( cat=='' ) ? projects_url : projects_url+'?cat='+cat;
	
	$.get(projects_url,{},dataObtained,'html');
	function dataObtained(data){
		// Add projects
		$('#content .mobile').prepend(data);
		//$('#content .project .tag').click(clickTag);
		
		// Animations
		$('#content .loading').fadeOut(speed);
		$('#projects_count').fadeIn(speed);
		$('#btnAll').slideDown(speed);
		$('#content .mobile').css('opacity',0).animate({opacity:1},speed,'',isVisible);
		function isVisible(){
			$('#projects_menu').remove();
		}
		
		// sIFR
		sifrH2();
		
		// Update info
		projectsTotal = $('#content .mobile .project').length;
		$('#projects_count .total').text(projectsTotal);
		
		// Get initial project number
		if(projectString=='' || projectString=='#'){
			projectNum=1;
		}else{
			projectNum=$(projectString).index()+1;
		}
		
		// Show initial project
		projectsShow(projectNum,0);
	}
}

//::::: MOVE PROJECTS ::::://
function projectsShow(number,speed){
	// Validate number
	if(number<1) number=1;
	if(number>projectsTotal) number=projectsTotal;
	
	// Disable links
	disableLinks();
	
	// Animate projects
	var leftNum = (number-1) * -900;
	var left = leftNum+'px';
	$('#content .mobile').animate({left:left},speed,'',callBack);
	
	// Update info
	projectsAct=number;
	$('#projects_count .count').text(projectsAct);
	
	// Update buttons
	if(number==1) $('#btnPrev').fadeOut(speed);
		else $('#btnPrev').fadeIn(speed);
	if(number==projectsTotal) $('#btnNext').fadeOut(speed);
		else $('#btnNext').fadeIn(speed);
	
	// UpdateURL
	function callBack(){
		enableLinks();
		var id = $('#content .mobile .project').eq(number-1).attr('id');
		window.location.href = '#'+id;
		//$('body').prepend(id+' ');
	}
}

//::::: CHANGE TAGS ::::://
function clickTag(){
	var tag = $(this).text();
	changeTag(tag);
}
function changeTag(tag){
	// Visible projects
	projectsTotal=0;
	projectsAct=$('#content .mobile .project:visible').eq(projectsAct-1).attr('id');
	
	// Update each project
	$('#content .mobile .project').each(selectTag);
	function selectTag(){
		if( $(this).find('.tag'+tag).length > 0 ){
			$(this).show();
			$(this).find('.tag').removeClass('active');
			$(this).find('.tag'+tag).addClass('active');
			projectsTotal++;
		}else{
			$(this).hide();
		}
		
		if( $(this).attr('id') == projectsAct ){
			projectsAct = projectsTotal;
		}
	}
	
	// Update info
	$('#projects_count .category').text(tag);
	$('#projects_count .count').text(projectsAct);
	$('#projects_count .total').text(projectsTotal);
	
	/*// Update menu
	$('#nav .level2 li a').removeClass('active');
	$('#nav .level2 li a[href~='+cat+']').addClass('active');*/
	
	// Show first
	if(projectsViewAll){
		var height = projectsTotal * 420;
		$('#content').css('height',height);
	}else{
		projectsShow(projectsAct,0);
	}
}

//::::: SHOW ALL ::::://
function projectsAll(){
	// Disable links
	$('#btnPrev,#btnNext').fadeOut();
	$('#btnAll').addClass('active').unbind();
	
	// Hide counter
	//$('#projects_count .counter').hide();
	
	// Set speed
	var speed=1000;
	if(projectsAct==1) speed=0;
	
	// Animate mobile
	$('#content .mobile').animate({left:0},speed,'',endMobile);
	function endMobile(){
	
		// Remove floating
		$('#content .mobile .project').css('float','none');
		
		// Get height
		var heightNum = (projectsTotal * 420) - 40;
		var height = heightNum+'px';
		
		// Animate content
		$('#content').animate({height:height},1000,'',complete);
		function complete(){
			// Set link and var
			$('#btnAll').click(projectsOne);
			projectsViewAll = true;
			window.location.href = '#';
		}

	}
	return false;
}
function projectsOne(){
	// Disable link
	$('#btnAll').removeClass('active').unbind();
	
	// Animate content
	$('#content').animate({height:380},1000,'',complete);
	function complete(){
		// Set link and var
		$('#btnAll').click(projectsAll);
		projectsViewAll = false;
		
		// Show counter
		$('#projects_count .counter').show();
		
		// Restore float and go to first
		$('#content .mobile .project').css('float','left');
		projectsShow(1,1000);
	}
	
	return false;
}



//========== ABOUT ============
//=============================
function initAbout(){
	
	// CREATE MENU
	var sections = new Array();
	$('.col2 div.info div').each( function(){ sections.push($(this).find('h3:first').text()) } );
	
	var n=sections.length
	var html= '<ul class="submenu">';
	for(var i=0; i<n; i++){
		html+= '<li><a href="#">';
		html+= sections[i];
		html+= '</li></a>';
	}
	html+= '</ul>';
	
	$('.col2 h2:first').after(html);
	
	// SET MENU
	$('.submenu li a').click(aboutClick);
	aboutSelect(1);
}
function aboutClick(){
	var number = $('.submenu li a').index($(this)) + 1;
	aboutSelect(number);
	return false;
}
function aboutSelect(number){
	var numArray = number-1;
	$('.info div').hide();
	$('.info div:eq('+numArray+')').show();
	
	$('.submenu li').removeClass('selected');
	$('.submenu li:eq('+numArray+')').addClass('selected');
}



//========== RESUME ============
//==============================
function initResume(){
	var fileInput = $('#resumeForm input[type="file"]');
	fileInput.css('opacity','0').wrap('<div class="filesWrapper"></div>').after('<div class="fakeFile"><input /><label>Browse</label></div>');
	fileInput.bind( $.browser.msie ? 'propertychange' : 'change', changeFile);
	function changeFile(){
		var text = $(this).val();
		$('.fakeFile input').val(text);
	}
}



//========== sIFR ============
//============================
sifrVista = {src:'/chiva_site_files/swf/antenna.swf'};
sIFR.activate(sifrVista);
function sifrH2(){
	sIFR.replace(sifrVista,{
						selector: 'h2',
						css: ['.sIFR-root {background-color:#FFFFFF; color:#333333; leading:-8; letter-spacing:0; text-transform:uppercase}',
							  '.sIFR-root a {color:#F2BB00; text-decoration:none}',
							  '.sIFR-root a:hover {color:#FFCC00}'],
						filters:{
							DropShadow:{
								distance:1,
								angle:135,
								color: '#DDDDDD',
								strength: 2,
								alpha: 1,
								blurX: 0,
								blurY: 0
							}
						},
						leading:1.5,
						offsetTop:0,
						offsetLeft:0,
						tuneHeight:-5,
						wmode:'transparent'
						});
}



