/*
 *  JS
 */

/*** AJAX ***/
var http_new;

// HTTP-Request-Object erstellen
if (window.XMLHttpRequest) {
  http_new = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
  http_new = new ActiveXObject("Microsoft.XMLHTTP");
}

// AJAX Request
var http_request=false;

function newRequester() {
    if (window.XMLHttpRequest) { // alle Browser außer IE
        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) { }
        }
    }

    return http_request;
}

// Ausgabe für AJAX Response
function outputResult(text, container) {
 //alert(text);
    try {
        if (document.all) document.all[container].innerHTML = text;
        if (document.layers) document.layers[container].innerHTML = text;
        if (document.getElementById) document.getElementById(container).innerHTML = text;
    }
    catch(e) {alert("f"); }
}

/*** AJAX-Anfragen ***/

/* Speichern von Klicks "demnächst gern ansehen"
 * Argumente:
 *    fid: Film-ID
 *    user: Nickname
 *    outputId: Klick-span des Filmes
 */
function viewUpcoming(fid, user, outputId) {
    http_request = false;
    http_request = newRequester();

    if (!http_request) 
    {
        html = "<span><br />Lade...</span>";
        outputResult(html, outputId);
        return false;
    }

    http_request.onreadystatechange = function onreadystatechange() {
        if (http_request.readyState == 1) {
            // Ladeanzeige - Text oder Grafik
            html = "<span><br />Lade...</span>";
            outputResult(html, outputId);
        }
        else if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                if (http_request.responseText != "") {
                    html = "" + http_request.responseText + "";
                    outputResult(html, outputId);
                }
            }
            else {
                html = "<span>Fehler beim Laden! (2)</span>";
                outputResult(html, outputId);
            }
        }
    };

    http_request.open('GET',rurl+'ajax.php?action=upc_view&fid='+fid+'&user='+user, true);
    //http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.send(null);
}

/*** anderes ***/

/* Änderung Auswahl Alternativ-Titel
 *  Argumente:
     titleId: ID des Titels
     newValue: neuer Wert für Titel
 */
function setAlternative (titleId, newValue) {
    window.location.href = rurl+'index.php?p=myaccount/res_check&action=rc_alt&fid='+titleId+'&value='+newValue;
}

/*
 * Änderung Auswahl Videothek
 * Argumente:
    hashkey: Hash-Key der VT
 * -> wird aktuell nicht benötigt
 */
function setVideothek(hashkey) {
    window.location.href = rurl+'index.php?p=myaccount/res_check&action=rc_videoth&hashkey='+hashkey;
    
}

/*
 * Löschen eines Titels aus der Liste
 * Argumente:
    titleId: ID des Titels
 */
function delTitleFromList(tid) {
    resp = confirm("Willst du den Titel wirklich aus deiner Reservierungsliste entfernen?");
    if (resp) {
        window.location.href = rurl+'index.php?p=myaccount/res_check&action=rc_deltit&value='+tid;
    }
}

/*
 * Öffnen eines Popup
 */
function openWindow(page) {
  window.open(rurl+'index.php?p='+page,'videohunter','height=600,width=550,scrollbars=yes');
}
// für Trailer
function openWindowT(page) {
  window.open(rurl+'index.php?p=trailer&f='+page,'videohunter','height=550,width=600,scrollbars=no');
}
// für Hilfe
function openHelp(helppage) {
  window.open(rurl+'index.php?p=help&hp='+helppage,'videohunter','height=600,width=550,scrollbars=yes');
}

/*
 * Wechsel Videothek-Ansicht
 */
function switchVideothek(url, vt) {
    window.location.href = url + '&v=' + vt;
}

/*
 * Wechsel Video-Ansicht
 */
function toogleView(url, view) {
    window.location.href = url + '&view=' + view;
}

/*
 * VT Wechsel Res.-Ansicht
 */
function switchResView(view) {
    window.location.href = rurl + 'index.php?p=myvideothek/' + view;
}


function remindEancodeFilter(eancode, field) {
    new Ajax.Request('ajax.php', {
          method: 'post',
          parameters: 'remindEancodeFilter='+eancode,
          onSuccess: function(result) {
            if (200 == result.status) {
                res = result.responseText;
                $(field).update(''+ result.responseText);
            }
            else {
                $(field).update('Fehler beim Ermitteln der Ergebnisse: ' + result.status);
            }
          },
          onLoading: function(result) {
              $(field).innerHTML = "<img style=\"vertical-align:middle;\"src=\"images/general/loading.gif\"/> Suche...";
          }
        });
}

function remindTitleFilter(titel, field) {
    new Ajax.Request('ajax.php', {
          method: 'post',
          parameters: 'remindTitleFilter='+titel,
          onSuccess: function(result) {
            if (200 == result.status) {
                res = result.responseText;
                $(field).update(''+ result.responseText);
            }
            else {
                $(field).update('Fehler beim Ermitteln der Ergebnisse: ' + result.status);
            }
          },
          onLoading: function(result) {
              $(field).innerHTML = "<img style=\"vertical-align:middle;\"src=\"images/general/loading.gif\"/> Suche...";
          }
        });
}

