var infoboxheight = 0;
var infoboxheightsmall = 38;
var menuArray = [];
$(document).ready(function() {
		$("#date0").datepick();
		$("#date1").datepick();
		$("#date2").datepick();
		colorMenu();
		$("#firstnavi ul").mouseenter(function(){
			var t = $(this).text().replace(/^\s+|\s+$/g, "");
			if($(this).find("h2").css("background") == "rgb(0, 186, 255)" || $(this).find("h2").css("background")=="#00baff" ||
				$(this).find("h2").css("background") == "rgb(0, 186, 255) none repeat scroll 0% 0%"){
				return;
			}
			$(this).find(".menudisplay").show(0);
			$(this).find("h2").css("background", "#566673");
			$(this).find("h2").css("color", "#ffffff");
			$(this).find(".menu").mouseleave(function(){
				$(this).find(".menudisplay").hide(0);
				$(this).find("h2").css("background", "#ffffff");
				$(this).find("h2").css("color", "rgb(82, 102, 115)");
				colorMenu();
				});
			});
		$("#send").click(function(){
				$("#sendopen").toggle();
				$("#send").toggle();
				});
		$("#sendopen h1").click(function(){
				$("#send").toggle();
				$("#sendopen").toggle();
				$("#emailresult").empty();
				$("#emailresult").css("border", "none");
				$("#emailresult").css("margin", "0px");
				});
		$("#indexcalendarcal").datepick({onSelect: showDate});
		$("#indexcalendarcal").css("height", "135px");
		var option = gupDate();
		var dat = option.split("-");
		$("#kalendarium").datepick({ onSelect: setDate,  showDefault: true, defaultDate: new Date(dat[0], dat[1]-1, dat[2])});
		$("#kalendarium").css("height", "135px");
		$("#kalendarium").css("background", "rgb(221, 221, 221)");
});

function showDate(date){
	document.location.href = "kalendarium"+date+".html";
}

var calendarDate = "";
function setDate(date){
	calendarDate = date;
}

function add(id, inpid){
	var value = $('#id_fselect_'+id).val();
	var text = $('#id_fselect_'+id+' :selected').text();
	$(inpid).append("<div id='"+id+"_"+value+"'><image src=\"patroni/"+id+"/logo.jpg\"/><input type=\"hidden\" name=\""+id+"_"+value+"\" value=\"add\"/> <span style='font-weight: bolder;'>"+text+"</span>&nbsp;&nbsp;&nbsp;<a onclick='deleteItem(\"#"+id+"_"+value+"\", \""+inpid+"\");'>Usuń</a></div>");
}

function deleteItem(id, inpid){
	$(inpid + " " + id).remove();
}

function gup( name )
{
	var loc = window.location.href;
	loc = loc.replace(/.*\/([^0-9]+)([0-9-edn])*\.html/, "$1");
	loc = loc.replace(/(en|de)$/, "");
	return loc;
	/*
	   name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	   var regexS = "[\\?&]"+name+"=([^&#]*)";
	   var regex = new RegExp( regexS );
	   var results = regex.exec( window.location.href );
	   if( results == null )
	   return "";
	   else
	   return results[1];
	   */
}

function gupDate()
{
	var loc = window.location.href;
	loc = loc.replace(/[^0-9]+([0-9-]*)([edn]*)\.html/, "$1");
	return loc;
	/*
	   name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	   var regexS = "[\\?&]"+name+"=([^&#]*)";
	   var regex = new RegExp( regexS );
	   var results = regex.exec( window.location.href );
	   if( results == null )
	   return "";
	   else
	   return results[1];
	   */
}

function gupLanguage(){
	var loc = window.location.href;
	loc = loc.replace(/.+([de][en])\.html/, "$1");
	if(loc == null || (loc != "en" && loc != "de")) return "pl";
	return loc;
}


function setPreview(el){
	var newImg = new Image();
	newImg.src = $(el).attr('src');
	var height = newImg.height;
	var width = newImg.width;
	var out = 0;
	$('#topeximageimage').attr('src', newImg.src);
	if(width > height){
		var nwidth = 333;
		var nheight = (nwidth * height)/width;
		while(nheight > 333){
			--nwidth;
			nheight = (nwidth * height)/width;
		}
		out = "width:" + nwidth + "px";
	}
	else if(width < height){
		var nheight = 333;
		var nwidth = (nheight * width)/height;
		while(nwidth > 333){
			--nheight;
			nwidth = (nheight * width)/height;
		}
		out = "width:" + nwidth + "px";
	}
	else{
		out = "width: 333px";
	}
	$('#topeximageimage').attr('style', out);

}

