/* Functions for Navigation */
var menu = 
[
[
["DashBoard","","home","Strona główna"],
["Profile","","profile","Profil"],
["Profile/Write","","updateprofile","Edytuj profil"],
["Anecdote","","anecdote","Anegdoty"],
["Settings","","settings","Ustawienia"]
]
,
[
["Tree","","treeview","Widok drzewa"],
["Family","","treelistview","Widok listy"],
["FamilyHistory","","familyhistory","Historia rodziny"],
["NoticeBoard","","noticeboard","Tablica ogłoszeń"]
]
,

[
["Friends","","friendslist","Przyjaciele"],
["Friends/Pending","","pendingfriendslist","Oczekują"],
["Friends/Invited","","invitedfriendslist","Zaproszony"],
["","cSInvPopup()","","Zaproś znajomych"]
]
,
[
["Neighborhood","","myneighbourhood","Moje sąsiedztwa"],
["Neighborhood/Explore","","addneighbours","Poznawaj / Twórz Sąsiedztwa"],
["Neighborhood/ListNeighbours","","listneighbours","Lista Sąsiedztw"]
]
,
[
["Message","","messages","Wiadomości"]
]
,
[
["Photos/YourPhotos","","yourphotos","Twoje Zdjęcia"],
["Photos/YourAlbums","","album","Twoje Albumy"],
["Photos/PhotosInTree","","photosintree","Zdjęcia wg. Rodzin"],
["Photos/FriendsPhotos","","friendsphotos","Zdjęcia wg. Znajomych"],
["Photos/Write/Upload","","upload","Dodaj Zdjęcie"]
]
,
[
["TreeSelect","","treelist","Wybierz drzewo"]
]
,
];
var menuId = new Array('navhome','navfamily', 'navfriends', 'navneighbours', 'navmessages', 'navphotos', 'navtrees');

var current;

var note = {
	timer : null,
	activeNote : null,
	noteType : null,

	show : function(type) {
		var note_div = type;
		this.hold();
		if (this.activeNote != null) {
			if(this.noteType == 1){
				this.activeNote = $(note_div);
				this.instantHide();
			}
			else this.instantHide();
		}
		this.activeNote = note_div;
		if (this.activeNote != null ){
			if(this.noteType == 1){
				this.activeNote = $(this.activeNote);
				this.activeNote.style.display = "block";
			}
			else getMenu(this.activeNote);
		}
		else throw new Error("'this.activeNote' jest NULL kiedy nie powinno.");
		this.hold();
	},

	hide : function() {
		this.activeNote != null ? this.timer = setTimeout("note.instantHide();", 100) : false;
	},

	instantHide : function() {
		if(note.noteType == 1){
			if (note.activeNote != null) {
				note.activeNote.style.display = 'none';
				note.activeNote = null;
			}
		}
		else{
			if (note.activeNote != null && others != 1) {
				getMenu(module);
				if(module != note.activeNote) $(menuId[note.activeNote]).removeClass('current');
				note.activeNote = null;
			}
			if(others == 1){
				$(menuId[note.activeNote]).toggleClass('current');
				if($(menuId[current]).hasClass('current')) $(menuId[current]).removeClass('current');
				$('navigation2').innerHTML = '';
			}
		}
	},

	hold : function() {
		this.timer != null ? clearTimeout(this.timer) : false;
	}
}

function myAnalytics(path){
	if(typeof(urchinTracker) == 'function') urchinTracker(path);
}

function buttonClicked(id, type){
	var pdata = 'id='+id+'&type='+type;
	var url = baseUrl+'Home/buttonClicked';
	new Ajax(url, {method: 'post', data: pdata}).request();
}

function getMenu(type){
	view = '';
	for(i=0; i<menu[type].length; i++){
		for(j=0; j<menu[type][i].length; j++){
			switch(j){
				case 0: view += '<a href="' + baseUrl + menu[type][i][j] + '" '; break;
				case 1: view += menu[type][i][j] != "" ? 'onclick="' + menu[type][i][j] + '; return false"' : ''; break;
				case 2: view += (menu[type][i][j] == submodule && others != 1)  ? 'class="nav2"' : ''; view += '>'; break;
				case 3: view += menu[type][i][j] + '</a>&nbsp; | &nbsp;'; break;
			}
		}
	}
	if(view != ''){
		view = view.substring(0,view.length-19);
	}
	if(others != 1){
		if(type != module){
			$(menuId[type]).toggleClass('current');
			$(menuId[module]).toggleClass('current');
		}
		else{
			if(!$(menuId[module]).hasClass('current')) $(menuId[module]).addClass('current');
			if($(menuId[current])){
				if(current != module){
					if($(menuId[current]).hasClass('current')) $(menuId[current]).removeClass('current');
				}
			}
		}
	}
	if(others == 1){
		$(menuId[type]).toggleClass('current');
	}
	$('navigation2').innerHTML = view;
}

