﻿$(function () {
    //menu
    $('.menu-horizontal').each(function () {
        $(this).find('> ul > li:first').addClass('first-item');
        $(this).find('> ul > li:last').addClass('last-item');
        $(this).find('> ul > li:odd').addClass('odd-item');
    });
});

//BannerHome
$(function(){
    var loadContent = function() {
        $('#banner-text').fadeOut(150, function(){
            var index = $('#banner-container').data('nivo:vars').currentSlide;
            var content = $('.banner-item-text:eq('+index+')').html();
            $('#banner-text').html(content);
            $('#banner-text').fadeIn(150);
        });
    };

    $('#banner-container').nivoSlider({
        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        animSpeed:300, // Slide transition speed
        pauseTime:10000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        directionNavHide:true, // Only show on hover
        controlNav:true, // 1,2,3... navigation
        beforeChange: loadContent,
        //afterChange: loadContent,
        afterLoad: loadContent 
    });
});

//top tabs

$(function(){
	var emptyMessage = '';
	for(i=0; i < Page_Validators.length; i++){
		if(Page_Validators[i].id.search('RfvTxtComments') >= 0){
			emptyMessage = Page_Validators[i].initialvalue;
			break;
		}
	};
	
	//Hide after lost focus
	$(document).click(function(e){ 
		if(!$(e.target).parents('.boxtop').length > 0) {
			$('.top_tabs .phone_open, .top_tabs div .email_open').fadeOut();
	 		$('.top_tabs .tabs .email, .top_tabs .tabs .phone').removeClass('active');
		}
	})
	
	
	$('.top_tabs .tabs .phone').click(function(e){
	
		var phoneContainer = $('.top_tabs .phone_open');
		var contactContainer = $('.top_tabs div .email_open');
		var contactLink = $('.top_tabs .tabs .email');

	
		e.preventDefault();
		contactContainer.hide();
		phoneContainer.fadeToggle();
		contactLink.removeClass('active');
		$(this).addClass('active');
	});
	
	$('.top_tabs .tabs .email').click(function(e){
	
		var phoneContainer = $('.top_tabs .phone_open');
		var contactContainer = $('.top_tabs div .email_open');
		var phoneLink = $('.top_tabs .tabs .phone');

	
		e.preventDefault();
		phoneContainer.hide();
		contactContainer.fadeToggle();
		$(this).addClass('active');
		phoneLink.removeClass('active');
		contactContainer.find('.form_email_col2_linha3 a').removeClass('disabled').attr('disabled', null);
		contactContainer.find('.aviso_sucesso').hide();
		
		var textArea = $('.form_email_col1 textarea');	
		if(textArea.text() == '') textArea.text(emptyMessage);	
	});
	
	$(document).delegate('.form_email_col1 textarea', 'focus', function (e) {
		if($(this).text() == emptyMessage) $(this).text('');
	});
	
	$(document).delegate('.form_email_col1 textarea', 'blur', function (e) {
		if($(this).text() == '') $(this).text(emptyMessage);
	});
});


//Disable EmailUs Send Button
$(document).delegate('a.emailus-submit', 'click', function (e) {
    if ($(this).hasClass('disabled')) {
        e.preventDefault();
        return;
    }

    if (Page_ClientValidate('EmailUsForm')) {
        if (!$(this).hasClass('disabled')) {
            $(this).addClass('disabled');
        }
    }
});

//CarouselHome
$(function () {
	$('.carousel-home').jCarouselLite({
	        btnNext: '.carousel-next',
	        btnPrev: '.carousel-prev'
	});
});


//Products
$(function(){
    $('.product-link').click(function () {

        var productId = $(this).data('product-id');
        var openSlide = $('.product-description:visible');
        
        if(openSlide.attr('id') === $('.product-description-id-' + productId).attr('id')) {
            return;
        }
        
        $('.product-container > .back').hide();
        $('.product-link').removeClass('clicked');
        $(this).addClass('clicked');
        
        var showSlideFunction = function(){
                $('.product-description-id-' + productId).show('slide', null, null, function(){
                    $('.product-container > .back').show();
                });
        };
        
        if(openSlide.length > 0) {
            openSlide.hide('slide', null, null, showSlideFunction);
        }
        else {
            showSlideFunction();
        }
    });
    
    $('.product-container > .back').click(function() {
        $('.product-description:visible').hide('slide');
        $('.product-link').removeClass('clicked');
        $(this).hide();
    });
});

