﻿function showHideOneDivPerInfo(infoId, infoText, divId, controlId, type)
{
    // div = _divMoreInfo;
    var div = document.getElementById(divId);
    if (div == null) return;
    
    // info = _spPlusInfo
    var info = document.getElementById(infoId);
    if (info == null) return;
    var infoNode = info.firstChild;
    
    if (infoNode.nodeValue == '+ Info') 
    {
        // Apresentar informação, mas primeiro verificar se já existe no lado do cliente.
        var infoText = document.getElementById(infoText);
        if (infoText.firstChild == null)
        {
            getInfoByAJAX(infoText, controlId, type);
        }
        else
        {
            setScroll(infoText);
        }
        
        div.style.display = 'block';
        infoNode.nodeValue = '- Info';
    }
    else
    {
        div.style.display = 'none';
        infoNode.nodeValue = '+ Info';
    }
}

// Array indexado pelo divId que indica o id do ultimo controlo que mostrou a informação.
var selectedExtraInfoId = new Object();

function showHideInfo(infoId, divId, controlId, type)

{


    // div = _divMoreInfo;
    var div = document.getElementById(divId);
    if (div == null) return;
    
    // info = _spPlusInfo
    var info = document.getElementById(infoId);
    if (info == null) return;
    var infoNode = info.firstChild;
    
    var restartScroll = false;
    if (infoNode.nodeValue == '+ Info') 
    {        
        // O paragraph onde vai ser colocada a informação é o primeiro filho do div.
        var infoText = div.getElementsByTagName('p')[0];
        
        if (div.style.display == 'block')
        {
            // Se o div estiver visivel, verificar se a informação pertence ao extra clicado,
            // para em caso negativo primeiro fechar o anterior.
            if (selectedExtraInfoId[divId] != infoId) closeExtraInfo(divId);
        }
        
        // Se o div já tiver informação, verificar se pretence ao extra clicado.
        if (infoText.firstChild == null || selectedExtraInfoId[divId] != infoId)
        {
            // Guardar o extra que pedio a informação para quando se carregar em "Fechar" alterar texto para +Info.
            selectedExtraInfoId[divId] = infoId;
            
            getInfoByAJAX(infoText, controlId, type);
        }
        else
        {
            restartScroll = true;
        }

        div.style.display = 'block';
        infoNode.nodeValue = '- Info';
        
        if (restartScroll) setScroll(infoText);
    }
    else
    { 
        div.style.display = 'none';
        infoNode.nodeValue = '+ Info';
    }
}

function closeExtraInfo(divId)
{
    if (selectedExtraInfoId[divId] == undefined) return;
    
    // div = _divMoreInfo;
    var div = document.getElementById(divId);
    if (div == null) return;
    
    // info = _spPlusInfo
    var info = document.getElementById(selectedExtraInfoId[divId]);
    if (info == null) return;
    
    // "Fechar" informação
    div.style.display = 'none';
    info.firstChild.nodeValue = '+ Info';
}

function getInfoByAJAX(infoText, controlId, type)
{

    var xhr = new createXHR();
    xhr.open('GET', '../Handlers/getInfoFor.ashx?id='+controlId+'&type='+type, true);
    xhr.onreadystatechange = function ()
        {
            if (xhr.readyState == 4 && xhr.status == 200)
            {
                if (xhr.responseText=="") return;
                
                var info = eval("("+xhr.responseText+")");

                // Graças a bugs no IE temos que adicionar um filho novo ao elemento!
                var divChilds = infoText.getElementsByTagName('div');
                for (var i = 0; i < divChilds.length; ++i)
                {   
               
                    if (divChilds[i].id == 'ajaxDivInfo')
                    {
                       
                     try{
                            divChilds[i].innerHTML = info.infoText;
                            setScroll(infoText);
                            return;
                        }
                        catch (ex)
                        {
                            infoText.removeChild(divChilds[i]);
                            setScroll(infoText);
                            break;
                        }
                    }
                }
                
                var ajaxDivInfo = document.createElement("div");
                ajaxDivInfo.innerHTML = info.infoText;
                ajaxDivInfo.id = 'ajaxDivInfo';
                infoText.appendChild(ajaxDivInfo);
                
                // Verificar se é necessário fazer scroll ... e caso seja de quanto.
                setScroll(infoText);
            }
        };
    xhr.setRequestHeader('If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT');
    xhr.setRequestHeader('Cache-Control', 'no-cache');
    xhr.send(null);
}


