﻿// constants
var DEFAULT_FONT_SIZE = 13;
var FONT_SIZE_RADIUS = 3;
var FONT_SIZE_COOKIE = 'fontSize';
var STYLESHEET_ID = 'styles';
var PERSISTENT_EXECUTE_DELAY = 200;
var SEARCH_AVAILABLE = false;

var fontSize;

// init section
var YUIModules = YUI().use('cookie','stylesheet','substitute','history',init);
function init() {
    fontSize=parseInt(getCookie(FONT_SIZE_COOKIE));
    if(isNaN(fontSize))fontSize=DEFAULT_FONT_SIZE;
    putTextSize(fontSize);
    YUIModules.StyleSheet(STYLESHEET_ID).set('body',{display:'block'});
}

// functions
function $(id) {return document.getElementById(id);}
function persistentExecute(func) {
    try {
        func();
    } catch (e) {
        setTimeout(function(){persistentExecute(func);},PERSISTENT_EXECUTE_DELAY);
    }
}
function getCookie(cookie) {
    return YUIModules.Cookie.get(cookie);
}
function setCookie(cookie,value,days) {
    var expireDate = new Date();
    expireDate.setTime(expireDate.getTime()+days*24*60*60*1000);
    YUIModules.Cookie.set(cookie,value,{expires:expireDate,path:'/'});
}
function putTextSize(size) {
    YUIModules.StyleSheet(STYLESHEET_ID).set('body',{fontSize:size+'px'});
    setCookie(FONT_SIZE_COOKIE,size,7);
}
function changeTextSize(diff) {
    var newFontSize = fontSize + diff;
    if (newFontSize >= DEFAULT_FONT_SIZE - FONT_SIZE_RADIUS &&
        newFontSize <= DEFAULT_FONT_SIZE + FONT_SIZE_RADIUS) {
        putTextSize(newFontSize);
        fontSize = newFontSize;
    }
}
function writeMail(user, domain) {
    address = user + "@" + domain;
    document.write(address);
}
function writeMailLink() {
    if (arguments.length >= 2) {
        address = arguments[0] + "@" + arguments[1];
        link = "<a href=\"mailto:" + address + "\">";
        if (arguments.length == 2) link += address;
        else if (arguments.length == 3) link += arguments[2];
        else return;
        link += "</a>"
        document.write(link);
    }
}
var searchBox;
var searchBoxOriginalValue;
function searchBoxOnfocus(sender) {
    if (sender.value == searchBoxOriginalValue) {
        sender.value = "";
    }
}
function searchBoxOnblur(sender) {
    sender.value = YUIModules.Lang.trim(sender.value);
    if (sender.value == "") {
        sender.value = searchBoxOriginalValue;
    }
}
function searchSubmit(page) {
    searchBox.value = YUIModules.Lang.trim(searchBox.value);
    if (searchBox.value != "" && searchBox.value != searchBoxOriginalValue) {
        document.location.href = page+'?query='+encodeURIComponent(escape(searchBox.value));
    }
}
