$(document).ready(function(){
    var form = 0;
    $('#sign-in').click(function () {
        $('#login-form').toggle();

        if (form == 0) {
            $('#top-email').focus().select();
            form = 1;
        } else {
            form = 0;
        }

        return false;
    });

    /*
     * Open external links in new window.
     */
    $('a[rel="external"]').click(function(){
        this.target = '_blank';
    });

    /*
     * Toggler.
     */
    $('.toggler').click(function() {
	var link = $(this).attr('href');

        /*
         * Open/closed arrows toggler.
         */
        if ($(this).hasClass('open') || $(this).hasClass('closed')) {
            if ($(this).hasClass('open')) {
                $(this).removeClass('open');
                $(this).addClass('closed');
            } else {
                $(this).removeClass('closed');
                $(this).addClass('open');
            }
        }

	$(link).toggle();
        return false;
    });

    /*
     * Reload captcha on image cilck.
     */
    if ($('.captcha').length > 0){
	$('.captcha').click(function () {
	    $(this).attr({src: '/captcha/?sid=' + Math.random()});
	});
    }

    /*
     * Zebra tables.
     */
    if ($('.zebra').length > 0){
	 $('.zebra tbody > tr:nth-child(even)').addClass('odd');
    }

    /*
     * Gallery items
     */
    if ($('.gallery').length > 0){
	$('.gallery a').fancybox({});
    }

    if ($('.videos').length > 0){
	$('.videos a').fancybox({
	    frameWidth: 640,
	    frameHeight: 505,
	    hideOnContentClick: false
	});
    }

    /*
     * Check/Uncheck/Toogle checkboxes.
     */
    if ($('.chkOn').length > 0){
	$('.chkOn').click(function() {
	    $($(this).val() + ' input[type="checkbox"]').each(function() {
		this.checked = true;
	    });
	    return false;
	});

	$('.chkOff').click(function() {
	    $($(this).val() + ' input[type="checkbox"]').each(function() {
		this.checked = false;
	    });
	    return false;
	});

	$('.chkTog').click(function() {
	    $($(this).val() + ' input[type="checkbox"]').each(function() {
		this.checked = ( (this.checked == false) ? true : false);
	    });
	    return false;
	});
    }

    /*
     * Autoselect
     */
    if ($('.autoselect').length > 0){
	$('.autoselect').focus(function(){
	    $(this).select();
	    $(this).autocomplete(0);
	});
    }

    /*
     * Autocomplete
     */
    if ($('#city').length > 0){
	$("#city").autocomplete('/autocomplete/', {
	    minChars: 2,
	    max: 10,
	    extraParams: {
		q: 'locality',
		r: function() { return $("#state").val(); },
		l: function() { return $("#city").val(); },
		z: function() { return $("#zip").val(); }
	    }
	});

	$("#zip").autocomplete('/autocomplete/', {
	    extraParams: {
		q: 'zip',
		r: function() { return $("#state").val(); },
		l: function() { return $("#city").val(); },
		z: function() { return $("#zip").val(); }
	    }
	});
    }


    /*
     * Search
     */
    if ($('#search').length > 0){
	$("#Mcity").autocomplete('/autocomplete/', {
	    minChars: 2,
	    max: 10,
	    extraParams: {
		q: 'locality',
		r: function() { return $("#Mstate").val(); },
		l: function() { return $("#Mcity").val(); }
	    }
	});

	$("#Mzip").autocomplete('/autocomplete/', {
	    extraParams: {
		q: 'zip',
		z: function() { return $("#Mzip").val(); }
	    }
	});

	$("#Bcity").autocomplete('/autocomplete/', {
	    minChars: 2,
	    max: 10,
	    extraParams: {
		q: 'locality',
		r: function() { return $("#Bstate").val(); },
		l: function() { return $("#Bcity").val(); }
	    }
	});

	$("#Bzip").autocomplete('/autocomplete/', {
	    extraParams: {
		q: 'zip',
		z: function() { return $("#Bzip").val(); }
	    }
	});

	$("#Icity").autocomplete('/autocomplete/', {
	    minChars: 2,
	    max: 10,
	    extraParams: {
		q: 'locality',
		r: function() { return $("#Istate").val(); },
		l: function() { return $("#Icity").val(); }
	    }
	});

	$("#Izip").autocomplete('/autocomplete/', {
	    extraParams: {
		q: 'zip',
		z: function() { return $("#Izip").val(); }
	    }
	});

	$("#Czip").autocomplete('/autocomplete/', {
	    extraParams: {
		q: 'zip',
		z: function() { return $("#Czip").val(); }
	    }
	});
    }
});