﻿var GB_ROOT_DIR = "./scripts/greybox/";

function UpdateAddTxt(sender) {
    if (sender.value == '') { sender.value = 'Ajoutez votre meilleur'; }

}
function GB_myShow(caption, url, /* optional */height, width, callback_fn) {

    var options = {
        caption: caption,
        height: height || 500,
        width: width || 500,
        fullscreen: false,
        show_loading: false,
        callback_fn: callback_fn
    }
    var win = new GB_Window(options);
    return win.show(url);
}

var dhtmlgoodies_slideSpeed = 10; // Higher value = faster
var dhtmlgoodies_timer = 10; // Lower value = faster

var objectIdToSlideDown = false;
var dhtmlgoodies_activeId = false;
var dhtmlgoodies_slideInProgress = false;
//var dhtmlgoodies_counter = 0;

function showHideContent(inputId) {
    if (dhtmlgoodies_slideInProgress) return;
    dhtmlgoodies_slideInProgress = true;
    inputId = inputId + '';
    var numericId = inputId.replace(/[^0-9]/g, '');
    var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);

    objectIdToSlideDown = false;

    if (!answerDiv.style.display || answerDiv.style.display == 'none') {
        if (dhtmlgoodies_activeId && dhtmlgoodies_activeId != numericId) {
            objectIdToSlideDown = numericId;
            slideContent(dhtmlgoodies_activeId, (dhtmlgoodies_slideSpeed * -1));
        } else {

            answerDiv.style.display = 'block';
            answerDiv.style.visibility = 'visible';

            slideContent(numericId, dhtmlgoodies_slideSpeed);
        }
    } else {
        slideContent(numericId, (dhtmlgoodies_slideSpeed * -1));
        dhtmlgoodies_activeId = false;
    }
}

function slideContent(inputId, direction) {

    var obj = document.getElementById('dhtmlgoodies_a' + inputId);
    var contentObj = document.getElementById('dhtmlgoodies_ac' + inputId);
    height = obj.clientHeight;
    if (height == 0) height = obj.offsetHeight;
    height = height + direction;
    rerunFunction = true;
    if (height > contentObj.offsetHeight) {
        height = contentObj.offsetHeight;
        rerunFunction = false;
    }
    if (height <= 1) {
        height = 1;
        rerunFunction = false;
    }

    obj.style.height = height + 'px';
    var topPos = height - contentObj.offsetHeight;
    if (topPos > 0) topPos = 0;
    contentObj.style.top = topPos + 'px';
    if (rerunFunction) {
        setTimeout('slideContent(' + inputId + ',' + direction + ')', dhtmlgoodies_timer);
    } else {
        if (height <= 1) {
            obj.style.display = 'none';
            if (objectIdToSlideDown && objectIdToSlideDown != inputId) {
                document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.display = 'block';
                document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.visibility = 'visible';
                slideContent(objectIdToSlideDown, dhtmlgoodies_slideSpeed);
            } else {
                dhtmlgoodies_slideInProgress = false;
            }
        } else {
            dhtmlgoodies_activeId = inputId;
            dhtmlgoodies_slideInProgress = false;
        }
    }
}



function initShowHideDivs() {
    var divs = document.getElementsByTagName('DIV');
    var divCounter = 1;
    for (var no = 0; no < divs.length; no++) {
        if (divs[no].className == 'dhtmlgoodies_question') {
            divs[no].id = 'dhtmlgoodies_q' + divCounter;
            //divs[no].onclick = PopulateCommerceInfoDiv(divs[no].id);
            var answer = divs[no].nextSibling;
            while (answer && answer.tagName != 'DIV') {
                answer = answer.nextSibling;
            }
            answer.id = 'dhtmlgoodies_a' + divCounter;
            contentDiv = answer.getElementsByTagName('DIV')[0];
            contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px';
            contentDiv.className = 'dhtmlgoodies_answer_content';
            contentDiv.id = 'dhtmlgoodies_ac' + divCounter;
            answer.style.display = 'none';
            answer.style.height = '1px';
            divCounter++;
        }

        //dhtmlgoodies_counter = divCounter;
    }
}

