/* 
 (c) 2007 Intersim s.c. dla Ogrody Wspomnien
 all right reserved
*/
var loggedIn = 0;
var wwwdir = '';
var oldtext = '';

window.onload = function () {
	$(".scrollerbp").jCarouselLite({
		visible: 3,
		auto:3000,
		speed:2000,
		circular: true,
		hoverPause: true
	});
}

$(document).ready(function() {

	wwwdir = $("span#wwwdir").html();

$(function() {
  $('.window, .wieniecWin').appendTo('body');
  $('.sendingWin').appendTo('body');
});

$(function() {
  $('.statusWin').appendTo('body');
});

$(function() {
  $('.window').appendTo('body');
});

$(function() {
    $('.txtinput').focus(function() {
      if($(this).attr('value') == $(this).attr('default')) {
          $(this).attr('value', '');
      }
    }).blur(function() {
        if($(this).attr('value').length < 1) {
            $(this).attr('value', $(this).attr('default'))
        }
    }).each(function() {
        if($(this).attr('value').length < 1) {
            $(this).attr('value', $(this).attr('default'))
        }
    });
});
/*
* login form
*/
$(function() {
/*
    $("#loginForm .passFake").focus(function(){
        $(this).hide().next().show().focus();
    }).next().blur(function() {
        if($(this).attr('value')<1)
        $(this).hide().prev().show();
    });
*/
     $("#loginForm .passFake").focus(function(){
        $(this).css('display','none').next().css('display', 'block').focus();
    }).next().blur(function() {
        if($(this).attr('value')<1)
        $(this).css('display','none').prev().css('display', 'block');
    });
});

/*
 * menu
 */
$(function() {
    $('#menu li').hover(function() {
        $(this).find('> ul').fadeIn(200);
        $(this).find('> a').addClass('hover');
    }, function() {
        $(this).find('> ul').fadeOut(200);
        $(this).find('> a').removeClass('hover');
    });
});

	$('.rmore').click(function() {
		var params = $(this).attr('id').split('_');
		$('.hideme_'+params[1]).css('display', 'inline');
		$(this).css('display', 'none');
		return false;
	});

	$('#grupaa').click(function() {
		$('#grupa').css('display', 'block');
		$(this).css('display', 'none');
		return false;
	});

	$('.selbox').selectbox();
	
	$('.agen').click(function() {
		var awin = window.open($(this).attr('href'), $(this).attr('title'), 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=yes, status=yes') 
		awin.focus();
		return false;
	});

	$("input[type=file]").stylefile({ 
		image: wwwdir + "/gf/fchose.jpg",
		imageheight: 14,
		imagewidth: 55,
		width: 163
	});

	//alert('p logged = '+$("p#loggedIn").html());

	if ($("p#loggedIn").html() != null) {
		loggedIn = 1;
	}

/*
	$("#innerSearchWin").jqm( {
		trigger: '.innerSearch'
	});
*/
	$("#makeCoupleWin").jqm( {
		trigger: '.makeCouple'
	});

	$("#addChildrenWin").jqm( {
		trigger: '.addChildren'
	});

	$(".btnSend").click(function() {
		var form = $(this).parents("form");
		//alert('form '+form);
		//alert(dump(form));
		var fid = form[0].id;
		//alert('fid '+fid)
		x = processForm(fid);
		//alert('proces '+x);
		return x;
	});
	
	/*
	$.mask.addPlaceholder('1',"[01]");
	$.mask.addPlaceholder('2',"[12]");
	$.mask.addPlaceholder('3',"[0123]");
	$(".date").mask("39/19/2999");
	*/

	$(".uc").click( function() {
		alert("Kliknięty odnośnik nie jest na ten moment do końca aktywny.");
		return false;
	})

	$(".confirm").click(function() {
		return confirm('Czy na pewno wykonać tę operację?');
	});

	$(".delitem").click(delitem);

	$(".herbit").click(herbit);

	/* formy ogrodow */
    $(".of :input").keyup(function() {
        updateStatus(this.id);
    });

	$(".of :input").change(function() { 
        updateStatus(this.id); 
    });

	$(".of :input").focus(function() {
		var p = $(this).position();
		var pBegin = $("#formInfoBegin").position();
		/*alert(p.top+' '+pBegin.top);*/
		var newPos = p.top - pBegin.top;
		$("#formInfo").css("margin-top", newPos + "px");
		sendInfo(this.id, '#formInfo');
	});

	$(".of :input").click(function() {
		var p = $(this).position();
		if (p.top == 0) {
			/* podmieniony select */
			p = $("#" + this.id + "_input").position();
		}
		var pBegin = $("#formInfoBegin").position();
		var newPos = p.top - pBegin.top;
		$("#formInfo").css("margin-top", newPos + "px");
		sendInfo(this.id, '#formInfo');
	});

	$(".showhide").click(function() {
		var tid = "#sh_" + this.id;

		$(tid).slideToggle('fast');

		return false;
	});

	$('.tt').Tooltip({
		delay: 0,
		showURL: false,
		showBody: " - ",
		extraClass: "pretty",
		fixPNG: true,
		opacity: 0.95
		//left: -120
	});

	if (loggedIn == 0) {
		$("#question").jqm( { 
			trigger: '.lr'
		});

		$(".lr").click(function()  {
			$("#lrf").val(this.href);
		});

	}

	$("#fname").focus();
	$("#imie").focus();

	if (loggedIn != 0) 
	{
	$(".know").click(adddf);
	$(".rknow").click(rknow);
	$(".honor").click(adddh);
	$(".wieniec").click(adddw);
	}

	//$('.selectbox-wrapper').css('display', 'block');

	$(".bgi").bgiframe();

	$(".scrollerb, .scrollerd").jCarouselLite({
		vertical: true,
		visible: 2.9,
		auto:3000,
		speed:2000,
		circular: true,
		hoverPause: true
	});

	$(".scrollerf, .scrollerp").jCarouselLite({
		visible: 3.7,
		auto:1000,
		speed:3000,
		circular: true,
		hoverPause: true
	});

	$('#herbyshow').click(function() {

		if ( $('#herby').is(":hidden") ) {
			$('#herby').slideDown('slow', function() {
				// Animation complete.
				oldtext = $('#herbyshow span:first').text();
				$('#herbyshow span:first').text('ukryć');
			});
		}
		else
		{
			$('#herby').slideUp('slow', function() {
				// Animation complete.
				$('#herbyshow span:first').text(oldtext);
			});
		}

		return false;

	});

	$('#videoshow').click(function() {

		if ( $('#video').is(":hidden") ) {
			$('#video').slideDown('slow', function() {
				// Animation complete.
				oldtext = $('#videoshow span:first').text();
				$('#videoshow span:first').text('ukryć');
			});
		}
		else
		{
			$('#video').slideUp('slow', function() {
				// Animation complete.
				$('#videoshow span:first').text(oldtext);
			});
		}

		return false;

	});

           $("input:radio").mousedown(function(e) {
                if ($(this).attr("checked") == true) {
                   setTimeout("$('input[id=" + $(this).attr('id') + "]').removeAttr('checked');", 200);}
                else {
                    return true
                }
            });

}); 

/* doc ready */

function sendInfo(item, dstSel) {
	var h = (typeof info[item] == 'undefined') ? '' : info[item];
	$(dstSel).html(h);
	if (h.length > 0)
		$(dstSel).show();
	else
		$(dstSel).hide();
}

function changeItemStatus(item, code) {
    var stSel = '#st_'+item;
    if (code == 'ok')
        $(stSel).css('background-image', 'url('+wwwdir+'/gf/ico/yes.gif)');
    else if (code == 'err')
        $(stSel).css('background-image', 'url('+wwwdir+'/gf/ico/no.gif)');
    else
        $(stSel).css('background-image', 'none');
}

function adddf() {
	var zid = $("#zid").val();
	var action = wwwdir + "/ax";

		$('#sendingAdddf').jqm({modal: true}).jqmShow();
		$.ajax({
			dataType: 'json',
			type: "POST",
			url: action,
			data: { c: "adddf", zid: zid },
			complete: function() {
				$('#sendingAdddf').jqmHide();
			},
			error: function(msg) {
				$('#rStatus').html($("#axe").html());
				$('#formStatus').jqm({modal: true}).jqmShow();
			},
			success: function(r, status) {
				if (r.rdr != '') {
					window.location = r.rdr;
				}
				if (r.err == 0) {
					$('#rStatus').html(r.msg);
				}
				else {
					$('#rStatus').html(r.msg);
				}
				if (r.url != '') {
					$(".jqmClose").click(function() {
						window.location = r.url;
					});
				}
				if (r.rdr == '')
					$('#formStatus').jqm({modal: true}).jqmShow();
			}
		});
	
	return false;
}

function rknow() {
	var zid = $("#zid").val();
	var action = wwwdir + "/ax";

		$('#sendingAdddf').jqm({modal: true}).jqmShow();
		$.ajax({
			dataType: 'json',
			type: "POST",
			url: action,
			data: { c: "adddf", zid: zid, act: "rem" },
			complete: function() {
				$('#sendingAdddf').jqmHide();
			},
			error: function(msg) {
				$('#rStatus').html($("#axe").html());
				$('#formStatus').jqm({modal: true}).jqmShow();
			},
			success: function(r, status) {
				if (r.rdr != '') {
					window.location = r.rdr;
				}
				if (r.err == 0) {
					$('#rStatus').html(r.msg);
				}
				else {
					$('#rStatus').html(r.msg);
				}
				if (r.url != '') {
					$(".jqmClose").click(function() {
						window.location = r.url;
					});
				}
				if (r.rdr == '')
					$('#formStatus').jqm({modal: true}).jqmShow();
			}
		});
	
	return false;
}

function adddh() {

	var zid = $("#zid").val();

	var action = wwwdir + "/ax";

		$('#sendingAdddh').jqm({modal: true}).jqmShow();

		$.ajax({
			dataType: 'json',
			type: "POST",
			url: action,
			data: { c: "adddh", zid: zid },
			complete: function() {
				$('#sendingAdddh').jqmHide();
			},
			error: function(msg) {
				$('#rStatus').html($("#axe").html());
				$('#formStatus').jqm({modal: true}).jqmShow();
			},
			success: function(r, status) {
				if (r.rdr != '') {
					window.location = r.rdr;
				}
				if (r.err == 0) {
					$('#rStatus').html(r.msg);
				}
				else {
					$('#rStatus').html(r.msg);
				}
				if (r.url != '') {
					$(".jqmClose").click(function() {
						window.location = r.url;
					});
				}
				if (r.rdr == '')
					$('#formStatus').jqm({modal: true}).jqmShow();
			}
		});
	
	return false;
}

function adddwSend(wieniec) {

	var zid = $("#zid").val();
	var action = wwwdir + "/ax";
	$('#sendingAdddh').jqm({modal: true}).jqmShow();

		$.ajax({
			dataType: 'json',
			type: "POST",
			url: action,
			data: { c: "addwieniec", zid: zid, wieniec: wieniec },
			complete: function() {
				$('#sendingAdddh').jqmHide();
			},
			error: function(msg) {
				$('#rStatus').html($("#axe").html());
				$('#formStatus').jqm({modal: true}).jqmShow();
			},
			success: function(r, status) {
				if (r.rdr != '') {
					window.location = r.rdr;
				}
				if (r.err == 0) {
					$('#rStatus').html(r.msg);
				}
				else {
					$('#rStatus').html(r.msg);
				}
				if (r.url != '') {
					$(".jqmClose").click(function() {
						window.location = r.url;
					});
				}
				if (r.rdr == '')
					$('#formStatus').jqm({modal: true}).jqmShow();
			}
		});

	return false;
}

function adddw() {

		$('#Adddh').jqm({modal: true}).jqmShow();
		$('#Adddh img').click(function() {
			var wieniec = $(this).attr('id').split('_');
			wieniec = wieniec[1];
			$('#Adddh').jqmHide();
			adddwSend(wieniec);
		} );

	return false;
}

/* kul */
function processForm(formid) {
	var formsel = "form#" + formid;
	var d = $(formsel).serialize();
	//alert(d);
	var action = $(formsel).attr("action");
	var err = 0;

	var photo = $(formsel + " #photo");
	if (photo && typeof photo != 'undefined' && typeof $(photo).attr('id') != 'undefined') {
		if ($(photo).val().length > 0) {
			alert('Zostało wybrane zdjęcie, żeby je jednak dodać, należy kliknąć przycisk Dodaj zdjęcie.');
			return false;
		}
	}

		var fi = formsel + " :input";
		$(fi).each(function() {
			if (updateStatus(this.id) == false)
				err++;
		});

        if (err > 0) {
            sendInfo('err1', "#formInfo");
            //window.location = "#" + formid;
            return false;
        }

		$('#sendingForm').jqm({modal: true}).jqmShow();
		$.ajax({
			dataType: 'json',
			type: "POST",
			url: action,
			data: d,
			complete: function() {
				$('#sendingForm').jqmHide();
			},
			error: function(msg) {
				$('#rStatus').html($("#axe").html());
				$('#formStatus').jqm({modal: true}).jqmShow();
			},
			success: function(r, status) {
				if (r.rdr != '') {
					window.location = r.rdr;
				}
				if (r.err == 0) {
					$('#rStatus').html(r.msg);
				}
				else {
					$('#rStatus').html(r.msg);
				}
				if (r.url != '') {
					$(".jqmClose").click(function() {
						if (r.url.match(/#/) && r.url.split('#')[0] == window.location.pathname.split("#")[0]) {
							window.location = r.url;
							window.location.reload(true);
						}
						else
							window.location = r.url;
					});
				}
				else if (r.rfr != '') {
					$(".jqmClose").click(function() {
						window.location.reload(true);
					});
				}
				if (r.rdr == '')
					$('#formStatus').jqm({modal: true}).jqmShow();
			}
		});

		return false;

}

function updateStatus(item) {
    var itemSel = '#'+item;
    var i = $(itemSel);
	var val = $(itemSel).val();
    if (checkItem(i) == true) {
		if (val == 'undefined' || val == null || val == '' || val.langth == 0)
			changeItemStatus(item, 'empty');
		else
			changeItemStatus(item, 'ok');
        return true;
    }
    else {
        changeItemStatus(item, 'err');
        return false;
    }
}


function showtree(){
	window.open('test.html', 'popup','scrollbars=yes, resizable=yes, top=40, left=40, width=fullscreen, height=fullscreen');
}

function delitem() {
	var action = $(this).attr("href");
	var err = 0;
	var params = $(this).attr('id').split('_');
	var mode = params[0];
	var what = params[1];
	var i = params[2];
	var c = mode + "_" + what;

	if (!confirm('Czy na pewno wykonać tę operację? Zmiany są nieodwracalne!'))
		return false;

	if (mode == 'user' && what == 'del' && !confirm('Pamiętaj, że po usunięciu Twojeg profilu użytkownika nie będziesz mógł już edytować profili osób zmarłych i gości, które dodałeś wcześniej. Jeśli chcesz usunąc z bazy danych profile osób zmarłych i gości, które dodałeś musisz to zrobić przed usunięciem swojego profilu użytkownika. W przeciwnym wypadku profile osób zmarłych i gości dodanych przez Ciebie pozostaną w bazie danych Ogrodów Wspomnień, pomimo usunięcia Twojego profilu użytkownika.\n\nKontynuować?'))
		return false;

		$('#sendingForm').jqm({modal: true}).jqmShow();
		$.ajax({
			dataType: 'json',
			type: "POST",
			url: action,
			data: "id=" + i + "&c=" + c,
			complete: function() {
				$('#sendingForm').jqmHide();
			},
			error: function(msg) {
				$('#rStatus').html($("#axe").html());
				$('#formStatus').jqm({modal: true}).jqmShow();
			},
			success: function(r, status) {
				if (r.rdr != '') {
					window.location = r.rdr;
				}
				if (r.err == 0) {
					$('#rStatus').html(r.msg);
				}
				else {
					$('#rStatus').html(r.msg);
				}
				if (r.url != '') {
					$(".jqmClose").click(function() {
						window.location = r.url;
					});
				}
				else if (r.rfr != '') {
					$(".jqmClose").click(function() {
						window.location.reload(true);
					});
				}
				if (r.rdr == '')
					$('#formStatus').jqm({modal: true}).jqmShow();
			}
		});

		return false;

}

function herbit() {
	var action = $(this).attr("href");
	var err = 0;
	var params = $(this).attr('id').split('_');
	var mode = params[0];
	var what = params[1];
	var i = params[2];
	var c = mode + "_" + what;

		$('#sendingForm').jqm({modal: true}).jqmShow();
		$.ajax({
			dataType: 'json',
			type: "POST",
			url: action,
			data: "id=" + i + "&c=" + c,
			complete: function() {
				$('#sendingForm').jqmHide();
			},
			error: function(msg) {
				$('#rStatus').html($("#axe").html());
				$('#formStatus').jqm({modal: true}).jqmShow();
			},
			success: function(r, status) {
				if (r.rdr != '') {
					window.location = r.rdr;
				}
				if (r.err == 0) {
					$('#rStatus').html(r.msg);
				}
				else {
					$('#rStatus').html(r.msg);
				}
				if (r.url != '') {
					$(".jqmClose").click(function() {
						window.location = r.url;
					});
				}
				else if (r.rfr != '') {
					$(".jqmClose").click(function() {
						window.location.reload(true);
					});
				}
				if (r.rdr == '')
					$('#formStatus').jqm({modal: true}).jqmShow();
			}
		});

		return false;

}

