﻿// Widget de procura
function writeHTMLasJS(partner) {
    if (partner == "iOnline") {
        writeWidgetIOnline();
        return;
    }
    
    document.write("<link href=\"http:\/\/www.myway.pt\/Styles\/mywaysearch.css\" rel=\"stylesheet\" type=\"text\/css\" \/>");
    document.write("<div id=\"searchBarTop\" class=\"MWsearchBar\">");
    document.write("    <div class=\"MWslidingBarClosed\">");
    document.write("        <img border=0 usemap=\"#Image-Maps_6201006141221104\" src=\"http:\/\/www.myway.pt\/imgOrange\/bcg_widget01.png\" alt=\"\" \/>");
    document.write("<map id=\"_Image-Maps_6201006141221104\" name=\"Image-Maps_6201006141221104\">");
    document.write("<area shape=\"rect\" coords=\"0,43,57,66\" target=\"_blank\" href=\"http:\/\/www.myway.pt\/\" alt=\"\" title=\"\"    \/>");
    document.write("<area shape=\"rect\" coords=\"0,0,62,56\" onclick=\"toggleFold();\" href=\"javascript:void(0)\" alt=\"\" title=\"\"    \/>");
    document.write("<\/map>");
    document.write("    <\/div>");
    document.write("    <div id=\"slidingBar\" class=\"MWslidingBarOpen\" style=\"margin-left:-200px;\">");
    document.write("       <div id=\"btUnfold\" class=\"MWbtUnfold\">");
    document.write("       <a href=\"javascript:void(0);\" onclick=\"toggleFold();\"  class=\"MWbtUnfoldLink\">");
    document.write("            <img id=\"imgBtOpen\" src=\"http:\/\/www.myway.pt\/imgOrange\/bcg_widgetbt_open.png\" alt=\"\" border=\"0\" \/>");
    document.write("            <img id=\"imgBtClose\" src=\"http:\/\/www.myway.pt\/imgOrange\/bcg_widgetbt_close.png\" alt=\"\" border=\"0\" style=\"display:none\" \/><\/a>");
    document.write("        <\/div>");
    document.write("        <div class=\"MWtxtSearchBG\">");
    document.write("            <input id=\"txtSearch\"  type=\"text\" onfocus=\"onFocusInput(this)\" onkeydown=\"onTxtSearchKeyDown()\"; onkeyup=\"onTxtSearchKeyUp('" + partner + "');\" class=\"MWtxtSearch\"");
    document.write("                value=\"Procurar artista ou género\" \/>");
    document.write("        <\/div>");
    document.write("        <div id=\"loaderImg\" class=\"MWloaderImg\">");
    document.write("            <img src=\"http:\/\/www.myway.pt\/imgOrange\/ajax-loaderPartner.gif\" alt=\"\" \/>");
    document.write("        <\/div>");
    document.write("    <\/div>");
    document.write("<\/div>");
    document.write("        <div id=\"PanelSearch\" class=\"MWpanelSearch\">");
    document.write("<iframe id=\"ifr\" onload=\"iframeLoaded()\" SCROLLING=\"no\" width=\"1px\" height=\"1px\" allowtransparency=\"true\" frameborder=0 src=\"\"></iframe>");
    document.write("<div id=\"btFechar\" style=\"position: absolute; z-index: 100; left: 258px; top: 6px;display:none\">");
    document.write("<a href=\"javascript:void(0);\" class=\"pesquisar_Fechar\" onclick=\"closeResultPanel();\">X</a></div>");
    document.write("<\/div>");
}

