/*
 * In-Field Label jQuery Plugin
 * http://fuelyourcoding.com/scripts/infield.html
 *
 * Copyright (c) 2009 Doug Neiner
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://docs.jquery.com/License
 *
 * @version 0.1
 */
(function($){$.InFieldLabels=function(b,c,d){var f=this;f.$label=$(b);f.label=b;f.$field=$(c);f.field=c;f.$label.data("InFieldLabels",f);f.showing=true;f.init=function(){f.options=$.extend({},$.InFieldLabels.defaultOptions,d);if(f.$field.val()!=""){f.$label.hide();f.showing=false};f.$field.focus(function(){f.fadeOnFocus()}).blur(function(){f.checkForEmpty(true)}).bind('keydown.infieldlabel',function(e){f.hideOnChange(e)}).change(function(e){f.checkForEmpty()}).bind('onPropertyChange',function(){f.checkForEmpty()})};f.fadeOnFocus=function(){if(f.showing){f.setOpacity(f.options.fadeOpacity)}};f.setOpacity=function(a){f.$label.stop().animate({opacity:a},f.options.fadeDuration);f.showing=(a>0.0)};f.checkForEmpty=function(a){if(f.$field.val()==""){f.prepForShow();f.setOpacity(a?1.0:f.options.fadeOpacity)}else{f.setOpacity(0.0)}};f.prepForShow=function(e){if(!f.showing){f.$label.css({opacity:0.0}).show();f.$field.bind('keydown.infieldlabel',function(e){f.hideOnChange(e)})}};f.hideOnChange=function(e){if((e.keyCode==16)||(e.keyCode==9))return;if(f.showing){f.$label.hide();f.showing=false};f.$field.unbind('keydown.infieldlabel')};f.init()};$.InFieldLabels.defaultOptions={fadeOpacity:0.5,fadeDuration:300};$.fn.inFieldLabels=function(c){return this.each(function(){var a=$(this).attr('for');if(!a)return;var b=$("input#"+a+"[type='text'],"+"input#"+a+"[type='password'],"+"textarea#"+a);if(b.length==0)return;(new $.InFieldLabels(this,b[0],c))})}})(jQuery);

/*
 * Abigail Acuña
 * @utor: abiacu@gmail.com
 *
 */

_px 		= 60;
_div		= 3;
_ani		= 300;
_slideTime 	= 8000;
var Plugins = {};

