// JavaScript Document
// page init

//setting up variable for array of images and timer
/*var counter = 0;
var timer;
var images = new Array();*/

///setting up variable for array of images and timer on home page
var counter2 = 0;
var timer2;
var images2 = new Array();


function initialize(){
//creating array for cycling of images
	/*images[0] = new Image();
	images[1] = new Image();
	images[2] = new Image();
	
	images[0].src = 'images/home-1.png';
	images[1].src = 'images/home-2.png';
	images[2].src = 'images/home-3.png';
	
	//changes image every 4 sec from start
	timer = setInterval('changeImage()',4000);*/
	
	//creating array for cycling of images for home page
	images2[0] = new Image();
	images2[1] = new Image();
	images2[2] = new Image();
	images2[3] = new Image();
	
	images2[0].src = 'images/home-4.png';
	images2[1].src = 'images/home-5.png';
	images2[2].src = 'images/home-6.png';
	images2[3].src = 'images/home-7.png';
	
	//changes image every 4 sec from start
	timer2 = setInterval('changeImage2()',4000);
	
	
	 /*
     *  ADDED STUFF BY MARIO
     *  I check each field to see if the information was entered.
     *  If the field was empty i display a message and return false (so the form doesn't submit)
     *  If allt he fields are good, then i make an ajax call to the contact.php which sends the email
     *  Ajax is used so the page doesn't refresh when the form is submitted. I needed
     *  to do this because your whole website is on one page and once the page refreshes
     *  it goes back to the Accueil instead of the contact page.
     */

     // Check if the user entered all the information
    $('#button').click(function(){
        var name = $("input#username").val();  
        if (name == "") {  
            $('#message').html('Your name is required'); 
            $("input#username").focus();  
            return false;  
        }  
        var email = $("input#email").val();  
        if (email == "") {  
            $('#message').html('Your email is required');   
            $("input#email").focus();  
            return false;  
        }  
        
        var msg = $("#msg").val();  
        if (msg == "") {  
            $('#message').html('Your message is required');  
            $("#msg").focus();  
            return false;  
        }
        
      
        //AJAX call
        var dataString = 'username='+ name + '&email=' + email + '&msg=' + msg;
		
        $.ajax({
            type: "POST",
            url: "contact.php",
            data: dataString,
            success: function() {
                $('#message').html("Contact Form Submitted!") //Add any message you want here
            }
        });
        return false;
        
    });
}


////CYCLING IMAGES ROTATING EVERY 2 SEC
function changeImage(){
	if(counter > 2){
	counter = 0;
	}
	$('#adBanner').attr('src',images[counter].src);
	counter++;
}
////CYCLING IMAGES ROTATING EVERY 2 SEC
function changeImage2(){
	if(counter2 > 3){
	counter2 = 0;
	}
	$('#adBanner2').attr('src',images2[counter2].src);
	counter2++;
}
//initialize document
$('document').ready(initialize);


$(function(){

	/* BANNER */
	//$('#banner').slider({'wdith':974, 'height':357});
	
	/* BOTONES DE FLECHA */
	$('#goto-1').click(function(e){ e.preventDefault();e.stopPropagation(); $('a.bt-services').click();return false; });
	$('#goto-2').click(function(e){ e.preventDefault();e.stopPropagation(); $('a.bt-portafolio').click();return false; });
	$('#goto-3').click(function(e){ e.preventDefault();e.stopPropagation(); $('a.bt-contact').click();return false; });
	
	/* FIX FOR THE TABS */
	$('.tab-container a').click(function(e){
		$('.tab-content').hide(); $(this).parent().find('.tab-content').show();
		e.preventDefault();e.stopPropagation(); return false;
	});
	
	initScrollSidebar();
	var topen = $("#bio-skills-titulo").position();
	var _mitop = topen.top ;
	
	$('#caja_izquierda').css({position:'absolute', top: _mitop} );
});