// Widget de procura IOnline
function writeWidgetIOnline() {
    document.write("<link href=\"http:\/\/www.myway.pt\/Styles\/mywaysearch.css\" rel=\"stylesheet\" type=\"text\/css\" \/>");
    document.write("<div id=\"searchBarTop\" class=\"MWsearchBar\">");
    document.write("    <div class=\"MWslidingBarClosed\">");
    document.write("        <img usemap=\"#Image-Maps_6201006141221104\" src=\"http:\/\/www.myway.pt\/widgets\/img\/bcg_widget01_ionline.png\" alt=\"\" \/>");
    document.write("<map id=\"_Image-Maps_6201006141221104\" name=\"Image-Maps_6201006141221104\">");
    document.write("<area shape=\"rect\" coords=\"0,43,57,66\" target=\"_blank\" href=\"http:\/\/www.myway.pt\/\" alt=\"\" title=\"\"    \/>");
    document.write("<area shape=\"rect\" coords=\"0,0,62,56\" onclick=\"toggleFold();\" href=\"javascript:void(0)\" alt=\"\" title=\"\"    \/>");
    document.write("<\/map>");
    document.write("    <\/div>");
    document.write("    <div id=\"slidingBar\" class=\"MWslidingBarOpenIOnline\" style=\"margin-left:-200px;\">");
    document.write("       <div id=\"btUnfold\" class=\"MWbtUnfold\">");
    document.write("       <a href=\"javascript:void(0);\" onclick=\"toggleFold();\"  class=\"MWbtUnfoldLink\">");
    document.write("            <img id=\"imgBtOpen\" src=\"http:\/\/www.myway.pt\/imgOrange\/bcg_widgetbt_open.png\" alt=\"\" border=\"0\" \/>");
    document.write("            <img id=\"imgBtClose\" src=\"http:\/\/www.myway.pt\/imgOrange\/bcg_widgetbt_close.png\" alt=\"\" border=\"0\" style=\"display:none\" \/><\/a>");
    document.write("        <\/div>");
    document.write("        <div class=\"MWtxtSearchBG\" style=\"margin-left:-20px;\">");
    document.write("            <input id=\"txtSearch\"  type=\"text\" onfocus=\"onFocusInput(this)\" onkeydown=\"onTxtSearchKeyDown()\"; onkeyup=\"onTxtSearchKeyUp('iOnline');\" class=\"MWtxtSearch\"");
    document.write("                value=\"Procurar artista ou género\" \/>");
    document.write("        <\/div>");
    document.write("        <div id=\"loaderImg\" class=\"MWloaderImg\">");
    document.write("            <img src=\"http:\/\/www.myway.pt\/imgOrange\/ajax-loaderPartner.gif\" alt=\"\" \/>");
    document.write("        <\/div>");
    document.write("    <\/div>");
    document.write("<\/div>");
    document.write("        <div id=\"PanelSearch\" class=\"MWpanelSearch\">");
    document.write("<iframe id=\"ifr\" onload=\"iframeLoaded()\" SCROLLING=\"no\" width=\"1px\" height=\"1px\" allowtransparency=\"true\" frameborder=0 src=\"\"></iframe>");
    document.write("<div id=\"btFechar\" style=\"position: absolute; z-index: 100; left: 258px; top: 6px;display:none\">");
    document.write("<a href=\"javascript:void(0);\" class=\"pesquisar_Fechar\" onclick=\"closeResultPanel();\">X</a></div>");
    document.write("<\/div>");
}



// Funções utilizadas pela widget

var ajaxRequest; // Variavel com o objecto que faz os pedidos ajax
var _WAIT_TIME = 500; // Tempo esperado até se enviar o texto de procura para o servidor
var _timeoutFunction; // handler do temporizador para o envio da procura

function onTxtSearchKeyDown() {
    if (_timeoutFunction != null) clearTimeout(_timeoutFunction);
}
function onTxtSearchKeyUp(partnerName) {
    if (_timeoutFunction != null) clearTimeout(_timeoutFunction);
    _timeoutFunction = setTimeout("normalSearch('" + partnerName + "')", _WAIT_TIME);
}

function normalSearch(partnerName) {
    if (isOpen == false)
        return;

    // Obter objectos
    var PanelSearch = document.getElementById("PanelSearch");
    var ifr = document.getElementById("ifr");
    var txtSearch = document.getElementById("txtSearch");

    // Testar se há algo para procurar
    if (!txtSearch.value) {
        closeResultPanel();
        return;
    }

    // Gif de actividade
    showPreLoad('loaderImg');
    openResultPanel(); 
    ifr.src = "";


    // Função que vai receber a resposta ajax
    ifr.src = "http://www.myway.pt/ground/PartnerSearch.aspx?search=" + txtSearch.value + "&partner=" + partnerName;
}
function iframeLoaded() {
    hidePreLoad('loaderImg');
}

