/**
 *  Tutka.org - Ajan tasalla mediasta
 *  +++++++++++++++++++++++++++++++++
 *  Javascript       //  Crasman 2006
 */

$(document).ready(function() {

$('select[name=ohjelma]').bind('change', function() { $('input[name=q0]').attr('value', $(this).attr('value')); })


  $('#headHaku input').focus(function() {
    if($(this).val() == ' Sisältöhaku') $(this).val('');
  }).blur(function() {
    if($(this).val() == '') $(this).val(' Sisältöhaku');
  });  
});

var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=750,height=500';

function raw_popup(url, target, features) {
  var theWindow = window.open(url, target, features);
  theWindow.focus();
  return theWindow;
}

function link_popup(src, features) {
  return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function galleriaPopup(img, alt) {  
  var popupoptions = 'location=0,statusbar=0,menubar=0,width=410,height=400';
  raw_popup(img, 'galleria', popupoptions);


/*
  $('#galleriaPopup').show().find('div').html('<img src="'+img+'" alt="" style="display: none; margin: auto;" />');*/
  
  /* Opera Fix 
  if(navigator.appName == 'Opera') {
    $('#galleriaPopup img').show();
    $('#galleriaPopup div').css('background', 'transparent');
    $('#galleriaPopup div').append('<p>'+alt+'</p>');
  }
  else {
    $('#galleriaPopup div img').oneload(function() {
      $('#galleriaPopup div').css('background', 'transparent');
      $(this).fadeIn('slow');
      setTimeout(function() {
        $('#galleriaPopup div').append('<p>'+alt+'</p>');
      }, 500);
    });
  }
  
  $('#galleriaPopup').find('a.sulje').click(function() {
    $('#galleriaPopup').hide().find('div').empty().css('background', "url('/files/tutka/gfx/loader.gif') no-repeat center");
  });*/
}


   var http_request = false;
   function makeRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {

            var xmldoc = http_request.responseXML;
            var root = xmldoc.getElementsByTagName('root').item(0);
            
            for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
               var node = root.childNodes.item(iNode);
               for (i = 0; i < node.childNodes.length; i++) {
                  var sibl = node.childNodes.item(i);
                  var len = parseInt(sibl.childNodes.length / 2);
                  var arr = new Array(len);
                  var cnt = 0;
                  for (x = 0; x < sibl.childNodes.length; x++) {
                     var sibl2 = sibl.childNodes.item(x);
                     var sibl3;
                     if (sibl2.childNodes.length > 0) {
                        sibl3 = sibl2.childNodes.item(0);
                        arr[cnt] = sibl3.data;   
                        cnt++;
                     }
                  }
                  addrow("mytable", arr);
               }
            }
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   function do_xml() {
      makeRequest('http://www.yle.fi/radiopeili/podcast/aplus.xml', '');
   }
   function addrow(tablename, arr) {
   var tbl = document.getElementById(tablename);
   var lastRow = tbl.rows.length;
   var row = tbl.insertRow(lastRow);
      for (r = 0; r < arr.length; r++) {   
         var cell = row.insertCell(r);
         cell.innerHTML = arr[r];
      }
   }


function show_hide(elemid) {
  if(document.getElementById(elemid).style.display!='block') {
    document.getElementById(elemid).style.display='block';
  }
  else {
    document.getElementById(elemid).style.display='none';
  }
}


function updateArticleViewCount (articleId, views) {
  var url = 'artikkeli_lukemiskerrat';
  views++;
  
  $.post(
    url,
    {
      cmf_op: "edit",
      cmf_pids: articleId,
      id: articleId,
      cmf_0_49: views
    },
    function () { }
  );
}
