﻿

$(document).ready(function() {
    getActiveFontSize();


  

    if (typeof eidsvollMen != 'undefined') {

        showEidsvollMan(selectedMan);
    }

    if (document.getElementById('day') != null)
        showDiaryByDate(document.getElementById('day').value, document.getElementById('month').value);

    $('#show').click(function(event) {
        showDiaryByDate(document.getElementById('day').value, document.getElementById('month').value);
    });

    $('.arrow-left').click(function(event) {
        showDiary(-1);
    });

    $('.arrow-right').click(function(event) {
        showDiary(+1);
    });


    $('#searchname').keyup(function(event) {
        if (event.keyCode == 13) {
            $("#searchclick").click();
        }
    });
});






function FacebookLink() {
    var link = 'http://www.facebook.com/share.php';
    link += '?u=' + encodeURI(location.href);
    link += '&amp;t=' + encodeURI(document.title);
    return link;
}



function showTimeline(linkId, eventId, posX, posY) 
{
    var evnt = $("#" + eventId);
    var lnk = $("#" + linkId);

    evnt.css('top',posY + 50);
    evnt.css('left',posX);
    evnt.fadeTo("fast", 1.0);
}

function closeTimeline(eventId) 
{
    var evnt = $("#" + eventId);
    evnt.hide();
}

function showEidsvollMan(selectedMan) {
    var sel = 0;
    if (selectedMan > 0) {
        for (i = 0; i < eidsvollMen.length; i++) {
            if (eidsvollMen[i] == selectedMan) {
                sel = i;
            }
        }
    }            
    selectedMan = eidsvollMen[sel];    

        
    showEidsvollManById(selectedMan);
}

function showEidsvollManById(id) {
    $("#eidsvollmen-details").html($("#m" + id).html());


    
    
    var src = $("#mi" + id).attr("src");
    if(src != "")
        $("#eidsvollmen-photo").css("background-image", "url(" + src + ")");
}

function searchEidsvollmen2(query, sitedirId) {
    window.location = '?aid=' + sitedirId + '&q=' + query;
}

function searchEidsvollmen(evt, query, sitedirId) {
    if (checkForEnter(evt)) {
        window.location = '?aid=' + sitedirId + '&q=' + query;
    }     
}

function showDiaryByDate(day, month) {
    //if (!validDate(day, month)) {
    //    alert('Vennligst velg en gyldig dato:' + day + "/" + month);        
//        return;
  //  }

    var id = diaryDates[day + '|' + month];
    
    //var flashEmbed = document.getElementById('Eidsvoll-diary');
    //if (flashEmbed) {
    //    alert('Flash elem.id: ' + flashEmbed.id);
    //    flashEmbed.selectedId(id); // For Flash    
    //} else {
        showDiaryById(id); // For fallback
    //}
}

function showDiary(direction) {
    var sel = 0;
    for (i = 0; i < diaries.length; i++) {
        if (diaries[i] == selectedDiary) {
            sel = (i + direction);
            if ((sel) >= diaries.length) sel = 0;
            if ((sel) < 0) sel = (diaries.length - 1);
        }
    }
    
    
    
    showDiaryById(diaries[sel]);
}

function showDiaryById(id) {
    selectedDiary = id;
    setCookie('selecteddiary', id, 100);    
    $(".diary-box").hide();
    $("#diary-content").html($("#diary" + id).html());
    $(".diary-box").fadeTo("slow", 1.0);
}


var selectedPhoto = 0;
function showPhoto(direction) {
  
    var sel = (selectedPhoto + direction);
    if ((sel) >= photos.length) sel = 0;
    if ((sel) < 0) sel = (photos.length - 1);
    
    selectedPhoto = sel;
    var src = photos[selectedPhoto];
    var txt = texts[selectedPhoto];
    $(".photos").hide();
    if (src != "") {
        $("#guide-photo").css("background-image", "url(" + src + ")");
        $("#hover-mid-img").attr("alt", txt);
        $("#hover-mid-img").attr("title", txt);                  
    }    
    $(".photos").fadeTo("slow", 1.0);
}

function validDate(day, month) {
    var timestamp = Date.parse('1814-' + padNum(month) + '-' + padNum(day));
    return !isNaN(timestamp);
}

function padNum(number) {
    return (number < 10 ? '0' : '') + number
}

function checkForEnter(event) {
    if (event.keyCode == 13) {
        return true;
    }
    return false;
}

function getActiveFontSize() {
    var size = getCookie('fontsize');    
    setActiveFontSize(size);
}

function setActiveFontSize(size) {
    // Change body font size and set cookie
    var red = '62.5%';
    switch (size) {
        case "small": red = '50%'; break;
        case "large": red = '82%'; break;
        case "largest": red = '99%'; break;        
    }
    $("body").css("font-size", red);
    setCookie('fontsize', size,100);
}

function getCookie(Name) {
    var search = Name + "="
    if (document.cookie.length > 0) { // if there are any cookies
        offset = document.cookie.indexOf(search)
        if (offset != -1) { // if cookie exists
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset)
            // set index of end of cookie value
            if (end == -1)
                end = document.cookie.length
            return unescape(document.cookie.substring(offset, end))
        }
    }
}

function setCookie(name, value, daysExpire) {
    if (daysExpire) {
        var expires = new Date();
        expires.setTime(expires.getTime() + 1000 * 60 * 60 * 24 * daysExpire);
    }
    document.cookie = name + "=" + escape(value) + (daysExpire == null ? "" : (";expires=" + expires.toGMTString())) + ";path=/";
}