function ajaxSearch(partnerName) {
    if (isOpen == false)
        return;

    // Obter objectos
    var PanelSearch = document.getElementById("PanelSearch");
    var txtSearch = document.getElementById("txtSearch");

    // Testar se há algo para procurar
    if (txtSearch.value == '') {
        closeResultPanel();
        return;
    }
    
    // Gif de actividade
    showPreLoad('loaderImg');
    PanelSearch.innerHTML = '';
    openResultPanel();
    
    
    ajaxRequest = getAjaxRequest();  // Obter objecto ajax

    // Função que vai receber a resposta ajax
    ajaxRequest.onreadystatechange = function() {
        if (ajaxRequest.readyState == 4) {
            var PanelSearch = document.getElementById("PanelSearch");

            PanelSearch.innerHTML = ajaxRequest.responseText;
            hidePreLoad('loaderImg');
        }
    }
    ajaxRequest.open("GET", "http://www.myway.pt/ground/PartnerSearch.aspx?search=" + txtSearch.value + "&partner=" + partnerName, true);
    ajaxRequest.send(null);
}

function getAjaxRequest() {
    if (ajaxRequest) {
        ajaxRequest.abort();

        return ajaxRequest;
    }
    var request = false;
    try {
        request = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (err2) {
        try {
            request = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (err3) {
            try {
                request = new XMLHttpRequest();
            }
            catch (err1) {
                request = false;
            }
        }
    }
    return request;
}

function openResultPanel() {
    var ifr = document.getElementById("ifr");
    showPreLoad('PanelSearch');
    showPreLoad('btFechar');
    ifr.height = "360px";
    ifr.width = "275px";
}
function closeResultPanel() {
    var ifr = document.getElementById("ifr");
    hidePreLoad('PanelSearch');
    hidePreLoad('btFechar');
    ifr.height = "1px";
    ifr.width = "1px";
}


// Mostrar um DIV invisivel
function showPreLoad(a) {
    var objPreload = document.getElementById(a);
    if (objPreload != null) {
        objPreload.style.visibility = "visible";
        objPreload.style.display = "block";
    }
}
// Esconder um DIV invisivel
function hidePreLoad(a) {
    var objPreload = document.getElementById(a);
    if (objPreload != null) {
        objPreload.style.visibility = "hidden";
        objPreload.style.display = "none";
    }
}


// Abrir e fechar a barra de procura
var isOpen = false;

function toggleFold() {
    if (isOpen == true) {
        closeBar();
    }
    else {
        openBar();
    }
}
// Abrir a barra de procura
function openBar() {
    var searchBarTop = document.getElementById("searchBarTop");
    var slidingBar = document.getElementById("slidingBar");
    var currentMargin = parseInt(slidingBar.style.marginLeft);
    var txtSearch = document.getElementById("txtSearch");
    searchBarTop.style.width = "280px";
    if (currentMargin >= 0) {
        slidingBar.style.marginLeft = "0px";
        isOpen = true;
        showPreLoad("imgBtClose");
        hidePreLoad("imgBtOpen");
    }
    else {
        txtSearch.value = 'Procurar artista ou género';
        slidingBar.style.marginLeft = (currentMargin - ((currentMargin / 5)))+"px";
        setTimeout('openBar()', 10);
    }
}
// Fechar a barra de procura
function closeBar() {
    closeResultPanel();
    var searchBarTop = document.getElementById("searchBarTop");
    var slidingBar = document.getElementById("slidingBar");
    var currentMargin = parseInt(slidingBar.style.marginLeft);
    if (currentMargin <= -200 ) {
        slidingBar.style.marginLeft = "-200px";
        isOpen = false;
        hidePreLoad("imgBtClose");
        showPreLoad("imgBtOpen");
        searchBarTop.style.width = "80px";
    }
    else {
        var delta = 200 + currentMargin;
        slidingBar.style.marginLeft =  currentMargin - (1+(delta / 5)) + "px";
        setTimeout('closeBar()', 10);
    }
}

// Apagar o conteudo da barra de procura
function onFocusInput(a) {
    if (a.value == 'Procurar artista ou género') {
        a.value = '';
    }
}

function openWidget(url) {
    window.open(url, 'mywayWidgetWindow', 'width=470,height=382,location=no,toolbar=no,menubar=no,resizable=no,status=no,scrollbars=no');
}

