// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//ticker vars
    var obj0,obj1,obj2,j,k,tickerRestart;
    var i=800;
    var ticker_start = i;
    var speed=15; 

  function doOnload(){
    
    if (ie_above_ver5()) {
      

      if (document.getElementById("ticker0")){
         obj0=document.getElementById('ticker0');
         //obj1=document.getElementById('ticker1');
         obj2=document.getElementById('tickerHolder');

         j=obj0.offsetWidth;
         //k=obj1.offsetWidth;

         tickerRestart=950;
         

         //obj1.className='';
         obj2.className='setup'

         obj0.style.width=j+'px';
         //obj1.style.width=k+'px';

         imgScroll();

      obj2.onmouseover=function(){
         clearTimeout(scroller);
       }
      obj2.onmouseout=function(){
         imgScroll();
      }
    }
  }
}    

function ie_above_ver5() {
  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
    var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
    if (ieversion<6)
      return false;
    else
      return true;
  } else {
    return true;
  }

}

function imgScroll() {

     obj0.style.left=i+'px';
     //obj1.style.left=j+'px';
        
     i--;
     j--;

  if(i<-tickerRestart) {
     i=ticker_start;
   }
  //if(j<-k) {
  //   j=temp;
   //} 
    scroller=setTimeout('imgScroll()',speed);
}

// Codes to hide and display questions and answer - by Paul Brennan
function onload_function(onload_params) {
    try {
        //main onload function
    }
    catch (e) {
        //not sure if we want to have an alert here?
    }
    try {
        //page specific onload.
        eval(onload_params)
    }
    catch (e) {
    }

    //Add autocomplete
    elements = document.getElementsByTagName("input")
    if (elements != null) {
        for (var i = 0; i < elements.length; i++) {
            element = elements[i];
            element.setAttribute("autocomplete", "off");
        }
    }
   
  
}

function other_onload_function(onload_params,dummy) {
    try {
        //main onload function
    }
    catch (e) {
        //not sure if we want to have an alert here?
    }
    try {
        //page specific onload.
        eval(onload_params);
        hide_questions();
    }
    catch (e) {
    }

    //Add autocomplete
    elements = document.getElementsByTagName("input")
    if (elements != null) {
        for (var i = 0; i < elements.length; i++) {
            element = elements[i];
            element.setAttribute("autocomplete", "off");
        }
    }
          
    //populate rating
    try{
      given_rating = parseInt(document.location.href.split('?')[1].split('=')[1])
      document.getElementById('current-rating').className += "rating-"+given_rating;
    }catch(e){
      //either XSS attack or no rating submitted
    }
}

function switchJsForFeedbacks(){
    document.getElementById('noJavascript').innerHTML = "Results will update automatically";
}


function hide_questions(){
    sections = getElementsByClassName('section')
    for (var i = 0; i < sections.length; i++) {
        showHideDiv(sections[i].id, false);
    }
    answers = getElementsByClassName('answer')
    for (var i = 0; i < answers.length; i++) {
        showHideDiv(answers[i].id, false);
    }
}

function hide_documents(){
    sections = getElementsByClassName('section')
    for (var i = 0; i < sections.length; i++) {
        showHideDiv(sections[i].id, false);
    }
    answers = getElementsByClassName('answer')
    for (var i = 0; i < answers.length; i++) {
        showHideDiv(answers[i].id, false);
    }
    showHideSection('doc_selection')
}

function showAnswer(answer_id, link){
    elem = document.getElementById(answer_id);
    //Effect.toggle(elem, 'blind');
    if (elem != null) {
        elem.style.display = elem.style.display == 'none' ? 'block' : 'none';
    }
    answers = getElementsByClassName('answer');

    if (answers != null) {
        for (var i = 0; i < answers.length; i++) {
            if (elem != null && answers[i].id != elem.id) {
                showHideDiv(answers[i].id, false);
            }
        }
    }
    questions = getElementsByClassName('questionLink');
    for (var j = 0; j < questions.length; j++) {
        if (questions[j] == link) {
         
      questions[j].id= elem.style.display == 'none' ? '' : 'selectedQuestion';
        } else {
            questions[j].id="";
        }
    }
}

function showHideSection(section_name){
    elem = document.getElementById(section_name)
    elem.style.display = elem.style.display == 'none' ? 'block' : 'none'
    sections = getElementsByClassName('section')
    for (var i = 0; i < sections.length; i++) {
        if (sections[i].id != elem.id) {
            showHideDiv(sections[i].id, false);
        }
    }
}

function toggleElem(div_id){
    Effect.toggle(document.getElementById(div_id), 'blind');
}

