// Fix all pngs so IE6 can view them
function fix_png()
{
	$('img[@src$=.png]').ifixpng(); 
	$('input[@src$=.png]').ifixpng();
  //$('div').ifixpng();//.css{'background-image'}); 
	$("div[@style.background-image.url$=.png][@class!=center]").ifixpng();
}


// popup stuff

var popupWnd = null;
var rulezShown = 0;

function doPopup(xs,ys,url,resizable)
{
  if (popupWnd != null && !popupWnd.closed && popupWnd.focus) popupWnd.focus();
  
  if (xs <= 0 || xs > window.screen.width-20) xs = window.screen.width-20;
  if (ys <= 0 || ys > window.screen.height-60) ys = window.screen.height-60;
  x = window.screen.width;
  y = window.screen.height;
  if (x != 0 && y != 0) {
    x = (x - (xs + 20)) / 2;
    y = (y - (ys + 60)) / 2;
  }
  // coords = ",screenx=" + x + ",screeny=" + y + ",left=" + x + ",top=" + y;
  coords = ",screenx=" + 100 + ",screeny=" + 50 + ",left=" + 100 + ",top=" + 50;
  popupWnd = window.open(url,"CareerTV","toolbar=0,location=0,status=0,menubar=0,width="+xs+",height="+ys+",scrollbars=0,resizable="+(resizable?"1":"0")+coords);
  popupWnd.focus();
}

function doPopupRulez()
{
  rulezShown = 1;
  doPopup(812, 600, "rulez.php?popup=1", true);
}

function doPopupAbuse(link)
{
  doPopup(450, 300, "abuse_add.php?" + link, true);
}

function doPopupMpoll(mpoll,view)
{
  doPopup(400, 500, "mpoll.php?mpoll_id=" + mpoll + (view>0 ? '&view=1' : ''), true);
}


// image stuff

function img_over(obj, file)
{
  obj.old_src = obj.src;
  obj.src = file;
}

function img_out(obj)
{
  obj.src = obj.old_src;
}


var image_load_obj = new Array();
var image_load_cnt = new Array();

function img_onload(id, max_w)
{
  img = getHtmlElement('imgLoad'+id);
  xsrc = img.getAttribute('xsrc');
  if (xsrc == '') return;

  if (xsrc.indexOf('_file.php?')>0) xsrc = xsrc + '&xsrc';
  obj = image_load_obj[id];

  if (obj == null)
  {
    obj = new Image();
    obj.src = xsrc;
    image_load_obj[id] = obj;
    image_load_cnt[id] = 0;
  }

  if (!obj.complete)
  {
    count = 0 + image_load_cnt[id];
    if (count < 20)
    {
      image_load_cnt[id] = count + 1;
      setTimeout('img_onload(' + id + ',' + max_w + ')', (count ? 1500 : 500));
      return;
    }
    else
    {
      img.setAttribute('xsrc', '');
      img.src = '/gfx/img_error.gif';
      img.style.width = '14px';
      img.style.height = '14px';
      return;
    }
  }

  max_h = max_w * 0.75;
  new_w = obj.width;
  new_h = obj.height;

  if (new_w>max_w || new_h>max_h)
  {
    ratio_w=(new_w/max_w);
    ratio_h=(new_h/max_h);
    if (ratio_w > ratio_h)
    {
      new_w = max_w;
      new_h = (new_h * (1/ratio_w));
    }
    else
    {
      new_w = (new_w * (1/ratio_h));
      new_h = max_h;
    }
  }

  if (new_w && new_h)
  {
    img.setAttribute('xsrc', '');
    img.src = xsrc;
    img.style.width = parseInt(new_w) + 'px';
    img.style.height = parseInt(new_h) + 'px';
  }
  else
  {
    img.setAttribute('xsrc', '');
    img.src = '/gfx/img_error.gif';
    img.style.width = '14px';
    img.style.height = '14px';
  }  
}

function img_onerror(img)
{
  img.setAttribute('xsrc', '');
  img.src = '/gfx/img_error.gif';
  img.style.width = '14px';
  img.style.height = '14px';
}


// form input stuff

function formTrim(field)
{
  field.value = field.value.replace(/[ ]/g, ' ');
  field.value = field.value.replace(/(^\s*)|(\s*$)/g, '');
}

function formUpperCase(field)
{
  field.value = field.value.toUpperCase();
}

function formLowerCase(field)
{
  field.value = field.value.toLowerCase();
}


// dhtml stuff

function getHtmlElement(id)
{
  return (document.getElementById ? document.getElementById(id) : (document.all ? document.all[id] : null));
}


function getHtmlStyle(element)
{
  return element.style;
}


// hide or display object

function vh_slide(id, speed)
{
   if ($('#'+id).is(":hidden")) 
    $('#'+id).slideDown(speed);
   else
    $('#'+id).slideUp(speed);
}

function vh_fade(id, speed)
{
   if ($('#'+id).is(":hidden")) 
    $('#'+id).fadeIn(speed);
   else
    $('#'+id).fadeOut(speed);
}

function vh(id)
{
  elemObj = getHtmlElement(id);
  styleObj = getHtmlStyle(elemObj);

	if (styleObj.display == 'block')
	{
		styleObj.display = 'none';
		return false;
	}
	else
	{
		styleObj.display = 'block';
		return true;
	}
}


function vhPm(pm_id)
{
  elemObj = getHtmlElement('pm' + pm_id);
  if (elemObj == null) return;

  styleObj = getHtmlStyle(elemObj);

	if (styleObj.display == 'block')
	{
		styleObj.display = 'none';
		return false;
	}
	else
	{
		styleObj.display = 'block';

  	unread = elemObj.getAttribute('unread');
    if (unread != null && unread != '')
  	{
    	elemObj.removeAttribute('unread');
      iconObj = getHtmlElement('icon' + pm_id);
  		iconObj.title = 'Läst';
  		iconObj.src = 'pm_read.php?pm_id=' + pm_id;
  	}
  	
		return true;
	}	
}


function go_to(url)
{
	window.location=url;
}

function menuSlide(id)
{
  $('#'+id).slideToggle('fast');
}
