$(function() {
	/* ---------------------------------------------------------------------- */
	/* Show text inside of keyword input
	/* ---------------------------------------------------------------------- */
	if ($('.parameter.keyword input:first').val() == '') {
		$('.parameter.keyword input:first').val($('.parameter.keyword input:first').attr('title'));
	}
	$('.parameter.keyword input:first').focus(function() {
		if($(this).val() == $(this).attr('title')) {
			$(this).val('').addClass('focus');
		}
	})
	$('.parameter.keyword input:first').blur(function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title')).removeClass('focus');
		}
	})
	
	$('.filter_parameters select').change(function() {
		$('.filter_parameters form').submit();
	})
	$('.parameter.cost').removeClass('cost')
		.css('width', '180px')
		.next().remove();
	

	/* ---------------------------------------------------------------------- */
	/* Add popup to i18n chooser and what's my model
	/* ---------------------------------------------------------------------- */
	$('.preferred_device input[type="submit"]').hide();
	$('.preferred_device select[name="session_preferred_device_id"]').change(function() {
		$('.preferred_device form').submit();
	});

	
	/* ---------------------------------------------------------------------- */
	/* Add popup to i18n chooser and what's my model
	/* ---------------------------------------------------------------------- */
	$('.popup').each(function() {
		var popup_left = -(Math.abs($(this).parent().find('.popup').innerWidth() - $(this).parent().innerWidth()) / 2);
		$(this).css({
			left: popup_left
		});
	})
	$('.trigger.down')
		.css({
			position: 'relative', 
			zIndex: '40'
		})
		.hoverIntent(
			function() {
				var self = $(this);
				setTimeout(function() {
					self.parent().find(".popup")
						.css({
							display: 'block',
							top: '15px',
							opacity: 0.1,
							zIndex: 3
						})
						.animate({
							opacity: 1, 
							top: '30px'
						}, 'slow');
				}, 500);
			}, 
			function() {
				var self = $(this);
				setTimeout(function() {
					self.parent().find('.popup')
						.animate({
							opacity: 0, 
							top: '45px'
						}, 'fast', 'linear', function() {
							$(this).css({
								display: 'none'
							});
						})
				}, 1500);				
			}
		);
	$('.trigger.up')
		.css({
			position: 'relative', 
			zIndex: '40'
		})
		.hoverIntent(
			function() {
				var self = $(this);
				setTimeout(function() {
					self.parent().find(".popup")
						.css({
							display: 'block',
							bottom: '15px',
							opacity: 0.1,
							zIndex: 3
						})
						.animate({
							opacity: 1, 
							bottom: '30px'
						}, 'slow');
				}, 500);
			}, 
			function() {
				var self = $(this);
				setTimeout(function() {
					self.parent().find('.popup')
						.animate({
							opacity: 0, 
							bottom: '45px'
						}, 'fast', 'linear', function() {
							$(this).css({
								display: 'none'
							});
						});
				}, 1500);
			}
		);
		
		
	/* ---------------------------------------------------------------------- */
	/* Update devices if service provider selected
	/* ---------------------------------------------------------------------- */
	$('.newsletter form').submit(function() {
		$(this).ajaxSubmit({
			url: "/email_subscribe/",
			success: function(html) {
				$('.newsletter p').html(html).addClass('submit_success');
			}
		});
		return false;
	});
});