function SiteLoadNewsById(sender,id) {
	
	$('.contentNewsListItemSelected').removeClass('contentNewsListItemSelected');
	
	$(sender).addClass('contentNewsListItemSelected');
	$(sender).addClass('contentNewsListItemLoading');
	

	$.ajax({
			url:	'news/' + id + '.html',
			type:	'get',
			dataType: "html",
			success:	function(response) {
							$('#contentNewsContent').html(response);
							$('.contentNewsListItemLoading').removeClass('contentNewsListItemLoading');
						}
	});
	
	
}

function animationFadeNext(name){
	
	var next = $('#' + name).attr('rel');
	$('#' + name).fadeOut(	'normal', 
							function()
								{ 
									$('#' + next).fadeIn(	'normal', 
															function()
																{ 
																	setTimeout( 'animationFadeNext("'+ next + '")', 2000); 
																}  
														); 
								} );
	
}



function ShowRegisterForm() {
	
	$('#reg1').hide();
	$('#reg2').show();
	return false;
}


function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

   if (strEmail.search(validRegExp) == -1)
   {
      return false;
   }
   return true;
}

function LoginFormSubmit() {

	var message = '';
	
	if(  $('#login').val() == '' || !isValidEmail( $('#login').val() ) ) {
		message += 'Adres e-mail jest niepoprawny!' + "\n";
	}
	
	if(  $('#password').val() == '' ) {
		message += 'Hasło jest puste!' + "\n";
	}
	
	if(message != '') {
		alert(message);
		return false;
	}
		
	var params = '';
	
	params += 'form[password]=' + $('#password').val();
	params += '&form[login]=' + $('#login').val();
	
	
	$.ajax({
		url:	'ajax/login.html?r=' + Math.random () ,
		type:	'post',
		dataType: "html",
		data:	params,
		success: function( response ) {			
					$('#html_strefa_projektanta').html( response );
			//alert( response );
				}
		});	
	
	
	return false;
}


function RegisterFormSubmit() {

	var message = '';
	
	if(  $('#email').val() == '' || !isValidEmail( $('#email').val() ) ) {
		message += 'Adres e-mail jest niepoprawny!' + "\n";
	}
	
	if(  $('#email').val() !=  $('#email').val()) {
		message += 'Adresy e-mail się nie pokrywają!' + "\n";
	}		
	
	if(  $('#pass').val() == '' ) {
		message += 'Hasło jest puste!' + "\n";
	}
	
	if(  $('#pass').val() !=  $('#pass2').val()) {
		message += 'Hasła się nie pokrywają!' + "\n";
	}
	
	if(  $('#firstname').val() == '' ) {
		message += 'Pole imie jest puste!' + "\n";
	}
	
	if(  $('#lastname').val() == '' ) {
		message += 'Pole nazwisko jest puste!' + "\n";
	}	
		
	if(  $('#company').val() == '' ) {
		message += 'Proszę podac nazwę firmy!' + "\n";
	}	
	
	if(  $('#state').val() == '' ) {
		message += 'Proszę wybrać województwo!' + "\n";
	}	

	if(  $('#city').val() == '' ) {
		message += 'Proszę podać nazwę miasta!' + "\n";
	}		
	
	if(  $('#phone').val() == '' ) {
		message += 'Proszę podać numer telefonu!' + "\n";
	}	
	
	if(  $('#street').val() == '' ) {
		message += 'Proszę podać nazwę ulicy i numer!' + "\n";
	}		
	
	if(  $('#zip').val() == '' ) {
		message += 'Proszę podać kod pocztowy!' + "\n";
	}		
	
	/* nie każda firma ma stronę WWW...
	if(  $('#www').val() == '' ) {
		message += 'Proszę podac firmową strone WWW!' + "\n";
	}
	*/	
	
	if(  $('#regon').val() == '' ) {
		message += 'Proszę podać numer REGON!' + "\n"; 
	}	
	
	if(message != '') {
		alert(message);
		return false;
	}
	
	var params = '';
	
	params +=  'form[firstname]=' + $('#firstname').val();
	params += '&form[lastname]=' + $('#lastname').val();
	params += '&form[password]=' + $('#pass').val();
	params += '&form[password2]=' + $('#pass2').val();
	params += '&form[email]=' + $('#email').val();
	params += '&form[company]=' + $('#company').val();
	params += '&form[phone]=' + $('#phone').val();
	params += '&form[state]=' + $('#state').val();
	params += '&form[city]=' + $('#city').val();
	params += '&form[street]=' + $('#street').val();
	params += '&form[zip]=' + $('#zip').val();
	params += '&form[www]=' + $('#www').val();
	params += '&form[regon]=' + $('#regon').val();
	
	$.ajax({
		url:	'ajax/register.html?r=' + Math.random () ,
		type:	'post',
		dataType: "html",
		data:	params,
		success: function( response ) {			
					$('#html_strefa_projektanta').html( response );
				}
		});
	
	return false;
}







