(function($) {	
	var tellAFriend = function()
	{
		$('#tell_send, #tell_cancel').attr('disabled', true);
		$('#tell_ajax_indicator').css('display', 'inline');
		
		$.ajax(
			{
				type			: 'POST',
				url				: '/ajaxserver.php',
				data			: 'request=tellafriend&email=' + $('#tell_email').val() + '&name=' + $('#tell_name').val() + '&friend_email=' + $('#tell_friends_email').val() + '&friend_name=' + $('#tell_friends_name').val()  + '&message=' + $('#tell_message').val()  + '&url=' + window.location.href,
				complete		: function()
				{
					$('#tell_send, #tell_cancel').attr('disabled', false);
					$('#tell_ajax_indicator').hide();
				},
				
				success			: function(xml)
				{
					errors = $('error', xml);
					okies = $('ok', xml);
					
					errors.each(
						function()
						{
							alert(this.firstChild.nodeValue);
						}
					);
									
					if (errors.size() == 0) {
						var message = $('message', xml)[0].firstChild.nodeValue;
						alert(message);
					}
				}
			}
		);
		this.blur();
		return false;
	}
	EYE.statesCitiesCombo = function() {
		var frm,
			targetSelect,
			sourceSelect,
			cached = {},
			changeState = function (e) {
				var stateID = sourceSelect.val();
				if (cached[stateID]) {
					targetSelect
						.html(cached[stateID])
						.val('0');
				} else {
					targetSelect
						.attr('disabled', true)
						.html('<option>Laden ... </option>');
					$.ajax({
						type: 'POST',
						url: '/ajaxserver.php',
						data: $.param({
							state: stateID,
							request: 'changeState'
						}),
						complete: function(){},
						success: function (responseXML) {
							cached[stateID] = $('cities', responseXML).text();
							targetSelect
								.html(cached[stateID])
								.val('0')
								.attr('disabled', false);
						}
					});
				}
			};
		return {
			init: function() {
				targetSelect = $('#city select');
				sourceSelect = $('#state select').bind('change', changeState);
				cached[sourceSelect.val()] = targetSelect.html();
			}
		};
	}();
	
	EYE.register(EYE.statesCitiesCombo.init, 'init');
	EYE.globalInit = function() {
		$('#search').each(function(){
			this.origValue = this.value;
		}).bind('focus', function(){
			if (this.value == this.origValue) {
				this.value = '';
			}
		}).bind('blur', function(){
			if (this.value == '') {
				this.value = this.origValue;
			}
		});
		$('#searchForm').bind('submit', function() {
			searchQuery = $('#search')[0];
			if (searchQuery.value == '' || searchQuery.value == searchQuery.origValue) {
				return false;
			}
		});
		$('#tellAFriend').bind(
			'click',
			function() {
				tellForm = $('#tellafriend_form');
				if (tellForm.css('display') == 'none') {
					tellForm.show().animate({top:10}, 300);
				} else {
					tellForm.animate({top:-400}, 300, function(){$(this).hide()});
				}
				window.scrollTo(0,0);
				this.blur();
				return false;
			}
		);
		$('#tell_cancel').bind(
			'click',
			function() {
				tellForm = $('#tellafriend_form');
				if (tellForm.css('display') == 'block') {
					tellForm.animate({top:-400}, 300, function(){$(this).hide()});
				}
				this.blur();
				return false;
			}
		);
		$('#tell_send').bind(
			'click',
			tellAFriend
		);
	}
	EYE.register(EYE.globalInit, 'init');
	
	EYE.topSelect = function (){
		var el,
			show = function(){
				$('ul', el).show();
				$(document).bind('mousedown', hide);
			},
			hide = function(ev){
				if (!EYE.isChildOf(el, ev.target, el)){
					$('ul', el).hide();
					$(document).unbind('mousedown', hide);
					return false;
				}
			};
		return {
			init: function(){
				if ((el = document.getElementById('topSelect'))) {
					$('>a', el).bind('click', show);
				}
			}
		};
	}();
	EYE.register(EYE.topSelect.init, 'init');
	
	EYE.widgets = function () {
		var el,
			trigger = function () {
				if ($('#serviceType').val() == 'js') {
					$(el).show();
				} else {
					$(el).hide();
				}
			};
		return {
			init: function () {
				if ((el = document.getElementById('jsOptions'))) {
					$('#serviceType').bind('change', trigger);
					trigger();
					$('#text_color').ColorPicker({
						onSubmit: function(hsb, hex, rgb) {
							$('#text_color').val('#'+hex.toUpperCase());
						},
						onBeforeShow: function () {
							$(this).ColorPickerSetColor(this.value);
						}
					})
					.bind('keyup', function(){
						$(this).ColorPickerSetColor(this.value);
					});
					$('#background_color').ColorPicker({
						onSubmit: function(hsb, hex, rgb) {
							$('#background_color').val('#'+hex.toUpperCase());
						},
						onBeforeShow: function () {
							$(this).ColorPickerSetColor(this.value);
						}
					})
					.bind('keyup', function(){
						$(this).ColorPickerSetColor(this.value);
					});
					$('#link_color').ColorPicker({
						onSubmit: function(hsb, hex, rgb) {
							$('#link_color').val('#'+hex.toUpperCase());
						},
						onBeforeShow: function () {
							$(this).ColorPickerSetColor(this.value);
						}
					})
					.bind('keyup', function(){
						$(this).ColorPickerSetColor(this.value);
					});
				}
			}
		};
	}();
	EYE.register(EYE.widgets.init, 'init');
})(jQuery);

