

var lsaWindowPopupId = false;
var lsaWindowBackgroundId = 'lsaWindowBackground';
var lsaAjaxMessageLoading = 'Proszę czekać...';

var lsaQuestionaryCheckUrl = '/lsa/index.php?con=QuestionaryController&sec=check';
var lsaQuestionaryShowUrl = '/lsa/index.php?con=QuestionaryController&sec=show';
var lsaQuestionarySaveUrl = '/lsa/index.php?con=QuestionaryController&sec=save';
var lsaMapContentUrl = '/lsa/index.php?con=MapController&sec=showContent';


/**
 * LSA Service: Header Icon and Disabilities Options
 */
var lsaServiceElementTextSizeRead = 'body';
var lsaServiceElementTextSize = 'body, div#wrapper div.top *, div#wrapper div.srodek *, div#wrapper div.lewa *, div#wrapper div.stopka *';
var lsaServiceTextSize = null;
var lsaServiceStyleCss = null;

$(document).ready(function() {
	
	// Load data from cookies
	lsaServiceTextSize = lsaServiceGetCookie( 'lsaServiceTextSize' );
	lsaServiceStyleCss = lsaServiceGetCookie( 'lsaServiceStyleCss' );
		
	// Load from cookie
	if( lsaServiceTextSize ) {
		//parseInt( $( lsaServiceElementTextSize ).css( 'font-size', lsaServiceTextSize +'px' ) );
		$( lsaServiceElementTextSize ).css( 'cssText', 'font-size: '+ lsaServiceTextSize +'px !important' );
	}
	
	// Set from css/html
	else {
		lsaServiceTextSize = parseInt( $( lsaServiceElementTextSizeRead ).css('font-size') );
	}
	
	// Set Disabilities css style
	if( lsaServiceStyleCss ) {
		$('head link[rel="stylesheet"]').each(function() {
			if( $(this).attr('media') == 'none' ) {
				$(this).attr( 'media', 'all' );
			}
		});
	}
	
});

function lsaServiceDisabilitiesTextSize( mode ) {

	// Save status of text size in cookies
	if( mode == 0 ) {
		lsaServiceSetCookie( 'lsaServiceTextSize', '', 0 );
		document.location.reload();
	}
	else {
		lsaServiceTextSize = parseInt( lsaServiceTextSize ) + parseInt( mode );
		
		if( lsaServiceTextSize < 8 ) {
			lsaServiceTextSize = 8;
		}
		else if( lsaServiceTextSize > 17 ) {
			lsaServiceTextSize = 17;
		}
		
		//$( lsaServiceElementTextSize ).css( 'font-size', lsaServiceTextSize +'px' );
		$( lsaServiceElementTextSize ).css( 'cssText', 'font-size: '+ lsaServiceTextSize +'px !important' );
		lsaServiceSetCookie( 'lsaServiceTextSize', lsaServiceTextSize, 360 );
	}
	
}

function lsaServiceDisabilitiesStyle() {
	
	// Disabilities True, now switch to False
	if( lsaServiceStyleCss ) {
		lsaServiceStyleCss = false;
		lsaServiceSetCookie( 'lsaServiceStyleCss' );
	}
	
	else {
		lsaServiceStyleCss = true;
		lsaServiceSetCookie( 'lsaServiceStyleCss', 'true', 360 );
	}
	
	document.location.reload();
}

function lsaServiceAddToHomepage() {
	lsaWindowPopupShowing( true, 'lsaWindowPopupAddToHomepage' );
}

function lsaServiceAddToFavorites() {
	var title = document.title;
	var url = document.location;
	if( window.sidebar ) {
		window.sidebar.addPanel( title, url, '' );				// Mozilla Firefox
	}
	else if( window.external ) {
		window.external.AddFavorite( url, title );				// Internet Explorer
	}
}


function lsaServiceTranslatePage() {
	var href = window.location.href;
	document.location = 'http://translate.google.com/translate?js=n&prev=_t&hl=pl&ie=UTF-8&layout=2&eotf=1&sl=pl&tl=en&u='+ escape( href ) +'&act=url';	
}


/**
 * LSA Service: Show Questionary
 */
$(document).ready(function() {
	lsaServiceCheckQuestionaryByPage();
});

