function setLayoutConfig(){
	var post_data = { action: 'getLayoutConfig'};
	    $.ajax({
	      url: '../capi_html/_ajax/ajax.layoutConfig.php',
	      type: "post",
	      dataType: 'json',
	      data: post_data,
	      success: function(data){
		  if(data.success)
		  {
			  $('.menuLeft').css(data.position, '1px');
			  $('#showMenuButton').css(data.position, '1px');
			  $('#projectsList').css(data.position, '76px');

		  }
		  else
			alert(data.error);
	      },
	    error: function(){
	    	  alert('Unknown errror on server!');
	    }
	    });
}

window.ContinueBrokenQuestion = function(url){
	window.location = url;
};

window.openBrokenQuestions = function(idProj, projName){
	link = '_forms/forms.broken_questions.php?idProj=' + idProj + '&projName=' + projName;
	$('#quotaIframe').attr('src',link);
	$('#closeQuotaIframe').css('display', 'block');
	$('#quotaIframe').css('display', 'block');
};



$(document).ready(function(){
	   			var idProj = 0;
	   			var projName = '';
	   			var userName;
	   			var id_site;

	   			$('.button').css('height','50px');
	   			$('.emptyButton').css('height','50px');
////   			var supportsOrientationChange = "onorientationchange" in window,
////   		    orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
////
////
////			if(supportsOrientationChange)
////			{
////	   			showMenu = function(){
////	   				if(window.orientation == 0)
////	   		    	{
////		   		    	$('.menuLeft').slideUp();
////		   		    	$('.menuTop').slideDown();
////	   		    	}
////	   		    	else
////   		    		if(window.orientation == 90 || window.orientation == -90)
////   		    		{
////   		    			$('.menuLeft').slideDown();
////   		    			$('.menuTop').slideUp();
////   		    		}
////	   			}
////	   			showMenu();
////			}
////			else
////			{
////				$('.menuTop').slideUp();
////			}
////
////   			window.addEventListener(orientationEvent, function() {
////   				showMenu();
////   			}, false);

	   			setLayoutConfig();

	   			$('#startProjectButtonSurvey').click(function(){
	   				startProject('../capi_html/_ajax/ajax.project-actions.php');
				});

	   			$('#startProjectButton').click(function(){
	   				startProject('_ajax/ajax.project-actions.php');
				});

	   			var startProject = function(ajaxLink, testSurvey){

						  if(typeof testSurvey === 'undefined') testSurvey = false;
//	   				if(idProj == 0 || projName == '')
//		   			{
//			   			alert('No project is selected! Select one first!');
//			   			return;
//		   			}
		   			$('#projectsList').slideUp();
					$('.menuLeft').slideUp('slow', function(){
						$('#showMenuButton').css('display','block');
					});
					var post_data = { action: 'start-new', id_project: idProj };
		   		    $.ajax({
		   		      url: ajaxLink,
		   		      type: "post",
		   		      dataType: 'json',
		   		      data: post_data,
		   		      success: function(data){
						  if(data.success)
						  {
							  //$('#projectIframe').attr('src',data.link);
							  //alert(data.link);
							  //alert(data.name);
							  userName = data.name;
							  if(testSurvey)
									document.location = data.link + '&capiTablet=1&test=true';
						 	 	else
								 	document.location = data.link + '&capiTablet=1';
						  }
						  else
							alert(data.error);
		   		      }
		   		    });
	   			};

				$('#startTestSurveyButton').click(function(){
					startProject('_ajax/ajax.project-actions.php', true);
				});

	   			$('#liveUpdateButtonSurvey').click(function(){
	   		 		doLiveUpdate('../capi_html/_forms/forms.live-update.php');
				});

	   		 	$('#liveUpdateButton').click(function(){
	   		 		doLiveUpdate('_forms/forms.live-update.php');
				});

		   		 doLiveUpdate = function(link){
			   		 	var post_data = {id_project: idProj };
						    $.ajax({
						      url: link,
						      type: "post",
						      dataType: 'json',
						      data: post_data,
						      success: function(data){
								    $('#liveUpdateIframe').attr('src',data.link);
								    $('#closeLUIframe').css('display', 'block');
								    $('#liveUpdateIframe').css('display', 'block');
						      },
						      error: function(error){
						    	  alert(error.responseText);
						      }
						    });
		   		 	};

		   		$('#quotasButton').click(function(){
		   			link = '_forms/forms.quota_html.php?idProj=' + idProj + '&projName=' + projName;
		   			$('#quotaIframe').attr('src',link);
		   			$('#closeQuotaIframe').css('display', 'block');
				    $('#quotaIframe').css('display', 'block');
				});

				$('#brokenQuestions').click(function(){
					window.openBrokenQuestions(idProj, projName);
/*
		   			link = '_forms/forms.broken_questions.php?idProj=' + idProj + '&projName=' + projName;
		   			$('#quotaIframe').attr('src',link);
		   			$('#closeQuotaIframe').css('display', 'block');
				    $('#quotaIframe').css('display', 'block');*/
				});

				$("#infoButton").click(function(event) {
					event.preventDefault();
					link = '_forms/forms.project_information.php';
					$("#quotaIframe").attr('src', link);
					$('#closeQuotaIframe').css('display', 'block');
				    $('#quotaIframe').css('display', 'block');
				});




	   			$('.button#cancelProjectButton').click(function(){
	   	            //alert('gt');
	   	            jQuery.alerts.okButton = '  Yes  ';
	   	            jQuery.alerts.cancelButton = 'Cancel';
	   	         	//jConfirm('Are you sure u want to cancel the interview?', 'Confirmation', function(r) {
	   	                //if(r)
	   	               // {
	   	               	//20150605 Tanis jConfirm je javljal napako, zato  sem ga nadomestil z default javascript confirm windowam
						var r = confirm("Are you sure you want to cancel the interview?");
						if (r === true)
						{
							id_site = $('input[name=idSite]').val();
	   		   				var post_data = {action: 'interrupt', id_visit: id_site };
	   		   				//alert(id_site);
	   			   		    $.ajax({
	   			   		      url: "../capi_html/_ajax/ajax.project-actions.php",
	   			   		      type: "post",
	   			   		      dataType: 'json',
	   			   		      data: post_data,
	   			   		      success: function(data){
	   							  if(data.success)
	   							  {
	   								  //$('#projectIframe').attr('src',data.link);
	   								  //alert();
	   								  document.location = '../capi_html/index.php';
	   								  idProj = undefined;
	   							  }
	   							  else
	   								alert(data.error);
	   			   		      }
	   			   		    });
						}
						else
						{
							return false;
						}

	   	               // }
	   	               // else
	   	                 //   return false;
	   	           // });
	   	        });


	   			$('#logoutButtonSurvey').click(function(){
	   				doLogOut('../capi_html/index.php?logOut=1');
				});
	   			$('#logoutButton').click(function(){
	   				//alert('out survey');
	   				doLogOut('index.php?logOut=1');
				});

	   		    doLogOut = function(link){
	   				jQuery.alerts.okButton = '  Yes  ';
	   	            jQuery.alerts.cancelButton = 'Cancel';
	   	            //20150605 Tanis jConfirm je javljal napako, zato  sem ga nadomestil z default javascript confirm windowam
	   	            //jConfirm('Are you sure you want to logout?', 'Confirmation', function(r) {
	   	            	var r = confirm("Are you sure you want to logout?");
	   	                if(r)
	   	                {
	   	                	window.location = link;
	   	                }
	   	                else
	   	                	return false;
	   	           // });
	   			};

				$('#projectsButton').click(function(){
					//20160906 Bojan Orter - i changed that project for selection are loaded when button is clicked
					var post_data = { action: 'loadProjects'};
					    $.ajax({
					      url: "_ajax/ajax.project-actions.php",
					      type: "post",
					      dataType: 'json',
					      data: post_data,
					      success: function(data){
					  		if(data.success)
							{
								var projects = data.projects;

								var projectsSelectHtml = "";
								if(data.projects != null)
								{
									projectsSelectHtml = "<ul>";
									for(var projId in projects)
									{
										projectsSelectHtml += "<li class=\"projectSelect\" projId=\"" + projId + "\">" +  projects[projId] + "</li>";
									}
									projectsSelectHtml += "</ul>";
								}
								else
								{
									projectsSelectHtml = "<ul><li>No projects available!</li></ul>";
								}

								$('#projectsList').html(projectsSelectHtml);
								var selectedElement = null;
								if(data.selectedProjectId != null && data.selectedProjectId != "")
								{
									selectedElement = $("[projId='" + data.selectedProjectId + "']");
								}
								$('#projectsList').slideToggle('slow');

								//20160906 Bojan Orter - we move this function so that we always add click function
								$('.projectSelect').click(function(){
									idProj = $(this).attr('projId');
								    projName = $(this).html();

									var nodes = document.getElementById('projectsList').getElementsByTagName('li');

									for(var i=0; i<nodes.length; i++)
										if(nodes[i].attributes.projId.value != idProj)
										{
											nodes[i].style.background = '#CCDBEE';
											nodes[i].style.color =  'black';
										}
										else
										{
											if(nodes[i].style.color == 'white')
											{
												nodes[i].style.background = '#CCDBEE';
												nodes[i].style.color =  'black';
												idProj = undefined;
												projName = undefined;
											}
											else
											{
												nodes[i].style.background ='#ABC7EC';
												nodes[i].style.color = 'white';
											}
										}

								    var post_data = { action: 'change-project', id_project: idProj };
									    $.ajax({
									      url: "_ajax/ajax.project-actions.php",
									      type: "post",
									      dataType: 'json',
									      data: post_data,
									      success: function(data){
									  		if(!data.success)
										  		alert(data.error);
							   		  },
									      error: function(response){
											alert(response.responseText);
							   		  }
									    });
								});
								if(selectedElement != null)
								{
									$(selectedElement).click();
								}
							}
							else
							{
								alert(data.error);
							}

			   		  	  },
					      error: function(response){
							alert(response.responseText);
			   		  }
					});
				});
				$('#newProjectButtonSurvey').click(function(){
					window.location = "../capi_html/index.php";
				});

				$('#hideMenu').click(function(){
					$('#projectsList').slideUp();
					$('.menuLeft').slideUp('slow', function(){
						$('#showMenuButton').css('display','block');
					});
				});

				$('#showMenuButton').click(function(){
					$('#showMenuButton').css('display','none');
					$('.menuLeft').slideDown();
					$('.menuLeft').css('opacity', 1.0);
				});

				$('#closeLUIframe').click(function(){
					$('#liveUpdateIframe').css('display', 'none');
					$(this).css('display', 'none');
				});

				$('#closeQuotaIframe').click(function(){
					$('#quotaIframe').css('display', 'none');
					$(this).css('display', 'none');
				});

	});
