//document.domain = "gportal.hu";

function toplistData(type) {
    if (type != null) {
        var old = document.getElementById('selected');
        if (old != null) old.id = null;
        type.id = 'selected';
    }
	var mp = document.getElementById('myposition');
	if (mp != null) {
		var tp = mp.innerHTML.indexOf(' (');
		var pure = (tp != -1 ? mp.innerHTML.substring(0, tp) : mp.innerHTML);
		mp.innerHTML = pure;
		document.getElementById('mypostext').innerHTML = '';
	}
    var qs = qstring();
    if (qs == null) return;
    var url = '/mportallist_top5_load.php?' + qs;
//    var url = '/mportallist_top5_load.php?' + qs + '&rnd=' + Math.random();
    ivy.http.get(url, toplistDataCallback);
}

function qstring() {
    var pcategory = document.getElementsByName('category');
    var i = 0;
    while (i < pcategory.length && pcategory[i].tagName != 'SELECT') i++;
    pcategory = (i < pcategory.length ? pcategory[i].value : '');
    var ptype = document.getElementById('selected');
    if (ptype != null) {
        ptype = ptype.getAttribute('type');
    } else {
        return;
    }
    var qs = 'cat=' + pcategory + '&type=' + ptype;
    if (location.href.indexOf('debug=true') != -1) qs += '&debug=true';    
    return qs;
}

function toplistDataCallback(wr) {
    var xml = wr.requester.responseXML;
//    alert(ivy.xml.serialize(xml));
    document.getElementById('toplist-content').innerHTML = ivy.xml.serialize(xml);
}


function switchTab(o) {
    var p = o.parentNode.parentNode; 
    p.style.display = 'none';
    if (p.id == 'first') getNextSibling(p).style.display = 'block';
    if (p.id == 'second') getPreviousSibling(p).style.display = 'block';
}

function dynaBlaster() {
    if (ivy) {
        fframe = document.getElementById('forumframe');
        innerDoc = (fframe.contentDocument) ? fframe.contentDocument : fframe.contentWindow.document;
        objToResize = (fframe.style) ? fframe.style : fframe;
        objToResize.height = (ivy.browser.isIE ? innerDoc.body.scrollHeight : innerDoc.body.offsetHeight) + 'px';
    }
}

var myposlink;

function myPosition(o, uid) {
    myposlink = o;
    var qs = qstring();
    if (qs == null) return;
//    var url = '/xslt/myposition-load.xslt?uid=' + uid + '&' + qs;
    var url = '/scripts/ownpos.php?uid=' + uid + '&' + qs;
    ivy.http.get(url, myPositionCallback);
}

function myPositionCallback(wr) {
    var xml = wr.requester.responseXML;
    var pos = ivy.xml.xpath.evalSingleValue(xml, "/root/position/text()");
    var inc = ivy.xml.xpath.evalSingleValue(xml, "/root/incategory/text()");
    var tp = myposlink.innerHTML.indexOf(' (');
    var pure = (tp != -1 ? myposlink.innerHTML.substring(0, tp) : myposlink.innerHTML);
    var mypostext = document.getElementById('mypostext');
    myposlink.innerHTML = pure;
    if (pos != null && pos != -1 && inc != null && inc == 'true') {
        myposlink.innerHTML += ' (' + pos + ')';
        mypostext.innerHTML = 'A kiválasztott listában portálod a(z) ' + pos + '.';
    } else {
        myposlink.innerHTML += ' (nincs)';
        mypostext.innerHTML = 'Portálod nem szerepel a kiválasztott listában.';
    }
}

function fullList(o) {
    var qs = qstring();
    location.href = '/portallista?' + qs;
}

function keyHandler(e, o) {
//    if (!ivy.browser.isIE) return;
    if (e.keyCode == 13) {
        if (o.name != 'password') {
            e.keyCode = 9;
        } else {
            loginSubmit();
        }    
    }    
}

function loginSubmit() {
    var f = document.gportal_login;
    var i = f.getElementsByTagName('INPUT');
    for (var j = 0; j < i.length; j++) {
        if ((i[j].type == 'text' || i[j].type == 'password') && i[j].getAttribute('focused') != 'true') i[j].value = '';
    }
    f.submit();
}