// show_finished = false;
// hide_finished = false;
function LoadSiteByName(name) {
	
	$.ajax({
		url:	'ajax/' + name + '.html?r=' + Math.random () ,
		type:	'get',
		dataType: "html",
		success:	function(response)
					{
						$('#subpage_'+name+'_content').html(response);
	
						$('a.lytebox').attr('rel', 'lytebox');
						$('a.flash').each(function(){
							$(this).attr('rel', 'lyteframe');							
							var href = $(this).attr('href');							
							$(this).attr('href', 'flash.php?flash='+href);
							$(this).attr('rev', 'width: 510px; height: 330px; scrolling: no;');
														
						});
	
						initLytebox();
		
						setupScrollbar(name);
	
						var id 		 	= '';
						var position 	= '';
						var width 		= $('.subpage_content').width();					
						
						
						// szukamy ktora podstrona zostala wybrana i pobieramy jej ID oraz pozycje
						$('.subpage').each(function(){
							if($(this).attr('rel') == name)
							{
								id 		 	= $(this).attr('id');
								position 	= $(this).attr('position');
								scroll_page = name;
							}
						});
					
								
						// jezeli jest z lewej strony
						if(position == 'left')
						{
							var fromThis = false;
							$('.subpage').each(function(){											
								if($(this).attr('id') == id)
								{
									fromThis = true;
								}
								else if(fromThis && $(this).attr('position') == 'left')
								{								
									$(this).attr('position', 'right');
									$(this).animate({left: '+='+width }, 1000);
									$('#subpage_'+$(this).attr('rel')+'_content').animate({left: '+='+width }, 1000);												
								}
							});
						}
						else // jezeli jest z prawej strony
						{
							var toThis = false;
							$('.subpage').each(function(){								
								if(!toThis && $(this).attr('position') == 'right')
								{
									$(this).attr('position', 'left');
									$(this).animate({left: '-='+width }, 1000);
									$('#subpage_'+$(this).attr('rel')+'_content').animate({left: '-='+width }, 1000);												
								}
								
								if($(this).attr('id') == id)
								{
									toThis = true;
								}								
							});
						}
												
						$('.scroll_up').unbind();
						$('.scroll_down').unbind();
												
						$('#scroll_'+name).children('.scroll_up').mousedown(function(){ direction = 'up'; start_scroll(); }).mouseup(function(){ stop_scroll(); return false; }).mouseleave(function(){ stop_scroll(); return false; });
						$('#scroll_'+name).children('.scroll_down').mousedown(function(){ direction = 'down'; start_scroll(); }).mouseup(function(){ stop_scroll(); return false; }).mouseleave(function(){ stop_scroll(); return false; });						
						
						$('#subpage_'+scroll_page+'_content').mousewheel(function(event, delta) {
							if(delta > 0)
							{
								direction = 'up';
								scrollPage(70);
							}
							else
							{
								direction = 'down';
								scrollPage(70);
							}
							return false;
						});
				}
		});	
	
	return false;
}

var scroll_page = '';
var intervalID = null;
var direction = '';
var interval = 50;
// const scroll_step = 20;

// stop interval
function stop_scroll()
{
	if(intervalID)
	{
		clearInterval(intervalID);
	}
}

// clear interval before each scrolling to be sure it would not be scrolled n-times at one and set up intervall loop
function start_scroll()
{
	if(intervalID)
		clearInterval(intervalID);
		
	intervalID = setInterval("scrollPage(20)", interval);
}