function colorMenu(){
	var page = gup("page");
	var sel = $(".menu:has('a[href*="+page+"]')");
	sel.css("background", "#00baff");
	sel.find('h2').css("background", "#00baff");
	sel.find('h2').css("color", "#ffffff");
	sel.find('h2 a').attr("style", "background: #00baff; color: #ffffff; height: 1.2em");
	/*
	   var main = "";
	   var sp;
	   for(var i = 0; i < menuArray.length; i++){
	   sp = menuArray[i].split(";");
	   main = sp[0];
	   sp = sp.join(" ");
	   main = main.split(" ");
	   main = main[0];
	   if(sp.match(page)){
	   var sel = $(".menu h2:contains('"+main+"')");
	//if(sel.text().match(main)){
	sel.css("background", "#00baff");
	sel.css("color", "#ffffff");
	sel.addClass("choosen");
	//}
	sel = $(".menu h2 a:contains('"+main+"')");
	//if(sel.text().match(main)){
	sel.attr("style", "background: #00baff; color: #ffffff; height: 13px;");
	//}
	return;
	}
	}
	*/
}

function bmark() {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel("Muzeum Architektury we Wrocławiu", window.location.href,"");
	}
	else if( window.external ) { // IE Favorite
		window.external.AddFavorite("Muzeum Architektury we Wrocławiu", window.location.href); 
	}
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

function sendEmail(href){
	var tx = $("#emailtxid").val();
	if(!echeck(tx)){
		return;
	}
	var rx = $("#emailrxid").val();
	if(!echeck(rx)){
		return;
	}
	var com = $("#emailcomid").val();
	var result = $("#emailresult");
	$.post("lib/php/mail.sender.php", {addr: href, from: tx, to: rx, comment: com},
			function(data){
			if(data == "OK"){
			$("#emailresult").empty();
			$("#emailresult").append("Wiadomość z powodzeniem wysłano.");
			$("#emailresult").css("color", "#ffffff");
			$("#emailresult").css("border", "1px solid white");
			$("#emailresult").css("background", "#6a7f95");
			$("#emailresult").css("font-size", "10px");
			$("#emailresult").css("font-weight", "bolder");
			$("#emailresult").css("padding", "5px");
			$("#emailresult").css("margin", "5px");
			$("#emailresult").css("text-align", "center");
			}
			else{
			$("#emailresult").empty();
			$("#emailresult").append("Wiadomość nie została wysłana z powodu błędu.");
			$("#emailresult").css("color", "#ffffff");
			$("#emailresult").css("border", "1px solid white");
			$("#emailresult").css("background", "#e4003c");
			$("#emailresult").css("font-size", "10px");
			$("#emailresult").css("font-weight", "bolder");
			$("#emailresult").css("padding", "5px");
			$("#emailresult").css("margin", "5px");
			$("#emailresult").css("text-align", "center");
			}
			});

}

function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		alert("Niepoprawny E-mail");
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert("Niepoprawny E-mail");
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Niepoprawny E-mail");
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
		alert("Niepoprawny E-mail");
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Niepoprawny E-mail");
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1){
		alert("Niepoprawny E-mail");
		return false;
	}

	if (str.indexOf(" ")!=-1){
		alert("Niepoprawny E-mail");
		return false;
	}
	return true;
}

function newsletter(){
	var val = $('#newsletter').val();
	$('#newsletter').val("");
	if(echeck(val)){
		$.get("lib/php/newsletter.php?email="+val, function(data){
			if(data != "ok"){
				if(data == "already"){
					alert("Podany e-mail już występuje w bazie");
					return;
				}
				alert("Wystąpił błąd. Proszę poinformować obsługę: admin@ma.wroc.pl");
			}
			else{
				alert("Adres "+val+" dodany do bazy.");
			}
		});
	}
}

function zamowDoCzytelni(id){
	var nazwa = $('#zamow-nazwa-'+id).val();
	var email = $('#zamow-email-'+id).val();
	var data = $('#zamow-data-'+id).val();
	var dodatkowe = $('#zamow-dodatkowe-'+id).val();

	if(echeck(email)){
		$.post("lib/php/czytelnia.php", {id: id, nazwa: nazwa, email: email, data: data, dodatkowe: dodatkowe}, function(data){
			alert(data);
			if(data != "OK"){
				alert("Informacja została wysłana");
			}
			else{
				alert("Wystąpił błąd. Wiadomość nie wysłana");
			}
		});
	}
}

