var is_ie = 0;
var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
is_ie = (agt.indexOf("msie") != -1);
var is_opera = (agt.indexOf("pera") != -1);

var is_ie3  = (is_ie && (is_major < 4));
var is_ie4  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5") == -1) && (agt.indexOf("msie 6") == -1) && (agt.indexOf("msie 7") == -1));
var is_ie7  = (is_ie && (agt.indexOf("msie 7") != -1));
var is_ie5up  = (is_ie  && !is_ie3 && !is_ie4);
var is_gecko = (agt.indexOf("gecko") != -1);


/**
* try turning background-caching on
*/
if(is_ie)
{
  try
  {
    document.execCommand("BackgroundImageCache", false, true);
  }
  catch(err) {}
}


/**
* cross-browser element-getter
*/

if (is_ie4)
{
  $ = function (id) {return eval(id);};
}
else
{
  $ = function (id) {return  document.getElementById(id);};
}




function setTabActive(container, buttonid)
{
  var elements = $(container).getElementsByTagName("a");
  button = $(buttonid);

  for(i = 0; i < elements.length; i++)
  {

    elements[i].className = elements[i].className.replace(/\bactivetab\b/,'');
    if(is_ie) elements[i].onmouseout = function(){clearTabDown(container)};
  }
  button.className += " activetab";
}


function changeTab(container, classname, showid)
{
  var elements = getElementsByClassName(classname, $(container));

  for(i = 0; i < elements.length; i++)
  {
    // if(elements[i].parentNode != container)continue;
    // else
    hide(elements[i].id);
  }
  show(showid);
}



//get all elements by classname (hack, y..)
function getElementsByClassName(class_name, target)
{
  if(target == undefined)target = document;

  var all_obj, ret_obj=new Array(), j=0, teststr;

  if(target.all)all_obj = target.all;
  else if(target.getElementsByTagName)all_obj = target.getElementsByTagName("*");

  for(i=0;i < all_obj.length;i++)
  {
    if(!all_obj[i].className)continue;
    if(all_obj[i].className.indexOf(class_name) != -1)
    {
      teststr = "," + all_obj[i].className.split(" ").join(",") + ",";
      if(teststr.indexOf("," + class_name + ",") != -1)
      {
        ret_obj[j] = all_obj[i];
        j++;
      }
    }
  }
  return ret_obj;
}

/**
* sets the opacity of an element
*
*/
if(is_ie)
{
  setOpacity = function (element, opacvalue)
  {
    element.style.filter = 'alpha(opacity='+opacvalue*100+')';
  }
}
else
{
  //ff, compatible
  setOpacity = function (element, opacvalue)
  {
    element.style.opacity = opacvalue;
  }
}


function hide(id)
{
  $(id).style.display = 'none';
}

function show(id)
{
  $(id).style.display = 'block';
}


function change(id)
{
  if($(id).style["display"] != 'block')
  {
    show(id);
  }
  else
  {
    hide(id);
  }
}

function picturesResize()
{
  pictures_at_all = 0;
  if(getCheckedValue($('sizeform').elements['sizeradio']) == "copy")loopPictures(0, 0);
  if(getCheckedValue($('sizeform').elements['sizeradio']) == "radio800")loopPictures(800, 600);
  if(getCheckedValue($('sizeform').elements['sizeradio']) == "radio640")loopPictures(640, 480);
  if(getCheckedValue($('sizeform').elements['sizeradio']) == "radioind")
  {
    if(isNaN($('size_x').value))
    {
      alert('Warning! The size entered in size_x is not a number. Please try to correct that and try again!');
      return;
    }

    if(isNaN($('size_y').value))
    {
      alert('Warning! The size entered in size_y is not a number. Please try to correct that and try again!');
      return;
    }

    loopPictures($('size_x').value, $('size_y').value);
  }
}

var req;

function loopPictures(x, y)
{
  req = getAsynchRequest();
  var local_x = x;
  var local_y = y;


  if(!req) return false;
  req.open('GET', "php/picturesResize?user=" + $('userid').value + "&x=" + local_x + "&y=" + local_y, true);

    req.onreadystatechange = function(){tryLoopingAgain(local_x, local_y)};
  req.send(null);
}

var pictures_at_all = 0;

function tryLoopingAgain(x, y)
{
  try
  {
    if(req.readyState == 4 && req.status == 200)
    {
      if(req.responseText >= 1)
      {
        if(pictures_at_all == 0) pictures_at_all = req.responseText;
        $('statustext').innerHTML = "Resized " + (pictures_at_all - req.responseText + 1) + " of " + pictures_at_all + " pictures";
        loopPictures(x, y);
      }
    }
    if(req.readyState == 4 && req.status != 200)
    {
      //dont do error-handling. 'nuff said.
    }
  }
  catch(e)
  {
    //when we catch something like this, it means we aborted the loading.
  }
}



function getCheckedValue(radioObj)
{
  var radioLength = radioObj.length;
  if(radioObj.checked)  return radioObj.value;

  for(var i = 0; i < radioLength; i++)
  {
    if(radioObj[i].checked)
    {
      return radioObj[i].value;
    }
  }
  return "";
}

