/*
 * dsv.js - Javascript code for DSV's web
 *
 * By Per Olofsson <pelle@dsv.su.se>, 2008-2012
 */

/* Constants */
var WEBAPI_URL = "/app/";
var DAISY_URL = "http://daisy.dsv.su.se/";
var DAISY_SHOW_PUB_URL = DAISY_URL +
    "anstalld/publikation/publicationInfo.jspa?publikationID=";

/* Language */
var lang = document.documentElement.lang;
var en = (lang == 'en');

/* Placeholder support for browsers which lack it */
$(function () {
    $('input[placeholder]').placeholder();
});

/* Edit page */
$(document).keydown(function (e) {
    if (e.ctrlKey && e.keyCode == 81) { // DOM_VK_Q == 81
        var hostname = window.location.hostname;
        if (hostname == "www.dsv.su.se") hostname = "dsv.su.se";
        window.location = "https://" + hostname +
	    "/adm/edit.cgi?file=" + window.location.pathname;
	return false;
    } else {
	return true;
    }
});

/* Some layout support code */

$(function () {
    /* $('#HeadRowMenu li').mouseover(function () {
        $(this).addClass('hover');
    }).mouseout(function () {
        $(this).removeClass('active');
    }); */
});

/* Throbber */

function createThrobber()
{
    var el = document.createElement("img");
    el.src = "/images/throbber.gif";
    el.width = 24;
    el.height = 24;
    el.className = "throbber";
    return el;
}

/* Daisy stuff */

var DAISY_URL_PATHS = {
    moment: {
	path: "servlet/Momentinfo",
	dim: "width=640,height=450"
    },
    person: {
	path: "servlet/anstalld.Anstalldinfo",
	dim: "width=500,height=250"
    },
    schema: {
	path: "servlet/schema.moment.Momentschema",
	dim: "width=500,height=500"
    },
    student: {
	path: "servlet/student.Studentinfo",
	dim: "width=400,height=300"
    },
    publikation: {
	path: "anstalld/publikation/publicationInfo.jspa",
	dim: "width=500,height=500"
    }
};

function daisyURLFor(what)
{
    return DAISY_URL + DAISY_URL_PATHS[what].path;
}

function daisyPopupDimensionsFor(url)
{
    for (var i in DAISY_URL_PATHS) {
	if (url.indexOf(daisyURLFor(i)) == 0)
	    return DAISY_URL_PATHS[i].dim;
    }
    return "width=500,height=500";
}

function doDaisyPopup(url)
{
    window.open(url, "_blank", "scrollbars=yes,resizable=yes,"
		+ daisyPopupDimensionsFor(url));
    return false;
}

function handleDaisyPopupClick()
{
    return doDaisyPopup(this.href);
}

function makeDaisyPopup(link)
{
    link.onclick = handleDaisyPopupClick;
}

function popupifyDaisyLinks()
{
    var maincol = document.getElementById('MainColumn');

    if (!maincol) return;

    var anchors = maincol.getElementsByTagName('a');

    for (var i = 0; i<anchors.length; i++) {
	var a = anchors[i];

	if (!a.href || a.href.indexOf(DAISY_URL) != 0) continue;

	for (var j in DAISY_URL_PATHS) {
	    if (a.href.indexOf(daisyURLFor(j)) == 0) {
		makeDaisyPopup(a);
		break;
	    }
	}
    }
}

$(popupifyDaisyLinks);

/* Legacy Daisy popup functions */

function showmoment(idnr)
{
    return doDaisyPopup(DAISY_URL + 'servlet/Momentinfo?id='
		        + idnr.toString() + '&ejmedv=true');
}

function showmoment2(kurs, moment)
{
    return doDaisyPopup(DAISY_URL + 'servlet/Momentinfo?kurs='
		        + kurs.toString() + '&moment=' + moment.toString()
		        + '&ejmedv=true');
}

function showperson(idnr)
{
    return doDaisyPopup(DAISY_URL + 'servlet/anstalld.Anstalldinfo' +
		        '?id=' + idnr.toString());
}

function showschema(idnr)
{
    return doDaisyPopup(DAISY_URL
		        + 'servlet/schema.moment.Momentschema?id='
		        + idnr.toString());
}

function showstudent(idnr)
{
    return doDaisyPopup(DAISY_URL + 'servlet/student.Studentinfo?id='
		        + idnr.toString());
}

function showpublication(idnr)
{
    return doDaisyPopup(DAISY_SHOW_PUB_URL + idnr.toString());
}

