/*
 * @copyright .apeiro
 * @author Marcin Stanisławski
 */

timerMainNews = null;
currentNews = 1;
newsTime = 8000;
function showMainNews( n ) {
	
	if( $('.main-news-in').length < n )
		return false;
	
	currentNews = n;
	
	i = (n-1)*-718;
	if( timerMainNews != null ){
		clearTimeout( timerMainNews );
	}
	
	if($('#main-news-container:first').position().left != i ){
		$('#main-news-container:first').stop().animate(
				{
					left: i, 
					duration: 12000 
				},
				{ 
					specialEasing: { 
						left: 'easeInQuart'
					},
					complete : function(){
						k = currentNews+1;
						if( $('.main-news-in').length < k ){
							k = 1;
						}
						
						timerMainNews = setTimeout( "showMainNews( "+k+" )", newsTime );
					} 
				}
				
		);
	}
}

currentGame =  '#games-1';

function showGame( n ) {
	newG = '#games-'+n;
	if( newG != currentGame ){		
		
		$( currentGame ).slideUp( 490 );
		$( '#games-' + n ).slideDown( 450 );
		if( n == 1 )
			n = 0;
		$('.page .games, .main-news-box .games').css( 'backgroundPosition', 'center '+28*n+'px' );
		$( newG ).parent().find('span').addClass( 'accActive' );
		$( newG ).parent().find('span').removeClass( 'accNotActive' );
		$( currentGame).parent().find('span').removeClass( 'accActive' );
		$( currentGame ).parent().find('span').addClass( 'accNotActive' );
		currentGame = newG;
	}
	
}

$(document).ready(
		function(){
			// auto news
			if($('#main-news-container').length > 0)
				timerMainNews = setTimeout( "showMainNews( 2 )", newsTime );
			
			$('.menuItem').mouseenter(
					function(){
						showSubmenu( $(this).attr('id').substr( 9 ) );
					}
			);
			
			$('.search').mouseenter(
					function(){
						hideSubmenu();
					}
			);
			
			$('.bar').mouseleave(
					function(){
						hideSubmenu();
					}
			);
			
			$('.logos img').mouseover(
					function(){
						$(this).attr(
								'src',
								$(this).attr('src').replace('grey','normal')
						);
					}
				
			);
			
			
			$('.logos img').mouseout(
					function(){
						$(this).attr(
									'src',
									$(this).attr('src').replace('normal','grey')
							);
					}
					
			);
			
			
			$('.searchTabs .tab').click(
					function(){ 
						tabToOpen = $(this).attr('id').replace('tabH','tabC');
						if( $('#'+tabToOpen+':visible').length == 0 )
							$('.searchResult .tabContent').hide( 'fast',
									function(){
											console.log($( '#'+$(this).attr('id').replace('tabH','tabC')));
											$( '#'+tabToOpen ).show();
									}
							);
					}
			);
			
			$('.hitSwitch').click(
					function(){
						id = $(this).attr( 'rel' );
						if( !$(this).hasClass('blue_link_active') ){
							$('.hit:visible').hide();
							$('#'+id ).fadeIn('slow');
							$('.blue_link_active').addClass('blue_link').removeClass('blue_link_active');
							$(this).removeClass('blue_link').addClass( 'blue_link_active' );
						}
					}
			);
			
			$('.mainShopSwitch').click(
					function(){
						id = $(this).attr( 'rel' );
						if( $(this).parent().hasClass( 'blue_bar' ) ){
							$('.mainShopItem:visible').hide();
							$('#'+id ).fadeIn('slow');
							$('#popular_bar div').addClass('blue_bar');
							$(this).parent().removeClass('blue_bar');
						}
					}
			);
			
			// shop switcher
			$('#shopPoductAdditionalImg img').click(
			    function(){
				$('#productImgMainCnt img').attr( 'src', $(this).attr('rel') );
				$('#productImgMainCnt img').parent().attr( 'href',  $(this).attr('rel') );
			    }
			);
			
		}
);

menuInProgress = false;
lastMenuRequest = null;
function showSubmenu( el ){
	if( $('#subMenu_'+el+' > *').length > 0 ){
		if( !menuInProgress ){
			$('#subMenuCnt').slideDown('fast');
		}else
			lastMenuRequest = el;
		
		$('.subMenuItem').hide();
		$('#subMenu_'+el).show();
	}else
		hideSubmenu();
}

function hideSubmenu(){
	if( !menuInProgress ){
		menuInProgress = true;
		$('#subMenuCnt').slideUp('slow', 
				function(){
					menuInProgress = false;
					if( lastMenuRequest != null ){
						showSubmenu( lastMenuRequest );
						lastMenuRequest = null;
					}
				}
			);
		$('.subMenuItem').hide();
	}
}

currentPlayer = null;
function showPlayerInfo( id ){
	if( currentPlayer != id ){
		currentPlayer = id;
		if( $('.playerDescItem:visible').length == 0 ){
			$('#playerItem-'+id ).fadeIn('slow');
			loadPlayerMedia( id );
		}else{
			loadPlayerMedia( id );
			$('.playerDescItem:visible').fadeOut( function(){
				$('#playerItem-'+id ).fadeIn('slow');
			});
		}
	}
}

function loadPlayerMedia( id ){
	$('#playerMedia').load( 
			'/'+langId+'/kadra/?k='+id,
			function(){
				Cufon.refresh('h2');
			}
	);
}


function showMore( el, className ){
	$('.'+className ).fadeIn();
	$(el).remove();
}

function reloadPhoto( el, url ){
	$(el).attr( 'src', url );
}

function loadGaleryImage( id ){
	$('#ajaxGaleryCnt').load('/galeria.php5?ajax=1&id='+id);
}

