(function($) {

    $.fn.clickable = function(options) {
        var settings = $.extend({
            hoverClass: 'hover'
        }, options);
        var jq = this.click(function() {
			var a = $(this).find('a');
            if (a) {
                window.location = a[0].href;
            }
        });
        if (settings.hoverClass) {
            jq.hover(function() {
                $(this).addClass(settings.hoverClass);
            }, function() {
                $(this).removeClass(settings.hoverClass);
            });
        }
        return this;
    };

    $.fn.asBlock = function(options) {
        var settings = $.extend({
            textIndent: '-90210px'
        }, options);
        return this.each(function() {
            var me = $(this);
            me.css({
                display: 'block',
                textIndent: settings.textIndent
            }).attr('title', me.text());
        });
    };

    $.fn.animateOnHover = function(out, over) {
        return this.each(function() {
            $(this).css(out)
        }).hover(function() {
            $(this).animate(over);
        }, function() {
            $(this).animate(out);
        });
    };

	$.misc = {};

	// MAPIFY THE BRANCH MAP IN CONTACT PAGE
	$.misc.mapify = function() {
		var $map = $('#branch-list');
		if (!$map.length) return;

		var map_size = {
			width: $map.width(),
			height: $map.height()
		};
		var box_size = {
			width: 225,
			height: 100
		};
		var mmargin = 10;
		var tmargin = 5;

		var bleft = map_size.width - box_size.width - mmargin;
		var btop  = map_size.height - box_size.height - mmargin;

		// add the transparent box where the address is wrapped in
		var  box = document.createElement('LI');
		var $box = $(box);
		$box.css({
			width: box_size.width+'px',
			height: box_size.height+'px',
			left: bleft+'px',
			top: btop+'px',
			background: '#FFF',
			opacity: '0.75',
			cursor: 'default',
			'-moz-border-radius': '0px'
		});
		$box.addClass('caption');
		$map.append(box);

		// add the actual address box
		var $addr = $box.clone();
		$addr.css({
			width: (box_size.width-(tmargin*2))+'px',
			height: (box_size.height-(tmargin*2))+'px',
			left: (bleft+tmargin)+'px',
			top: (btop+tmargin)+'px',
			background: 'transparent',
			opacity: '1',
			textIndent: '0',
			cursor: 'inherit'
		});
		$addr.addClass('caption');
		$addr.html('Move mouse on a red circle to view branch location.');
		$map.append($addr[0]);

		$('#branch-list>li').hover(function(){
			var $this = $(this);
			if ($this.hasClass('caption')) {
				return;
			}
			$this.addClass('active');
			$addr.html($this.html());
		}, function(){
			$(this).removeClass('active');
		});
	};

	// DISABLE CONTEXT MENU (FOR IMAGES ONLY)
	$.misc.disable_context_menu = function() {
		$('img,.nomenu').bind("contextmenu",function(e){
			return false;
		});
	};

	$(function() {
		$.misc.mapify();
		$.misc.disable_context_menu();
	});

})(jQuery);