Plugins = function ()
{
	var self  = {};	 
		
	self.ini = function () {					
		self._slide();						
		$.post('ajax/get.php', {'Languages':'ok'},
			function(data){
				if (data) {
					$(data).each(function(){eval(this.replace(/'/gi, '"'));});
					self._scroll();
					self._scrolling();
					self._sendmail();
					self._lightBox();
				}
			}, 'json');

	},
	
	self._lightBox = function() {
		$('div.clientes_img').children("a").lightBox();
	},
	
	self._slide = function() {
		var img = $('#slide img');	
		img.each(function(i){if(i>0){$(this).hide();}});
		setInterval(function () {
			var control=1;
			for (var i=1;i<=img.length;i++) {
				if(img[i-1].style.display!='none' && control==1) {
					$(img[i-1]).fadeOut(_ani*2, function(){});
					el = (i==img.length) ? img.get(0) : img.get(i);
					$(el).fadeIn((_ani*2), function(){});
					control = 0;
				};
			}				
		}, _slideTime);		
	},
	
	self._sendmail = function() {
		$('span.loading').hide();
		self.select = $('ul.select');	
		self._forms	= $('form#contacto-form');	
		
		self._reset_form();
		
		$("div.label label").inFieldLabels(); 
		$("div.label input").attr("autocomplete","off");
		self._select();

		self._forms.find('a.send').click(function(){
			$('span.loading').show();
			$.post("ajax/get.php", self._forms.serialize(),
		   		function(data){		
					$('span.loading').hide();
								
					if (data.error) {	
						$(self._fields).each(function(){ 		
							eval('var msg = data.field.'+this[0]+'');
							if (msg==undefined) {
								$('#'+this[0]).parent('div').next('div.required').fadeOut(_ani, function(){});
							} else {
								$('#'+this[0]).parent('div').next('div.required').fadeIn(_ani, function(){
									$(this).html(msg);									
								});
							}
						});
					
					} else {
						var message = $('#send-message');
						self._forms.fadeOut(_ani, function(){
							message.fadeIn(_ani, function(){
								$(this).html(data.message);
							});		
							self._reset_form();					
						});
						
						setTimeout(function(){							
							message.html('');
							self._forms.fadeIn(_ani, function(){});							
						}, 10000);
					}
			}, 'json');
		});
	},

	self._reset_form = function() {
		$('ul.select ul,div.required').hide();
	
		self._forms.find('input, textarea').each(function(){
			this.value	= '';
			var obj 	= this;
			self._value = '';
			
			$(self._fields).each(function(){ 
				if(obj.name==this[0]) eval('self._value = this[1];'); 
			});		

			if(this.type!='hidden') {
				var label = $(this).next('label').get(0);
				if(label==undefined){
					$(this).parent().append('<label for="'+this.name+'">'+self._value+'</label>')
				} else {
					$(label).show();
					$(label).animate({'opacity':1, 'display':'block'}, _ani);
				}				
			} else {
				self.select.find('span').html(self._value);
				self.select.find('span').animate({'opacity':1}, _ani);
			}
			this.id = this.name;			
		});
	},

	self._select = function() {
		self.select.click(function(){
			if (self.select.find('ul').get(0).style.display=='none') {
				self.select.find('ul').fadeIn(_ani, function(){});
				$(self.select.find('span')).animate({'opacity' : 0.5}, _ani);
				self.select.find('ul li').click(function(){
					var _value = $(this).find('a').get(0).innerHTML;
					$(self.select.find('span')).html(_value);
					$('input[type="hidden"]').get(0).value = _value;					
					self.select.find('ul').fadeOut(_ani, function(){})
				});
			} else {
				self.select.find('ul').hide();
				$(self.select.find('span')).animate({'opacity' : 1}, _ani);
			}
			return false;
		});	
		$('#contacto').click(function(){ self.select.find('ul').hide(); });	
	},
	
	self._scrolling = function() {
		$('a').click(function(){
			var id = this.href.match(/[a-z]+$/);
			if (id!=null ) {
				if (id=='home') id = 'top';
				
				if (id=='contacto') {
					var li = this.href.match(/[0-9]+/);
					var _value = self.select.find('ul li').get((li-1));					

					if (_value!=undefined && li!=null) {						
						$(self.select.find('span')).html($(_value).find('a').get(0).innerHTML);
						$('input[type="hidden"]').get(0).value = $(_value).find('a').get(0).innerHTML;											
					}
				}
				
				eval('var targetOffset = $("#'+id+'").offset().top');
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;				
			}
		});		
	},

	self._scroll = function() {
		self.flag 		= 0;
		
		var f = '';var a = 0;
		$(self._sections).each(function(){ 		
			eval('self.'+this[0]+'=$("#'+this[0]+'");');				
			if(f!='') {
				eval('self.'+f+'H = ['+a+', self.'+this[0]+'.offset().top];');				
				eval('a = self.'+f+'H[1]'); 
			} f = this[0];
		});		
		eval('self.'+f+'H = ['+a+', 3000000];');				
		
		self.profile_pic = [$('.profile_pic'), 0];
		
		var title = $('title');
		title.html(self._sections[0][1]+' | INVELAB');
		
		self.homeHeight = self.home.height();
		
		self._window=$(window).height();
		$(window).resize(function(){self._window=$(window).height();});

		$(window).scroll(function() {			

			self._top = self._window-self.homeHeight-_px;

			self._y = $(this).scrollTop();

			if (self._y==0) self.set_team_photo('proyectos');
			
			$(self._sections).each(function(){ 
				eval('a = self.'+this[0]+'H'); 
				if (a[0]<=(self._y+20) && a[1]>self._y+20) {
					if(f!=this[0]) {
						f=this[0];
						title.html(this[1]+' | INVELAB');
						self.set_team_photo(f);
					}					
				}
			});

			
			if (self._top>0) {
				self.flag = (-self._y/_div);				
			} else {
				self.flag = (self._y<=-self._top) ? -self._y : ((self._y+self._top)/_div-self._y);				
			}
			
			$('#scroll').html(self._y+' '+a[0]+' '+a[1]+' '+self._top+' | '+(self._y+self._top)/2.5);
			
			if (self.flag<=0 && self._y<self.homeH[1]) {
				self.home.css({'margin-top': self.flag+'px'});		
				self.team.css({'background-position':(self.flag*10)+'px 0px'});	
							$('#team_top').css({'background-position':(self.flag*20)+'px 0px'});						
			}
		});	
		
	},
	
	self.set_team_photo = function(name) {
		if (name=='proyectos') {
			self.profile_pic[1] = ((self.profile_pic[1]+200)>400) ? 0 : (self.profile_pic[1]+200);
			self.profile_pic[0].each(function(){
				$('#'+this.id).css({'background-position':-self.profile_pic[1]+'px 0px'});				
			});
		}
	}
	
	return self;	
}();

$(document).ready(function(){Plugins.ini();});

