var locsrch = window.location.search;
var Getp = new Object();

if (locsrch) {
    var pa = locsrch.substring(1).split(/&|;/);
    for (var j=0; j<pa.length; j++) { 
        var kv = pa[j].split('=');
        Getp[kv[0]] = kv[1];
    }
}

var st_param_str;
var st_max_pos;

function myactivateXControls() {

	var strBrowser = navigator.userAgent.toLowerCase();
	if(strBrowser.indexOf("msie") > -1 && strBrowser.indexOf("mac") < 0) {
		var theObjects = document.getElementsByTagName('object');

		for (i = 0; i < theObjects.length; i++) {
			var theOuterHTML = theObjects[i].outerHTML;
			theObjects[i].outerHTML = theOuterHTML;
		}
	}

};

function do_movie() {

        var st_movie = eval('document.movie');
        //st_max_pos = "20";
        st_movie.SetVariable("maxPos", st_max_pos);
	//st_param_str = "10,1,12,2,11,3,10,4,7,5,3,6,5,7,5,8,5,9,3,10";
        st_movie.SetVariable("paramString", st_param_str);
	st_movie.GotoFrame(2);
}

function substContent (req) {

   var text = req.responseText;
   var panel = $( "stats" );
   panel.innerHTML = text;

}

function substContentTeam (req) {

   var text = req.responseText;
   var panel = $( "stats" );
   panel.innerHTML = text;
   st_param_str = document.getElementById("st_paramString").firstChild.nodeValue || "1";
   st_max_pos = document.getElementById("st_maxPos").firstChild.nodeValue || "1,1";
   setTimeout("do_movie();", 800);

}

function dynaCont() {

    var xmlroot = "/"+ page_lang + "/xml";
    var statroot= "/"+ page_lang + "/statistics";
    var ftable = new Object();

    ftable.article =   {file: Getp.f                    ,handler: processArticle};
    ftable.artlist =   {file: xmlroot+  "/anynews.xml"  ,handler: processNews};
    ftable.socnews =   {file: xmlroot+  "/socnews.xml"  ,handler: processNews};
    ftable.sponews =   {file: xmlroot+  "/sponews.xml"  ,handler: processNews};
    ftable.articlehp = {file: xmlroot+  "/top_art.xml"  ,handler: processArticle};

    for (var c=0, al=arguments.length; c < al; c++) {

        var o = ftable[arguments[c]];

        new Ajax.Request(o.file, {method: 'get', onComplete: o.handler})
    }
}

function childNodesMatch(n, criteria) {

   if (!n || n.nodeType != 1 ) return [];

   var subs = n.childNodes;
   var ret = new Array();

   for (var i=0, len=subs.length; i<len; i++) {

       if (subs[i].nodeType != 1) continue;

       var found = 1;
       for (var c in criteria) {

           if (c == 'tag' && subs[i].tagName == criteria[c])
              continue;

           var attrv = subs[i].getAttribute(c);
           if (attrv && attrv == criteria[c])
              continue;

           found = 0;
       }

       if (found) ret.push(subs[i]);
   }

   return ret;
}

function fseMatch(n, criteria) {

    if (!n || n.nodeType != 1) return null;

    for (var m = n.firstChild; m != null; m = m.nextSibling) {

        if (m.nodeType != 1) continue;

        var found = 1;
        for (var i in criteria) {

            if (i == 'tag') 
                if (m.tagName == criteria[i]) 
                    continue;
                else { found = 0; break; }

            var attrv = m.getAttribute(i);
            if (attrv != "")
                if (attrv == criteria[i])
                    continue;
                else { found = 0; break; }

            found = 0;
        }

        if (found) return m;

        var retnode = fseMatch(m, criteria);
        if (retnode != null) { return retnode; }
    }

    return null;
}

function buildText(n) {

    var txt = "";
    if (n.nodeType != 1 && n.nodeType != 3) return "";

    if (n.nodeType == 3)
        return n.nodeValue;

    txt += "<" + n.tagName + ">";

    for (var m = n.firstChild; m != null; m = m.nextSibling)
            txt += buildText(m);

    txt += "</" + n.tagName + ">\n";

    return txt;
}

