function getQueryStringVarValue(q, v){
  var a = q.substring(q.indexOf('?') + 1).split('&');
  var p = [];
  for(var i = 0; i < a.length; ++i){
    var t = a[i].split('=');
    p[decodeURI(t[0])] = decodeURI(t[1]);
  }
  if(v){
    return p[v];
  }
  return p;
}

$(document).ready(function(){
  if(!window.location.hostname.match(/(www\.)?webminster\.(org|de)/) && !window.location.hostname.match(/(www\.)?(.+\.)?langlotz\.info/)){
    var path = (window.location.hostname == "beta.webminster.org" || window.location.hostname == "127.0.0.1") ? window.location.pathname : '';
    $("#siteinfo").each(function(){ $(this).html("This isn't the official site! Goto <a href='http://www.webminster.org"+path+"'>www.webminster.org</a> to visit the official one!"); });
    $("#siteinfo").slideDown("slow");
  }
  $("#wikicmds li.print a").each(function(){ $(this).attr("href", "javascript:window.print();"); $(this).removeAttr("target"); });
  
  $("#sitesearchfield").focus(function(){
    if(this.value == this.defaultValue){
      this.value = "";
      this.className = "selected";
    }
  });
  
  $("#sitesearchfield").blur(function(){
    if(this.value == ""){
      this.className = "unselected";
      this.value = this.defaultValue;
    }
  });
  
  var thumbs = $("a[rel^='colorbox']");
  var thumbs_ss = $("a[rel^='colorboxss']");

  if(thumbs.length > 0){
    thumbs.each(function(){prepareThumbs($(this));}).colorbox({speed:350,opacity:0.85,current:"Image {current} of {total}",slideshow:true,slideshowSpeed:5000,maxWidth:'100%',maxHeight:'100%',slideshowAuto:false},addAdditionalElements);
  }

  if(thumbs_ss.length > 0){
    thumbs_ss.each(function(){prepareThumbs($(this));}).colorbox({speed:350,opacity:0.85,current:"Image {current} of {total}",slideshow:true,slideshowSpeed:5000,maxWidth:'100%',maxHeight:'100%',slideshowAuto:true},addAdditionalElements);
  }

  function prepareThumbs(a){
    a.attr("title",a.children("img").attr("title"));
    if(a.children("img").attr("src").match(/th\d+---[0-9a-f]{6}--.+\.\w+$/i)){
      a.attr("href",a.children("img").attr("src").replace(/th\d+---[0-9a-f]{6}--/,"").replace(/\.(jpg|png|gif|bmp)$/i,""));
    }else{
      a.attr("href",a.children("img").attr("src"));
    }
  }

  function addAdditionalElements(){
    if($("#cboxInfo").length == 0){
      $("#cboxContent").append('<div id="cboxInfo">Image Information</div>');
    }
    if($(".cboxPhoto").length > 0){
      $("#cboxInfo").click(function(){
        var a = $(".cboxPhoto").attr("src").split("/");
        $.fn.colorbox.close();
        window.setTimeout(function(){ window.location.href = "?action=imgtpl&updir=" + a[a.length - 3] + "." + a[a.length - 2] + "&upname=" + a[a.length - 1] + "#imageinfo"; }, $.fn.colorbox.settings.speed);
      });
      $("#cboxInfo").css("visibility", "visible");
    }else{
      $("#cboxInfo").css("visibility", "hidden");
    }
    var leftpadding = 32;
    if($("a[rel='" + this.rel + "']").length > 1){
      leftpadding = 82;
    }
    $("#cboxTitle").wrapInner('<div style="display:table; margin:0 auto; height:100%;"><div style="display:table-cell; vertical-align:middle; padding:0 60px 0 ' + leftpadding + 'px;"></div></div>');
  }
});