function GetIp() {
    return document.getElementById('ctl00_MyIP').value;
}

function VoteForCommerce(commerceId, categoryId, categoryName, mainPanelId, subPanelId, side, loadingPanelId, scrollId, btnVote) {
    btnVote.disabled = true;
    toggleDisabled(document.getElementById(subPanelId));
    showdiv(loadingPanelId);
    reqviaprovy = PollService.VoteForCommerce(GetIp(), commerceId, categoryId, urlencode(categoryName), mainPanelId, subPanelId, side, document.getElementById(scrollId).scrollTop, scrollId, OnVoteComplete, OnVoteTimeout);
    
    return false;
}

function AddCommerce(txtAddCommerceId, categoryId, categoryName, mainPanelId, subPanelId, side, loadingPanelId, scrollId, btnAdd) {
    btnAdd.disabled = true;
    toggleDisabled(document.getElementById(subPanelId));
    var commerceName;
    commerceName = document.getElementById(txtAddCommerceId).value;
    if (commerceName != 'Entrez votre meilleur')
        reqviaprovy = PollService.AddCommerceAndVote(GetIp(), urlencode(commerceName), categoryId, urlencode(categoryName), mainPanelId, subPanelId, side, document.getElementById(scrollId).scrollTop, scrollId, OnVoteComplete, OnVoteTimeout);

    return false;
}

function PopulateCommerceInfoDiv(inputId) {
    if (dhtmlgoodies_slideInProgress) return;
    var origInputId = inputId;
    inputId = inputId + '';
    var numericId = inputId.replace(/[^0-9]/g, '');
    var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);

    if (!answerDiv.style.display || answerDiv.style.display == 'none') {
        var commerceId = document.getElementById(answerDiv.id).getAttribute('commerceId');
        req = CommerceInfoService.GetInfoPanel(commerceId, numericId, answerDiv.id, origInputId, OnInfoLoadComplete, OnInfoLoadFail);
    }
    else {
        showHideContent(origInputId);
    }

}

function OnInfoLoadComplete(results) {
    document.getElementById(results.split('SPLITHERE')[0]).innerHTML = results.split('SPLITHERE')[2];
    showHideContent(results.split('SPLITHERE')[1]);
}
function OnInfoLoadFail(results) {
    
}

function OnVoteComplete(results) {
    document.getElementById(results.split('SPLITHERE')[0]).innerHTML = results.split('SPLITHERE')[3];
    document.getElementById(results.split('SPLITHERE')[2]).scrollTop = parseInt(results.split('SPLITHERE')[1]);

    initShowHideDivs();
}
function OnVoteTimeout(results) {
    
}

function hidediv(panel) {
    if (document.getElementById) { // DOM3 = IE5, NS6 
        document.getElementById(panel).style.visibility = 'hidden';
    }
    else {
        if (document.layers) { // Netscape 4 
            document.hideShow.visibility = 'hidden';
        }
        else { // IE 4 
            document.all.hideShow.style.visibility = 'hidden';
        }
    }
}

function showdiv(panel) {
    if (document.getElementById) { // DOM3 = IE5, NS6 
        document.getElementById(panel).style.visibility = 'visible';
    }
    else {
        if (document.layers) { // Netscape 4 
            document.hideShow.visibility = 'visible';
        }
        else { // IE 4 
            document.all.hideShow.style.visibility = 'visible';
        }
    }
}

function toggleDisabled(el) {
    try
    {
        if (el.className == 'CommerceVoteButton') 
        {
            el.disabled = true;
        }
    }
    catch (E) { }
    if (el.childNodes && el.childNodes.length > 0) {
        for (var x = 0; x < el.childNodes.length; x++) {
                toggleDisabled(el.childNodes[x]);
        }
    }
}

window.onload = initShowHideDivs;
/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/



    // public method for url encoding
    function urlencode(string) {
        return escape(_utf8_encode(string));
    }

    // public method for url decoding
    function urldecode(string) {
        return _utf8_decode(unescape(string));
    }

    // private method for UTF-8 encoding
    function _utf8_encode(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }

    // private method for UTF-8 decoding
    function _utf8_decode(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

