Global = {
	
	fancyLabels: [],
	
	init: function() {
		
		$(document).ready(function() {
			
			// Prepare fancy labels
			var fancylabels = $('.fancylabel[title]');
			fancylabels.each(function(){
				Global.fancyLabels[this.id] = this.title;
				this.title = '';
			});
			fancylabels.each(Global.doFancyLabel);
			fancylabels.bind('blur', Global.doFancyLabel);
			fancylabels.bind('focus', Global.doFancyLabel);
			$('button[type=submit],input[type=submit]').bind('click',Global.clearFancyLabel);
			
			// Bind links & functionality
			Global.furtherInfoLink();
			Global.searchButton();
			Global.searchResultList();
			Global.inputColours();
		});
		
	},
	
	doFancyLabel: function(e) {
		if (this.value == '' && (e.type == 'blur' || e.type != 'focus')) { // deal with initial load of fancylabels, as well as blur events
			this.value = Global.fancyLabels[this.id];
			$(this).addClass('inputlabel');
		} else if (this.value == Global.fancyLabels[this.id] && e.type == 'focus') {
			this.value = '';
			$(this).removeClass('inputlabel');
		} 
	},
	
	clearFancyLabel: function() {
		for (var id in Global.fancyLabels) {
			if ($('#'+id).val() == Global.fancyLabels[id])
				$('#'+id).val('');
		}
	},
	
	furtherInfoLink: function() {
		$('a#furtherinfo-link').click(function() {
			$('div#furtherinfo').slideToggle('slow');
			if ($(this).text() == 'Read more') 
				$(this).text('Hide information');
			else 
				$(this).text('Read more');
		});
	},
	
	searchButton: function() {
		$('a#searchbutton').click(function() {
			var searchinput		=	document.getElementById('inputStringName').value; 
			if ($('input#'+searchinput).val() == 'Enter product name ...' ) {				
				$('input#'+searchinput).animate({color: "#f8f8f8"}, 125)
															.animate({color: "#888"}, 125)
															.animate({color: "#f8f8f8"}, 125)
															.animate({color: "#888"}, 125)
															.animate({color: "#f8f8f8"}, 125)
															.animate({color: "#888"}, 125);
			}
		});
	},
	
	searchResultList: function() {
		$('ol#results li').bind('mouseenter mouseleave', function() {
			$(this).toggleClass('over');
		});
	},
	
	inputColours: function() {
		$('input, textarea').bind('change', function() {
			$(this).removeClass('fancylabel');
		});
	}

	
}

Global.init();