﻿var lastSelectedDivId = '';
var lastSelectedLinkId = '';

/// Toogle the visiblity of a branch.
function toogleBranch(linkId, divId) {
    var divElem = $(divId);
    var linkElem = $(linkId);

    if (lastSelectedDivId == '') 
    {
        if (divElem.style.display == 'none') {
            new Effect.BlindDown(divId, {duration: .3});
            
            Element.addClassName(linkId, 'shrink');
            Element.removeClassName(linkId, 'expand');
            
            lastSelectedDivId = divId;
            lastSelectedLinkId = linkId;
        }
        else 
        {
            new Effect.BlindUp(divId, {duration: .3}); 
            
            Element.addClassName(linkId, 'expand');
            Element.removeClassName(linkId, 'shrink');
            
            lastSelectedDivId = '';
            lastSelectedLinkId = '';
        }
    }
    else {
        if (divElem.style.display == 'none') {
            new Effect.BlindDown(divId, {duration: .3});
            
            Element.addClassName(linkId, 'shrink');
            Element.removeClassName(linkId, 'expand');
            
            new Effect.BlindUp(lastSelectedDivId, {duration: .3}); 
            
            Element.addClassName(lastSelectedLinkId, 'expand');
            Element.removeClassName(lastSelectedLinkId, 'shrink');
            
            lastSelectedDivId = divId;
            lastSelectedLinkId = linkId;
        }
        else {
            new Effect.BlindUp(divId, {duration: .3}); 
            
            Element.addClassName(linkId, 'expand');
            Element.removeClassName(linkId, 'shrink');
            
            lastSelectedDivId = '';
            lastSelectedLinkId = '';            
        }   
    }    
}

function toogleByTitleDisplay() {
    var divId = 'medProfilerDiv';
    var divElem = $(divId);
    if (divElem.style.display == 'none') {
    
        divElem.style.top =  Position.cumulativeOffset($('dnn_dnnSEARCH_imgWithTitleShow'))[1] + 24 + 'px';
        divElem.style.left = $('andenNiveauMenu').offsetLeft + 'px';
        divElem.style.width = '944px';
        
        divElem.style.left = Position.cumulativeOffset($('andenNiveauMenu'))[0] + 'px';
        
        //alert(divElem.style.left);
        
        //alert(Position.cumulativeOffset($('dnn_dnnSEARCH_imgWithTitleShow'))[0]);
              
        $('dnn_dnnSEARCH_imgWithTitleShow').src = $('dnn_dnnSEARCH_imgWithTitleShow').src.replace('Show', 'Hide');
              
        new Effect.BlindDown(divId, {duration: .3});
    }
    else {
        $('dnn_dnnSEARCH_imgWithTitleShow').src = $('dnn_dnnSEARCH_imgWithTitleShow').src.replace('Hide', 'Show');

    
        new Effect.BlindUp(divId, {duration: .3}); 
    }   
}

function callback() {}

function onCheckboxClick(checkboxName) {
    var isFirst = true;
    var textForChoices = '';
    var idChoices = '';
    
    var checkBoxes = Form.getElements('Form');
    
    checkBoxes.each(function(checkBox) {
        
        if (checkBox.type == 'checkbox' && checkBox.name == checkboxName && checkBox.checked) {
                
            if (isFirst) {
                isFirst = false;                
            }
            else {
                textForChoices = textForChoices + ", ";
                idChoices = idChoices + ",";
            }
            
            textForChoices = textForChoices + getTitleByValueTable[checkBox.value];
            idChoices = idChoices + checkBox.value;
        }        
    });   
    
    //alert('-' + textForChoices + '-');
    
    if (textForChoices == '') {
        textForChoices = '&lt; Der er ikke valgt titler endnu &gt;';
    }
    
    $('valgteTitlerDiv').style['background-color'] = '#ffffff';
    $('valgteTitlerDiv').innerHTML = textForChoices;
    new Effect.Highlight('valgteTitlerDiv', { duration: 0.5 ,startcolor:'#F9FF92', endcolor:'#ffffff'});
    
    
    gemTitle(idChoices, callback);
}

