$(document).ready(function() {

load_cufon();

$('#topnav ul > li:last-child').css('border', 'none');
$('#mainnav ul > li:last-child').css('border', 'none');
$('#footer ul > li:last-child').css('border', 'none');

//---------------------------
// Taller Pages
//---------------------------
$(".tallers").hide();
$(".taller-nav ul li").click(function(){
	var item=$(this).find('a').attr('href');
	$(".taller-nav ul li").removeClass('active');
	$(this).addClass('active');
	if($(".tallers:visible").length > 0)
	$(".tallers:visible").fadeOut('slow', function() {
	    	$(".taller").find(item).fadeIn('slow');
	  });
	else
		$(".taller").find(item).fadeIn('slow');
});
//---------------------------
// Ficha Pages
//---------------------------
$("#equsel ul").hide();
$("#equsel > ul:first").show();
$("#equ li").click(function(event){
    event.preventDefault();
	$("#equ li").removeClass('active');
	var item=$(this).find('a').attr('rel');
	$(this).addClass('active');
	if($("#equsel ul:visible").length > 0)
	$("#equsel ul:visible").fadeOut('slow', function() {
	    	$("#equsel").find(item).fadeIn('slow');
	  });
	else
		$("#equsel").find(item).fadeIn('slow');
});
$("#datossel ul").hide();
$("#datossel > ul:first").show();
$("#datos li").click(function(event){
    event.preventDefault();
	$("#datos li").removeClass('active');
	var item=$(this).find('a').attr('rel');
	$(this).addClass('active');
	if($("#datossel ul:visible").length > 0)
	$("#datossel ul:visible").fadeOut('slow', function() {
	    	$("#datossel").find(item).fadeIn('slow');
	  });
	else
		$("#datossel").find(item).fadeIn('slow');
});
//---------------------------
// Slider
//---------------------------

var precioDesde = parseInt($('input[name*=precioDesde]').val());
var precioHasta = parseInt($('input[name*=precioHasta]').val());
var min = parseInt($('input[name*=precioMin]').val());
var max = parseInt($('input[name*=precioMax]').val());

if ($("#slider-range").length > 0) {
    $("#slider-range").slider({
        range: true,
        min: min,
        max: max,
        values: [precioDesde, precioHasta],
        step: 1000,
        slide: function(event, ui) {
            $(".ui-slider-handle:first").html("<span>" + ui.values[0] + " € </span>");
            $(".ui-slider-handle:last").html("<span>" + ui.values[1] + " € </span>");
            $(".ui-slider-handle span:last").css({
                lineHeight: '18px'
            });
        },
        change: function(event, ui) {
            $('form#buscadorOcasion [name*=precioDesde]').val(ui.values[0]);
            $('form#buscadorOcasion [name*=precioHasta]').val(ui.values[1]);
            var combo = $('select[name^="buscador"]').first();
            combo.selectbox("change", combo.val());
        }
    });

//    $("#amount").val("$" + $("#slider-range").slider("values", 0) + " - $" + $("#slider-range").slider("values", 1));

    $(".ui-slider-handle:first").html("<span>" + $("#slider-range").slider("values", 0) + " € </span>");
    $(".ui-slider-handle:last").html("<span>" + $("#slider-range").slider("values", 1) + " € </span>");
    $(".ui-slider-handle:last").css({
        backgroundPosition: '0px bottom',
        top: '-19px'
    });
    $(".ui-slider-handle span:last").css({
        lineHeight: '18px'
    });
}

//---------------------------
// Cycle
//---------------------------
$('.cycle').cycle({
    fx: 'fade',
	speed: 2000,
	timeout: 2000,
	random: true
});
$('#slider-quienes, #home-slider, #slider-vehiculos-nuevos').cycle({
    fx: 'fade',
    speed: 1500,
    next: '#next',
    prev: '#prev',
    after: onAfter,
	timeout: 5000,
	pause: 1
});

//---------------------------
// Tabs
//---------------------------		
//Default Action
$(".tab_content").hide();
//Hide all content
$("ul.tabs li:first").addClass("active").show();
//Activate first tab
$(".tab_content:first").show();
//Show first tab content
//On Click Event
$("ul.tabs li").click(function() {
    $("ul.tabs li").removeClass("active");
    //Remove any "active" class
    $(this).addClass("active").show();
    //Add "active" class to selected tab
    $(".tab_content").hide();
    //Hide all tab content
    var activeTab = $(this).find("a").attr("href");
    //Find the rel attribute value to identify the active tab + content
    $(activeTab).fadeIn('fast');
    //Fade in the active content
    return false;
});

//---------------------------
// Categ Carr
//---------------------------
zoomHover("ul.categ-carro li");

//---------------------------
// Prettyphoto
//---------------------------	
$(".photos a[rel^='prettyPhoto']").prettyPhoto({
    theme: 'facebook',
    social_tools: '',
    overlay_gallery: false,
    deeplinking: false    
});

//---------------------------
// Ubicationes
//---------------------------
$('.box-quienes .infos').css('display', 'none');
var signal = $('.ubi').html();
$('.innerbox-quienes,.innerbox-quienes-last').click(function() {
    $('.ubi').html($(this).find('.infos').html());
    load_cufon();
    /*
		$('.ubi').fadeOut('slow',function(){
			$(this).html($(this).find('.infos').html())
			.fadeIn('slow');
			load_cufon();
		});*/
});

//---------------------------
// Selectbox
//---------------------------
 if ($('select').length != 0) {
    $('select').selectbox();
    if ($(".sbHolder").length > 0) {
        $(".sbHolder").reverse().each(function(index, elem) {
            $(elem).css('z-index', '99' + index);
        });
    }
}

$('table a').each(function(index, elem) {
    if (this.rel != '') {
        $(this).imgPreview({
            containerID: 'imgPreviewWithStyles',
            /* Change srcAttr to rel: */
            srcAttr: 'rel',
            imgCSS: {
                // Limit preview size:
                height: 200
            },
            // When container is shown:
            onShow: function(link) {

                },
            // When container hides:
            onHide: function(link) {
                $('span', this).remove();
            }
        });
    }
});

});
function onAfter(curr, next, opts) {

    var index = opts.currSlide;
    $('#prev')[index == 0 ? 'hide': 'show']();
    $('#next')[index == opts.slideCount - 1 ? 'hide': 'show']();
}
function zoomHover(element) {
    /*create zoom effect on images*/
    $(element).hover(function() {

        /* add high z-index value to push-up image*/
        $(this).css('z-index', '190');

        /*add hover class to image*/
        $(this).find('img').addClass("hover").stop()

        /*start animation*/
        .animate({
            /* impedisco che l'immagine si sposta mentre si ingrandisce */
            marginTop: '-10px',
            marginLeft: '-8px',
            /* imposto le nuove dimesioni all'immagine */
            width: '115px',
            height: '78px'
        },
        200,
        function() {

            });
        $(this).find('span').css('fontSize', '11px');
    },
    function() {
        $(this).css('z-index', '10');
        $(this).find('span').css('fontSize', '10px');

        $(this).find('img').removeClass("hover").stop()
        .animate({
            marginTop: '0',
            marginLeft: '0',
            width: '90px',
            height: '61px'
        },
        400,
        function() {
            $(this).parent().css('z-index', '0');
        });
    });

}

jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};

function load_cufon() {
    Cufon.set('fontFamily', 'Helvetica Neue LT Std').replace('td.desc,#topnav ul li a, #mainnav ul li a, #footer ul li a', {
        hover: {
            color: '#1c9bbb'
        },
        hoverables: {
            a: true,
            div: true
        }
    });
    Cufon.set('fontFamily', 'Helvetica Neue LT Std').replace('.taller-box,.caption,.box-quienes,.title,h3,ul.tabs,label,.contacto small,.button,h4,.results,table,.paging_options,.pagination,.paginacion,#text-content-963,#contactform label,.oblig,.resp,.resp2>label,.ubi,.acceso-desc,.acceso-form>label,.acceso-form>button,.acceso-form>p,.ventajas,.promo-taller-box,.equipo,.fin');
    Cufon.set('fontFamily', 'Helvetica Neue LT Std').replace('.paging_options ul li a', {
        hover: {
            color: '#000'
        },
        hoverables: {
            a: true,
            div: true
        }
    });
}

/*
 * imgPreview jQuery plugin
 * Copyright (c) 2009 James Padolsey
 * j@qd9.co.uk | http://james.padolsey.com
 * Dual licensed under MIT and GPL.
 * Updated: 09/02/09
 * @author James Padolsey
 * @version 0.22
 */
	(function(c){c.expr[':'].linkingToImage=function(a,g,e){return!!(c(a).attr(e[3])&&c(a).attr(e[3]).match(/\.(gif|jpe?g|png|bmp)$/i))};c.fn.imgPreview=function(j){var b=c.extend({imgCSS:{},distanceFromCursor:{top:10,left:10},preloadImages:true,onShow:function(){},onHide:function(){},onLoad:function(){},containerID:'imgPreviewContainer',containerLoadingClass:'loading',thumbPrefix:'',srcAttr:'href'},j),d=c('<div/>').attr('id',b.containerID).append('<img/>').hide().css('position','absolute').appendTo('body'),f=c('img',d).css(b.imgCSS),h=this.filter(':linkingToImage('+b.srcAttr+')');function i(a){return a.replace(/(\/?)([^\/]+)$/,'$1'+b.thumbPrefix+'$2')}if(b.preloadImages){(function(a){var g=new Image(),e=arguments.callee;g.src=i(c(h[a]).attr(b.srcAttr));g.onload=function(){h[a+1]&&e(a+1)}})(0)}h.mousemove(function(a){d.css({top:a.pageY+b.distanceFromCursor.top+'px',left:a.pageX+b.distanceFromCursor.left+'px'})}).hover(function(){var a=this;d.addClass(b.containerLoadingClass).show();f.load(function(){d.removeClass(b.containerLoadingClass);f.show();b.onLoad.call(f[0],a)}).attr('src',i(c(a).attr(b.srcAttr)));b.onShow.call(d[0],a)},function(){d.hide();f.unbind('load').attr('src','').hide();b.onHide.call(d[0],this)});return this}})(jQuery);
	
	
