// JavaScript Document
var q,modConfig;
var ajaxWorking = false

$(document).ready(function() {
	q = $("#_mid").val();
	modConfig = jQuery.parseJSON($("#_midConfig").val());

	//ak todo lo que puede ir en la intro y en el index
	
	$(document).ready(function(){
		$('.desplegable').each(function(){
			if($(this).hasClass('servicios')){
				ancho = 320;
			}else{
				ancho = 171;
			}
			var cantidadLinks = $(this).children("li").length;
			var ancho = cantidadLinks * ancho;
			$(this).css('width', ancho);
			$(this).children('li').last().addClass('sinBorde');
		});	
		$('#menuPrincipal').children('li').hover(
			function(){
				$('#menuPrincipal').children('li').fadeTo(300, 0.2);									
				$(this).stop(true, true).fadeTo(0,1);
			},
			function(){
				$('#menuPrincipal').children('li').stop(true, true).fadeTo(300, 1);					
			}
		)
		$('.box50').scrollbar({arrows: false});
	});

	//
	/*--------------- MAIN SWITCH --------------------*/
	switch (q) {
		case "portada":
			$(document).ready(function(){
				$('#slide').nivoSlider({
					effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
					slices: 15, // For slice animations
					boxCols: 8, // For box animations
					boxRows: 4, // For box animations
					animSpeed: 1000, // Slide transition speed
					pauseTime: 5000, // How long each slide will show
					startSlide: 0, // Set starting Slide (0 index)
					directionNav: true, // Next & Prev navigation
					directionNavHide: false, // Only show on hover
					controlNav: false, // 1,2,3... navigation
					controlNavThumbs: false, // Use thumbnails for Control Nav				
					keyboardNav: true, // Use left & right arrows
					pauseOnHover: true, // Stop animation while hovering
					manualAdvance: false, // Force manual transitions
					captionOpacity: 0.8, // Universal caption opacity
					prevText: 'Prev', // Prev directionNav text
					nextText: 'Next', // Next directionNav text
			    });
			});
		break;
		case 'proyecto':
			$(document).ready(function(){
				seteoProyecto();
		});		
		break;
		case 'prensa':
				$('.light').colorbox({arrowKey: true, iframe: true, width: 420, height: 315, speed: 600, transition: "fade", current: true});
		break;
		case "contacto.en":
		case "contacto":
			$("#formContacto").validationEngine();
			$("#btnEnviar").click(function(){
				contactoSubmit();	
			});
			$("#formContacto input").keydown(function(event) {
				if (event.keyCode == '13') $("#btnEnviar").click();
			});
		break;
	}
});
function contactoSubmit(){
	if($("#formContacto").validationEngine('validate')){
		$('#formContacto').find('.submit').html('<img src="img/ajax-loader.gif" width="16" height="11"/>');
		$.post(
			'index.php?mid=contacto', 
			$("#formContacto").serialize(), 
			function(data) {
				try{console.debug(data);}catch(e){}
				if(data.error == 'ok')	{
					if(q=="contacto") 
						html = '<span class="retmsg"><strong>Gracias!</strong><br/>Su mensaje fue enviado correctamente.</span>';
					else 
						html = '<span class="retmsg"><strong>Thank you!</strong><br/>Your message was sent successfully.</span>';
				} else {
					if(q=="contacto") 
						html='<span class="retmsg"><strong>Error!!</strong><br />No hemos podido procesar su mensaje, por favor inténtelo más tarde.</span>';
					else 
						html='<span class="retmsg"><strong>Error!!</strong><br />We could not process your message, please try again later.</span>';
				}
				$('#formContacto').find('.submit').html(html);
			},
			'json'
		);	
	}
}

