/* global functions go here */

/** GETBROWSERSIZE */

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

  return {"width":myWidth,"height":myHeight};
}

/** GETBROWSERSIZE */

/*--------------------------------------------------------------------------*/

/* COOKIE */

function set_cookie(name,value,expires) {
  if (expires) document.cookie = name + '=' + escape(value) + '; expires=' + expires.toGMTString() + '; path=/';
  else document.cookie = name + '=' + escape(value) + '; path=/';
}

function get_cookie(name) {

  var dcookie = document.cookie;
  var cname = name + "=";
  var clen = dcookie.length;
  var cbegin = 0;
  while (cbegin < clen) {
    var vbegin = cbegin + cname.length;
    if (dcookie.substring(cbegin, vbegin) == cname) {
      var vend = dcookie.indexOf (";", vbegin);
      if (vend == -1) vend = clen;
      return unescape(dcookie.substring(vbegin, vend));
    } cbegin = dcookie.indexOf(" ", cbegin) + 1;
    if (cbegin == 0) break;
  }
  return null;
}

function del_cookie(name) {
  document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
}

function dump_cookies() {
  if (document.cookie == '') document.write('No Cookies Found');
  else {
    thisCookie = document.cookie.split('; ');
    for (i=0; i<thisCookie.length; i++) {
      document.write(thisCookie[i] + '<br \/>');
    }
  }
}

/* COOKIE */

/*--------------------------------------------------------------------------*/

/* ENGINE DETECTION */
/* if ( Engine.isMSIE ) {} */

var Engine = {
  detect: function() {
    var UA = navigator.userAgent;
    this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
    this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
    this.isOpera = /Opera/.test(UA);
    this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
    this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(UA) && !this.isOpera);
    this.isMSIE6 = this.isMSIE && !this.isMSIE7;
  }
}
Engine.detect();

/* ENGINE DETECTION */

/*--------------------------------------------------------------------------*/

/* SERVER NAME */

function getServerName() {
  var href = document.location.href;
  var rExp = /^http*:\/\//;
  var rExp2 = /\/.*$/;
  var href2 = href.replace(rExp, '')
  var href3 = href2.replace(rExp2, '')
  return href3;
}

/* SERVER NAME */

/*--------------------------------------------------------------------------*/

/* CONSOLE LOG */

function loggi(myvar,win) {

  try{
    console.log(myvar);
  } catch(e){}
}

/* CONSOLE LOG */

/*--------------------------------------------------------------------------*/

/* BASE 64 */

function base64_encode(str)
{
  var r=[];
  var i=0;
  var d=[];
  for (i=0; i<str.length; i++) {
    d[i] = str.charCodeAt(i);
  }
  i=0;
  var dl=d.length;
  if ((dl%3) == 1) {
    d[d.length] = 0; d[d.length] = 0;
  }
  if ((dl%3) == 2) {
    d[d.length] = 0;
  }
  while (i<d.length) {
    r[r.length] = b64[d[i]>>2];
    r[r.length] = b64[((d[i]&3)<<4) | (d[i+1]>>4)];
    r[r.length] = b64[((d[i+1]&15)<<2) | (d[i+2]>>6)];
    r[r.length] = b64[d[i+2]&63];
    i+=3;
  }
  if ((dl%3) == 1) {
    r[r.length-1] = r[r.length-2] = '=';
  }
  if ((dl%3) == 2) {
    r[r.length-1] = '=';
  }
  var t=r.join('');
  return t;
}

function base64_decode(t)
{
  var d = [];
  var i = 0;
  var retval = '';
  t=t.replace(/\n|\r/g, '');
  t=t.replace(/=/g, '');
  while (i<t.length) {
    d[d.length] = (f64[t.charAt(i)]<<2) | (f64[t.charAt(i+1)]>>4);
    d[d.length] = (((f64[t.charAt(i+1)]&15)<<4) | (f64[t.charAt(i+2)]>>2));
    d[d.length] = (((f64[t.charAt(i+2)]&3)<<6) | (f64[t.charAt(i+3)]));
    i+=4;
  }
  if (t.length%4 == 2) {
    d = d.slice(0, d.length-2);
  }
  if (t.length%4 == 3) {
    d = d.slice(0, d.length-1);
  }
  for (i=0; i<d.length; i++) {
    retval += String.fromCharCode(d[i]);
  }
  return retval;
}

function base64_init() {
  var b64s='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  b64 = [];
  f64 = [];
  for (var i=0; i<b64s.length ;i++) {
    b64[i] = b64s.charAt(i);
    f64[b64s.charAt(i)] = i;
  }
}

/* BASE 64 */

/*--------------------------------------------------------------------------*/

/* ATTACH EVENT */

function addOnloadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
          oldonload();
          func();
        }
    }
}

/* ATTACH EVENT */

/*--------------------------------------------------------------------------*/

/* PROTOTYPE EXTEND */

// AJAX Eval

if( !isAdmin ) {

  Ajax.Eval = Class.create();
  Object.extend(Object.extend(Ajax.Eval.prototype, Ajax.Request.prototype), {
    initialize: function(url, pars) {
      this.transport = Ajax.getTransport();
      this.setOptions({method:'post', parameters:pars});
      this.options.onComplete = (function(transport) {
        eval(transport.responseText);
      });
      this.request(url);
    }
  });

}

/* PROTOTYPE EXTEND */

/*--------------------------------------------------------------------------*/

/* URL DECODE*/

function URLDecode(psEncodeString) {
  var lsRegExp = /\+/g;
  return unescape(String(psEncodeString).replace(lsRegExp, " "));
}

/* URL DECODE*/

/*--------------------------------------------------------------------------*/

/* SWF CONTROLLER */

function getSwfObject(movieName) {
  return $(movieName);
}


function sendMovieToSwf(value)
{
  var flashMovie = getSwfObject("player");

  path = "../../../../data/media/_shared/media/";
  newmovie = path + value;
  flashMovie.SetVariable("/:my_FLVPlybk.contentPath", newmovie);
  flashMovie.SetVariable("/:my_FLVPlybk.autoPlay","true");
}

function stopSmallPlayer()
{
	var flashMovie=getSwfObject("video-player");
	flashMovie.Play();
}



/* SWF CONTROLLER */

/*--------------------------------------------------------------------------*/


/* MENAGE DEFAULT FIELD TEXT FUCUS BLUR */

function  defaultText(element){

  $(element).onblur = function(){
    if(this.value=='') this.value= this.defaultValue;
  }

  if ( $(element).defaultValue == '' ) $(element).defaultValue = $(element).value;

  if($(element).value == $(element).defaultValue) $(element).value='';

}

/* MENAGE DEFAULT FIELD TEXT FUCUS BLUR */

function correctPNG() {
  try{
    var arVersion = navigator.appVersion.split("MSIE");
    var version = parseFloat(arVersion[1]);

    if ((version >= 5.5) && (document.body.filters) ) {

      for(var i=0; i<document.images.length; i++) {

        var img = document.images[i];
        var imgName = img.src.toUpperCase();

        if ( imgName.indexOf('.PNG') > 0 ) {
          img.style.cssText = img.style.cssText + ";width:" + img.width + "px; height:" + img.height + "px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + img.src + "\", sizingMethod='scale');";
          img.src = "templates/html/master/img/transparent.gif";
        }

      }
    }
  } catch(e){}

}

if ( !isAdmin )  Event.observe(window, 'load', correctPNG);

