var ajax_loader = '<img src="/eg2/admin/images/ajax-loader.gif"/>';

/*if (typeof($) != 'function') {
  function $(element) {
    if (!element) return false;
    if (element.substr(0,1)=='#') element = element.substr(1);
    return document.getElementById(element);
  }
}*/

function $id(element) {
  return document.getElementById(element);
}

//identify browser through object detection
var browser = '';
if (Array.every)
  browser = 'Firefox';
else if (document.all)
  browser = 'IE';
else
  browser = 'other';
  
var isMSIE = /*@cc_on!@*/false;
var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/;
var gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/;

var no_fixed_positions = (isMSIE && !gteIE7);

function check_browser() {
  if (no_fixed_positions) {
      var bg = $id('javabuttongroup'); if (bg) bg.style.position = 'absolute';
      var bg2 = $id('javabuttongroup2'); if (bg2) bg2.style.position = 'absolute';      
  }
}

validate_emailform = function() {
    return true;
}

image_viewer_resize = function(i_ow, i_oh) {
    sxy = getScrollXY();
    var myWidth = 0, myHeight = 0;
    if( typeof(window.innerWidth) == 'number' ) {
        //Firefox
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE (funkar även i FF)
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    adminPanel = $id('adminpanel');
    if (adminPanel && adminactive) {
      adminPanel.style.height = myHeight + 'px';
      if (adminPanel.style.position == 'absolute')
        adminPanel.style.top = sxy[1] + 'px';
    }
    imageWindow = $id('image_viewer');
    if (imageWindow) {
        if (!i_ow) i_ow = imageWindow.offsetWidth;
        if (!i_oh) i_oh = imageWindow.offsetHeight;
        imageWindow.style.left = Math.round((myWidth - i_ow)/2) + 'px';
        if (imageWindow.style.position == 'absolute')
          imageWindow.style.top = Math.round((myHeight - i_oh)/3) + sxy[1] + 'px';
        else
          imageWindow.style.top = Math.round((myHeight - i_oh)/3) + 'px';
    }
    buttongroup = $id('javabuttongroup');
    if (buttongroup && buttongroup.style.position == 'absolute') {
      buttongroup.style.top = (sxy[1]+10)+"px";
      buttongroup.style.left = (sxy[0]+10)+"px";
    }
    buttongroup2 = $id('javabuttongroup2');
    if (buttongroup2 && buttongroup2.style.position == 'absolute') {
      buttongroup2.style.top = (sxy[1]+10)+"px";
      buttongroup2.style.right = 10-sxy[0]+"px";
    }
}

image_viewer = function(id,w,h,bt) {
    imageWindow = $id('image_viewer');
    if (!imageWindow) {
        imageWindow = document.createElement('DIV');
        if (no_fixed_positions) {
          var pos = 'position:absolute;'
        } 
        else var pos = 'position:fixed;'
        imageWindow.style.cssText = pos + 'text-align:center;z-index:999999; background:white; border:1px solid #eee;display:none; border-right:2px solid #000; border-bottom:2px solid #000; ';
        imageWindow.id = 'image_viewer';
        document.body.appendChild(imageWindow);
        
        imageViel = document.createElement('DIV');
        imageViel.style.cssText = pos + 'top:0;left:0;width:100%;height:100%;z-index:999998; background:black; display:none; opacity:0.2; filter: alpha(opacity=20);';
        imageViel.id = 'image_viel';
        document.body.appendChild(imageViel);
        
    }
    imageWindow.style.display = 'block';
    $id('image_viel').style.display = 'block';
    
    //imageWindow.style.width = (w*1.1) + 'px';
    //imageWindow.style.height = (h*1.1) + 'px';
    var content = "<img style=\"margin:" + (w*0.06) + "px; border:0px solid #666;\" onclick=\"$id('image_viel').style.display='none'; $id('image_viewer').style.display='none';\" width=\""+w+"\" height=\""+h+"\" src=\""+eg_url_path+"/eg_image.php?id="+id+"&w="+w+"&h="+h+"\"/><img title=\"St&auml;ng\" onclick=\"$id('image_viewer').style.display='none'; $id('image_viel').style.display='none';\" onmouseout=\"this.src='" + eg_url_path + "/admin/images/close.gif';\" onmouseover=\"this.src='"+eg_url_path+"/admin/images/close_r.gif';\" src=\"" + eg_url_path + "/admin/images/close.gif\" style=\"border:0px;position:absolute;z-index:1000000;margin-top:3px;margin-left:-34px\"/>";
    content = content + "<p>"+bt+"</p>";
    imageWindow.innerHTML = content;
    window.onresize = image_viewer_resize;
    window.onscroll = image_viewer_resize;
    image_viewer_resize();
}

function getScrollXY() {
  //beroende på DOCTYPE ligger scrollTop antingen på HTML elementet eller BODY
  // funkar med IE7, IE6 och Firefox
  sx = document.body.scrollLeft + document.body.parentNode.scrollLeft;
  sy = document.body.scrollTop + document.body.parentNode.scrollTop;
  return [sx, sy];
}

var mouseover_submenu = false;
function over_sm() { mouseover_submenu = true; }
function out_sm() { mouseover_submenu = false; }

var menu_editing = false;

function menu_over(sub_id, caller) {
  if (menu_editing) return null;
  if (sub_id && (sub_id == timeout_sub_id)) timeout_caller = null;
  // hide any other menu waiting to be closed
  if (timeout_caller) hide_menu();
  set_class(caller.firstChild, 'menu_item_browsing');
  if ($id(sub_id)) $id(sub_id).style.display = 'block';     
}

var timeout_sub_id;
var timeout_caller;

function hide_menu() {
  if (timeout_caller && !menu_editing) {
    if (browser == 'IE') 
      timeout_caller.firstChild.className = timeout_caller.firstChild.getAttribute('orig_class'); 
    else 
      timeout_caller.firstChild.setAttribute('class', timeout_caller.firstChild.getAttribute('orig_class'));
    if ($id(timeout_sub_id)) {
      $id(timeout_sub_id).style.display = 'none';
      timeout_sub_id = 0;
    }
    timeout_caller = false;    
  }
}

function menu_out(sub_id, caller) {
  //any other menu waiting to be hidden?
  if (timeout_caller) hide_menu();

  timeout_sub_id = sub_id;
  timeout_caller = caller;
  //if contextmenu is active, wait until it closes
  if ($id('contextmenu')) if ($id('contextmenu').style.display == 'block') return null;
  if (menu_editing) return null;        
  window.setTimeout('hide_menu()',150);
}

var texpanded = [];
function flipsrc(id) {
  var s2 = $id(id+'_img').getAttribute('src');
  $id(id+'_img').setAttribute('src', $id(id+'_img').getAttribute('src2'));
  $id(id+'_img').setAttribute('src2', s2);   
}
function expandtext(id) {
  if ($id(id+'_short').style.display == 'none') {
      //hide
      $id(id+'_short').style.display = 'block';
      $id(id+'_full').style.display = 'none';
      texpanded = [];
      flipsrc(id);
  } else {
      //show
      while (texpanded.length) {
        t = texpanded.pop();
        $id(t+'_short').style.display = 'block';
        $id(t+'_full').style.display = 'none';
        flipsrc(t);
      }
      $id(id+'_short').style.display = 'none';
      $id(id+'_full').style.display = 'block';
      flipsrc(id);
      texpanded.push(id);
  }
  if (use_bsc) BodySizeChanged();
}

function inarray(a, search_phrase)
{
  for( var i = 0; i < a.length; i++ ) if( search_phrase == a[i] ) return true;
  return false;
}

function update_section_user(section, options, use_loader) {
    if (use_loader != false) $('#'+section).innerHTML = ajax_loader;
    $.post(eg_url_path+'/eg_section.php', { s: section, options: options }, function(data) { $('#'+section).replaceWith(data); } );
}

function set_class(obj, cls) {
  if (obj) {
    if (browser == 'IE') obj.className = cls; 
    else obj.setAttribute('class', cls);
  }
}

function get_class(obj) {
  if (obj) {
    if (browser == 'IE') return obj.className; 
    else return obj.getAttribute('class');
  }
}

function section_callback_user(type, data, id) {
    if (type == 'error' || data == '') {
      alert('NŒgot gick fel! Prova att ladda om sidan.');
    }
    else {
      temp = document.createElement('DIV');
      temp.innerHTML = data;
      if (get_class(temp.firstChild) == 'text_group') {
          var tid = temp.firstChild.getAttribute('text_id');
          var node = $id('text_group_'+tid); 
          if (node) replaceElement(node, temp.firstChild);
      } else {
          var section_id = temp.firstChild.getAttribute('id');
          var section_node = $id(section_id);
          if (section_node) {
            replaceElement(section_node, temp.firstChild);
            load_javascripts($id(section_id).firstChild);
          }
      }
    }
}

function removeElement(e) { if (e) e.parentNode.removeChild(e); }
function replaceElement(olde, newe) {  olde.parentNode.replaceChild(newe, olde); }

function getPosition(target){
  //scrollŠge alltid inkluderat
	var left = 0;
	var top  = 0;
	var e = target;
	if (e) while (e.offsetParent){
		left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		e     = e.offsetParent;
	}
	left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
	return {left:left, top:top, x:left, y:top, width:target.offsetWidth, height:target.offsetHeight};
}

//quirksmode recode winner
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