//Facilities

var facilitiesMap = {

    facilitiesGoogleMap: null,

    changeMapLocation: function (facilityId, latitude, longitude) {
        $('.facility-description').hide();
        $('.facility-description-id-' + facilityId).show();
        $('.facilities-link').removeClass('clicked');
        $(this).addClass('clicked');
        facilitiesMap.facilitiesGoogleMap.setZoom(12);
        facilitiesMap.facilitiesGoogleMap.setCenter(new google.maps.LatLng(latitude, longitude));
    },

    addMarker: function (facilityId, latitude, longitude, headQuarter) {

        var facilityImage = '/SiteAssets/css/img/map_icon_filial.png';
        
        if(headQuarter) {
            facilityImage = '/SiteAssets/css/img/map_icon_head.png';
        }
        
        var facilityLatLng = new google.maps.LatLng(latitude, longitude);
        var facilityMarker = new google.maps.Marker({
            position: facilityLatLng,
            map: facilitiesMap.facilitiesGoogleMap,
            icon: facilityImage
        });

        google.maps.event.addListener(facilityMarker, 'click', function () {
            $('#facilities-link-' + facilityId).click();
        });
    }
};

$(function() {

    if (typeof google != 'object') return;
    
    var unitedStateSouthwest = new google.maps.LatLng(25, -130);
    var unitedStateNortheast = new google.maps.LatLng(48, -66);

    var unitedStateBounds = new google.maps.LatLngBounds(unitedStateSouthwest, unitedStateNortheast);

    var myOptions = {
        zoom: 3,
        mapTypeControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    facilitiesMap.facilitiesGoogleMap = new google.maps.Map(document.getElementById("map_canvas_facilities"), myOptions);
    facilitiesMap.facilitiesGoogleMap.fitBounds(unitedStateBounds);
    facilitiesMap.facilitiesGoogleMap.setZoom(3);

    facilitiesMap.callAddMarkers();

    var intervalId = setInterval(function() {
        var zoomElements = $('.gmnoprint:visible');
        
        if(zoomElements.length > 0) {
            clearInterval(intervalId);

            zoomElements.hide();
            $('#map_canvas_facilities').hover(
                function() {
                    zoomElements.show();
                },
                function() {
                    zoomElements.hide();
                });
        }
    }, 500);
});

    //Share
    /*
    $(function() {
    	var linkedin = $('.share .linkedin');
		var facebook = $('.share .facebook');
		var site = escape($('#header > h1 > a').text());
		var description = escape($('meta[name="description"]').attr('content'))||'';
		var title = escape(document.title);
		var url = escape(window.location.href)
		
		var linkedinUrl = 'http://www.linkedin.com/shareArticle?mini=true&url=';
		linkedinUrl += url + '&title=' + site + ' | ' + title;
		linkedinUrl += '&summary='+description+'&source=' + site;
		
		var facebookUrl = 'http://www.facebook.com/sharer.php?u=' + url;
		facebookUrl += '&t=' + site + ' | ' + title + ': ' + description;
				
		linkedin.attr('target', '_blank').attr('href', linkedinUrl);
		facebook.attr('target', '_blank').attr('href', facebookUrl); 
    });
	*/


//Contact Form

$(function(){

	if(typeof(contactForm) != 'undefined' && contactForm.messageWaterMark) {

		var emptyMessage = contactForm.messageWaterMark;
		
		$('.comments textarea').text(emptyMessage);
		
		$(document).delegate('.comments textarea', 'focus', function (e) {
			if($(this).text() == emptyMessage) $(this).text('');
		});
		
		$(document).delegate('.comments textarea', 'blur', function (e) {
			if($(this).text() == '') $(this).text(emptyMessage);
		});
	}
});

