jQuery( document ).ready( function() {
	jQuery( '#mainMenu' ).append( '<span class="marker"></span>' );
	jQuery( '#mainMenu .marker' ).css( {
		width: 129,
		left: jQuery( '#mainMenu li.active' ).position().left
	} );
	jQuery( '#offer article' ).append( '<div class="overlay"></div>' );

	//news
	var html = '<li class="active" data="1"></li>';
	for ( var i = 2; i <= Math.ceil( jQuery( '#news .list > article' ).length / 9 ); i++ ) {
		html = html + '<li data="' + i + '"></li>';
	}
	jQuery( '#news' ).append( '<div class="nav"><ul>' + html + '</ul></div>' );
	jQuery( '#news .nav' ).css( {
		'margin-left': - jQuery( '#news .nav' ).outerWidth() / 2,
		'margin-bottom': - jQuery( '#news .nav' ).outerHeight() / 2
	} );
	jQuery( '#news .list' ).animate( { scrollTop: 0 } );

	jQuery( '#news .list > article:first-child' ).addClass( 'active' );
	jQuery( '#news .list .active header a' ).trigger( 'click' );
} );


//logo
if ( ! jQuery.browser.msie ) {
	jQuery( 'h1 a' ).live( 'mouseover', function() {
		jQuery( this ).stop( false, true ).animate( {
			opacity: 0.6
		}, 400 );
	} );

	jQuery( 'h1 a' ).live( 'mouseout', function() {
		jQuery( this ).stop( false, true ).animate( {
			opacity: 1
		}, 400 );
	} );
}


//search

jQuery( '#top .domain' ).live( 'click focus', function() {
	jQuery( this ).addClass( 'domainActive' );
	if ( jQuery( this ).val() == 'Wpisz nazwę domeny...' ) {
		jQuery( this ).val( '' );
	}
} );

jQuery( '#top .domain' ).live( 'blur', function() {
	jQuery( this ).removeClass( 'domainActive' );
	if ( jQuery( this ).val() == '' ) {
		jQuery( this ).val( 'Wpisz nazwę domeny...' );
	}
} );


//form button

jQuery( '#top .button' ).live( 'mouseover', function() {
	jQuery( this ).addClass( 'buttonHover' );
} );

jQuery( '#top .button' ).live( 'mouseout', function() {
	jQuery( this ).removeClass( 'buttonHover' );
} );


//offer

var offerTimeout;

jQuery( '#offer article' ).live( 'mouseover', function() {
	var button = jQuery( this ).children( '.button' );
	clearTimeout( offerTimeout );
	jQuery( this ).addClass( 'hover' ).children( '.overlay' ).fadeIn();
	button.css( {
		'margin-left': - button.outerWidth() / 2
	} ).fadeIn();

	jQuery( '#offer article' ).not( this ).removeClass( 'hover' ).children( '.overlay' ).stop( false, true ).fadeOut();
	jQuery( '#offer article' ).not( this ).children( '.button' ).stop( false, true ).fadeOut();
} );

jQuery( '#offer article' ).live( 'mouseout', function() {
	offerTimeout = setTimeout( offerHoverDisabled, 1000 );
} );

jQuery( '#offer article' ).live( 'click', function() {
	window.location = jQuery( this ).find( 'a' ).attr( 'href' );
} );

function offerHoverDisabled() {
	var button = jQuery( '#offer article.hover' ).children( '.button' );
	jQuery( '#offer article.hover' ).removeClass( 'hover' ).children( '.overlay' ).stop( false, true ).fadeOut();
	button.stop( false, true ).fadeOut();
}


//news

jQuery( '#news :not(.full) header a:not(.active)' ).live( 'mouseover', function() {
	jQuery( this ).stop( false, true ).animate( {
		backgroundColor: '#7c3f20'
		//color: '#160700'
	}, 200 );
} );

jQuery( '#news :not(.full) header a:not(.active)' ).live( 'mouseout', function() {
	jQuery( this ).stop( false, true ).animate( {
		backgroundColor: '#210b01',
		color: '#e2d397'
	}, 200 );
} );

jQuery( '#news .nav li' ).live( 'mouseover', function() {
	jQuery( this ).stop( false, true ).addClass( 'hover', 400 );
} );

