//
// Context Javascript Code
//

function checkChanges() {
  if (changed)
   return confirm("The changes you made have not yet been saved, do your really want to quit this page?");
}

function fastTimeout(code, waitFor, time, count) {
	if (count > 10) {
		alert("Could not initiliase site. everything ok?");
	}
	var obj = findObj(waitFor);
	if (obj)
		eval(code);
	else
		setTimeout("fastTimeout('"+code+"', '"+waitFor+"', "+time+", "+(count+1)+");", time);
}

function imgPreload(name, imgPre, imgNum, imgPost, imgAct) {
	var imgName = name + imgNum + imgAct;
	var img = new Image();
	img.src = imgPre + imgNum + imgAct + imgPost;
	imgCache[imgName] = img;

	// if (imgAct) imgPreload(name, imgPre, imgNum, imgPost, "");
}

function imgPreloadArray(images, path) {
	for (i=0; i<images.length; ++i) {
	    var img = new Image();
	    img.src = path + images[i];
	}
}

function winOpen(winName, path, feat) {
	if (feat == "full")
		feat = "toolbar=1,location=1,status=1,menubar=1,resizable=1,scrollbars=1";
	var win = window.open(winName, path, feat);
	win.focus();
}

function popup(path, width, height, feat, notcenter) {
	if (!width)
            width = 300;
        if (!height)
            height = 300;
        if (feat == "full")
                feat = "toolbar=1,location=1,status=1,menubar=1,resizable=1,scrollbars=1";
        else 
                feat = "width="+width+",height="+height+(feat ? ","+feat : "");
        if (!notcenter) 
                feat = "left="+(screen.width/2-width/2)+",top="+(screen.height/2-height/2) + ","+feat;
        var win = window.open(path, width+"x"+height, feat);

        win.focus();
}


function help(helpId, size) {
        size = size ? size : "small";
        width = 360;
        height = 480;
        path = "?_a=help&_hid="+helpId;
        feat = "width="+width+",height="+height+",scrollbars=1,resizable=1";
	var helpWin = window.open(path, "help", feat);
	helpWin.focus();
}


/* Cookie Library */


function acceptsCookies() {
	if (document.cookie == '') {
	    document.cookie = 'acceptsCookies=yes'; 
	    if(document.cookie.indexOf('acceptsCookies=yes') != -1) {
	        return true; 
	    }
	} else {
	    return true;
	}
	return false;
}


function setCookie (name, value, hours, path, domain, secure) {
    if (acceptsCookies()) { 
	var not_NN2 = (navigator && navigator.appName && (navigator.appName == 'Netscape') && navigator.appVersion && (parseInt(navigator.appVersion) == 2))?false:true;
	if(hours && not_NN2) {
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) {
		var numHours = hours;
	    } else if (typeof(hours) == 'number') { 
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} 


function readCookie(name) {
    if(document.cookie == '') {
	return false; 
    } else {
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) {
	    firstChar += name.length + 1;
	    lastChar = theBigCookie.indexOf(';', firstChar); 
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { 
	    return false;
	}
    }	
} 

function removeCookie(name, path, domain) {
  var val = readCookie(name);
  if (val) {
      document.cookie = name + '=' + val + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');
  }
} 

// USED BY editToolbar !

function toggleDiv(id, show) {
  div = findObj(id);
  if (! div) 
    return true;
  
  if (show == 'toggle')
    show = (div.style.display == 'block' ? 'none' : 'block');
  else
  if (show == 1 || show == 'show' || show == true) 
    show = 'block';
  else
    show = 'none';
  div.style.display = show;
  return false;
}


// Tracking Functions

trackUrl = '?_a=show&m=TrackingTime';
trackBug = 'tracker';
trackTimer = false;

function startTrack(nid, minutes) {
  minutes = minutes ? minutes : 5;
  // inFocus = window.hasFocus ? window.hasFocus() : true;

   if (true) {
    var rand = Math.round(Math.random() * 9999999);
    var url = '?_a=show&m=TrackingAction' + '&_nid=' + nid + '&minutes=' + minutes + '&' + rand;
    trackAction(url);
  }
  if (trackTimer) 
    clearTimeout(trackTimer);
  if (minutes > 0)
    trackTimer = setTimeout('startTrack('+nid+', '+minutes+')', minutes * 60 * 1000);
}

function saveNote(nid) {
  form = document.trackForm;
  value = form.note.value;
  setValue(nid, "note", value);
}

function saveStatus(nid) {
  form = document.trackForm;
  value = form.status.options[form.status.selectedIndex].value;
  setValue(nid, "status", value);
}

function saveAll(nid) {
  form = document.trackForm;
  status = form.status.options[form.status.selectedIndex].value;
  note = form.note.value;
  var url = '?_a=show&m=TrackingAction&_nid=' + nid + '&note' + escape(note) + '&status=' + escape(status);
  trackAction(url);
}

function setValue(nid, field, value) {
  var url = '?_a=show&m=TrackingAction&_nid=' + nid + '&set=' + field + '&' + field +'=' + escape(value);
  trackAction(url);
}

function trackAction(url) {
  var img = new Image();
  img.onload = saveNoteSuccess;
  img.onerror = saveNoteFailed;
  img.src = url;
  
  form = document.trackForm;
  btn = form.btnNote;
  btn.value = "Speichern";
}

function saveNoteSuccess() {
  form = document.trackForm;
  btn = form.btnNote;
  btn.value = "Gespeichert.";
}

function saveNoteFailed() {
  form = document.trackForm;
  btn = form.btnNote;
  btn.value = "Fehler!";
}

function loadPage() {
  //
}


function unloadPage(nid, trackable) {
  // 
}

function over(pic) {
  img = findObj(pic);
  if (img) {
    now = img.src;
    act = now.substr(0, now.indexOf(pic)) + pic + "_a" + now.substr(now.indexOf(pic) + pic.length);
    img.src = act;
  }
}

function out(pic) {
  img = findObj(pic);
  if (img) {
    now = img.src;
    inact = now.substr(0, now.indexOf(pic)) + pic + now.substr(now.indexOf(pic) + 2 + pic.length);
    img.src = inact;
  }
}

function addFav(id) {
  img = "fav_"+id;
  if (document.images[img])
    return document.images[img].src = "?_a=show&_nid="+id+"&m=AddFav&random="+Math.random();
}


function removeFav(id) {
  addFav(id);
}