function lsaServiceCheckQuestionaryByPage() {
	
	// If "disabilities mode"	in on, then questionary not show
	if( lsaServiceStyleCss ) {
		return;
	}
	
	var href = window.location.href;
	var param = href.split(',');
	var idPage = 1;
	if( param[1] ) {
		idPage = param[1];
	}
	
	$.getJSON( lsaAjaxUrlPrepare( lsaQuestionaryCheckUrl +'&idPage='+ idPage ), function(data) {
		if( data ) {
			if( data.idQuestionary ) {
				
				// If questionary has been "completed", then questionary not show
				if( lsaServiceGetCookie( 'lsaServiceQuestionary_'+ data.idQuestionary ) ) {
					return;
				}
				
				lsaServiceShowQuestionary( data.idQuestionary );
			}
		}
	});
	
}

function lsaServiceShowQuestionary( idQuestionary ) {
	lsaWindowPopupShowing( true, 'lsaWindowPopupQuestionary' );
	
	lsaAjax( 'div#lsaWindowPopupQuestionary td.frameContent', lsaQuestionaryShowUrl, 'idQuestionary='+ idQuestionary, 'questionary' );
}

function lsaServiceQuestionaryPage( direction ) {
	
	// Hide all buttons
	$('div#lsaWindowPopupQuestionary td.frameFooter a.button').css( 'display', 'none' );

	// Count of page
	var pageCount = $('div#lsaWindowPopupQuestionary div.questPage').length;

	// Find visible page
	var currentPage = false;
	var iCurrentPage = 0;
	$('div#lsaWindowPopupQuestionary div.questPage').each(function() {
		if( !currentPage ) {
			iCurrentPage++;
		}
		if( $( this ).css('display') == 'block' ) {
			currentPage = this;
		}
	});
	
	// Hide all page 
	$('div#lsaWindowPopupQuestionary div.questPage').css( 'display', 'none' );
	
	// Show new page
	iCurrentPage += direction; 
	$('div#lsaWindowPopupQuestionary div.questPage').eq( ( iCurrentPage - 1 ) ).css( 'display', 'block' );
	
	// Show buttons
	if( iCurrentPage < pageCount && pageCount > 1 ) {
		$('div#lsaWindowPopupQuestionary td.frameFooter a.buttonNext').css( 'display', 'block' );
	}
	if( iCurrentPage > 1 && pageCount > 1 ) {
		$('div#lsaWindowPopupQuestionary td.frameFooter a.buttonPrev').css( 'display', 'block' );
	}
	if( iCurrentPage == pageCount ) {
		$('div#lsaWindowPopupQuestionary td.frameFooter a.buttonSend').css( 'display', 'block' );
	}
	
	// Centering window on page
	lsaWindowPopupResize();
	
}

$(document).ready(function() {
	$('div#lsaWindowPopupQuestionary td.frameFooter a.buttonNext').click(function() {
		lsaServiceQuestionaryPage( 1 );
	});

	$('div#lsaWindowPopupQuestionary td.frameFooter a.buttonPrev').click(function() {
		lsaServiceQuestionaryPage( -1 );
	});

	$('div#lsaWindowPopupQuestionary td.frameFooter a.buttonSend').click(function() {
		
		// Read data from form
		var idQuestionary = $('div#lsaWindowPopupQuestionary input#idQuestionary').val();
		var data = lsaAjaxFormPrepareData('div#lsaWindowPopupQuestionary');
		
		// Save data
		lsaAjax( 'div#lsaWindowPopupQuestionary td.frameContent', lsaQuestionarySaveUrl, data )

		// Save in cookie status Completed for this Questionary
		lsaServiceSetCookie( 'lsaServiceQuestionary_'+ idQuestionary, 'true', 360 );
		
		// Hide window - if not show message after send data eg. Thanks Message
		lsaWindowPopupButtonCloseAction();
		
	});
});


/**
 * LSA Service: Show Map and Map Content
 */
function lsaServiceShowMap() {
	lsaWindowPopupShowing( true, 'lsaWindowPopupMap' );
}

function lsaServiceShowMapContent( show, idMapContent ) {
	
	if( idMapContent ) {
		lsaAjax( 'td#lsaWindowPopupMapContentContent', lsaMapContentUrl, 'idMapContent='+ idMapContent );
	}
	
	// Temporarity switch map to content of map
	if( show ) {
		$('div#lsaWindowPopupMapFlashContainer').css( 'height', '1px' );
		$('table#lsaWindowPopupMapContent').show();
	}
	else {
		$('table#lsaWindowPopupMapContent').hide();	
		$('div#lsaWindowPopupMapFlashContainer').css( 'height', 'auto' );
	}
}


/**
 * LSA Service: Window Popup Showing Controller
 */
