$(document).ready(function() {
    $("#twitterFeed").tweet({	    
      join_text: "auto",
      username: "formebikes",
      count: 4
    });
    
    $(".lightbox").lightBox();
    $(".largeBikeLink").lightBox();

    /*$(".panel").hover(function() {
    $(this).find(".newbadge").hide();
    }, function() {
    $(this).find(".newbadge").show();    
    });*/

/*    Cufon.replace('.cufon', {
        hover: true
    });*/

    $("input[type=text], textarea").each(function() {
        if ($(this).val() == $(this).attr("title") && !$(this).hasClass("textBoxError")) {
            $(this).css("color", "#999999");
        }
    });

    $("input[type='text'], textarea").focus(function() {
        if ($(this).attr("title") == $(this).val()) {
            $(this).val('');
            $(this).removeAttr("style");
        }
    });

    $("input[type='text'], textarea").blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
            if (!$(this).hasClass("textBoxError")) {
	            $(this).css("color", "#999999");
	        }
        }
    });

    $("#logo a").hover(function() {
        $("#logo span").show();
    },
    function() {
        $("#logo span").hide();
    });

    /* IMAGE MAP */

    $("#dealerimagemap area").hover(function() {
        var area = $(this).attr("id").replace("dealerMap_", "");
        $("#dealerMap #mapoverlay").addClass(area);
        $("#dealerMap #mapoverlay").css("display", "block");
        $("#dealerMap #areaName").html("<h2 class=\"cufon\">" + $(this).attr("title") + "</h2>");
    }, function() {
        $("#dealerMap #mapoverlay").removeAttr("class");
        $("#dealerMap #mapoverlay").removeAttr("style");
        $("#dealerMap #areaName").html("");
    });

    /* CAROUSEL */

    if ($('.carouselBannerArea').length != 0) {
        $('.carouselBannerArea').serialScroll({
            items: '.carouselItem',
            prev: '#carouselControls .previous a',
            next: '#carouselControls .next a',
            offset: 0,
            start: 0,
            duration: 400,
            force: false,
            stop: true,
            lock: false,
            cycle: true

        });
    };
    
    
    /* PANEL HEIGHTS */
    var maxHeight = 0;
    
    if ($("#threeColumns").length > 0) {
	    panelSelector = "#threecolumns .panel";
    } else {
	    panelSelector = ".panel";
    }
    
    $(panelSelector).each( function() {
	    var panelHeight = parseFloat($(this).height());
	    if (panelHeight > maxHeight) {
		    maxHeight = panelHeight;
	    }
    });
    
    $(panelSelector).css("height", maxHeight);

    
    /* SCROLLING PHOTOS */
    
    $("#scrollingphotosArea").css("overflow", "hidden");
    
    var paneHeight = $("#scrollingphotosArea").height();
    
    $("#paging a").click( function() {
	    var currentpage = parseFloat($("#paging .current").text().replace('[','').replace(']',''));
	    var clickedpage = parseFloat($(this).text().replace('[','').replace(']',''));
	    
	    //alert(currentpage + "|" + clickedpage); // debugging
	    
	    if (currentpage != clickedpage) {
		    var scrollHeight = paneHeight * (clickedpage - 1);
		    $("#paging a").each(function() {
			    $(this).removeClass("current");
			    $(this).html($(this).text().replace('[','').replace(']',''));			    
		    });		    
		    $(this).addClass("current");
		    $(this).text('[' + $(this).text() + ']');
		    //alert("scrolling to: " + scrollHeight); // debugging
		    
		    $("#scrollingphotosArea").scrollTo({top:'' + scrollHeight + 'px', left:'0px'}, 400);
		    
	    }
    });
    
    /* GEOMETRY */
    
    $(".geoLink").click( function() {
	    var geoURL = $(this).attr("href").replace("/bikes/geometry/", "/include/geometry_").replace(".php", ".txt");
	    $.get(geoURL, function(data) {
		  $('.geoDataContainer').html(data);
	    	$("#geoOverlay, #geoPopUp").show();
		});
		
		var winHeight = $(window).height();
		var docHeight = $(document).height();
		var winWidth = $(window).width();
		var winScroll = $(window).scrollTop();
		$("#geoOverlay").height(docHeight);
		$("#geoPopUp").css("top", (((winHeight/2) - 75) + winScroll) + "px").css("left", ((winWidth/2) - 440) + "px");
		
		return false;
    });
    
    $(".closeBtn a, #geoOverlay").click( function() {
    	$("#geoOverlay, #geoPopUp").hide();
    });
    
    /* SPEC */
    
    $(".specLink").click( function() {
	    $("#geoOverlay, #specPopUp").show();
		
		var winHeight = $(window).height();
		var docHeight = $(document).height();
		var winWidth = $(window).width();
		var winScroll = $(window).scrollTop();
		$("#geoOverlay").height(docHeight);
		$("#specPopUp").css("top", (((winHeight/2) - 75) + winScroll) + "px").css("left", ((winWidth/2) - 440) + "px");
		
		return false;
    });
    
    $(".closeBtn a, #geoOverlay").click( function() {
    	$("#geoOverlay, #specPopUp").hide();
    });
    
    /* FAQ */
    $(".faqpanel p").not(".subhead").hide();
    $(".faqpanel .subhead a").click( function() {
	    $(this).toggleClass("open");
	    $(this).parent().next().slideToggle("slow");
    });
});