function showWhisperer(o) {
    var t = document.getElementById('questions');
    if (t == null) return;
    t = t.firstChild.firstChild.childNodes;
    for (var i = 0; i < t.length; i=i+2) {
        if (t[i].firstChild == o && t[i+1].firstChild.firstChild.style.display == 'none') {
                t[i].firstChild.firstChild.src = '/gfx/nyil_up.jpg';
                t[i+1].firstChild.firstChild.style.display = 'block';
        } else {
            t[i].firstChild.firstChild.src = '/gfx/nyil_down.jpg';
            t[i+1].firstChild.firstChild.style.display = 'none';
        }
    }
}

function showWhisperer2(o, qid) {
    var t = document.getElementById('questions'+qid);
    if (t == null) return;
    t=getFirstChild(getFirstChild(t)).childNodes;
    var tlength=t.length;
    var a;
    var b;
    var skipthis=false;
    for (var i = 0; i < tlength; i++) {
      if ( t[i].nodeType == 1 ) {
        if(!skipthis) {
	  skipthis=true;
          a=getFirstChild(t[i]);
	  b=getFirstChild(getFirstChild(getNextSibling(t[i])));
          if (a == o && b.style.display == 'none') {
                getFirstChild(a).src = '/gfx/nyil_up.jpg';
                b.style.display = 'block';
          } else {
            getFirstChild(a).src = '/gfx/nyil_down.jpg';
            b.style.display = 'none';
          }
	} else skipthis=false;
      }
    }
}

function showWhispererInnerOld(o) {
    var t = o.parentNode.parentNode.parentNode.childNodes;
    for (var i = 0; i < t.length; i=i+2) {
        getFirstChild(t[i]).childNodes[1].style.display = (getFirstChild(getFirstChild(t[i])) == o && getFirstChild(t[i]).childNodes[1].style.display == 'none' ? 'block' : 'none');
    }
}

function showWhispererInner(o) {
    var t = o.parentNode.parentNode.parentNode.childNodes;
    var tlength=t.length;
    var a;
    for (var i = 0; i < tlength; i++) {
      if ( t[i].nodeType == 1 && getFirstChild(t[i]).className == "question_row") {
        a=getFirstChild(getFirstChild(t[i]));
        getNextSibling(a).style.display = (a == o && getNextSibling(a).style.display == 'none' ? 'block' : 'none');
      }
    }
}

function calculateTopLeft(htmlElement) {
	var result = new Object();
	result.top = 0;
	result.left = 0;
	result.log = '';
	var i = 0;
	while (htmlElement != null) {
	    result.log += htmlElement.tagName + ' {' + htmlElement.id + '} ' + htmlElement.style.position + ' ' + htmlElement.offsetLeft + ' ' + htmlElement.offsetTop + ' ' + result.left + ' ' + result.top + '\n';
	    i++;
		result.top += htmlElement.offsetTop/1;
		result.left += htmlElement.offsetLeft/1;
	   htmlElement = htmlElement.offsetParent;
	}
	return result;
}

function getNextSibling(startBrother) {
  endBrother = startBrother.nextSibling;
  while (endBrother && endBrother.nodeName != startBrother.nodeName) {
    endBrother = endBrother.nextSibling;
  }
  if (!endBrother) {
    return false;
  }

  return endBrother;
}

function getPreviousSibling(startBrother) {
  endBrother = startBrother.previousSibling;
  while (endBrother && endBrother.nodeName != startBrother.nodeName) {
    endBrother = endBrother.previousSibling;
  }
  if (!endBrother) {
    return false;
  }

  return endBrother;
}

function getFirstChild(elm) {
  if ( !elm.childNodes.length ) {
    return;
  }

  var children = elm.childNodes.length;
  for ( var i = 0; i <= children; ++i ) {
    if ( elm.childNodes[i].nodeType == 1 ) {
      return elm.childNodes[i];
    }
  }

  return;
}