function setCheckedValue(radioObj, newValue)
{
  var radioLength = radioObj.length;

  if(radioLength == undefined)
  {
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
  }

  for(var i = 0; i < radioLength; i++)
  {
    radioObj[i].checked = false;
    if(radioObj[i].value == newValue.toString())
    {
      radioObj[i].checked = true;
    }
  }
}

//returns a new object for an asynch request, depending on the browser.
function getAsynchRequest()
{
  if (window.XMLHttpRequest)
  { // moz
    return new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  { // ie
    try
    {
      return new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        try
        {
          return new ActiveXObject("Microsoft.XMLDOM");
        }
        catch (e)
        {
          return false;
        }
      }
    }
  }
}


// load a simple html
function loadHTML(filename, target)
{
  var filename_local = filename;
  var target_local = target;

  req_used = true;
  var req = getAsynchRequest();

  if(!req) return false;
  req.open('GET', filename, true);

  if(target!=null)
  {
    req.onreadystatechange = function(){loadHTMLOnReadyStateChange.call(req, target_local)};
  }

  req.send(null);
}


function loadHTMLOnReadyStateChange(target)
{
  try
  {
    if(this.readyState == 4 && this.status == 200)
    {
      target.innerHTML = this.responseText;
    }
    if(this.readyState == 4 && this.status != 200)
    {
      this.abort();
    }
  }
  catch(e)
  {
    //when we catch something like this, it means we aborted the loading.
  }
}



function create_on_top_request(filename, what, width)
{
  if(width == undefined) width = 356;
  backdiv = document.createElement('div');
  backdiv.style.backgroundColor = '#000000';
  backdiv.style.height = document.body.scrollHeight;
  backdiv.style.width = document.body.scrollWidth;
  backdiv.style.position = 'absolute';
  backdiv.style.left = '0px';
  backdiv.style.top = '0px';
  backdiv.style.zIndex = 5;
  backdiv.style.verticalAlign = 'middle';
  backdiv.id = 'backdiv';

  topdiv = document.createElement('div');
  topdiv.style.height = document.body.scrollHeight;
  topdiv.style.width = document.body.scrollWidth;
  topdiv.style.position = 'absolute';
  topdiv.style.left = '0px';
  topdiv.style.top = '0px';
  topdiv.style.zIndex = 6;
  topdiv.style.verticalAlign = 'middle';
  topdiv.id = 'topdiv';


  setOpacity(backdiv, 0.8);


  prependElement(backdiv, document.body);
  prependElement(topdiv, document.body);


  topdiv.innerHTML = "<div class=\"tablecontent\" id=\"innertopdiv\" style=\"position:absolute; width:"+width+"px; left:" + (getWindowWidth() - width)/2 + "px;" + " top:" + Math.floor(getPageScroll() + getWindowHeight()/2 - 160) +"px;\"><div class=\"tabletitle\"><table width=\"100%\"><tr><td>" + what + "</td><td><a href=\"JavaScript:close_on_top_request('backdiv', 'topdiv');\">Schliessen</a></td></tr></table></div><div class=\"tablecontent_content\" style=\"height:auto;\" id=\"top_inner_content\" >Lade...</div>";

  //
  // when experiencing additional problems with the pos, we can use this too:
  // #innertopdiv
  // {
  //   top: expression(document.body.scrollTop - this.offsetHeight + this.offsetHeight + (getWindowHeight() - 340)/2);
  // }


  loadHTML(filename, $('top_inner_content'));
}

// getheight & width taken from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow with many thanks!
function getWindowHeight()
{
  var myWidth = 0, myHeight = 0;
  if( typeof(window.innerWidth) == 'number' )
  {
    //Non-IE
    return window.innerHeight;
  }
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    return document.body.clientHeight;
  }
}

function getWindowWidth()
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' )
  {
    //Non-IE
    return window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientWidth;

  }
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  {
    //IE 4 compatible
    return document.body.clientWidth;
  }
}

function close_on_top_request(div1, div2)
{
  div1_obj = $(div1);
  div1_obj.parentNode.removeChild(div1_obj);
  div2_obj = $(div2);
  div2_obj.parentNode.removeChild(div2_obj);
}

function update_with_data(filename, content_div)
{
  if(content_div)content_div.parentNode.parentNode.removeChild(content_div.parentNode);
  if($('backdiv'))$('backdiv').parentNode.removeChild($('backdiv'));
  if($('topdiv'))$('topdiv').parentNode.removeChild($('topdiv'));

  loadHTML(filename, $('javascripterror'));
}

function update_and_close_fullpage(filename, content_div)
{
  if($('backdiv'))$('backdiv').parentNode.removeChild($('backdiv'));
  if($('topdiv'))$('topdiv').parentNode.removeChild($('topdiv'));

  loadHTML(filename, content_div);
}

function addElement(element, target)
{
  try { target.appendChild(element, null); } // non IE
  catch(e) { target.appendChild(element); } // IE
}

function prependElement(element, target)
{
  try { target.appendChild(element, null); } // non IE
  catch(e) { target.appendChild(element); } // IE
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll()
{
  if (self.pageYOffset)
  {
    return self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
  { // Explorer 6 Strict
    return document.documentElement.scrollTop;
  }
  else if (document.body)
  {// all other Explorers
    return document.body.scrollTop;
  }
}

