
function checkUser(user) {
    //alert(user)
    if(user == "") return
    new Ajax.Request('/api/username.jsp?user=' + encodeURIComponent(user), {
        method: 'get',
        onSuccess : function(transport) {
            //alert(transport.responseText)
            if(transport.responseText == "1"){
                //$('idusername').innerHTML = 'Your username is available.',
                /*$('idusername').style.backgroundColor = '#00ff00'
                $('idusername').style.color = '#000000'
                $('idusername').style.border = '1px solid #0f0';*/
                $('idusername').removeClassName("username_not_available")
            }
            else{
                //$('idusername').innerHTML = 'Sorry, that username is already taken.',
                /*$('idusername').style.backgroundColor = '#D01F3C';
                $('idusername').style.color = '#ffffff';
                $('idusername').style.border = '1px solid #D01F3C';*/
                $('idusername').addClassName("username_not_available")
            }
            //alert(transport.responseText)
        }
    });
}

function checkStatus() {
    new Ajax.Request('/api/checkStatus.jsp', {
        method: 'get',
        onSuccess : function(transport) {
            if(transport.responseText == "1"){
                $('userLogout').removeClassName("hiddenByDefault")
            }
            else{
                $('userLogout').addClassName("hiddenByDefault")
            }
        }
    });
}

/* Platespiller */

function omg() {
    var width = 0
    if(typeof(window.innerWidth) == "number") {
        width = window.innerWidth
    }
    else if(document.documentElement && document.documentElement.clientWidth) {
        width = document.documentElement.clientWidth
    }
    else {
        width = document.body.clientWidth
    }
    return width
}

function realignNavigation(e) {
if($("navigation") == null) return

if(omg() < 972) {
$("navigation").addClassName("movenavigation");
}
else {
$("navigation").removeClassName('movenavigation');
}
}
Event.observe(window, "resize", realignNavigation)
//Event.observe(window, "load", realignNavigation)


onload = function() {
    realignNavigation()

    if(document.forms[1] && document.forms[1].elements[0] && !document.forms[1].elements[0].disabled) {
        //document.forms[1].elements[0].focus()
    }

    if($("idq") && $("idq").value == "" && $("searchStore") == null) $("idq").focus()

    //if($("login")) $("idemail").focus()


    /*$('homeArea').onmouseover = function() {
        //$('topp').style.background='magenta'
    }

    $('homeArea').onmouseout = function() {
        //$('topp').style.background=''
    }*/

    if($('idusername')) {
        $('idusername').onblur = function() {
            checkUser($("idusername").getValue())
        }
        $('idusername').onkeyup = function() {
            if($('idusername').value != "")
            $('realURL').innerHTML = "http://"+location.host+"/user/" + $('idusername').value
            else
            $('realURL').innerHTML = "http://"+location.host+"/user/USERNAME"
        }
    }
}


function barrier(id) {
    $(id).src = "/barrier.png"
}

function availability(id) {
    $('idusername').style.background='#d01f3c';
    $('idusername').style.color='#ffffff';
    $('idusername').onblur = function() {
        $('idusername').style.background=''
        $('idusername').style.color=''
    }
}

var current, currentURL
function w(o, d) {
    //o.style.background='#ccc';;
    current = o;
    currentURL = d;
    window.status = d;
    return true;
}
function l(d) { location.href = d; }

/*if(navigator.userAgent.indexOf("Safari") != -1) {
    document.write("<style>input[disabled='disabled'] { color:gray; }</style>")
}
else if(navigator.userAgent.indexOf("iPhone") != -1) {
    document.write("<style>#main{ width:100%; color:red !important; }</style>")
}*/


/* Dark CSS */

var wideMode = false


/*if(location.href.indexOf("app.teppefall.com") == -1) {
    alert("df")
    setActiveStyleSheet('Dark')
    wideMode = true
}*/

function appMode() {
    if(getActiveStyleSheet() != "Dark") {
        setActiveStyleSheet('Dark')
        wideMode = true
    }
    else {
        setActiveStyleSheet('Normal')
        wideMode = false
    }
}

function toggleStylesheet() {
    if(!wideMode) {
        //document.images["layout"].src = "/ikoner/liste.png"
        setActiveStyleSheet('Wide') // Dark før
        wideMode = true
    }
    else {
        //document.images["layout"].src = "/ikoner/bokser.png"
        setActiveStyleSheet('Normal');
        wideMode = false
    }
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
if(title == "Wide") {
    wideMode = true
}

onunload = function(e) {
    var title = getActiveStyleSheet();
    if(title != null) createCookie("style", title, 365);
}


/*
http://www.alistapart.com/stories/alternate/
*/

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days,https) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  var secure="";
  //if(https) secure="; secure=true" /// ANGST
  document.cookie = name+"="+value+expires+"; path=/"+secure;
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


function deleteCookie(name) {
    createCookie(name, "")
}


function mail2(name) {
    var alpha = "@"
    var hostname = (location.hostname.indexOf(".") != location.hostname.lastIndexOf(".")) ?
        location.hostname.substring(location.hostname.indexOf(".")+1,location.hostname.length) :
        location.hostname
        ;
    prompt("Report junk email to your ISP.", name + alpha + hostname)
}

function mail(name) {
var alpha = "@", domain = "teppefall", dot = ".", com = "com"
prompt("Email", name + alpha + domain /* bla bla */ +dot+ com)
}

function lazy(file, id, callback) {
   var head = document.getElementsByTagName("head")[0];
   var script = document.createElement('script');
   script.type = 'text/javascript';
   if(id) {
       script.id = id;
       script.addEventListener("load", callback)
    }
   script.src = file;
   head.appendChild(script);
}

checkStatus();
