/**
*
* Ajax page that returns all download types for the empire site
*
* @author  Kai Price <pricek@soe.sega.co.uk>
* @date    01 August 2008
*
**/

/**
*
* gets the screenshots for a given game group and territory
*
* @param    int     id of the game
* @param    int     id of the territory
* @param    int     starting offset of the resultset
* @param    int     id of the download type
* @param    string  the root of the empire website
* @param    string  the root of the segawest website
* @param    boolean indicating whether to use ajax to load an image or us the $_GET method
* @param    boolean indicating whether the results should be accompanied by pagination
* @param    string  title of the download type
* @param    string  localised translation of download
* @param    boolean modify the heading of the heading to relect the new download type
* @access   public
*
**/
function getDownloads(gameid,territory,start,downloadtypeid,htmltop,segawesttop,paginateres,sectiontitle,downloadtrans,modifyheading) {
    var xmlHttp = GetXmlHttpObject();

    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request")
        return
    }
    //set the 'loading' icon
    document.getElementById("downloadsviewer").innerHTML='<div class="loadingajax" style="height:380px;">&nbsp;<\/div>';

    //set the new heading (if required)
    if(modifyheading=='true'){
        document.getElementById("changingdownloadtitle").className='downloadtitle_' + downloadtypeid;
    }

    //because the maximum limit varies depending on the download type
    //this switch determines how many results are to be returned
    var limit = 0;
    switch(downloadtypeid){
        case '15'://screensavers
            limit = 9;
        break;
        case '19': //wallpapers
            limit = 9;
        break;
        case '21'://IM Icons
            limit = 6;
        break;
        case '22'://podcasts
            limit = 2;
        break;
        case '23'://tools and mods
            limit = 2;
        break;
    }

    var url= htmltop + "ajax/ajax-getDownloads.php"
    url    = url + "?g="+gameid;
    url    = url + "&t="+territory;
    url    = url + "&st="+start;
    url    = url + "&l="+limit;
    url    = url + "&d="+downloadtypeid;

    //alert(url);

    xmlHttp.open("GET",url,true);

    xmlHttp.onreadystatechange = function() {
        switch (xmlHttp.readyState) {
            case 0 : case 1 : case 2 : case 3 : break;
            case 4 :
            case 'complete' :
                //tear apart the response text which is XML
                var xml = xmlHttp.responseXML;

                var thisset = xml.getElementsByTagName("result")[0].getAttribute("thisset");
                var count   = xml.getElementsByTagName("result")[0].getAttribute("count");

                switch(downloadtypeid){
                    case '22'://podcasts
                        var html = '<div class="downloadCatHeading">' + sectiontitle + '<\/div>';
                    break;
                    case '23'://tools and mods
                        var html = '<div class="downloadCatHeading">' + sectiontitle + '<\/div>';
                    break;
                    default:
                        var html = '';
                    break;
                }


                for(var i=0; i<thisset; i++){//loop through the resultset
                    var downloadid  = xml.getElementsByTagName("downloadid")[i].firstChild.nodeValue;
                    var title       = xml.getElementsByTagName("title")[i].firstChild.nodeValue;
                    var description = xml.getElementsByTagName("description")[i].firstChild.nodeValue;

                    //try to get a thumbnail for the screenshot
                    var image = xml.getElementsByTagName("image1")[i].firstChild.nodeValue;
                    if(image!=''){//image 1 has content
                        image = image.substring(1,image.length);
                    }else{//image 1 is empty
                        //use the second image instead
                        var image = xml.getElementsByTagName("image2")[i].firstChild.nodeValue;
                        if(image!=''){//image 2 has content
                            image = image.substring(1,image.length);
                        }else{
                            image = '';
                        }
                    }

                    //download items are rendered differently so this ensures that
                    //they are rendered properly
                    switch(downloadtypeid){
                        case '15': //screensavers
                            html += '<div class="downloadItem_' + downloadtypeid + '">';
                            //html += '<a href="' + htmltop + 'downloads/downloads.php?d=' + downloadid + '">';
                            html += '<div style="background: url(\'' + segawesttop + image + '\') no-repeat top center" class="image">&nbsp;<\/div>';
                            //html += '<\/a>';
                            html += '<div class="title">' + title + "<\/div>";
                            html += '<a href="' + htmltop + 'downloads/downloads.php?d=' + downloadid + '">' + downloadtrans + '<\/a>';
                            html += '<\/div>';
                        break;
                        case '19': //wallpapers
                            html += '<div class="downloadItem_' + downloadtypeid + '">';
                            //html += '<a href="' + htmltop + 'downloads/downloads.php?d=' + downloadid + '">';
                            html += '<div style="background: url(\'' + segawesttop + image + '\') no-repeat top center" class="image">&nbsp;<\/div>';
                            //html += '<\/a>';
                            html += '<div class="title">' + title + "<\/div>";
                            html += '<a href="' + htmltop + 'downloads/downloads.php?d=' + downloadid + '">' + downloadtrans + '<\/a>';
                            html += '<\/div>';
                        break;
                        case '21': //IM Icons
                            html += '<div class="downloadItem_' + downloadtypeid + '">';
                            //html += '<a href="' + htmltop + 'downloads/downloads.php?d=' + downloadid + '">';
                            html += '<div style="background: url(\'' + segawesttop + image + '\') no-repeat top center" class="image">&nbsp;<\/div>';
                            //html += '<\/a>';
                            html += '<div class="title">' + title + "<\/div>";
                            html += '<a href="' + htmltop + 'downloads/downloads.php?d=' + downloadid + '">' + downloadtrans + '<\/a>';
                            html += '<\/div>';
                        break;
                        case '22': //podcasts
                            html += '<div class="downloadItem_' + downloadtypeid + '">';
                            html += '<div style="background: url(\'' + segawesttop + image + '\') no-repeat top center;" class="image">&nbsp;<\/div>';
                            html += '<div class="content">';
                            html += '<div class="title">' + title + "<\/div>";
                            html += '<div class="description">' + description + "<\/div>";
                            html += '<div class="downloadnow"><a class="bodyanchor" href="' + htmltop + 'downloads/downloads.php?d=' + downloadid + '">' + downloadtrans + '<\/a><\/div>';
                            html += '<\/div>';
                            html += '<\/div>';
                        break;
                        case '23': //tools and mods
                            html += '<div class="downloadItem_' + downloadtypeid + '">';
                            html += '<div class="title">' + title + "<\/div>";
                            html += '<div class="description">' + description + "<\/div>";
                            html += '<div class="downloadnow"><a class="bodyanchor" href="' + htmltop + 'downloads/downloads.php?d=' + downloadid + '">' + downloadtrans + '<\/a><\/div>';
                            html += '<\/div>';
                        break;
                    }
                }

                html += '<div class="clear">&nbsp;</div>'; //make sure that there is a clearing div
                document.getElementById("downloadsviewer").innerHTML = html;
                //alert(html);

                if(paginateres=='true'){
                    //put all of the parameters that have been passed in into an array, in the order that they
                    //were called as we are going to need to use these variables in the pagination
                    var param_ar = new Array();

                    param_ar['gameid']          = gameid;
                    param_ar['territory']       = territory;
                    param_ar['start']           = start;
                    param_ar['downloadtypeid']  = downloadtypeid;
                    param_ar['htmltop']         = htmltop;
                    param_ar['segawesttop']     = segawesttop;
                    param_ar['paginateres']     = paginateres;
                    param_ar['sectiontitle']    = sectiontitle;
                    param_ar['downloadtrans']   = downloadtrans;
                    param_ar['modifyheading']   = 'false';//we dont want to keep updating the title when scrolling through the paginated sections

                    functionname                = 'getDownloads'; //it also needs to know what function to call!

                    paginate(start, limit, count, param_ar, functionname); //paginate the results
                }
            break;
        }
    }

    xmlHttp.send(null)
}

function GetXmlHttpObject() {
    var xmlHttp=null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
