$( function () {	
	header();
	gallery();
	sidebar_correction(3);
});

function sidebar_correction(i)
{
	var main_height = $('div#main').height();
	var sidebar_height = $('div#sidebar').height();
	if ( sidebar_height < main_height )
		$('div#sidebar').height(main_height);
	if ( i>0 )
		window.setTimeout(function(){sidebar_correction(i-1);},1000);
}

function gallery()
{
	var thumbs = $('div.thumbnails img');
	if ( thumbs.length > 0 )
	{
		var preview = $('<div style="display:block; position:absolute; left:100px; top:100px; padding:10px; background:#fff; border:1px solid #000; text-align:center">&nbsp;</div>').hide().appendTo($('body'));		
		thumbs.hover(
			function(){
				var img = $(this);
				var top = (window.pageYOffset || document.documentElement.scrollTop)+7; 
				preview.html('<img src="'+img.attr('src').replace(/small/i,'medium')+'" style="border:1px solid #777;"/>'+(img.attr('alt')?'<div style="margin-top:5px; text-align:left; width:540px">'+img.attr('alt')+'</div>':''));
				preview.css({'left':($('div#main').position().left)+'px','top':top+'px'});
				preview.show();					
			},
			function(){
				preview.hide();
			});
	}
	var thumbs2 = $('ul#pc_gallery img');
	if ( thumbs2.length > 0 )
	{
		var preview = $('<div style="display:block; position:absolute; left:160px; top:100px; padding:10px; background:#fff; border:1px solid #000; text-align:center">&nbsp;</div>').hide().appendTo($('body'));
		thumbs2.hover(
				function(){
					var img = $(this);
					var top = (window.pageYOffset || document.documentElement.scrollTop)+7; 
					preview.html('<img src="'+img.attr('src').replace(/small/i,'medium')+'" style="border:1px solid #777;"/>');
					preview.css({'left':($('div#main').position().left+110)+'px','top':top+'px'});
					preview.show();					
				},
				function(){
					preview.hide();
				});
	}
}

function header()
{
	var header = $('div#header div.center_wrapper').css('background','none');
	var text = $('<div id="hText" style="background:none; position:absolute; width:751px; height:33px; padding:52px; color:#fff; font-size:33px; font-weight:bold; font-family:Verdana,sans-serif;">&nbsp;</div>').hide();
	var background = $('<div id="hBack" style="background:url(http://ogrodzenia-bramy.pl/gfx/img/header/h6.jpg) no-repeat 0 0; position:absolute; width:855px; height:137px;">&nbsp;</div>');
	header.prepend(background).prepend(text);
	
	var t = ['PANELE','OGRODZENIA DEKORACYJNE','SIATKI','BRAMY','KLATKI','DRUTY','SIATKI HEKSAGONALNE','SIATKI DLA HODOWLI','SIATKI'];
	var b = ['h1.jpg','h13.jpg','h8.jpg','h11.jpg','h9.jpg','h14.jpg','h10.jpg','h12.jpg','h6.jpg'];

	window.setTimeout(function(){headerAnim(0);},2000);

	function headerAnim (i)
	{
		text.text(t[i]).fadeIn();
		background.fadeOut(1000,function(){
			text.fadeOut();
			background.css('background','url(http://ogrodzenia-bramy.pl/gfx/img/header/'+b[i]+') no-repeat 0 0').fadeIn(1000,function() {
				window.setTimeout(function(){
					headerAnim((i+1)%t.length);
				},2000);
			});
		});
	}	
}