var carousel_inprogress = false;
var going_for_adds_inprogress = false;
var sponsors_inprogress = false;
var going_for_adds_timeout;


$(function() {
	$('input[type=text]:first:not(.search_txt)').focus();
	
	$('.carousel li img').each( function() {
		var meta = $(this).attr( 'alt' ).split( '|' );
		
		if ( meta.length > 0 ) {
			$(this).parent().append( '<span class="name">' + meta[0].replace(/^\s+|\s+$/g,"") + '</span>' );
		
			if ( meta.length > 1 ) {
				$(this).parent().append( '<span class="genre">' + meta[1].replace(/^\s+|\s+$/g,"") + '</span>' );
			}
		}
		
	} );


	$('.going_for_adds li a img').each( function() {
		var meta = $(this).attr( 'alt' ).split( '|' );
		
		if ( meta.length > 0 ) {
			$(this).closest('li').find('.meta .title').text( meta[0].replace(/^\s+|\s+$/g,"") );
		
			if ( meta.length > 1 ) {
				$(this).closest('li').find('.meta .subtitle').text( meta[1].replace(/^\s+|\s+$/g,"") );
			}
		}
	} );


	$('.carousel_more').click(function() {
		if ( !carousel_inprogress ) {
		
			carousel_inprogress = true;
		
			$('.carousel ul').animate(
				{ marginLeft: '-570px' },
				1000,
				function() {
					$(this)
						.append( $(this).find('li:first') )
						.append( $(this).find('li:first') )
						.append( $(this).find('li:first') )
						;
					$(this).css( 'marginLeft', '0' );
					
					carousel_inprogress = false;
				}
			);
		}
		
		return false;
	});
	
	
	$('.sponsors_more').click(function() {
		if ( !sponsors_inprogress ) {
		
			sponsors_inprogress = true;
		
			$('.sponsors ul').animate(
				{ marginTop: '-205px' },
				1000,
				function() {
					$(this)
						.append( $(this).find('li:first') )
						.append( $(this).find('li:first') )
						;
					$(this).css( 'marginTop', '0' );
					
					sponsors_inprogress = false;
				}
			);
		}
		
		return false;
	});
	
	
	$('.going_for_adds').hover(
		function() {
			$(this)
				.find('.left,.right')
				.css( 'opacity', '0' )
				.show()
				.fadeTo( 1000, .6 )
				;
		},
		function() {
			$(this).find('.left,.right').fadeOut();
		}
	);
	
	
	$('.going_for_adds .left').click(function() {
		if ( !going_for_adds_inprogress ) {
		going_for_adds_inprogress = true;
		clearTimeout( going_for_adds_timeout );
	
	
		if ( $(this).parent().hasClass('.going_for_adds2') ) {
			$(this).parent().find('ul')
				.css( 'marginLeft', '-726px' )
				.prepend( $(this).parent().find('ul li:last') )
				.animate(
					{ marginLeft: '0' },
					1000,
					function () { 
						going_for_adds_inprogress = false;
						going_for_adds_timeout = setTimeout( "$('.going_for_adds .right').click()", 4500 );
					}
				)
			;

		} else {
			$(this).parent().find('ul')
				.css( 'marginLeft', '-630px' )
				.prepend( $(this).parent().find('ul li:last') )
				.animate(
					{ marginLeft: '0' },
					1000,
					function () { 
						going_for_adds_inprogress = false;
						going_for_adds_timeout = setTimeout( "$('.going_for_adds .right').click()", 4500 );
					}
				)
			;
		}
		
		}
		
		return false;
	});
	
	
	$('.going_for_adds .right').click(function() {
		if ( !going_for_adds_inprogress ) {
		going_for_adds_inprogress = true;
		clearTimeout( going_for_adds_timeout );
		
		
		if ( $(this).parent().hasClass('.going_for_adds2') ) {
			$(this).parent().find('ul').animate(
				{ marginLeft: '-726px' },
				1000,
				function() {
					$(this).append( $(this).parent().find('ul li:first') );
					$(this).css( 'marginLeft', '0' );
					going_for_adds_inprogress = false;
					going_for_adds_timeout = setTimeout( "$('.going_for_adds .right').click()", 4500 );
				}
			);
		} else {
			$(this).parent().find('ul').animate(
				{ marginLeft: '-630px' },
				1000,
				function() {
					$(this).append( $(this).parent().find('ul li:first') );
					$(this).css( 'marginLeft', '0' );
					going_for_adds_inprogress = false;
					going_for_adds_timeout = setTimeout( "$('.going_for_adds .right').click()", 4500 );
				}
			);
		}
		
		}
		
		return false;
	});
	
	going_for_adds_timeout = setTimeout( "$('.going_for_adds .right').click()", 4500 );
	

});
