 	$(document).ready(function() {
	
	
	if(location.href.toString() == "http://www.turyzm.edu.pl/") {
		history.replaceState('', "IGMiT ", "?state=latest");	
	} 		
 		
 		
	//Test przeglądarki
	$("#browserTester").dialog({
		width: 500,
		modal: true,
		buttons: {
			"Chrome": function() {
				window.open("https://www.google.com/chrome?hl=pl");
			},
			"Mozzila Fire Fox": function() {
				window.open("http://www.mozilla.org/pl/firefox/new/");
			},
			"Opera": function() {
				window.open("http://www.opera.com/browser/");
			},
			"Internet Explorer": function() {
				window.open("http://windows.microsoft.com/pl-PL/internet-explorer/products/ie/home");
			}	
		},
		autoOpen: false
	}); 		

	if($.browser.msie){
		userAgent = $.browser.version;
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));	
		if(userAgent <= 7) {
			$("#browserTester").dialog("open");
		}
	}	
	
	if($.browser.mozilla){
		if($.browser.version <= 4) {
			$("#browserTester").dialog("open");
		}
	} 	
 		
	//Nagłówek
	$("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
     $("#featured").removeAttr("class");
     $("#featured ui-tabs-nav").removeClass("ui-widget-header");
     $("#fragment-1, #fragment-2, #fragment-3, #fragment-4").removeClass("ui-widget-content ui-corner-bottom");
	//Event trigger
	$(".normalLink").click(function() {
		window.location = "konferencje.php";
	});
	$(".tourismOnClick, .geoOnClick").parent().parent().parent().find("img").click(function() {
		$(".clicked").removeClass("clicked").css({
				opacity: "0.85",
				width: "-=50px",
				left: '0px',
				background: "#222222 url('images/stabilebox_bck.png') no-repeat right"
		});
		$("#faculties").css({
			opacity: "1",
			width: "299px",
			position: "relative",
			left: '-50px',
			background: "#222222"
		}).addClass("clicked");
		var target = $(this).attr("id");
		$("#news").empty().append("<div class='simpleInner'>Ładowanie treści...</div>");
			$.ajax({
			  type: "POST",
			  url: "views/init/" + "faculties" +".php",
			  cache: false,
			  statusCode: {
			    404: function() {
			       $("#news").empty().append("<div class='simpleInner'>Nie odnaleziono wskazanego modułu.</div>");
			    }
			  },
			  success: function(data){
			   $("#news").empty().append(data);
			   $("#accordion").accordion()
			   if(target == "geoOnClick"){
					$( "#tabs" ).tabs( "option", "selected", 1);
				}
			  }
			 }); 			
	});      
           
           
           
           
     //Animacja menu
    $("#main-nav li a.main-link").click(function(){
		$("#main-nav li a.close").fadeIn();
		$("#main-nav li a.main-link").removeClass("active");												 
		$(this).addClass("active");										 
		$("#sub-link-bar").css({
			borderColor: "#0972A5",
			height: "40px"					   
		}, 300);
		$(".main-link").css({
			opacity: "1"				   
		});
		$(".sub-links").hide();
		$(this).siblings(".sub-links").fadeIn(1000);
	});

	$("#main-nav li a.close").click(function(){
		//$("#main-nav li a.main-link").removeClass("active");												 									 
		$(".sub-links").fadeOut();
		$("#sub-link-bar").css({
			borderColor: "#dcdcdc",
			height: "10px"			   
		});	
		$(".main-link").css({
			opacity: "1"				   
		});
		$("#main-nav li a.close").fadeOut();
	});
	
	

	$(".stabileBox").hover(function(){
		if($(this).hasClass("clicked")) {
		
		} else {
			$(this).css({
				opacity: "1",
				width: "299px",
				position: "relative",
				left: '-50px',
				background: "#222222"
			});	
		}
	}, function() {		
		if($(this).hasClass("clicked")) {
			
		} else {
			$(this).css({
				opacity: "0.85",
				width: "249px",
				position: "relative",
				left: '0px',
				background: "#222222 url('images/stabilebox_bck.png') no-repeat right"
			});	
		}
	});	
	

	
	$(".stabileBox").click(function() {
		var target =  $(this).attr("id");
		var preciseTarget = "?state=" + $(this).attr("id");
			$("#news").empty().append("<div class='simpleInner'>Ładowanie treści...</div>");

			$.ajax({
			  type: "POST",
			  url: "views/init/" + target +".php",
			  cache: false,
			  statusCode: {
			    404: function() {
			       $("#news").empty().append("<div class='simpleInner'>Nie odnaleziono wskazanego modułu.</div>");
			    }
			  },
			  success: function(data){
			   $("#news").empty().append(data);
			   //var currentPage = window.history.state;		
			   if(target == "latest"){
					 readyLatest()		   
			   } else if (target == "pracownicy") {
			   	bulidEmployeesList()
			   } else if(target == "student_info") {
			   	readyStudentInfo()
			   } else if(target == "galeria") {
			   	initGallery() 
			   } else if (target == "wirtual_schedule") {
			   	initWirtualSchedule()
			   } else if(target == "sekretariat") {
			   	$(".tip").tipTip();
			   } else if(target == "employee_info") {
			   	readyEmployeeInfo()
			   } else if(target == "parnterLinks") {
			   	linkToPartner()
			   } else if(target =="scientificActivities" || target =="educationalActivities") {
					pageActivities()			   	
			   } else if(target == "faculties") {
			   	tabs()
			   } else if (target == "feed_module") {
			   	feedAdd()
			   } else if(target == "events") {
			    readyEventsInfo()
			   } else if(target == "gov"){
					buildGov()		
				}	   
			  } 
			});	
				
		 var url = location.href.toString();
			   
			   function checkUrl() {
			   	if(url == "http://www.turyzm.edu.pl/")
			   	return "index.php";
			   	else
			   	return "";
			   }

		history.pushState('', "IGMiT "+target, "?state="+ target);			 
		 
		if($(this).hasClass("clicked")) {
			
		} else {
			$(".clicked").removeClass("clicked").css({
				opacity: "0.85",
				width: "-=50px",
				left: '0px',
				background: "#222222 url('images/stabilebox_bck.png') no-repeat right"
			});
			$(this).addClass("clicked");
		}

	});
	
	
	window.onpopstate = function(event) {	
		//Przejęcia previous
		var url = location.href.split("?state=");
		if(url[1] != null) {
			//console.debug(url[1])
			target = "#" + url[1];
			$(".clicked").removeClass("clicked").css({
				opacity: "0.85",
				width: "-=50px",
				left: '0px',
				background: "#222222 url('images/stabilebox_bck.png') no-repeat right"
			});
			$(target)
		//	.click()
			.addClass("clicked")
			.css({
					opacity: "1",
					width: "299px",
					position: "relative",
					left: '-50px',
					background: "#222222"
				});	
				
				$.ajax({
			  type: "POST",
			  url: "views/init/" + url[1] +".php",
			  cache: false,
			  statusCode: {
			    404: function() {
			       $("#news").empty().append("<div class='simpleInner'>Nie odnaleziono wskazanego modułu.</div>");
			    }
			  },
			  success: function(data){
			   $("#news").empty().append(data);
			   //var currentPage = window.history.state;		
			   if(url[1] == "latest"){
					 readyLatest()		   
			   } else if (url[1] == "pracownicy") {
			   	bulidEmployeesList()
			   } else if(url[1] == "student_info") {
			   	readyStudentInfo()
			   } else if(url[1] == "galeria") {
			   	initGallery() 
			   } else if (url[1] == "wirtual_schedule") {
			   	initWirtualSchedule()
			   } else if(url[1] == "sekretariat") {
			   	$(".tip").tipTip();
			   } else if(url[1] == "employee_info") {
			   	readyEmployeeInfo()
			   } else if(url[1] == "parnterLinks") {
			   	linkToPartner()
			   } else if(url[1] =="scientificActivities" || target =="educationalActivities") {
					pageActivities()			   	
			   } else if(url[1] == "faculties") {
			   	tabs()
			   } else if (url[1] == "feed_module") {
			   	feedAdd()
			   } else if(url[1] == "events") {
			    readyEventsInfo()
			   } else if(url[1] == "gov"){
					buildGov()		
				}	   
			  } 
			});	
		}	else {
			//window.location.href = "index.php";
		}
	}
	
	//ToolTip
	$(".tip").tipTip();
	$(".partnerBox").tipTip();
	//mapa google
	$(".mapAnchor").click(function() {
		window.open("http://g.co/maps/w2ucv");
	});
			
	//Acordion na start latest
	readyLatest()	
     
     
     
       //box
 $("#boxHover").hover(function(){
 	$(this).find("#box").animate({
 	left: '+=175'	
 		
 	});
 	},
 
 function(){
 	$(this).find("#box").animate({
 	left: '-=175'	
 		
 	});
 	});
 	
 	
 	if($("#konsultacjeInfo").hasClass("fetchData")) {
 	var target = $("#employeeId").val();
 	var pureTarget = target.replace("employee", "");
 	$.ajax({url: '../json_files/pracownicy.json',
			cache:false,
			dataType: 'json',
					  success: function(data) {
								$("#konsultacjeInfo").html(data[pureTarget].konsultacje_1 + "<br>"+ data[pureTarget].konsultacje_2);
					 }
			});
		}
	//END
 });
 
 
 	//Aktualności - wyzwalanie accordion
	function readyLatest() {
		$( "#accordion" ).accordion({
				autoHeight: false
				//animated: false
		});
	}
	
