$(document).ready(function() {


	/* main menu */

	$('ul.submenu').hide();

	$('li.main-item').hover(
		function () {
		var li_width = $(this).width();
		$(this).find('ul.submenu').css('width', li_width);
			if (!$(this).find('ul.submenu').is(':animated')) {
				$(this).find('ul.submenu').stop().slideToggle('fast');
			}
		}, 
		function () {
		$(this).find("span:last").remove();
			if (!$(this).find('ul.submenu').is(':animated')) {
			$(this).find('ul.submenu').stop().slideToggle('fast');
			}
		}
	);
	
	
	/* slider */
	
	var buttons = $('#slider ul li').length;
	
	for(i=0;i<=buttons-1; i++) {
		$('#slider-buttons').append('<a href="#" class="the-button">Button</a>');
	}
	
	$('#slider-buttons a:first').addClass('the-button-active')
	
	$('.the-button').click(function(e) {
		e.preventDefault();
		
		clearInterval(theSliderTimer);
	
		var click_index = $(this).index();
		var click_margin = click_index*-979;
		
		if ( !$('#slider ul').is(':animated') ) {
			$('#slider ul').fadeOut(400);
			$('#slider ul').animate({marginLeft: click_margin}, 1);
			$('#slider ul').fadeIn(800);
			$('a.the-button-active').removeClass('the-button-active');
			$(this).addClass('the-button-active')
		
		};
	
	});
	
	autoSlider();
	
	function theSlider() {
	
	var number = $('#slider ul li').length;
	var widthLi = (number-1)*-605;
	
	if (parseInt( $('#slider ul').css('marginLeft'))<=widthLi ) {
		if ( !$('#slider ul').is(':animated') ) {
			$('#slider ul').animate({opacity: 0}, 400);
			$('#slider ul').animate({marginLeft: 0}, 1, function () {
			var tmp = parseInt( $('#slider ul').css('marginLeft'))*(-1);
			var tmp2 = tmp/979;

			$('a.the-button-active').removeClass('the-button-active');
			$('#slider-buttons a:eq('+tmp2+')').addClass('the-button-active')
	
			});
			$('#slider ul').animate({opacity: 1}, 800);
		};
	} else {
		if ( !$('#slider ul').is(':animated') ) {
			$('#slider ul').animate({opacity: 0}, 400);
			$('#slider ul').animate({marginLeft: "-=979px"}, 1, function () {
			var tmp = parseInt( $('#slider ul').css('marginLeft'))*(-1);
			var tmp2 = tmp/979;

			$('a.the-button-active').removeClass('the-button-active');
			$('#slider-buttons a:eq('+tmp2+')').addClass('the-button-active')
	
			});
			$('#slider ul').animate({opacity: 1}, 800);
		};
	};
	};
	
	var theSliderTimer;
	function autoSlider() {
	theSliderTimer = window.setInterval(theSlider, 5000);  
	};
	
	
	/* input background */
	
	$('input#email').focus(function() {
		$(this).css('backgroundPosition', '-174px 2px')
	});
	
	$('input#email').blur(function() {
		var emailVal = $('input#email').val();
		if ( emailVal == "" ) {
			$(this).css('backgroundPosition', '0 2px')
		}
	});

});
