/**
 * Slider bar in gallery presentation at product-details page;
 * @template: product.html
 */
$(window).load(function()
{
	var s = $('#GallerySlider');
	if( s.length != 0 )
	{
		var mu = $('img.mu', s); // move up button;
		var md = $('img.md', s); // move down button;
		var sc = $('div.sc', s); // slider content;

		if( mu.lenght == 0 || sc.lenght == 0 || md.lenght == 0 )
			jQuery.error( "ERROR" );

		// slider definition;
		s.content = sc;
		s.items = $('img', sc);

		s.position = 0;
		s.isinmove = false;
		s.step = 101;
		s.sead = 10;
		s.spead = 50;
		s.mu = mu;
		s.md = md;

		//
		s.showGrots = function()
		{
			this.mu.css( 'cursor', this.position < 0 ? 'pointer' : 'auto' );
			this.mu.attr( 'src', this.position < 0  ? 'layout/images/produkt-detale/grot-up_.jpg' : 'layout/images/produkt-detale/grot-up.jpg' );
			this.md.css( 'cursor', this.position > 4 - this.items.length ? 'pointer' : 'auto' );
			this.md.attr( 'src', this.position > 4 - this.items.length ? 'layout/images/produkt-detale/grot-down_.jpg' : 'layout/images/produkt-detale/grot-down.jpg' );
		};

		// moves galley 
		s.move = function( direction )
		{
			direction = direction > 0 ? 1 : -1;
			var p = this.position + direction;
			if( p >= 0 - this.items.length + 4 && p <= 0 )
			{
				this.position += direction;
				var margin = this.position * this.step;
				this.content.css( 'margin-top', margin +'px' );
			}
			this.showGrots();
		};

		// buttons definition;
		mu.slider = s;
		md.slider = s;

		// move up;
		mu.click( function() {
			mu.slider.move( 1 );
		} );

		// move down;
		md.click( function() {
			md.slider.move( -1 );
		} );
		
		// init;
		s.showGrots();
		
		$('.sc .galeriaFoto a', s).each( function() {
			$(this).click( function() {
				$('#zdjecie_gl').attr( 'src', $(this).attr('href') );
				var dsc = $('#GalleryDescription');
				if( dsc.length != 0 )
					dsc.html( $(this).attr('title') );
				return false;
			});
		});
		
		// load first image;
		var firstImg = $( '.sc .galeriaFoto a:first', s );
		$('#zdjecie_gl').attr( 'src', firstImg.attr('href') );
		var dsc = $('#GalleryDescription');
		if( dsc.length != 0 )
			dsc.html( firstImg.attr('title') );

		if( $('.dom', s).length != 0 && s.items.length > 0 )
			$( '.sc .galeriaFoto:first', s ).remove();
		s.items = $('img', sc);
	}
	
	var list = $('#poradyLista');
	if( list.length != 0 )
	{
		list.items = $('div.porada', list );
		list.show = function( no )
		{
//			alert( no );
			// close all items;
			for( var i = 0; i < list.items.length; ++i )
			{
				var display = i == no ? 'block' : 'none';
				list.items[i].desc.css( 'display', display );
				list.items[i].side.css( 'display', display );
			}
		};

		// init;
		for( var i = 0; i < list.items.length; ++i )
		{
			var item = $(list.items[i]);
			item.no = i;
			item.list = list;
			item.desc = $( '#porada_'+ i +'_desc' );
			item.side = $( '#porada_'+ i +'_side' );

//			item.click( function() {
//				item.list.show( item.no );
//			} );
			
			( function(item) {
				item.click( function() {
					item.list.show( item.no );
				} );
			} (item) );
			
			list.items[i] = item;
		}

		// show first item;
		list.show( 0 );
	}
	
	$('#searchform #fraza').click( function() {
		if( $(this).attr('value') == 'Szukaj w serwisie' )
			$(this).attr( 'value', '' );
	} );
});

/**
 * Zamyka obrazek 
 */
function showYTMovie( item )
{
	item.style.display = "none";
	var yt = item.parentNode.childNodes[1];
	yt.style.display = "block";
}

$(window).load(function()
{
	var s = $('#contactForm');
	if( s.length != 0 )
	{

	}
});

function showProducts( item, close )
{
	var parent = item.parentNode;
	close = close === undefined ? false : close;
	if( parent.className == "active" || close )
	{
		// close;
		parent.className = null;
		parent.childNodes[1].style.display = "none";
		item.childNodes[1].style.display = "inline";
	}
	else
	{
		// close all other;
		$(".zielonagrupa").each( function() {
			showProducts( this, true );
		});
		// open this one;
		parent.className = "active";
		parent.childNodes[1].style.display = "block";
		item.childNodes[1].style.display = "none";
	}
	
	
};