//Ogłoszenia dla studenta - wyzwalanie accordion
	function readyStudentInfo() {
		//$( "#accordion_student_info" ).empty();
		// usunąć empty gdy Bartek wpisywać będzie treść ręcznie
			$.ajax({
				url: 'json_files/studentInfoRemote.txt', 
				dataType: 'json',
				
				success: function(data) {
				if(data != null) {
					$.each(data.comments, function() {
						var mainDate= new Date(this.dateP.substring(3, 24));
				//	alert(this.dateP.substring(3, 24))
						var year = mainDate.getFullYear();
				 		var month = mainDate.getMonth()+1;
				 		if(month<10){
				 			month='0'+month
				 		}
						var day = mainDate.getDate();
						if(day<10){
				 			day='0'+day
				 		}
				 		maxDate = new Date();
				 		maxDate.setDate(maxDate.getDate());
				 		//Sprawdzam czy aktualne - termin 1 miesiąc
						actual = maxDate.getMonth() - mainDate.getMonth();
						var i = 1;
						//Formatuję tekst ze spacami i enterami
						var textA = this.text.replace(/(^|\n)/g, function() { 
						var j = i++;			
						if(j >1) {					
							return '<br>'; 
						}	else {
							return ''; 
						}
						});
					//	if(day != 'NaN' ) {
							var finalDate = day+"."+month+"."+year;
								$( "#accordion_student_info" ).prepend(
								"<h3><a href='#'>"+ "<span style='font-size: smaller;'>" + finalDate + "</span><br><strong>"+ this.employeetxt + "</strong></a></h3>" +
								"<div>" +
									"<p class='newsTxt'>" +
									  textA +
									"</p>" +
								"</div>"	
								);
					//	}
					});
					$( "#accordion_student_info" ).accordion({
						autoHeight: false,
						active: false
					});
					//$( "#accordion_student_info h3" ).click();
				}
			},	
			cache: false
			});
			
	}	
	
	function readyEventsInfo() {
		$( "#accordion_events_info" ).accordion({
				autoHeight: false
		});
		$("img").click(function(){
			if($(this).attr("class")!=null) {
				target = $(this).attr("class");
				$.ajax({
				  type: "POST",
				  url: "gallery/" + target + "/galleryfilling.php",
				  statusCode: {
				    404: function() {
				       $("#news").empty().append("Nie odnaleziono wskazanej strony");
				    }
				  },
				  success: function(data){
				   $("#news").empty().append(data);
				  }	 
				});		
			}
		});
	}	
	
	function readyEmployeeInfo() {
		//$( "#accordion_employee_info" ).empty();
		
			$.getJSON('json_files/EmployeeInfoRemote.txt', function(data) {
				if(data != null) {
					$.each(data.comments, function() {
						var mainDate= Date(this.date);
						var mainDate = new Date(mainDate);
						var year = mainDate.getFullYear();
				 		var month = mainDate.getMonth()+1;
				 		if(month<10){
				 			month='0'+month
				 		}
						var day = mainDate.getDate();
						if(day<10){
				 			day='0'+day
				 		}
				 		maxDate = new Date();
				 		maxDate.setDate(maxDate.getDate());
				 		//Sprawdzam czy aktualne - termin 1 miesiąc
						actual = maxDate.getMonth() - mainDate.getMonth();
						var i = 1;
						//Formatuję tekst ze spacami i enterami
						var textA = this.text.replace(/(^|\n)/g, function() { 
						var j = i++;			
						if(j >1) {					
							return '<br>'; 
						}	else {
							return ''; 
						}
						});
						if(actual == 0 || actual == 1 ) {
							var finalDate = day+"."+month+"."+year;
								$( "#accordion_employee_info" ).append(
								"<h3><a href='#'>"+ finalDate + " - " + this.employeetxt + "</a></h3>" +
								"<div>" +
									"<p class='newsTxt'>" +
									  textA +
									"</p>" +
								"</div>"	
								);
						}
					});
					
				}
			});
			//Zmiana 03.01.2012
					$( "#accordion_employee_info" ).accordion({
						autoHeight: false
					});
	}	
	
	function bulidEmployeesList() {
		
		
		$.ajax({
			url: 'json_files/pracownicy.json', 
			dataType: 'json',
			cache: false,
			success: function(data) {
			$("#personBase").empty().append("<option> - Wybierz pracownika - </option>");
			$.each(data, function() {
				$("#personBase").append("<option id='"+ this.id +"'>"+ this.tytul + " " + this.imie + " " + this.nazwisko +"</option>");
			});
			}
			
			
		});
		
			$("#personBase").bind('change keyup', function(){
			var target = $(this).find("option:selected").attr("id");
			//var stateObj = { foo: "index.php" };
			//history.pushState(stateObj, "pracownicy IGMiT", "index.php?"+ target);
			$.ajax({url: 'json_files/pracownicy.json',
			cache:false,
			dataType: 'json',
					  success: function(data) {
						$.each(data, function() {
							if (this.id == target) {
								$("#wiazkaSite").html("<a href='employees_sites/" + this.imie + " " + this.nazwisko + ".php?turystyka=1&hotelarstwo=1&łódź=1&Instytut=1&geografia=1&Uniwerstet%20Łódzki=1&"+ this.przedmioty + "'>" + "Wejdź na stronę (treść i layout w fazie testów)" + "</a>");
								$("#tytul").html(this.tytul);
								$("#imie").html(this.imie);
								$("#nazwisko").html(this.nazwisko);
								$("#stanowisko").html(this.stanowisko);
								$("#pokoj").html(this.pokoj);
								$("#jednostka").html(this.jednostka);
								$("#konsultacjeInfo").html(this.konsultacje_1 + "<br>"+ this.konsultacje_2);
								$("#email").html("<a href='mailto:" +this.email + "'>" + this.email + "</a>");
								$("#telefon").html(this.telefon);
								$("#skype").html(this.skype);
								$("#web").html("<a href='" +this.http + "'>" + this.http + "</a>");
								$("#problematyka").html(this.problematyka_badawcza);
								$("#publikacje").html(this.lista_publikacji);
								$("#przedmioty").html(this.przedmioty);
							}
						});
					}
			});
		});
	}
	
	//Obsługa galerii
	function initGallery(){
		$(".galleryBox").hover(function(){
			$(this).toggleClass("galleryBoxHover",1000);
		}, 
		function(){
			$(this).toggleClass("galleryBoxHover", 300);
		});	
		
		$(".galleryBox").each(function(){
			if($(this).attr("id")){
				target = $(this).attr("id");
				$(this).css("background", "url('gallery/" + target + "/thumbnail.jpg')");
			}	
		});
		
		$(".galleryBox").click(function(){
			target = $(this).attr("id");
			$.ajax({
			  type: "POST",
			  url: "gallery/" + target + "/galleryfilling.php",
			  statusCode: {
			    404: function() {
			       $("#news").empty().append("Nie odnaleziono wskazanej strony");
			    }
			  },
			  success: function(data){
			   $("#news").empty().append(data);
			  }	 
			});		
		});
	}
	
	function initWirtualSchedule() {
		//Wirtual schedule dev
		$(".hiddeninfo").tipTip();
		//Today is
		var today = new Date();
		var weekday=new Array(7);
			weekday[0]="Niedziela";
			weekday[1]="Poniedziałek";
			weekday[2]="Wtorek";
			weekday[3]="Środa";
			weekday[4]="Czwartek";
			weekday[5]="Piątek";
			weekday[6]="Sobota";
			var month=new Array(12);
			month[0]="Styczeń";
			month[1]="Luty";
			month[2]="Marzec";
			month[3]="Kwiecień";
			month[4]="Maj";
			month[5]="Czerwiec";
			month[6]="Lipiec";
			month[7]="Sierpień";
			month[8]="Wrzesień";
			month[9]="Październik";
			month[10]="Listopad";
			month[11]="Grudzień";	
		$("#todayIs").html(weekday[today.getDay()] + ", " + today.getDate() + " " + month[today.getMonth()] + " " + today.getFullYear())	;
		
		$("#scheduleSelector").change( function() {
			$("#wirtualScheduleCont .floorElements").find("img").animate({
	 			opacity: "0" 		
	 		});
	 	});
	}
	
	//Obsługa pola szerszego niż a href
	function linkToPartner() {
		$(".partnerRow").click(function(event) {
			event.preventDefault();
			target =	$(this).find("a").attr("href");
			window.open(target);
		});
	}
	
	//Zmiana stron
	function pageActivities() {
		$(".pageControler span").eq(1).addClass("activePage");
		$(".toPage").click(function() {
			$('html, body').animate({ scrollTop: $(".news_header").offset().top }, 500);
			var target = $(this).attr("id").replace("toPage","");
			$(".activePage").removeClass("activePage");
			$(".divActive").hide().removeClass("divActive");
			$("#page" + target).addClass("divActive").show();
			$("#toPage" + target).addClass("activePage");
		});
		
		$("#next").click(function() {
			if($(".activePage").next().attr("id") != "next") {
				$(".activePage").next().click();
			}
		});
		
		$("#previous").click(function() {
			if($(".activePage").prev().attr("id") != "previous") {
				$(".activePage").prev().click();
			}
		});
	}
	
	function tabs() {
		$( "#tabs" ).tabs();
		$("#accordion").accordion({
			autoHeight: false,
			collapsible: true
			//active: false
		});
	}
	
	//Dodawanie ogłoszeń
	function feedAdd () {
 	$("#feedInfo").dialog({
 		modal: true	,
 		width: 'auto',
 		buttons: {
 			"Akceptuję wyżej wymienione zasady": function() {
 				$(this).dialog('close')
 			}
 		},
 		close: function() {
 			$(this).dialog('destroy');
 			$("#feedInfo").remove();
 		}
 	});		
		
	$("#todaysDate").attr("value", new Date());
		$("#send").click(function() {
			var e = $("#employeetxt");
			var p = $("#passw");
			var c = $("#comment");
			var er = $("#errorMsg");
			if(e.val() == "" || p.val() == "" || c.val() == "") {
				er.html("Uzupełnij wszystkie wymagane pola");
			} else if(c.val().length <30) {
				er.html("Treść ogłoszenia musi zawierać więcej niż 30 znaków.");
			} else {
			er.css("color", "green").html("Ogłoszenie zostało wysłane. Nastąpi odświeżenie strony");	
			var data = "comment=" + document.getElementById("comment").value + "&employeetxt=" + document.getElementById("employeetxt").value + "&todaysDate=" + document.getElementById("todaysDate").value + "&passw=" + document.getElementById("passw").value;
			$.ajax({  
			  	type: "POST",  
			  	url: "json_files/processJsonSend.php",
			  	data: data,
			  	success: function(data) {
			  		
			 	}
			});
			setTimeout(function(){
					location.reload()
			}, 1200 );
			return false;
			}
		});	
	}
	
	function	buildGov() {
		$.getJSON('json_files/pracownicy.json', function(data) {
			$("#struktura td").each(function() {
					if($(this).attr("id") != null) {
						var target = $(this).attr("id").replace("employee", "");
						$(this).attr("title", data[target].tytul + " " + data[target].imie + " " + data[target].nazwisko +
						"<br>Telefon: " + data[target].telefon + "<br>Pokój: " + data[target].pokoj + "<br>Konsultacje:<br>" +
						data[target].konsultacje_1 +"<br>"+ data[target].konsultacje_2 + "<br> "+ 
						data[target].email ); //.tipTip();
					}
				});	
					$(".authority .canBeTipped").tipTip({
						maxWidth: "500px"	
					});
			});	
	}
	
	

