/**
 * Functions
 */
	var url = window.location.protocol + "//" + window.location.host + "/";

/**
 * jQuery Utility Functions
 */
	function inputClear (el) {
		var i = '#'+el;
		if($(i).attr('value') == $(i).attr('title')) {
			$(i).attr('value','');
		}
	}
	function inputReset (el) {
		var i = '#'+el;
		if($(i).attr('value')== '') {
			$(i).attr('value',$(i).attr('title'));
		}
	}
	function growlAlert (title, message, icon, timeout) {
		if(!title) {
			var title = 'Message';
		}
		if(!message) {
			var message = 'An Error Occurred';
		}
		if(!icon) {
			var icon = '';
		}
		if(!timeout) {
			var timeout = false;
		}
		$.Growl.show({
			'title'  : title,
			'message': message,
			'icon'   : icon,
			'timeout': timeout
		});
	}

$(document).ready(function(){

	$('.hide-me').hide();

	/**
	 * Voucher Effects
	 */
	$('.voucher a').betterTooltip({speed: 300, delay: 50});

	/**
	 * Forms
	 */
	var form_inputs = [
		'code',
		'where',
		'fullname',
		'name',
		'email',
		'company',
		'telephone',
		'enquiry',
		'search',
		'byemail',
		'author',
		'url',
		'comment',
		'advertiser_firstname',
		'advertiser_surname',
		'advertiser_email',
		'advertiser_company_name',
		'advertiser_address_1',
		'advertiser_address_2',
		'advertiser_city',
		'advertiser_county',
		'advertiser_postcode',
		'receiver_firstname',
		'receiver_surname',
		'receiver_email'
	];
	var num_inputs = form_inputs.length;
	for(var i=0; i<num_inputs; i++) {
		if(form_inputs[i]) {
			var id='#'+form_inputs[i];
			if($(id).length > 0) {
				$(id).bind({
					click: function() {
						inputClear($(this).attr('id'));
					},
					focus: function() {
						inputClear($(this).attr('id'));
					},
					blur: function() {
						inputReset($(this).attr('id'));
					}
				});
			}
		}
	}
	$("input[type='password']").bind({
		click: function(){
			if($(this).val().length < 1) {
				$(this).parent().find('span').text('');
			}
		},
		focus: function() {
			if($(this).val().length < 1) {
				$(this).parent().find('span').text('');
			}
		},
		blur: function() {
			if($(this).val().length < 1) {
				$(this).parent().find('span').text($(this).attr('title'));
			}
		}
	});

	/**
	 * Submit Buttons
	 */
	$('#btnsend,.btnsend').bind({
		mouseover: function(){
			$(this).attr('src',url+'images/btn.send_over.jpg');
		},
		mouseout: function(){
			$(this).attr('src',url+'images/btn.send_up.jpg');
		}
	});
	$('#enter-competition-button,#signup-button, .create-button').bind({
		mouseover: function(){
			$(this).css({'background-position':'-162px top'});
		},
		mouseout: function(){
			$(this).css({'background-position':'left top'});
		}
	});
	$('.login-button').bind({
		mouseover: function(){
			$(this).css({'background-position':'-91px top'});
		},
		mouseout: function(){
			$(this).css({'background-position':'left top'});
		}
	});

	/**
	 * Voucher Filter Search
	 */
	$('#voucher_category, #voucher_search').change(function(){
		var id = $(this).attr('id');
		var tag = '';
		if(id=='voucher_category') {
			tag = 'category';
		} else {
			tag = 'county';
		}
		window.location = url + 'vouchersnew/'+tag+'/'+$(this).val()+'/' + escape($('#'+id+' :selected').text().toLowerCase().replace(' ','+')+'/');
	});

	/**
	 * Load Map Functions Conditionally
	 */
	if('function' == typeof(mapHandler)) {
		mapHandler();
	}
});
