/**
 * American Express Avenue
 */

(function($){

	var Amex = {
		initialize:function(){

			// tell css js is there
			$("body").addClass("JS-active");
	
			// init menu
			$('div#filtering ul').simpleMenu();

			// init hints
			$('input:text[title]').hintValues();

			// clickable boxes
			$('.clickable').clickableBox();

			// hoverable elements
			$('#result-table tr, #result-table div').hoverable({
				className: 'hover'
			});

			// replace submit buttons
			$('input[type=submit]').replaceInputs({
				template: '<a href="#" class="$className"><span>$value</span></a>',
				simulateInputClick: true, // simulates a click on the original input
				replacedClass: 'replaced'
			});

			// autocomplete
			$('#sloc').autocomplete({
				path: '/AutoComplete'
			});

			// init sifr
			//initSIFR();	
		}
	};

	/**
	 * Hints
	 */
	$.fn.hintValues = function(settings) {
		this.each(function(){
			$(this).blur(function(){
				if(!this.value || this.value == this.title) {
					this.value = this.title;
					$(this).addClass('blurred');
				}
			});
			$(this).focus(function(){
				if(this.value == this.title) {
					this.value = '';
					$(this).removeClass('blurred');
				}
			});
			$(this).triggerHandler('blur');
		});
	};

	/**
	 * Clickable boxes
	 */
	$.fn.clickableBox = function(settings) {
		this.click(function(e){
			var target = e.target;
			if(!/^a$/.test(target.nodeName)) {
				var link = $(this).find('a').eq(0);
				var href = link.attr('href');
				if(href) {
					window.location = href;
					e.preventDefault();
				}
			}
		});
	};

	/**
	 * Hoverable
	 */
	$.fn.hoverable = function(settings) {
		this.hover(function(){
			$(this).addClass(settings.className);
		}, function(){
			$(this).removeClass(settings.className);
		});
	}

	/**
	 * Helpers
	 */
	Function.prototype.bind = function(scope) {
		var method = this;
		return function() {
			return method.apply(scope, arguments);
		}
	};

	/**
	 * SIFR
	 */
	function initSIFR() {
		var webRoot = "/";
		
		sIFR.useDomContentLoaded = true;
		sIFR.useStyleCheck = true;
		sIFR.delayCSS  = true;

		sIFR.replaceElement(named({ 
			sSelector:"h1 .sifr", 
			sFlashSrc:webRoot+"Content/swf/agenda.swf", 
			sColor:"#0068c3",
			sLinkColor:"#0068c3", 
			sHoverColor:"#0068c3",
			sWmode:"transparent",
			sCase:"upper"
		}));		
		sIFR.replaceElement(named({ 
			sSelector:"h3 .sifr", 
			sFlashSrc:webRoot+"Content/swf/agenda.swf", 
			sColor:"#0068c3",
			sLinkColor:"#0068c3", 
			sHoverColor:"#0068c3",
			sWmode:"transparent",
			sCase:"upper"
		}));
	
	}

	/**
	 * Init
	 */
	$(function(){
		Amex.initialize();
	});

})(jQuery)