function nodeInvite(uid, email, c, m){
	function em(e){
		e = prompt('To nie jest poprawny adres email! Chcesz spróbować inny?', e);
		if(e && !is_email(e)) return em(e);
		return e;
	}
	if(c) {if(!is_email(email)) email = em(email); if(!email) return false;}
	var url = baseUrl+'Tree/NodeInvite/'+uid;
	ndata = 'email='+email;
	if(m) ndata += '&m=1';
	var an = new Ajax(url, {method: 'post', data: ndata, evalResponse: true}).request();
	myAnalytics('/Tree/NodeInvite/'+uid);
}

function addNode(relation, uid, ndata){
	var url = baseUrl+'Tree/AddNodes/'+relation+'/'+uid;
	var an = new Ajax(url, {method: 'post', data: ndata, evalResponse: true}).request();
	myAnalytics('/Tree/AddNodes/'+relation+'/'+uid);
}

function editNode(uid, ndata){
	var url = baseUrl+'Tree/UpdateNode/'+uid;
	var en = new Ajax(url, {method: 'post', data: ndata, evalResponse: true}).request();
	myAnalytics('/Tree/UpdateNode/'+uid);
}

function deleteNode(uid){
	var c = confirm('Ta osoba zostanie usunięta z twojego drzewa rodzinnego! Jesteś tego pewny?');
	if(c) {
		flashProxy.call('callWait');
		var del = new Ajax(baseUrl+'Tree/DeleteNode', {method: 'post', data: 'perspectiveUid='+uid, evalResponse: true}).request();
		myAnalytics('/Tree/DeleteNode');
	}
}

function alt(feed){
	alert(feed);
}

function showStream(url, path, set){
	sendData(url, '', 0);
	base = photoSet;
	count = base.length;
	writer = '';
	prevSet = set - 1;
	nextSet = set + 1;
	for(i=0; i<count; i++){
		writer+="<a href='"+path+base[i]['pid']+"'><img class='photos' border='0' src='"+base[i]['pic_80_80']+"' alt='"+base[i]['caption']+"'  title='"+base[i]['caption']+"' width='50' height='50' /></a>";
	}
	$('streamBox').innerHTML = writer;
}

function addEventBase(obj,type,fn,name_hash){
	if(obj.addEventListener)
obj.addEventListener(type,fn,false);else if(obj.attachEvent)
{obj["e"+type+fn+name_hash]=fn;obj[type+fn+name_hash]=function(){obj["e"+type+fn+name_hash](window.event);}
obj.attachEvent("on"+type,obj[type+fn+name_hash]);}
}
function removeEventBase(obj,type,fn,name_hash){
	if(obj.removeEventListener)
obj.removeEventListener(type,fn,false);else if(obj.detachEvent)
{obj.detachEvent("on"+type,obj[type+fn+name_hash]);obj[type+fn+name_hash]=null;obj["e"+type+fn+name_hash]=null;}
}
function placeholderSetup(id){
	var el=$(id);if(!el)return;var ph=el.getAttribute("placeholder");if(ph&&ph!=""&&el.value.trim()==""){el.value=ph;el.style.color='#777';el.is_focused=0;addEventBase(el,'focus',placeholderFocus);addEventBase(el,'blur',placeholderBlur);}
	else if(el.value!="") addEventBase(el,'focus',el.select);
}
function placeholderFocus(){
	if(!this.is_focused){this.is_focused=1;this.value='';this.style.color='#000';var rs=this.getAttribute("radioselect");if(rs&&rs!=""){var re=$(rs);if(!re){return;}
if(re.type!='radio')return;re.checked=true;}}
}
function placeholderBlur(){
	var ph=this.getAttribute("placeholder")
if(this.is_focused&&ph&&this.value==""){this.is_focused=0;this.value=ph;this.style.color='#777';}
}

function placeholderSetupAll(list){
	for(var i=0;i<list.length;i++) placeholderSetup(list[i])
}
function phValueClean(id){
	var phv = $(id).getProperty('placeholder');
	if(phv && phv == $(id).getValue()) $(id).value = '';
}
function phValueCleanAll(list){
	for(var i=0;i<list.length;i++) phValueClean(list[i])
}

function is_email(email){return /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/i.test(email);}

function print_r(inobj)
{
op = window.open();
op.document.open('text/plain');
op.document.write('<pre>');
for (objprop in inobj)
{
op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
}
}

function flashSync(){}

/*
if(typeof('initLytebox') == 'function') window.addEvent('domready', function(){
	initLytebox();
});
*/

