$(document).ready(function(){

        $('.sub-menu').hide();
        
        
        $('#menu-principal li, #menu li').hover(
                                      
                function () {  
                        $(this).children('.sub-menu').slideDown(100);  
                },   
                function () {  
                        $(this).children('.sub-menu').slideUp(100);  
                }  
        );
        
        $("a[rel^='prettyGoogle']").prettyPhoto({
                custom_markup: '<div id="map_canvas" style="width:600px; height:387px"></div><div class="address">Santa Isabel 850, Parque Industrial Valle Grande, Lampa. Santiago - Chile</div>',
                default_width: 600,
		default_height: 387,
                changepicturecallback: function(){ initialize(); }
        });
        
        $("a[rel^='prettyPhoto']").prettyPhoto();
        
        $("a[rel^='prettyform']").prettyPhoto({
                allow_resize: false,
                default_width: 400,
                default_height: 500
        });
        
        $("a[rel^='prettyShare']").prettyPhoto({
                allow_resize: false,
                default_width: 400,
                default_height: 300
        });
        
        $('.contenedor_ofertas').tabSlideOut({
                tabHandle: '.tab_ofertas',
                pathToTabImage: 'wp-content/themes/fastpack/images/ofertas_bg.png',
                imageHeight: '266px',
                imageWidth: '55px',
                tabLocation: 'left',
                speed: 300,
                action: 'click',
                topPos: '150px',
                fixedPosition: true
        });
        
        $('.contenedor_offers').tabSlideOut({
                tabHandle: '.tab_offers',
                pathToTabImage: 'wp-content/themes/fastpack/images/ofertas_bg_eng.png',
                imageHeight: '266px',
                imageWidth: '55px',
                tabLocation: 'left',
                speed: 300,
                action: 'click',
                topPos: '150px',
                fixedPosition: true
        });
        
        $('.galeria_trigger').click(function() {
                var thiz = $(this);
                $(this).parent().prev().animate({height: 'toggle'}, 500, false, function(){
                        if (thiz.parent().prev().is(':visible')){
                                thiz.addClass('cerrar');
                        } else {
                                thiz.removeClass('cerrar');
                        }                
                });
        });
        
        $('.menu-item-13 a:first').attr('href', '#');
        $('.menu-item-11 a:first').attr('href', '#');
        $('.menu-item-162 a:first').attr('href', '#');
        
        
        $('.slide').bxSlider({
                displaySlideQty:4,
                moveSlideQty:4,
                margin: 3,
                padding:5

        });
	
	$('#flash_inner').bxSlider({
                displaySlideQty:1,
                moveSlideQty:1,
		auto: true,
		pager: true,
		controls: false,
		pagerSelector: '#myController'
        });
	
	$('#flash_inner_peru').bxSlider({
                displaySlideQty:1,
                moveSlideQty:1,
		auto: true,
		pager: true,
		controls: false,
		pagerSelector: '#peruslide'
        });
        
        $('.tab_ofertas').hover(
                        function(){
                                if($(this).parent().hasClass('open')){
                                        $(this).css('backgroundPosition', '-55px -267px');
                                } else {
                                        $(this).css('backgroundPosition', '0px -267px');
                                }
                        },
                        function(){
                                if($(this).parent().hasClass('open')){
                                        $(this).css('backgroundPosition', '-55px 0px');
                                } else {
                                        $(this).css('backgroundPosition', '0px 0px');    
                                }
                        }
                        
                
        );
         $('.tab_offers').hover(
                        function(){
                                if($(this).parent().hasClass('open')){
                                        $(this).css('backgroundPosition', '-55px -267px');
                                } else {
                                        $(this).css('backgroundPosition', '0px -267px');
                                }
                        },
                        function(){
                                if($(this).parent().hasClass('open')){
                                        $(this).css('backgroundPosition', '-55px 0px');
                                } else {
                                        $(this).css('backgroundPosition', '0px 0px');    
                                }
                        }
                        
                
        );
        
        $('#menu-item-12>a').click(function(e){
                e.preventDefault();
                $.ajax({
                        url: "/wp-content/themes/fastpack/contacto.php",
                        context: document.body,
                        success: function(data){
                                $('.contacto_container').html(data);
                                $('.contacto_container').parent().fadeIn();
                                $('.contacto_container').fadeIn();
                        }
                });
        });
	
	$('.cuek').click(function(e){
                e.preventDefault();
                $.ajax({
                        url: "/wp-content/themes/fastpack/contacto.php",
                        context: document.body,
                        success: function(data){
                                $('.contacto_container').html(data);
                                $('.contacto_container').parent().fadeIn();
                                $('.contacto_container').fadeIn();
                        }
                });
        });
        
        
        $('.cerrar').live('click', function() {
                $('.contacto_container form')[0].reset();
                $('.contacto_container').parent().fadeOut();
                $('.contacto_container').fadeOut();
        });
      
        $('.simpleform.contacter').live('submit', function(e) {
                e.preventDefault();
                
                var nombre = $('#nombre').val();
                var mensaje = $('#mensaje').val();
                var email = $('#e-mail').val();
                var valida = true;
                if(nombre == ""){
                        $('.nombre_error').html('Campo obligatorio');
                        $('.nombre_error').fadeIn();
                        $('#nombre').addClass('alert');
                        valida = false;
                } else {
                        $('.nombre_error').html('');
                        $('.nombre_error').fadeOut();
                        $('#nombre').removeClass('alert');
                }
                if(email == ""){
                        $('.email_error').html('Campo obligatorio');
                        $('.email_error').fadeIn();
                        $('#e-mail').addClass('alert');
                        valida = false;
                } else if(!isValidEmailAddress(email)){
                        $('.email_error').html('E-mail no válido');
                        $('.email_error').fadeIn();
                        $('#e-mail').addClass('alert');
                        valida = false;
                } else {
                        $('.email_error').html('');
                        $('.email_error').fadeOut();
                        $('#e-mail').removeClass('alert');
                }
                if(mensaje == ""){
                        $('.mensaje_error').html('Campo obligatorio');
                        $('.mensaje_error').fadeIn();
                        $('#mensaje').addClass('alert');
                        valida = false;
                } else {
                        $('.mensaje_error').html('');
                        $('.mensaje_error').fadeOut();
                        $('#mensaje').removeClass('alert');
                }
                
                
                if(valida){
                        $.ajax({
                                url: "/contactar.php",
                                data:  $('.simpleform').serialize(),
                                success: function(data){
                                        $('#respuestaContac').html(data);
                                        //$('.simpleform').slideDown();
                                },
                                beforeSend: function(){
                                        $('.simpleform fieldset').slideUp();
                                }
                        });        
                }
        });
        
        $('.simpleform.cotizer').live('submit', function(e) {
                e.preventDefault();
                var nombre = $('#nombre').val();
                var rut = $('#rut').val();
                var empresa = $('#empresa').val();
                var telefono = $('#telefono').val();
                var mensaje = $('#mensaje').val();
                var email = $('#e-mail').val();
                var valida = true;
                if(rut == ""){
                        $('.rut_error').html('Campo obligatorio');
                        $('.rut_error').fadeIn();
                        $('#rut').addClass('alert');
                        valida = false;
                } else {
                        $('.rut_error').html('');
                        $('.rut_error').fadeOut();
                        $('#rut').removeClass('alert');
                }
                if(empresa == ""){
                        $('.empresa_error').html('Campo obligatorio');
                        $('.empresa_error').fadeIn();
                        $('#empresa').addClass('alert');
                        valida = false;
                } else {
                        $('.empresa_error').html('');
                        $('.empresa_error').fadeOut();
                        $('#empresa').removeClass('alert');
                }
                if(telefono == ""){
                        $('.telefono_error').html('Campo obligatorio');
                        $('.telefono_error').fadeIn();
                        $('#telefono').addClass('alert');
                        valida = false;
                } else {
                        $('.telefono_error').html('');
                        $('.telefono_error').fadeOut();
                        $('#telefono').removeClass('alert');
                }
                if(nombre == ""){
                        $('.nombre_error').html('Campo obligatorio');
                        $('.nombre_error').fadeIn();
                        $('#nombre').addClass('alert');
                        valida = false;
                } else {
                        $('.nombre_error').html('');
                        $('.nombre_error').fadeOut();
                        $('#nombre').removeClass('alert');
                }
                if(email == ""){
                        $('.email_error').html('Campo obligatorio');
                        $('.email_error').fadeIn();
                        $('#e-mail').addClass('alert');
                        valida = false;
                } else if(!isValidEmailAddress(email)){
                        $('.email_error').html('E-mail no válido');
                        $('.email_error').fadeIn();
                        $('#e-mail').addClass('alert');
                        valida = false;
                } else {
                        $('.email_error').html('');
                        $('.email_error').fadeOut();
                        $('#e-mail').removeClass('alert');
                }
                if(mensaje == ""){
                        $('.mensaje_error').html('Campo obligatorio');
                        $('.mensaje_error').fadeIn();
                        $('#mensaje').addClass('alert');
                        valida = false;
                } else {
                        $('.mensaje_error').html('');
                        $('.mensaje_error').fadeOut();
                        $('#mensaje').removeClass('alert');
                }
                if(valida){
                        $.ajax({
                                url: "/cotizar.php",
                                data:  $('.cotizer').serialize(),
                                success: function(data){
                                        $('.cotizer').html(data);
                                        $('.cotizer').slideDown();
                                },
                                beforeSend: function(){
                                        $('.cotizer').slideUp();
                                }
                        });        
                }
        });
        
        $('.simpleform.sharer').live('submit', function(e) {
                e.preventDefault();
                var permalink = $(this).attr('action');
                var nombre = $('#friendname').val();
                var email = $('#friendemail').val();
                var mensaje = $('#yourremarks').val();
                var valida = true;
                if(nombre == ""){
                        $('.friendname_error').html('Campo obligatorio');
                        $('.friendname_error').fadeIn();
                        $('#friendname').addClass('alert');
                        valida = false;
                } else {
                        $('.friendname_error').html('');
                        $('.friendname_error').fadeOut();
                        $('#friendname').removeClass('alert');
                }
                if(email == ""){
                        $('.friendemail_error').html('Campo obligatorio');
                        $('.friendemail_error').fadeIn();
                        $('#friendemail').addClass('alert');
                        valida = false;
                } else if(!isValidEmailAddress(email)){
                        $('.friendemail_error').html('E-mail no válido');
                        $('.friendemail_error').fadeIn();
                        $('#friendemail').addClass('alert');
                        valida = false;
                } else {
                        $('.friendemail_error').html('');
                        $('.friendemail_error').fadeOut();
                        $('#friendemail').removeClass('alert');
                }
                if(mensaje == ""){
                        $('.yourremarks_error').html('Campo obligatorio');
                        $('.yourremarks_error').fadeIn();
                        $('#yourremarks').addClass('alert');
                        valida = false;
                } else {
                        $('.yourremarks_error').html('');
                        $('.yourremarks_error').fadeOut();
                        $('#yourremarks').removeClass('alert');
                }
                if(valida){
                        $.ajax({
                                url: "/wp-content/themes/fastpack/email-amigo.php",
                                type: "POST",
                                data:  $('.sharer').serialize(),
                                success: function(data){
                                        $('.sharer').html(data);
                                        $('.sharer').slideDown();
                                },
                                beforeSend: function(){
                                        $('.sharer').slideUp();
                                }
                        });        
                }
        });
         
         
        $('.ajax_tofriend').click(function(e){
                e.preventDefault();
                var link = $(this).attr('href');
                $.ajax({
                        url: link,
                        context: document.body,
                        success: function(data){
                                $('.contacto_container').html(data);
                                $('.contacto_container').parent().fadeIn();
                                $('.contacto_container').fadeIn();
                        }
                });
        });
        
        $('.cotizar').click(function(e){
                e.preventDefault();
                var link = $(this).attr('href');
                $.ajax({
                        url: link,
                        context: document.body,
                        success: function(data){
                                $('.contacto_container').html(data);
                                $('.contacto_container').parent().fadeIn();
                                $('.contacto_container').fadeIn();
                        }
                });
        });
        
        //$("#myController").jFlow({
        //        controller: ".jFlowControl", // must be class, use . sign
        //        slideWrapper : "#flash_wrapper", // must be id, use # sign
        //        slides: "#flash_inner",  // the div where all your sliding divs are nested in
        //        selectedWrapper: "jFlowSelected",  // just pure text, no sign
        //        width: "760px",  // this is the width for the content-slider
        //        height: "354px",  // this is the height for the content-slider
        //        duration: 600,  // time in miliseconds to transition one slide
        //        prev: ".jFlowPrev", // must be class, use . sign
        //        next: ".jFlowNext", // must be class, use . sign
        //        auto: true	
        //});
        //
        //$("#peruslide").jFlow({
        //        controller: ".jFlowControl", // must be class, use . sign
        //        slideWrapper : "#flash_wrapper_peru", // must be id, use # sign
        //        slides: "#flash_inner_peru",  // the div where all your sliding divs are nested in
        //        selectedWrapper: "jFlowSelected",  // just pure text, no sign
        //        width: "960px",  // this is the width for the content-slider
        //        height: "354px",  // this is the height for the content-slider
        //        duration: 600,  // time in miliseconds to transition one slide
        //        prev: ".jFlowPrev", // must be class, use . sign
        //        next: ".jFlowNext", // must be class, use . sign
        //        auto: true	
        //});
	
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			} else {
				window.location = $this.attr('href');
			}
		} 
		return false
	});
});


function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

function initialize() {
        fastpack=new google.maps.LatLng(-33.309308,-70.735436);
                var opciones = {
                   zoom: 16,
                   center: fastpack,
                   mapTypeId: google.maps.MapTypeId.HYBRID 
           }
           mapa = new google.maps.Map(document.getElementById("map_canvas"), opciones);
           var destino = new google.maps.Marker({
                   position: fastpack, 
                   map: mapa,
                   icon: 'http://fastpack.desarrollo.simplycit.cl/wp-content/themes/fastpack/images/pin.png',
                   title:"Fastpack"
           });

}

