var Notifier = function()
{
	this.init();
}

Notifier.prototype = {

	el: false,
	p: false,
	isWarning: false,

	init: function()
	{

	},

	showMessage: function( msg )
	{
		var el = $( document.createElement( 'div' ) );
		el.attr( 'id', 'notifycontainer' );
		var p = $( '<p></p>' ).html( msg );
		el.append( p );

		this.el = el;
		this.p = p;

		$( document.body ).append( el );
		el.css( 'opacity', 0 );
		el.fadeTo( 'slow', 1 );
	},

	updateMessage: function( msg )
	{
		if( this.p ) this.p.text( msg );
	},

	updateMessageWarning: function( msg )
	{
		if( this.p ) this.p.html( '<strong class="warning">' + msg + '</strong>' );
		this.isWarning = true;
	},

	fadeOut: function()
	{
		var el = this.el;
		var cur = this.current;
		var context = this;
		var sec = this.isWarning ? 6000 : 1000;
		this.isWarning = false;
		setTimeout( 
			function() 
			{ 
				el.fadeOut( 'slow', 
					function() 
					{ 
						el.remove();
						context.el = false;
						context.p = false;
					} 
				); 
			}, sec 
		);
	}

}

$( function()
{
	setTimeout( function() { $( '#feedback' ).remove(); }, 6000 );

	$( '#firstmenu a' ).click( firstMenuClick );

	var follower = $( '#firstmenu_follower' );
	var menu = $( '#firstmenu' );
	var pos = menu.getPos();

	menu.bind( 'mousemove', { follower: follower, pos: pos },
		function( e )
		{
			var maxHeight = e.data.pos.top + this.offsetHeight - 14;
			var minHeight = e.data.pos.top + 9;

			if( maxHeight > e.pageY && minHeight < e.pageY )
				e.data.follower.css( 'top', e.pageY - 10 + 'px' );
		}
	);

	$( 'a' ).each(
		function()
		{
			if( this.rel == 'external' ) this.target = '_blank';
		}
	);
} );

function firstMenuClick( e )
{
	var sub = $( this.parentNode ).find( '> ul' );

	if( sub.length )
	{
		sub.toggle();
		this.blur();
		return false;
	}
}

var style = $( '<link rel="stylesheet" type="text/css" href="'+ window.DOC_ROOT +'css/js-enabled.css">' );
$( 'head' ).append( style );





/****************************************
 *		Boxes			*
 ****************************************/
$( function()
{
	BoxHelpers.addLatestItemBoxEvents();
} );

var BoxHelpers = {};

BoxHelpers.addLatestItemBoxEvents = function( parent )
{
	if( !parent ) parent = $( document.body );
	parent.find( '.prevlatestitem, .nextlatestitem' ).click(
		function()
		{
			var i = this.href.replace( /^.+=/, '' );
			var link = this;
			var div = this.parentNode;

			$.get( window.DOC_ROOT + 'ajax/getlatestprod', { i: i },
				function( data )
				{
					data = eval( '('+ data +')' );
					if( !data )
					{
						$( link ).attr( 'href', $( link ).attr( 'href' ).replace( /[0-9]+$/, 0 ) );
						$( link ).click();
						return;
					}

					var itemimage = $( div ).find( 'img.itemimage' )
					var src = itemimage.attr( 'src' ).split( '/' );
					src[src.length - 1] = data.src;
					itemimage.attr( 'src', src.join( '/' ) );
					itemimage.parent().attr( 'href', data.href );

					var a = $( div ).find( '.latestitemsboximagewrapper a' );
					a.empty();
					a.append( itemimage );
					a.append( '<br>'+ data.itemname );

					$( link ).attr( 'href', $( link ).attr( 'href' ).replace( /[0-9]+$/, ++i ) );
				}
			);

			return false;
		}
	);
}




