/* Zipcar Global Javascript Functions

By Will Moore and friends  - ISITE Design


*/

var cities = [];
var city_matches = [];
var hybrid_matches = [];
var city_ids = [];
var extra_url_vars;
var initial_url_vars;

var opacity = 40;

var geocoder;
var geotimer;
var match_indices;
var matches;
var overlay = opacity;

//start the jQuery functions
$(document).ready(function() {


	 //apply automatic input clearing to the search input - add as needed
	$("#searchform input, #birthdate-year, #license_expiration_year").inputClear();

	// alternate version of the above plugin that pulls the label, sets as the input's default value and hides it.  so clever.
	$("#searchform input, #notify-new-locations input, #gmaps-search input, #location-search, #company-search, #univ-search, .company-search").inputSetter();

	// add class to replicate :hover for non a elements.  For drop downs and buttons for IE<=6.  Silly IE6.
	if(document.all){
	    $("#nav li, button, .nav-page li").hover(
	            function() {
					$(this).addClass("over");
				},
	            function() {
					$(this).removeClass("over");
				}
	    );
	    $(".png-btn").hover(
	            function() {
					$(this).addClass("png-btn-over");
				},
	            function() {
					$(this).removeClass("png-btn-over");
				}
	    );
	}// if document.all

	//locations toggle.  Note: Hide this sorta stuff with js, not css, in case js is disabled.
	// note too, we're adding the class "js-on" to the revealed boxes so we can style and position the js version differently
	// There is a bug with IE6/7 that forces the content in the footer down when this is revealed.  We're using js to move it up in the source order.
	$("#where_are_cars").hide().addClass("js-on").prepend("<a href='#' class='close jqmClose'>close</a>");

    $("#where_are_cars a.close").click(function () {
        $('#where_are_cars_wrapper').jqmHide();
    });

	$("#where_are_cars h3.nojsfriendly").hide();
	$("#locations-toggle").click(function(){
		// $("#where_are_cars").slideToggle(300)
        var wacw = $('#where_are_cars_wrapper');
        // having this overlay set to something > 0 seems to prevent firefox
        // rendering bug where background image appears to smear during slide down
        if (overlay > 1) {
            overlay = 1;
            wacw.jqmShow();
        } else {
            wacw.jqmHide();
        }
		return false;
	});


//		// inner univerities listing show/hide.  we're setting the text of the span via js so that when there's no js present,
//		// we don''t have meaningless text on the page about toggling.  instead we just have a heading for the list.
//		// same with the little arrow - added via class="open/closed" here in the js.
//       	$("#where_are_cars h3 span").show().addClass("open").text("expand to see");
//		$("#where_are_cars .universities").hide();
//		$("#where_are_cars h3.toggle-uni").toggle(
//			function () {
//				$("#where_are_cars .universities").slideDown(200);
//				$(this).find("span").addClass("closed").removeClass("open").text("collapse to hide");
//
//			},
//			function () {
//				$("#where_are_cars .universities").slideUp(200);
//				$(this).find("span").addClass("open").removeClass("closed").text("expand to see");
//			});


	// sign up form for notify inside of locations drop down
	$("#notify-new-locations").addClass("click-me").hide();
	$("#notify-new-locations").prev().addClass("click-me").click(function(){
		$("#notify-new-locations").toggle();
	});


	//show hide the login form
	$("#sign-in").addClass("js-on").hide()
	$("#sign-in").prepend("<a href='#' class='close'>close</a>")
	$("#sign-in .close").click(function(){
			$("#sign-in").slideToggle(200);
			$("#signin-trigger").toggleClass("active")
			return false;
	});
	$("#signin-trigger").click(
		function(){
			$("#sign-in").slideToggle(200, function() {
                $("#sign-in #user_name").focus();
            });
			$(this).toggleClass("active")
			return false;
   		}
	);


    try {
        //Did you know? content flipper
        $("#did-you-know .container ").jCarouselLite({
            btnNext: "#did-you-know .prev",
            btnPrev: "#did-you-know .next",
            visible: "1",
            speed:   425
        });
    } catch (e) {};

	// add class "last" to the last li in any list.  Used as a styling hook since :last-child isn't widely supported via css.  thank you jquery.
    $("ul li:last-child").addClass("last");

});// document ready / end jquery functions


/* clear search field on click - made into plugin so it can easily be used more than once. */
jQuery.fn.inputClear = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

// alternate version of the above. pull label and insert as field. clear with click.
jQuery.fn.inputSetter = function() {

	return this.each(function() {

		var $input = $(this);
		var $label = $("label[for='"+$input.attr("id")+"']");
		var labeltext = $label.text();

		$label.hide();
		$input.val(labeltext);

		$input.focus(function() {
			if (this.value == labeltext) {
				this.value = "";
			}
		}).blur(function() {
			if (!this.value.length) {
				this.value = labeltext;
			}
		});

	});
};


// clean console.log.  Wont break IE6 if you leave it in your code.  cl("stuff to log")
function cl(logit){ if(window.console&&window.console.firebug) { console.log(logit) } };//cl()




/*
 * basic function to work around people double clicking on submit buttons
 **/
var dblClickPreventer = new Object();
dblClickPreventer.ids = new Array();
dblClickPreventer.onclick = function( evt, perm ) {
    if( !evt ) evt = window.event;
    var ele = ( evt.target ? evt.target : evt.srcElement );
    var key = ele.tagName + "#" + ele.id + "#" + ele.name + "#" + ele.type + "#" + ele.value;
    
    if( perm ) {
        if( !dblClickPreventer.ids[ key ] ) {
            dblClickPreventer.ids[ key ] = true;
            return true;
        } else {
            return false;
        }
    } else {
        var oldVal = dblClickPreventer.ids[ key ];
        var newVal = (new Date()).getTime();
    
        if( !oldVal || ( newVal - oldVal > 1000 ) ) {
            dblClickPreventer.ids[ key ] = newVal;
            return true;
        } else {
            return false;
        }
    }
}
// function to wrap google analytics to avoid going to "false" in anchor tags
function trackEvent(category, action, optional_label, optional_value) {
    pageTracker._trackEvent(category, action, optional_label, optional_value);
}