function reetablishTitleRequest() {
    new Effect.Fade('sendTitleRequestConfirmDiv',{ duration: 0.2, queue: 'front' });
    new Effect.Move('sendTitleRequestDiv',{ x: -400, y: 0, mode: 'relative', duration: 0.2});
    new Effect.Appear('sendTitleRequestDiv');
}

function sendTitleRequest() {
        if ($('titleRequestTextbox').value != '') 
        {
            new Effect.Move('sendTitleRequestDiv',{ x: 400, y: 0, mode: 'relative', duration: 0.2});
            new Effect.Fade('sendTitleRequestDiv',{ duration: 0.2 });
            new Effect.Appear('sendTitleRequestConfirmDiv', {queue: 'end'});
            setTimeout ( reetablishTitleRequest, 3000 );

            
            DesktopModules_ucMinProfil_KategorierTab.SendTitelAnmodning($('titleRequestTextbox').value, callback);
            $('titleRequestTextbox').value = '';
        }
        else {
            new Effect.Pulsate('titleRequestTextbox', {duration:1});
        }
}

function changeTitle(titleId) {
    window.location.hash = titleId;
}

var selectedTitleId = '';

function selectTitle(titleId, titleName) {
    selectedTitleId = titleId;
    
    $('loadingProfiles').style.display = 'block';
    $('profilerEfterTitel').innerHTML = '';
    $('titelTekst').innerHTML = 'Profiler med titlen: ' + titleName;
    YourCompany.Modules.ucProfilerEfterTitel.ViewucProfilerEfterTitel.findProfiler(titleId, selectTitleCallback);
}

function profilTemplate(val) {
    var result = '';
    
    result = result + '<div class="profil" style="height: 95px; width: 190px; float: left;">';
    
    result = result + '<a class="profilLink" href="' + val.profilUrl + '" >';
    
    result = result + '    <table width="180" height="95" cellspacing="0" cellpadding="0" border="0">';
    result = result + '        <tbody><tr>';
    result = result + '            <td class="profilbilledeBaggrund" valign="middle" align="center" style="padding: 0px; height: 95px; width: 70px;">';
    result = result + '            <img class="centeredImage" style="border-width: 0px;display:block;" alt="Profil:' + val.navn + '" src="' + val.imageUrl + '" />';
    result = result + '            </td>';
    result = result + '            <td style="padding: 0px; height: 95px; width: 95px;vertical-align:top;padding-left:5px;">';
    
    result = result + '    <span style="font-size: 10px;">';
    result = result + '    <strong>' + val.navn + '</strong><br/>';
    result = result + '    ' + val.titel + '<br/>';        
    result = result + '    </span>';
    
    result = result + '            </td>';
    
    result = result + '         </tr>';
    result = result + '     </tbody></table>';
    
    
    result = result + '</a>';
    result = result + '</div>';
    
    return result;
}

function selectTitleCallback(result) {
    if (result.request.args.titelId == selectedTitleId) {
        $('loadingProfiles').style.display = 'none';
        $('profilerEfterTitel').innerHTML = '';
    
        var stringBuilder = '';
    
        for (var i = 0; i < result.value.length; i++) {
            stringBuilder = stringBuilder + (profilTemplate(result.value[i]));
    
            if ((i + 1) % 5 == 0) {
                stringBuilder = stringBuilder + '<br style="clear: both;" />';
                stringBuilder = stringBuilder + '<div style="height: 15px;"></div>';
            } 
        }
        $('profilerEfterTitel').innerHTML = stringBuilder;   
        $('profilerEfterTitel').scrollIntoView(true);
    }
}

window.onload = function() {
     // initialiseStateFromURL();
     setInterval(pollHash, 100);
}

var recentHash = "";
function pollHash() {

  if (window.location.hash==recentHash) {
    return; // Nothing's changed since last polled.
  }
  recentHash = window.location.hash;

  // URL has changed, update the UI accordingly.
  var titelId = recentHash.replace('#','');

  if (titelId != '') {
    selectTitle(titelId, getTitleByValueTable[titelId]);
  }
}