window.onload = jsonpCall;

function jsonpCall() {
    var script = document.createElement("script");        
    urlQuery = '';
    if(typeof(api_url)=="undefined")
       api_url = 'api.hiogi.net/1.0/questions.json';

    
    if(typeof(tag)!="undefined")
       urlQuery += '&tag='+escape(tag);
    
    if(typeof(amount)!="undefined")
       urlQuery += '&amount='+escape(amount);
    
    if(typeof(lang)!="undefined")
       urlQuery += '&lang='+escape(lang);

    if(typeof(keyword)!="undefined")
       urlQuery += '&keyword='+escape(keyword);
    
    if(typeof(filterKeyword)!="undefined")
        urlQuery += '&filterKeyword='+escape(filterKeyword);
    
    if(typeof(tagIDs)!="undefined")
       urlQuery += '&tagIDs='+escape(tagIDs);


    var apiUrl = "http://"+api_url+"?callback=apiResponse&app_id=18ec1801ea0e3768bec7"+urlQuery;
    var blankImg = document.getElementById("blankImg");
    var callingUrl = escape(window.location);
    var imageUrl = "http://apps.hiogi.net/html_widget/dot.gif?callingUrl=" + callingUrl + "&apiUrl=" + escape(apiUrl);
    blankImg.src = imageUrl;
    
    script.setAttribute("src",apiUrl);
    script.setAttribute("type","text/javascript");                
    scriptPos = document.getElementById("hiogi_container");
    scriptPos.appendChild(script)
}

function apiResponse(response) {
    if (typeof(response.questions) != 'undefined') {
        questions = response.questions;
        questionsHTML = '';
        i = 0;
        while (i < questions.length) {
            questionHTML = '<div class="hiogi_question"><a href="' + questions[i].link + '" target="_blank">' + questions[i].content + '</a></div>';
            questionHTML += '<div class="hiogi_answer"><a href="' + questions[i].link + '" target="_blank">' + questions[i].answers.primary_content + '</a></div>';
            questionsHTML += questionHTML;
            i++;
        }
    }
    else {
        if(lang == 'de')
            questionsHTML = '<div class="hiogi_question">Ups, keine Fragen gefunden!</div>';
        else
            questionsHTML = '<div class="hiogi_question">Sorry, no questions found!</div>';
  
    }              
    
    document.getElementById("hiogi_questions").innerHTML = questionsHTML;
}


