// JavaScript Document

var IE = false;
var IE6 = false;
var NS = false;
var browser_version = parseInt(navigator.appVersion);
var browser_type = navigator.appName;
if (browser_type == "Microsoft Internet Explorer" && (browser_version >= 4)) { 
  IE = true;
} 
if (browser_type == "Microsoft Internet Explorer" && (browser_version <= 6)) { 
  IE6 = true;
} 
else if (browser_type == "Netscape" && (browser_version >= 4) && (browser_version < 5)) { 
  NS = true;
}
else if (browser_type == "Netscape" && (browser_version >= 5)) { 
  NS = true;
}
// -----------------------------------------------------------------------------

/*
 * Importiert das angegebene Stylesheet
 * @author: Dennis Kleine
 */
function importStyle(stylesheet)
{
  var head = document.getElementsByTagName("head")[0];

  var link = document.createElement("link");
  link.rel = "stylesheet";
  link.href = "./css/js"+stylesheet+".css";
  link.type = "text/css";
  link.media = "all";

  head.appendChild(link);
}
// -----------------------------------------------------------------------------

/*
 * Gibt alle Elemente mit dem angegebenen Klassennamen in einem Array zurück
 * @author: Dennis Kleine
 */
function getElementsByClassName(class_name)
{
  var all_obj;
  var ret_obj = new Array();
  var j = 0;
  var teststr;

  if(document.all)
    all_obj = document.all;
  else if(document.getElementsByTagName && !document.all)
    all_obj = document.getElementsByTagName("*");

  for(i = 0; i < all_obj.length; i++)
  {
    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;
}
// -----------------------------------------------------------------------------

/*
 * Berechnet die Y Position eines Elements
 * @author: Dennis Kleine
 */
function getY(el)
{
  var iReturnValue = 0;
  while(el != null)
  {
    iReturnValue += el.offsetTop;
    el = el.offsetParent;
  }
  return iReturnValue;
}
// -----------------------------------------------------------------------------

/*
 * Berechnet die X Position eines Elements
 * @author: Dennis Kleine
 */
function getX(el)
{
  var iReturnValue = 0;
  while(el != null)
  {
    iReturnValue += el.offsetLeft;
    el = el.offsetParent;
  }
  return iReturnValue;
}
// -----------------------------------------------------------------------------

function getHCenter(W)
{
  if(NS)
  {
    left = window.pageXOffset+(window.innerWidth/2)-(W/2);
  }
  if(IE)
  {
    if(!document.documentElement.clientHeight)
    {
      left = document.body.scrollLeft+(document.body.clientWidth/2)-(W/2);
    }
    else
    {
      left = document.documentElement.scrollLeft+(document.documentElement.clientWidth/2)-(W/2);
    }
  }

  left = Math.floor(left)+'px';

  return left;
}
// -----------------------------------------------------------------------------

/*
 * Gibt die Hoehe eines El. ohne Padding zurueck
 * @author: DKL
 */
function getHeightWithoutPadding(element)
{
  str = element.style.height;
  wo = str.split("px");
  n = wo[0];
  return n;
}
// -----------------------------------------------------------------------------

/*
 * Gibt die Breite eines El. ohne Padding zurueck
 * @author: DKL
 */
function getWidthWithoutPadding(element)
{
  str = element.style.width;
  wo = str.split("px");
  n = wo[0];
  return n;
}
// -----------------------------------------------------------------------------

function getElementTOP(H)
{
  return $(window).scrollTop() + ( $(window).height() / 2 ) - ( H / 2 );
}
// -----------------------------------------------------------------------------

function getElementLEFT(W)
{
  return $(window).scrollLeft() + ( $(window).width() / 2 ) - ( W / 2 );
}
// -----------------------------------------------------------------------------


/**
 * A simple JavaScript image loaderimage loader
 * @author Cuong Tham
 * @url http://thecodecentral.com/2008/02/21/a-useful-javascript-image-loader
 * @usage
 * var loader = new ImageLoader('IMAGE_URL');
 * //set event handler
 * loader.loadEvent = function(url, image){
 *   //action to perform when the image is loaded
 *   document.body.appendChild(image);
 * }
 * loader.load();
 */

//source: http://snipplr.com/view.php?codeview&id=561
// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling)
{
  bubbling = bubbling || false;
  if(window.addEventListener)	{ // Standard
    element.addEventListener(type, expression, bubbling);
    return true;
  } else if(window.attachEvent) { // IE
    element.attachEvent('on' + type, expression);
    return true;
  } else return false;
}

var ImageLoader = function(url){
  this.url = url;
  this.image = null;
  this.loadEvent = null;
};

ImageLoader.prototype = {
  load:function(){
    this.image = document.createElement('img');
    var url = this.url;
    var image = this.image;
    var loadEvent = this.loadEvent;
    addListener(this.image, 'load', function(e){
      if(loadEvent != null){
        loadEvent(url, image);
      }
    }, false);
    this.image.src = this.url;
  },
  getImage:function(){
    return this.image;
  }
};
// -----------------------------------------------------------------------------

function calcuateMaxChars(chars, width, height)
{
  pxpc = 6;
  pxpr = 17;

  charswidth = chars*pxpc;

  rows = Math.ceil(charswidth / width);
  cols = Math.floor(width / pxpc);
  maxrows = Math.floor(height / pxpr);
  
  maxchars = cols*maxrows;

  return maxchars;
}
// -----------------------------------------------------------------------------

function truncateHTML(htmltext, text, length, ending, exact, considerHTML, morelink)
{
  length = length || 200;
  ending = ending || '...';
  exact = exact || true;
  considerHTML = considerHTML || false;

  if(considerHTML)
  {
    if (text.length <= length)
    {
      return htmltext;
    }
    else
    {
      var data = 'action=turnicateHTML&text='+encodeURIComponent(htmltext)+'&length='+length;

      $.ajax({
        type: 'POST',
        url: './php/ajaxactions.php',
        data: data,
        async: false,
        dataType: 'html',
        success: function(html)
        {
          htmltext = html;
        }
      });

      return htmltext;
    }
  }
  else
  {
    if(text.length <= length)
    {
      return text;
    }
    else
    {
      htmltext = text.substring(0, (length - 3));
      htmltext = htmltext.replace(/\w+$/, '');

      if(!morelink)
        htmltext += ending;
      else
        htmltext += '<a href="'+morelink+'" target="_blank" title="read more">'+ending+'</a>';
      
      return htmltext;
    }
  }
}
// -----------------------------------------------------------------------------

function loadImagesOfFolder(folder)
{
  var data = 'action=loadImagesOfFolder&folder='+encodeURIComponent(folder);

  $.ajax({
    type: 'POST',
    url: './php/ajaxactions.php',
    data: data,
    async: false,
    dataType: 'html',
    success: function(html)
    {
      htmltext = html;
    }
  });

  return htmltext;
}
// -----------------------------------------------------------------------------

function viewLoading()
{
  var W = 200;
  var H = 125;
  var opacity = 8;

  var top = getElementTOP(H);
  var left = getElementLEFT(W);

  left = Math.floor(left) + 'px';
  top = ( Math.floor(top) - 40 ) + 'px';

  container = document.createElement('div');
  container.id = 'dkxloader';
  container.style.display = 'none';
  container.style.position = 'absolute';
  container.style.zIndex = '9000';
  container.style.left = left;
  container.style.top = top;
  container.style.padding = Math.floor(( ( H - 50 ) / 2 ))+'px '+Math.floor(( ( W - 80 ) / 2 ))+'px';
  container.style.background = '#000000';
  container.style.opacity = opacity/10;
  container.style.filter = 'alpha(opacity=' + opacity*10 + ')';

  image = new Image();
  image.src = './images/dkx-loading.gif';

  container.appendChild(image);
  document.body.appendChild(container);

  $(container).corner('5px');
  $(container).fadeIn('fast');
}
// -----------------------------------------------------------------------------

function hideLoading()
{
  if (!document.createElement) return;
  d = document.getElementById('dkxloader');
  if (!d) return;
  document.body.removeChild(d);
}
// -----------------------------------------------------------------------------

function viewLocalLoading(id)
{
  var W = 80;
  var H = 50;

  var boxwidth = parseInt( $('#'+id).css('width') );
  var boxheight = parseInt( $('#'+id).css('height') );

  var top, left;

  left = Math.floor( ( ( boxwidth - W ) / 2 ) ) + 'px';
  top = Math.floor( ( ( boxheight - H ) / 2 ) ) + 'px';

  container = document.createElement('div');
  container.id = id+'loader';
  container.style.display = 'none';
  container.style.position = 'absolute';
  container.style.zIndex = '9000';
  container.style.left = left;
  container.style.top = top;

  image = new Image();
  image.src = './images/dkx-loading.gif';

  container.appendChild(image);
  document.getElementById(id).appendChild(container);

  $(container).fadeIn('fast');
}

function hideLocalLoading(id)
{
  if (!document.createElement) return;
  d = document.getElementById(id+'loader');
  if (!d) return;
  document.getElementById(id).removeChild(d);
}