// scrollPage interval loop func (scrolls page until interval is cleared)
function scrollPage(scroll_step)
{
	var outer_h = $('#subpage_'+scroll_page+'_content').height();
	var inner_h = $('#subpage_'+scroll_page+'_content').children('.subpage_content_html').children('.scrollable_subpage_content').height();
	var div_pos = $('#subpage_'+scroll_page+'_content').children('.subpage_content_html').children('.scrollable_subpage_content').position();	
	var top_pos = div_pos.top;
	
	if(direction == 'up')
	{
		if(top_pos < 0)
		{
			top_pos += scroll_step;
			if(top_pos > 0)
			{
				top_pos = 0;
				clearInterval(intervalID);
			}				
			$('#subpage_'+scroll_page+'_content').children('.subpage_content_html').children('.scrollable_subpage_content').css('top', top_pos);
		}	
	}
	else
	{				
		if(top_pos > -(inner_h-outer_h))
		{			
			top_pos -= scroll_step;				
			if(top_pos < (-1*(inner_h-outer_h)))
			{
				top_pos = -(inner_h-outer_h);
				clearInterval(intervalID);
			}
			
			$('#subpage_'+scroll_page+'_content').children('.subpage_content_html').children('.scrollable_subpage_content').css('top', top_pos);
		}	
	}
	
	var scroll_height = $('#scroll_'+scroll_page).children('.scroll_bar').height() - 20;	
	var percent_pos = (-1 * top_pos) / (inner_h-outer_h);
	
	//document.title = scroll_height;//$('#scroll_'+scroll_page).children('scroll_bar').children('scroll_pos').css('top');
	
	$('#scroll_'+scroll_page).children('.scroll_bar').children('.scroll_pos').css('top', Math.floor(percent_pos * scroll_height));
}

// set up scrollbar for current page (show it if it's needed)
function setupScrollbar(page)
{
	$('.scroll_v').hide();

	var outer_h = $('#subpage_'+page+'_content').height();
	var inner_h = $('#subpage_'+page+'_content').children('.subpage_content_html').children('div:first').height();
	
	if(inner_h > outer_h)
	{
		$('#scroll_'+page).show(function() {
			$(this).children('.scroll_bar').children('.scroll_pos').draggable({
				//appendTo: $('#scroll_'+page).children('scroll_bar'),
				axis:     		'y',
				containment: 	'parent',
				drag: function(event, ui)
				{
					var scroll_pos     = $('#scroll_'+scroll_page).children('.scroll_bar').children('.scroll_pos').position().top;
					var scroll_height  = $('#scroll_'+scroll_page).children('.scroll_bar').children('.scroll_pos').height();
					var scroll_parent  = $('#scroll_'+scroll_page).children('.scroll_bar').height();					
					var scroll_ratio   = scroll_pos / (scroll_parent - scroll_height);
					var outer_h = $('#subpage_'+scroll_page+'_content').height();
					var inner_h = $('#subpage_'+scroll_page+'_content').children('.subpage_content_html').children('.scrollable_subpage_content').height();
				
					var top_pos = Math.floor((outer_h - inner_h) * scroll_ratio);
					$('#subpage_'+scroll_page+'_content').children('.subpage_content_html').children('.scrollable_subpage_content').css('top', top_pos);
					return true;
				}
			});
		});
	}	
}





// document ready section
$(document).ready( function() {

		var element = 0;
		var left = 0;
		var position = 'left';
		
		var page_width = 1002;
		var tab_width  = 30;
		var content_width = page_width - (tab_width * $('.subpage').size());
		
		var current_selector = '';
		if($('.subpage_current').size() != 0) { current_selector = '.subpage_current'; }
		else { current_selector = '.subpage_first'; }		
		
		$('.subpage').each(function(){
			if(element == 0)
			{
				$('#content').height($(this).height());
			}	
			var name = $(this).attr('rel');
			
			$(this).css('z-index', element+2);
			$(this).css('position', 'absolute');
			$('#subpage_'+name+'_content').css('z-index', element+2);			
			$('#subpage_'+name+'_content').css('position', 'absolute'); 
			$('#subpage_'+name+'_content').css('width', content_width);
			$('#subpage_'+name+'_content').css('display', 'block');
			
			$(this).css('left', left);
			left += $(this).width();
			
			$('#subpage_'+name+'_content').css('left', left);			

			// ustawiamy pozycje, czy pasek znajduje sie z lewej czy z prawej strony okna
			// wszystkie elementy do .subpage_first (aktualnie wyswietlany) maja pozycje
			// 'left', a pozostale 'right'
			$(this).attr('position', position);
			if($(this).attr('id') == $(current_selector).attr('id'))
			{
				left += $('#subpage_'+name+'_content').width();
				position = 'right';
			}
			
			element ++;
		});
		
		

		$('.subpage').click( function(){				
				LoadSiteByName( $(this).attr('rel') );
				return false;				
			});		
		$(current_selector).click();
		
		animationFadeNext('trans1');
		// $('.subpage_no_click').unbind();
} );