function showHideDiv(div_id, show) {
    var elem = document.getElementById(div_id);
    //hides or shows the div given
    if (elem != null && show == true) {
        document.getElementById(div_id).style.display = 'block';
    }
    else {
        if (elem != null) {
            document.getElementById(div_id).style.display = 'none'
        }
    }
}


function sayHello(phrase){
    //This function can be used to check that javascript code is being called correctly
    //either pass a varaibe to check value of that variable. Otherwise it just alerts a
    //hello message
    if (!phrase) {
        phrase = "Hello World"
    }
    alert(phrase)
}


function getElementsByClassName(findClass){
    returnValue = new Array();
    index = 0;
    var aElm = document.body.getElementsByTagName('*');
    for (var i = 0; i < aElm.length; i++) {
        if (aElm[i].className == findClass) {
            returnValue[index] = aElm[i]
            index++;
        }
    }
    return returnValue;
}

function GetXmlHttpObject(handler){
    var objXMLHttp = null
    if (window.XMLHttpRequest) {
        objXMLHttp = new XMLHttpRequest()
    }
    else
        if (window.ActiveXObject) {
            objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
        }

    return objXMLHttp
}

function toggleFlag(id){
    xmlHttp = GetXmlHttpObject()
    var url = "/admin/toggle_flag/" + id;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}


/* JW Player events */
var player = null;
var playlist = null;

function playerReady(obj){

    player = document.getElementById(obj['id']);
    addListeners();
    //alert("Player is ready to be used.");
  setInterval("stateMonitor("+obj+")",4000);
}


//...loop until we get a valid playlist so we know that the player is really ready
function addListeners(){
    playlist = player.getPlaylist();

    if (playlist.length > 0) {
        player.addModelListener('STATE', 'stateMonitor');
    }
    else {
        setTimeout("addListeners();", 100);
    }
};



function gid(name){
    return document.getElementById(name);
};


/* Show/hide content for document area by Paul Brennan (paul.brennan@ahc.uk.com) */

function showHideDiv(div_id, show){
  //hides or shows the div given
  if (show==true){
    document.getElementById(div_id).style.display='block';
  }else{
    document.getElementById(div_id).style.display = 'none'
  }
}

function hideDivs(exception_div_id){
  var show_hide_container = document.getElementById('showHideContainer')
  if (show_hide_container) {
    var div_tags_array = show_hide_container.getElementsByTagName('div')
    for (var k = 0; k < div_tags_array.length; k++) {
      div_tags_array[k].style.display = 'none';
    }
  }
}

function hideAllItemsWithClass(hide_class) {
    if (ie_above_ver5()) {
    var showHideItems = document.getElementsByClassName(hide_class);
        for(var i=0; i<showHideItems.length; i++) {
      showHideItems[i].style.display = 'none';
        }
    }
}

function showAllDivs(){  
  var aElm=document.body.getElementsByTagName('div');
    for(var i=0; i<aElm.length; i++) {
      if(aElm[i].style.display == 'none') {
        aElm[i].style.display = 'block'
      }
  }
}

function toggle_div(div_id, hide_class){
  if (hide_class!=undefined){
    hideAllItemsWithClass(hide_class);
  }
  Effect.toggle(document.getElementById(div_id),'appear');  
}


function showHideID(div_id,className,toggle){
  if (toggle == null){
       toggle = true;
   }

  //this function hides all elements with the class name given
  //then toggles the display status of teh div_id given
  //className is optional can be run as just showHideID(div_id)
  //if toggle is set to true then it will not hide it when clicked for
  //the second time

  //first get the element that wants to change
   var element = null;
   if (div_id != null) {
       element = document.getElementById(div_id);
   }
  //If it is already shown will be only one open so only need to change to style to none
  if (element != null && element.style.display=='block' && toggle){
      element.style.display='none'
    }else{
    //show the element
    if (element != null) {
        element.style.display = 'block'
    }
      
      if (className) {
      //if given a class to hide
      var elements = document.getElementsByClassName(className);
      //go through all elements with selected class
      if (elements!=null){
        for (var i = 0; i < elements.length; i++) {
          if (elements[i].id != div_id) {
          //as long as isn't the selected element - hide
            elements[i].style.display = 'none'
          }
        }
      }
    }
    }

}

function hideDiv(div_id){
  document.getElementById(div_id).style.display = 'none';
}



function ie_above_ver5() {
  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
    var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
    if (ieversion<6)
      return false;
    else
      return true;
  } else {
    return true;
  }

}

// END of the codes to hide and display questions and answer
          
          
function setRating(rating)
{
  width = rating * 25;
  document.getElementById('current-rating').style.width = width+'px';
  var current_url = document.location.href.split('?')[0];
  document.location = current_url+"?rating="+rating;

}          