if (window.addEvent) {
	window.addEvent('domready', function(){
		var Tips1 = new Tips($$('.Tips1'), {
			maxTitleChars: 80,
			initialize:function(){
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
			},
			onShow: function(toolTip) {
				this.fx.start(1);
			},
			onHide: function(toolTip) {
				this.fx.start(0);
			}
		});
	});
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function addScript(scriptUrl){
	var script = document.createElement('script');
	script.setAttribute('type','text/javascript');
	script.setAttribute('src', scriptUrl);
	var head = document.getElementsByTagName('head')[0];
	head.appendChild(script);
	myAnalytics(scriptUrl.split('.com')[1]);
}

function sendData(url, d, m){
	if(m == 1) { m = 'POST'; }
	else { m = 'GET'; data = '' }
 	var ajx = new Ajax(url, {method: m, data: d, evalResponse: true, onSuccess: function(){closeLoadingBox('loadBox')}, onRequest: function(){loadingBox(1, 'Loading...', 'body', 'loadBox')} }).request();
 	myAnalytics(url.split('.com')[1]);
}

var flashPhotoUpload = 0;
function photoUploadTree(nodeuid, feeduid, name){
	//alert(nodeuid+'-'+feeduid+'-'+name);
	//$('nodeuid').value = nodeuid;
	document.profilePhotoForm.action = baseUrl+'Profile/Write/personal/'+nodeuid;
	$('redirect').value = baseUrl+'Tree/Index/'+feeduid+'/'+nodeuid;
	$('photoname').setHTML(name);
	if(flashPhotoUpload == 0) {
		flashTopSlide.toggle();
		flashPhotoUpload = 1;
	}

}

function photoUploadTreeClose(){
	var tog = 0;
	if(flashPhotoUpload) tog = 1;
	flashPhotoUpload = 0;
	if(tog) flashTopSlide.toggle();
}

///// tools///// probably not using any more
function html(element) {
	this.element = element; this.content = '';this.attributes = '';
}
html.prototype.attributes = function(arr) { // arguments eg: 'id="abc"','name="abc"','class="cl"'.....,'onclick="func()"'
	var attr='';
	for (var i = 0; i < arr.length; i++) {
		attr += ' '+arr[i]+' ';
	}
	this.attributes = attr
}
html.prototype.body = function(content) {
	this.content = content
}
html.prototype.generate = function() {
	this.outerHtml = '<'+this.element+this.attributes+'>';
	this.outerHtml += this.content+'</'+this.element+'>';
	return this.outerHtml;
}
//html.prototype.input

function newInput(id,name,value,onclick,type,extra,cls){
	var click='', attr='', clas='';
	if(extra) attr = extra;
	if(!type) type = 'button';
	if(cls) clas = ' class="'+cls+'" ';
	if(onclick) click ='onClick="'+onclick+'"';
	var str = '<input type="'+type+'" id="'+id+'" name="'+name+'" value="'+value+'" ' +click+ ' ' +attr+ clas +' />';
	return str;
}

function putHtml (id,data) {
	$(id).innerHTML += data;
}

function infrontHtml (id,data) {
	$(id).innerHTML = data + $(id).innerHTML;
}

function sytop (id) {
	return $(id).style.top;
}

function syleft (id) {
	return $(id).style.left;
}


function spliter(data){
	data = data.split("&");
	newdata = new Array();
	for(i=0; i<data.length; i++){
		newdata[i] = new Array();
		newdata[i] = data[i].split("=");
	}
	return newdata;
}

function sv(type, data, field, fieldvalue, f){
	data = spliter(data);
	val = '';
	val = looper(type, data, field, fieldvalue, f);
	return val;
}

function looper(type, data, field, fieldvalue, f){
	val = '';
	for(i=0; i<data.length; i++){//alert(data[i][0]);
		if(data[i][0] == type){
			switch(field){
				case 'text' : val = 'value = "'+data[i][1]+'"'; if(f)val=data[i][1]; break;
				case 'radio': val = (fieldvalue == data[i][1] && data[i][1] == 1) ? 'checked' : ''; break;
				case 'check': val = data[i][1] == 1 ? 'checked' : ''; break;
			}
		}
	}
	return val;
}

function DateGood(y, m, d){
    var D;
    with (D = new Date(y, --m, d)) {
        return getMonth() == m && getDate() == d ? D : NaN;
    }
}

function TimeGood(S){
    return /^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$/.test(S);
}

function HMStoSec(T){
    var A = T.split(/\D+/);
    return (A[0] * 60 + + A[1]) * 60 + + A[2];
}

function testTime(T1, T2){
	if(HMStoSec(T1) <= HMStoSec(T2))
		return true;
	else
		return false;
}
///// tools///// probably not using any more