var win_w=win_h=0;
var tlo;

$.rand = function(from, to){
	return Math.floor(Math.random() * (to - from + 1) + from);
};

$.image = function(url, w, h, callback) {
	if (url, w, h) {
		$.get('image.php', {
			'url': url,
			'w': w,
			'h': h
		}, function(data) {
			if ($.isFunction(callback)) {
				callback(data);
			}
		});
	} else return false;
};

$.korekcja = function(){
	win_w = $(window).width();
	win_h = $(window).height();
	$('#kontener, #loader').css({
		'width': win_w+'px',
		'height': win_h+'px'
	});
	$('#social_content').width(win_w);
}

$.backgroundChange = function(callback){
	$('#loader').fadeIn('slow', function(){
		tlo = $('body').attr('class');
		$.image(tlo, win_w, win_h, function(image){
			var index_bg = new Image();
			$(index_bg).load(function(){
				$('#kontener').css('background', 'url('+image+') no-repeat');
				if($.isFunction(callback)){
					callback();// na zakończenie callback powinien być fadeout na #loader
				}
				else{
					$('#loader').fadeOut('slow');
				}
			}).attr('src', image);
		});	
	});
}

$.startPage = function(){
	$('#loader').fadeTo('slow', 0.6);
	var menu_bg = new Image();
	$(menu_bg).load(function(){
		$('#m_pasek_poziom').css('background', 'url(/images/m_pasek_poziom_bg.png) top no-repeat');
		$('#loader').fadeOut('slow', function(){
			$('#m_pasek_poziom').fadeIn('slow', function(){
				$('#social').fadeIn('slow');
			});
		});
	}).attr('src', '/images/m_pasek_poziom_bg.png');
}

$.socialHub = function(){
	$('#off').click(function(){
		$('#conttwitter').hide();
		$('#contfacebook').hide();
		$('#contyoutube').hide();
		$('#social_content').stop().animate({
			bottom: '-338px'
		}, 'normal', function(){
			$('#social li a').each(function(){
				if($(this).hasClass('active')){
					$(this).removeClass('active');
				}
			});
		});
	});
	$('#social li').click(function(){
		var social = $(this).attr('id');
		$('#conttwitter').hide();
		$('#contfacebook').hide();
		$('#contyoutube').hide();
		if($('#social #'+social+' a').hasClass('active')){
			$('#social_content').stop().animate({
				bottom: '-338px'
			}, 'normal', function(){
				$('#social li a').each(function(){
					if($(this).hasClass('active')){
						$(this).removeClass('active');
					}
				});
			});
			return false;
		}
		var bg_position = $('#social #'+social+' a').css('background-position');
		$('#social li a').each(function(){
			if($(this).hasClass('active')){
				$(this).removeClass('active');
			}
		});
		$('#social #'+social+' a').addClass('active');
		if($('#social_content').css('bottom') == '-388px'){
			$('#cont'+social).show();
			$('#social_content').stop().animate({
				bottom: '0px'
			}, 'slow');
		}
		else{
			$('#social_content').stop().animate({
				bottom: '-338px'
			}, 'normal', function(){
				$('#cont'+social).show();
				$('#social_content').animate({
					bottom: '0px'
				}, 'slow');
			});
		}
		return  false;
	});
}

$.menuHover = function(){
	$('#m_pasek_poziom').hover(function(){
		$('#menu').stop().fadeTo('fast', 1, function(){
			$('#maska').stop().fadeTo('fast', 0);
		});
	}, function(){
		$('#maska').stop().fadeTo('slow', 1);
		$('#menu').stop().fadeTo('slow', 0);
	});
}

$(function(){
	$.korekcja();
	$.backgroundChange(function(){
		$.startPage();
	});
	$.menuHover();
	$.socialHub();
});

var t;
$.res = function(){
	$.backgroundChange();
}

$(window).resize(function(){
	$.korekcja();
	clearTimeout(t);
	t = setTimeout('$.res()', 500);
});