function parseNewsML(doc) {

  var ni = fseMatch(doc.documentElement, {tag: "NewsItem"});

  var nc0      = fseMatch(ni, {tag: "NewsComponent"});

  var title    = fseMatch(nc0, {tag: "HeadLine"}).firstChild.nodeValue;

  var nc1      = fseMatch(nc0, {tag: "NewsComponent"});

  var dc       = fseMatch(nc1, {tag: "DataContent"});

  var content  = buildText(dc);

  var ncp0     = fseMatch(nc0, {tag: "NewsComponent", Duid: "photo0"});

  if (ncp0 != null) {
      var am  = fseMatch(ncp0, {tag: "AdministrativeMetadata"});

      var pr  = fseMatch(fseMatch(am, {tag: "Provider"}), {tag: "Party"});
      if (pr)
          var prtxt = pr.getAttribute("FormalName");

      var cr       = fseMatch(fseMatch(am, {tag: "Creator"}), {tag: "Party"});
      if (cr)
          var crtxt = cr.getAttribute("FormalName");

      var credit   = prtxt + "/" + crtxt;

      var nps      = new Array();
      nps          = childNodesMatch(ncp0, {tag: "NewsComponent"});

      var href;
      if (nps.length > 0) {
          for (var i=0, len=nps.length; i<len; i++) {
              var role = fseMatch(nps[i], {tag: "Role", FormalName: "Preview"});
              if (role != null) {
                  href = fseMatch(nps[i], {tag: "ContentItem"}).getAttribute("Href");
                  href += ".scaled.jpg";
                  break;
              }
          }
      }
  }

  var article = {title: title, content: content, imgsrc: href, credit: credit};
  return article;
}

function processNews(req) {

    var xmldoc = req.responseXML;

    var news = new Array();

    var nlist  = xmldoc.getElementsByTagName("newsitem");

    for (var i=0, len = nlist.length; i<len; i++) {

         var item = {};
         item.title    = fseMatch(nlist[i], {tag: "title"}).firstChild.nodeValue;
         item.headline = fseMatch(nlist[i], {tag: "headline"}).firstChild.nodeValue;
         var phototext =  fseMatch(nlist[i], {tag: "photo"}).firstChild;
         if (phototext != null)
             item.photo = phototext.nodeValue;

         var filename = fseMatch(nlist[i], {tag: "filename"}).firstChild.nodeValue;
         item.href_article = "/" + page_lang + "/pbnart.html?f=/news/xml/" + page_lang + "/" + filename;
         news.push(item);
    }

    var panel = $( "lc" );
    var text = pbnet.templates.news( news );
    panel.innerHTML = text;

}

function processArticle(req) {

  var xmldoc = req.responseXML;
  var de = xmldoc.documentElement.tagName;

  var article = new Object();

  if (de == 'NewsML')
      article = parseNewsML(xmldoc);
  else if (de == 'nitf')
      article = parseNitf(xmldoc);
  else 
      article.title = "";

  var panel = $( "article" );
  var text  = pbnet.templates.article( article );

  panel.innerHTML = text;

}

function parseNitf(doc) {

  var bh    = fseMatch(doc.documentElement, {tag: "body.head"});
  var title = fseMatch(bh, {tag: "hl2"}).firstChild.nodeValue;

  var bc    = fseMatch(doc.documentElement, {tag: "body.content"});
  var bcp   = childNodesMatch(bc, {tag: "p"});

  var content = "";
  for (var i=0, len=bcp.length; i<len; i++) {
      if (bcp[i].firstChild)
         content += "<p>" + bcp[i].firstChild.nodeValue + "</p>";

  }

  var ph    = fseMatch(fseMatch(bc, {tag: "photo-inst", id: "preview"}), {tag: "photo-ref"});
  var imgsrc = null;
  var imgwidth = "";

  if (ph && ph.firstChild) {
     imgsrc = ph.firstChild.nodeValue;
     imgsrc += ".scaled.jpg";
     var wh = imgsrc.match(/.*-(\d+)x\d+\.(jpg|JPG)/);
     imgwidht = wh[1];
  }

  var cap    = fseMatch(fseMatch(bc, {tag: "photo-inst", id: "caption"}), {tag: "photo-ref"});
  if (cap && cap.firstChild)
      var caption = cap.firstChild.nodeValue;

  var cr= fseMatch(fseMatch(bc, {tag: "photo-inst", id: "credit"}), {tag: "photo-ref"});
  if (cr && cr.firstChild)
      var credit = cr.firstChild.nodeValue;

  var article = {title: title, imgsrc: imgsrc, credit: credit, content: content};
  return article;

}