// scrollable sidebar
function initScrollSidebar() {
	var _animSpeed = 300;
	var _easing = 'swing';
	var _animate = false;
	var _activeClass = 'activado';

	// different behaviour for IE6
	if($.browser.msie && $.browser.version < 7) _animate = true;

// sliding navigation
	var _sideBar = $('#caja_izquierda');
	var _sideBarTop = _sideBar.offset().top;
	var _sideNav = _sideBar;
	var _scrollTimer;
	var _fixTimer;
	var _scrollDelay = 50;
	var _fixedTop = 25; //para dejar el espacio por arriba del menu
	var _scrollDiff = 100;
	var _diff = 20;
	var tamanio = 270;

	// scrollto links

	var _slideLinks = _sideBar.find('ul.menu_vertical').find('a');
	var _slideSpeed = 1300;
	var _targetBlocks = [];

	_slideLinks.each(function(){
		var _opener = $(this);
		var _target = $((_opener.attr('href').length>1 ? _opener.attr('href') : 'none'));
		if(_target.length) {
			_targetBlocks.push({block:_target,opener:_opener, defaultClass:_opener.attr('className')});
			_opener.click(function(){
				$.scrollTo(_target, _slideSpeed, {easing:_easing, offset: - tamanio});
				return false;
			})
		}
	})
	
	/* Para que al pulsar en el inicio vaya a la primera caja */
	$("#s-home-contenido").click(function(){
		$.scrollTo("#bio-skills-titulo", 1300, {easing:_easing, offset: -tamanio+30});
	})
	
	// scroll function
	function scrollNav() {
		var _scrollTop = $(window).scrollTop();
		var topen = $("#bio-skills-titulo").position();
		var _mitop = topen.top - tamanio;
		var _size = _scrollTop - _mitop + _diff;
			
		// sidebar behaviour
		if(_animate) {
			if(_scrollTimer) clearTimeout(_scrollTimer);
			_scrollTimer = setTimeout(function(){

				if(_size < _mitop) {
					
					__sideNav.animate({paddingTop:_size},{duration:_animSpeed,queue:false});
				} else {
				
					_sideNav.animate({paddingTop:0},{duration:_animSpeed,queue:false});
				}
			},_scrollDelay);
		} else {
			if((_scrollTop + 30) > _mitop) {
				if($.browser.msie && $.browser.version < 7) {
					__sideNav.css({position:'absolute',top:_mitop});
				} else {
				
					_sideNav.css({position:'fixed',top:_fixedTop});
				}
			} else {
			
				_sideNav.css({position:'absolute',top:_mitop});
			}
		}

		// check current block
		//if(_fixTimer) clearTimeout(_fixTimer);
		//_fixTimer = setTimeout(function(){
		
			// check current block
		//if(_fixTimer) clearTimeout(_fixTimer);
		//_fixTimer = setTimeout(function(){
			for(var i=0; i< _targetBlocks.length; i++) {
				var _scrollTop = $(window).scrollTop();
				if(_scrollTop >= _targetBlocks[i].block.offset().top - _scrollDiff - tamanio) {
					if(i==_targetBlocks.length-1) {
						for(var j=0; j< _targetBlocks.length; j++) {_targetBlocks[j].opener.removeClass(_targetBlocks[j].defaultClass+'-'+_activeClass); }
						_targetBlocks[i].opener.addClass(_targetBlocks[i].defaultClass+'-'+_activeClass);
						break;
					} else {
						if(_scrollTop <= _targetBlocks[i+1].block.offset().top - _scrollDiff - tamanio) {
							for(var j=0; j< _targetBlocks.length; j++) { _targetBlocks[j].opener.removeClass(_targetBlocks[j].defaultClass+'-'+_activeClass); }
							_targetBlocks[i].opener.addClass(_targetBlocks[i].defaultClass+'-'+_activeClass);
							break;
						}
					}
				}
			}
		//},50);s
		
	}
	$(window).scroll(scrollNav);
	scrollNav();
}

/* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/ */
jQuery.easing.jswing=jQuery.easing.swing;jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(e,f,a,h,g){return jQuery.easing[jQuery.easing.def](e,f,a,h,g)},easeInQuad:function(e,f,a,h,g){return h*(f/=g)*f+a},easeOutQuad:function(e,f,a,h,g){return -h*(f/=g)*(f-2)+a},easeInOutQuad:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f+a}return -h/2*((--f)*(f-2)-1)+a},easeInCubic:function(e,f,a,h,g){return h*(f/=g)*f*f+a},easeOutCubic:function(e,f,a,h,g){return h*((f=f/g-1)*f*f+1)+a},easeInOutCubic:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f+a}return h/2*((f-=2)*f*f+2)+a},easeInQuart:function(e,f,a,h,g){return h*(f/=g)*f*f*f+a},easeOutQuart:function(e,f,a,h,g){return -h*((f=f/g-1)*f*f*f-1)+a},easeInOutQuart:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f*f+a}return -h/2*((f-=2)*f*f*f-2)+a},easeInQuint:function(e,f,a,h,g){return h*(f/=g)*f*f*f*f+a},easeOutQuint:function(e,f,a,h,g){return h*((f=f/g-1)*f*f*f*f+1)+a},easeInOutQuint:function(e,f,a,h,g){if((f/=g/2)<1){return h/2*f*f*f*f*f+a}return h/2*((f-=2)*f*f*f*f+2)+a},easeInSine:function(e,f,a,h,g){return -h*Math.cos(f/g*(Math.PI/2))+h+a},easeOutSine:function(e,f,a,h,g){return h*Math.sin(f/g*(Math.PI/2))+a},easeInOutSine:function(e,f,a,h,g){return -h/2*(Math.cos(Math.PI*f/g)-1)+a},easeInExpo:function(e,f,a,h,g){return(f==0)?a:h*Math.pow(2,10*(f/g-1))+a},easeOutExpo:function(e,f,a,h,g){return(f==g)?a+h:h*(-Math.pow(2,-10*f/g)+1)+a},easeInOutExpo:function(e,f,a,h,g){if(f==0){return a}if(f==g){return a+h}if((f/=g/2)<1){return h/2*Math.pow(2,10*(f-1))+a}return h/2*(-Math.pow(2,-10*--f)+2)+a},easeInCirc:function(e,f,a,h,g){return -h*(Math.sqrt(1-(f/=g)*f)-1)+a},easeOutCirc:function(e,f,a,h,g){return h*Math.sqrt(1-(f=f/g-1)*f)+a},easeInOutCirc:function(e,f,a,h,g){if((f/=g/2)<1){return -h/2*(Math.sqrt(1-f*f)-1)+a}return h/2*(Math.sqrt(1-(f-=2)*f)+1)+a},easeInElastic:function(f,h,e,l,k){var i=1.70158;var j=0;var g=l;if(h==0){return e}if((h/=k)==1){return e+l}if(!j){j=k*0.3}if(g<Math.abs(l)){g=l;var i=j/4}else{var i=j/(2*Math.PI)*Math.asin(l/g)}return -(g*Math.pow(2,10*(h-=1))*Math.sin((h*k-i)*(2*Math.PI)/j))+e},easeOutElastic:function(f,h,e,l,k){var i=1.70158;var j=0;var g=l;if(h==0){return e}if((h/=k)==1){return e+l}if(!j){j=k*0.3}if(g<Math.abs(l)){g=l;var i=j/4}else{var i=j/(2*Math.PI)*Math.asin(l/g)}return g*Math.pow(2,-10*h)*Math.sin((h*k-i)*(2*Math.PI)/j)+l+e},easeInOutElastic:function(f,h,e,l,k){var i=1.70158;var j=0;var g=l;if(h==0){return e}if((h/=k/2)==2){return e+l}if(!j){j=k*(0.3*1.5)}if(g<Math.abs(l)){g=l;var i=j/4}else{var i=j/(2*Math.PI)*Math.asin(l/g)}if(h<1){return -0.5*(g*Math.pow(2,10*(h-=1))*Math.sin((h*k-i)*(2*Math.PI)/j))+e}return g*Math.pow(2,-10*(h-=1))*Math.sin((h*k-i)*(2*Math.PI)/j)*0.5+l+e},easeInBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}return i*(f/=h)*f*((g+1)*f-g)+a},easeOutBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}return i*((f=f/h-1)*f*((g+1)*f+g)+1)+a},easeInOutBack:function(e,f,a,i,h,g){if(g==undefined){g=1.70158}if((f/=h/2)<1){return i/2*(f*f*(((g*=(1.525))+1)*f-g))+a}return i/2*((f-=2)*f*(((g*=(1.525))+1)*f+g)+2)+a},easeInBounce:function(e,f,a,h,g){return h-jQuery.easing.easeOutBounce(e,g-f,0,h,g)+a},easeOutBounce:function(e,f,a,h,g){if((f/=g)<(1/2.75)){return h*(7.5625*f*f)+a}else{if(f<(2/2.75)){return h*(7.5625*(f-=(1.5/2.75))*f+0.75)+a}else{if(f<(2.5/2.75)){return h*(7.5625*(f-=(2.25/2.75))*f+0.9375)+a}else{return h*(7.5625*(f-=(2.625/2.75))*f+0.984375)+a}}}},easeInOutBounce:function(e,f,a,h,g){if(f<g/2){return jQuery.easing.easeInBounce(e,f*2,0,h,g)*0.5+a}return jQuery.easing.easeOutBounce(e,f*2-g,0,h,g)*0.5+h*0.5+a}});

/* jQuery.ScrollTo - Date: 5/25/2009 - Ariel Flesler - @version 1.4.2 - http://flesler.blogspot.com/2007/10/jqueryscrollto.html */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