jQuery( '#news .nav li' ).live( 'mouseout', function() {
	jQuery( this ).stop( false, true ).removeClass( 'hover', 400 );
} );

jQuery( '#news .nav li:not(.active)' ).live( 'click', function() {
	var i = ( jQuery( this ).attr( 'data' ) - 1 ) * 9;
	jQuery( '#news .nav li' ).not( this ).stop( false, true ).removeClass( 'active', 400 );
	jQuery( this ).stop( false, true ).addClass( 'active', 400 );
	jQuery( '#news .list' ).stop( false, true ).animate( {
		scrollTop: i * jQuery( '#news .list article' ).outerHeight( true )
	} );
} );

jQuery( '#news .list a' ).live( 'click', function() {
	jQuery( '#news .full p' ).css( { opacity: 0 } );
	jQuery( '#news .full header' ).html( jQuery( this ).parents( 'header' ).html() );
	jQuery( '#news .full header a' ).removeAttr( 'style' );
	jQuery( '#news .full p' ).html( jQuery( this ).parents( 'article' ).children( 'p' ).html() );
	jQuery( '#news .full p' ).animate( { opacity: 1 } );
	return false;
} );


//footer

jQuery( '#footer a' ).live( 'mouseover', function() {
	jQuery( this ).stop( false, true ).animate( {
		color: '#ffffff'
	}, 300 );
} );

jQuery( '#footer a' ).live( 'mouseout', function() {
	jQuery( this ).stop( false, true ).animate( {
		color: '#a47662'
	}, 300 );
} );



//menu

var mainMenuMouseOutTimeout;

jQuery( '#mainMenu > ul > li > a' ).live( 'mouseover', function() {
	clearInterval( mainMenuMouseOutTimeout );
	jQuery( '#mainMenu > ul > li > a' ).not( this ).next( 'ul' ).stop( false, true ).slideUp();
	jQuery( '#mainMenu > ul > li > ul' ).hide();

	jQuery( '#mainMenu > ul > li' ).removeClass( 'hover' );
	jQuery( this ).parent( 'li' ).addClass( 'hover' );
	jQuery( '#mainMenu .marker' ).stop( false, true ).animate(
		{
			width: jQuery( '#mainMenu li.hover' ).width(),
			left: jQuery( '#mainMenu li.hover' ).position().left
		},
		300
	);
} );

jQuery( '#mainMenu > ul > li:not(.active) > a' ).live( 'mouseout', function() {
	mainMenuSetActive();
} );


function mainMenuSetActive() {
	mainMenuMouseOutTimeout = setTimeout( function() {
		jQuery( '#mainMenu > ul > li > ul' ).stop( false, true ).hide();
		jQuery( '#mainMenu > ul > li' ).removeClass( 'hover' );
		jQuery( '#mainMenu .marker' ).stop( false, true ).animate(
			{
				width: jQuery( '#mainMenu li.active' ).width(),
				left: jQuery( '#mainMenu li.active' ).position().left
			},
			300
		);
	}, 1000 );
}


//splash

$(document).ready(function() {
	$( '#top input[type="submit"]' ).click( function(event) {
		var strDomainName = jQuery( this ).parents( 'form' ).children( '.domain' ).val();
		if ( strDomainName =='Wpisz nazwę domeny...' || strDomainName=='www.' || strDomainName=='' ) {
			jQuery( this ).parents( 'form' ).children( '.domain' ).stop( false, true ).val( 'Wpisz nazwę domeny...' )
				.css( { 'background': '#ff0000', 'color': '#fff', 'filter': 'none' } )
				.delay( 1000 ).animate( { backgroundColor: '#fff5cb', color: '#9F7F00' }, 400, function() { jQuery( '#top input.domain' ).removeAttr( 'style' ); } );
			return false;
		}
		event.preventDefault();
        jQuery( '#loader_splash' ).show();
        
        scroll(0,0);
        $("#splash").css( { 'display': 'block' } ).animate( { opacity: 0.5 } );
        $("#splashCenter").fadeIn();
        $("html").css("overflow","hidden");
        $("body").css("height","100%");

        setTimeout( function() { jQuery( '#top form' ).submit(); }, 2000 );

   });
});

$(window).unload(function() {
    $("#splash").hide();
    $("#splashCenter").hide();
});