function seteoProyecto(){
	$('.proyecto').click(function(){
		if(!$(this).hasClass('activo')){
			var pagina = $(this).attr('lab')+'.html';
			$('#contenido').css('background', 'url(imagenes/loader.gif) 850px 180px no-repeat');
			$("#visorProyecto").fadeTo(500, 0, function(){						
					$("#visorProyecto").load(pagina, function(){
						$('#contenido').css('background', 'none');
						$(this).fadeTo(500, 1);
					});
			});
			limpiarActivo();
			$(this).addClass('activo');
		}
	});
	function limpiarActivo(){
		$('#linksProyecto').children('li').children('a').removeClass('activo');
	}
	$('.miniaturas').children('a').children('img').fadeTo(0, 0.3);
	$('.miniaturas').children('a').hover(
		function(){
			$(this).children('img').stop(true, true).fadeTo(300, 1);
		},
		function(){
			$(this).children('img').stop(true, true).fadeTo(300, 0.3);
		}
	);
	$('.light').colorbox({arrowKey: true, speed: 600, transition: "fade", current: true});	
}
/* placeholder emulator */
(function($) {
	$.extend({
		placeholder : {
			settings : {
				focusClass: 'placeholderFocus',
				activeClass: 'placeholder',
				overrideSupport: false,
				preventRefreshIssues: true
			},
			debug : false,
			log : function(msg){
				if(!$.placeholder.debug) return;
				msg = "[Placeholder] " + msg;
				$.placeholder.hasFirebug ?
				console.log(msg) :
				$.placeholder.hasConsoleLog ?
					window.console.log(msg) :
					alert(msg);
			},
			hasFirebug : "console" in window && "firebug" in window.console,
			hasConsoleLog: "console" in window && "log" in window.console
		}

	});

    // check browser support for placeholder
    $.support.placeholder = 'placeholder' in document.createElement('input');

	// Replace the val function to never return placeholders
	$.fn.plVal = $.fn.val;
	$.fn.val = function(value) {
		$.placeholder.log('in val');
		if(this[0]) {
			$.placeholder.log('have found an element');
			var el = $(this[0]);
			if(value != undefined)
			{
				$.placeholder.log('in setter');
				var currentValue = el.plVal();
				var returnValue = $(this).plVal(value);
				if(el.hasClass($.placeholder.settings.activeClass) && currentValue == el.attr('placeholder')){
					el.removeClass($.placeholder.settings.activeClass);
				}
				return returnValue;
			}

			if(el.hasClass($.placeholder.settings.activeClass) && el.plVal() == el.attr('placeholder')) {
				$.placeholder.log('returning empty because it\'s a placeholder');
				return '';
			} else {
				$.placeholder.log('returning original val');
				return el.plVal();
			}
		}
		$.placeholder.log('returning undefined');
		return undefined;
	};

	// Clear placeholder values upon page reload
	$(window).bind('beforeunload.placeholder', function() {
		var els = $('input.' + $.placeholder.settings.activeClass);
		if(els.length > 0)
			els.val('').attr('autocomplete','off');
	});


    // plugin code
	$.fn.placeholder = function(opts) {
		opts = $.extend({},$.placeholder.settings, opts);

		// we don't have to do anything if the browser supports placeholder
		if(!opts.overrideSupport && $.support.placeholder)
		    return this;
			
        return this.each(function() {
            var $el = $(this);

            // skip if we do not have the placeholder attribute
            if(!$el.is('[placeholder]'))
                return;

            // we cannot do password fields, but supported browsers can
            if($el.is(':password'))
                return;
			
			// Prevent values from being reapplied on refresh
			if(opts.preventRefreshIssues)
				$el.attr('autocomplete','off');

            $el.bind('focus.placeholder', function(){
                var $el = $(this);
                if(this.value == $el.attr('placeholder') && $el.hasClass(opts.activeClass))
                    $el.val('')
                       .removeClass(opts.activeClass)
                       .addClass(opts.focusClass);
            });
            $el.bind('blur.placeholder', function(){
                var $el = $(this);
				
				$el.removeClass(opts.focusClass);

                if(this.value == '')
                  $el.val($el.attr('placeholder'))
                     .addClass(opts.activeClass);
            });

            $el.triggerHandler('blur');
			
			// Prevent incorrect form values being posted
			$el.parents('form').submit(function(){
				$el.triggerHandler('focus.placeholder');
			});

        });
    };
})(jQuery);