var execChangePack = true;
function ChangePack(newPackId, exec){
    if( execChangePack == false ) return;
    var divAlert = document.getElementById(divId);
    
    var answer = confirm("Ao escolher um item vai reninciar todo o processo de compra. Deseja continuar?")
    if (answer){
        window.open('Packs.aspx?singlepack=' + newPackId,'_self');
    }else{
        var old=document.getElementById( "_rSelected" + selectedPackProducts[0] );
        execChangePack = false;
        old.click();
        execChangePack = true;
    }
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  } 
  return {width: myWidth, height: myHeight};
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return {x: scrOfX, y: scrOfY};
}

function setScroll(refElement)
{
    var elementY = findPosY(refElement) + refElement.offsetHeight;
    var windowSize = getWindowSize();
    var windowScroll = getScrollXY();
    //if (elementY > windowScroll.y + windowSize.height || elementY < windowScroll.y - windowSize.height)
    if (elementY < windowScroll.y || elementY > windowScroll.y + windowSize.height)
    {
        var scrollY = elementY - windowSize.height;
        if (scrollY < 0) scrollY = 0;
    
         window.scrollTo(0, elementY - windowSize.height + 2);
    }
}


function showHideInfoDetail(infoId, divId, controlId, type)

{


    // div = _divMoreInfoDetalhe;
    var div = document.getElementById(divId);
    if (div == null) return;
    
    // info = _spPlusInfoDetalhe
    var info = document.getElementById(infoId);
    if (info == null) return;
    var infoNode = info.firstChild;
    
    var restartScroll = false;
    if (infoNode.nodeValue == '+ Detalhe') 
    {        
        // O paragraph onde vai ser colocada a informação é o primeiro filho do div.
        var infoText = div.getElementsByTagName('p')[0];
        
        if (div.style.display == 'block')
        {
            // Se o div estiver visivel, verificar se a informação pertence ao extra clicado,
            // para em caso negativo primeiro fechar o anterior.
            if (selectedExtraInfoId[divId] != infoId) closeExtraInfo(divId);
        }
        
        // Se o div já tiver informação, verificar se pretence ao extra clicado.
        if (infoText.firstChild == null || selectedExtraInfoId[divId] != infoId)
        {
            // Guardar o extra que pedio a informação para quando se carregar em "Fechar" alterar texto para +Info.
            selectedExtraInfoId[divId] = infoId;
            
            getInfoByAJAX(infoText, controlId, 5);
        }
        else
        {
            restartScroll = true;
        }

        div.style.display = 'block';
        infoNode.nodeValue = '- Detalhe';
        
        if (restartScroll) setScroll(infoText);
    }
    else
    { 
        div.style.display = 'none';
        infoNode.nodeValue = '+ Detalhe';
    }
}


function closeInfoDetail(divId)
{
    if (selectedExtraInfoId[divId] == undefined) return;
    
    // div = _divMoreInfo;
    var div = document.getElementById(divId);
    if (div == null) return;
    
    // info = _spPlusInfo
    var info = document.getElementById(selectedExtraInfoId[divId]);
    if (info == null) return;
    
    // "Fechar" informação
    div.style.display = 'none';
    info.firstChild.nodeValue = '+ Detalhe';
}


function ShowHideOther(clickedDivIconId, clickedDivContentId)
{
    var clickedDivIcon = document.getElementById(clickedDivIconId);
    var clickedDivContent = document.getElementById(clickedDivContentId);

    
    if(clickedDivContent.style.display == 'none')// depois de clicar no mais
    {
        
        clickedDivIcon.className='menusImg'; 
        clickedDivContent.style.display='block';
        

        window.scrollTo(0, findPosY(clickedDivIcon));
        
    }
    else     
        {
        
        clickedDivIcon.className='plusImg'; 
        clickedDivContent.style.display='none';
        

    }
    
    closePackProductInfo('_productInfoDiv');
}




function pausecomp(millis) 
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); } 
while(curDate-date < millis);
} 


function BuyOrChangePack (packId,type)
{
        var hf_jumpFinal = document.getElementById('ctl00_ctl00__centralContentPlaceHolder__jumpFinal');
           
        if (type=='Link')
        {
            predefinedSelected_BuyNow(packId); 
            
           hf_jumpFinal.value = 'tv';
           pausecomp(2000);
           __doPostBack('ctl00$ctl00$_aNextMaster','');
           pausecomp(2500);
            //alert('Por favor continue a sua encomenda.');
        }

        if (type=='ComprarJa')
        {
           predefinedSelected_BuyNow(packId);
           
           hf_jumpFinal.value = 'true';
           pausecomp(2000);
           __doPostBack('ctl00$ctl00$_aNextMaster','');
           pausecomp(2500);
          //alert('Por favor conclua a sua encomenda.');
            
        }

}