function lsaWindowPopupButtonCloseAction() {
	lsaWindowPopupShowing( false );
}

function lsaWindowPopupShowing( show, idWindowPopup ) {
	if( show && idWindowPopup ) {
		lsaWindowPopupId = idWindowPopup;
		lsaWindowBackgroundCreate();
	}
	
	if( lsaWindowPopupId ) {
		if( show ) {
			lsaWindowPopupResize();
		
			$('div#'+ lsaWindowPopupId ).show();
			$('div#'+ lsaWindowBackgroundId ).show();
			
			$('#top_srodek').hide();
		}
		else {
			$('div#'+ lsaWindowPopupId ).hide();
			$('div#'+ lsaWindowBackgroundId ).hide();
			
			$('#top_srodek').show();			
		}
	}
}

function lsaWindowBackgroundCreate() {
	if( $('div#'+ lsaWindowBackgroundId ) ) {
		var background = $('<div>');
		background.attr( 'id', lsaWindowBackgroundId );
		$('body').prepend( background );
	}
}


/**
 * LSA Service: Window Popup Resize Controller
 */
function lsaWindowPopupResize() {
	if( lsaWindowPopupId ) {
		var element = $('div#'+ lsaWindowPopupId );
		var top = ( $(window).height() - element.height() ) / 2;
		if( top < 0 ) top = 0;
		
		var left = ( $(window).width() - element.width() ) / 2;
		if( left < 0 ) left = 0;
		
		// Exception for window popup for Map
		if( lsaWindowPopupId != 'lsaWindowPopupMap' ) {
			element.css( 'top', top );
		}
		
		element.css( 'left', left );
		
		var background = $('div#'+ lsaWindowBackgroundId );
		background.css( 'width', $(document).width() +'px' );
		
		var backgroundHeight = $(document).height();
		if( backgroundHeight < element.height() ) backgroundHeight = element.height() + 100;
		background.css( 'height', backgroundHeight +'px' );	
	}	
}
	
$(document).ready(function() {
	lsaWindowPopupResize();
});

$(window).resize(function() {
	lsaWindowPopupResize();
});


/**
 * LSA Service: Ajax & Form
 */
function lsaAjaxUrlPrepare( url ) {
	var ajaxUrl = url;
	if( ajaxUrl.indexOf( '?' ) == -1 ) {
		ajaxUrl += '?';
	}
	else {
		ajaxUrl += '&';
	}
	ajaxUrl += 'ajax=1&time='+ new Date().getTime();
	return ajaxUrl;
} 
 
function lsaAjax( element, url, data, questionary ) {
	
	$( element ).html( lsaAjaxMessageLoading );
	
	$.ajax({ 
		contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
		type: 'post',
		url: lsaAjaxUrlPrepare( url ),
		data: data,
		error: function() {
			console.log( 'System Ajax Extended - Failed!' );
		},
		success: function( content ) {
		
			if( content == 'false' ) {
				return false;
			}
			else {
		
				$( element ).html( content );
				
				if( questionary ) {
					lsaServiceQuestionaryPage( 0 );
				}
				
			}
			
		},
		complete: function( request, status ) {
		
			// Status: 500
			if( status == 'error' ) {
				$( element ).html( 'Wystąpiły problemy z połączeniem do serwera.' );
			}
			
		}
	});
}

function lsaAjaxFormPrepareData( form ) {
	var data = '';
	
	$(form).find('input, fieldset:not(.buttonList) input, textarea').each(function(index) {
		if( $(this).attr('id') ) {
			var id = $(this).attr('id');
			
			if( $(this).attr('class') == 'radiobox' || $(this).attr('class') == 'checkbox' ) {
				
				if( $(this).attr( 'checked' ) == true ) {
					data = data +'&'+ id +'='+ encodeURIComponent( $(this).val() );
				}
			}
			else {
				data = data +'&'+ id +'='+ encodeURIComponent( $(this).val() );
			}
			
		}
	});
	
	$(form).find('select option:selected').each(function() {
		if( $(this).parent().attr('id') && $(this).val() ) {
			var id = $(this).parent().attr('id');
			data = data +'&'+ id +'='+ $(this).val();
		}
	});
	
	return data;
}


/**
 * LSA Service: Cookies
 */
function lsaServiceSetCookie( id, value, days ) {
	if( days ) {
		var date = new Date();
		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
		$.cookie( id, value, {expires: date} );
	}
	else {
		$.cookie( id, '', {expires: ''} );
	}
}

function lsaServiceGetCookie( id ) {
	return $.cookie( id );
}


