$(function(){

	$("#btnSubmit").click(function(){
		$("#onlinerequest").submit();
		$(this).attr("disabled",true);
	});

    $('#onlinerequest select[name="productinterest"], #onlinerequest select[name="sizeinterest"]').unbind().change(function(){
        if($(this).val() == 'Other') {
            $(this).next('label').show();
        }else
            $(this).next('label').hide();
    });

    /***CUFON FONT**/
    Cufon.replace('.gotham', { fontFamily: 'Gotham Black' });
    Cufon.replace('.gothic', { fontFamily: 'Alternate Gothic' });
    Cufon.replace('.taku', { fontFamily: 'TakuSolid' });

    $('a.fancybox').fancybox({
        'overlayColor'		: '#000'
        });

    if($('#map').length) {
        $('#map').map({
                arrow_coord: '0,18',
                ajax: 1,
                path: "/_ajax_locate.php"
            });
    }
    $('#zipcode').unbind().click(function(){ 
        if(isNaN($(this).val()) === true) $(this).val('');
    });
     
    $('#lang label').click(function(){
        var lang = $(this).attr('ref');
        $.ajax({
				type: "POST",
				url: "/_ajax_language.php",
				data: ({'lang': lang}),
				success: function(){
					window.location = self.location;
				}
			});
    });

    if($('#locate').length) {
        $('#radius').change(function(){
        	if($("#zipcode").val() == "Enter Zip"){
      	      $('#zipcode').val('').focus();
        	}
        });
 
       $('#locate').map({
                input: '#zipcode',
                input2: '#radius',
                ajax: 1,
                path: "/_ajax_locate.php"
            });  
    }
     
    if($('#slideshow').length) {
        $('#slideshow').before('<div id="nav-cycle">').cycle({
            fx:     'fade',
            speed:  'fast',
            timeout: 4000,
            prev:   '#prev',
            pause:	true,
            next:   '#next',
            pager:  '#nav-cycle',
            after: cycleAfter,
            pagerAnchorBuilder: function (idx, slide) {
              return '<li><a href="#">'+(idx+1)+'</a></li>';
            }
        });
    }

    if($('#wheels-top').length){
        int_wheels(0, '');
        $('#wheels-top .wheels-make .items').unbind().click(function(){
            var $box = $('#wheels-top .wheels-detail .detail-container .block-wheel-box');
            var ref = $(this).attr('ref');
            $('#wheels-top .wheels-make .items').css({'border-left': '8px solid #f3f3f3'});
            $(this).css({'border-left': '8px solid #00eb64'});
            $('#wheels-top .wheels-detail .detail-container .block-wheel-box').fadeOut(200, function(){
                $box.empty();
                int_wheels(ref, $box);
            });
        });
    }

	$(".customertestimonials").cycle({
		fx: 'scrollUp',
		timeout: 5000
	});

	$("#newslettersignup").click(function(){
		var subscribe_email = $("input[name='subscribe_email']").val();
		if(subscribe_email == "" || isValidEmail(subscribe_email) == false){
			alert("Please enter a valid email address.");
		} else {
			$.ajax({
				type: "POST",
				url: "/_ajax_subscribe.php",
				data: ({
					subscribe_email: subscribe_email
				}),
				success: function(msg){
					$(".newsletterform").html("<div class=\"thankyou\">Thank you for subscribing!</div>");
				}
			});
		}
	});

	$("#application h4").hover(function(){
		var id = $(this).attr("id");
		$(".collapsed." + id).slideDown();
	});

	init();
    
    if($(".fancypopup").length) {
        $(".fancypopup").fancybox({
			'width'				: 950,
			'height'			: 650,
	        'autoScale'     	: false,
	        'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe',
            'scrolling'   : 'no'
		});
    }    
});
 
function cycleAfter(curr,next,opts) {
    var w = opts.slideCount * 13;
    $('#nav-cycle').width(w);
    $('#nav-marquee li:first').css({'padding-right': w+'px'});
}

function init(){
    if($("#content .revealbox.showit").length)
        $("#content").css({marginTop: 50});
	$("#content .revealbox").unbind().click(function(){
	    var org = ($(this).hasClass('showit')) ? 'showit' : 'hideit';
		$.ajax({
			type: "POST",
			url: "/_ajax_settings.php",
			data: ({ reveal: org }),
			success: function(msg){
                if(org == 'showit') {
				    $("#content .revealbox").removeClass('showit').addClass('hideit').html(msg);
                    $("#content").animate({marginTop: 310}, 200, "easeInOutQuad");
                }
                else {
                    $("#content .revealbox").removeClass('hideit').addClass('showit').html(msg);
                    $("#content").animate({marginTop: 50}, 200, "easeInOutQuad");
                }
                Cufon.replace('.gotham', { fontFamily: 'Gotham Black' });
			}
		});
	});
}
