// 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,"loading","dependent=yes,toolbar=no,directories=no,menubar=no,width="+xs+",height="+ys+",scrollbars=yes,resizable="+(resizable?"yes":"no")+coords);
  popupWnd.focus();
}

function doPopupRulez()
{
  rulezShown = 1;
  doPopup(700, 450, "rulez.php?popup=1", true);
}

function doPopupAbuse(link)
{
  doPopup(450, 300, "abuse_add.php?" + link, true);
}

function doPopupMpoll(mpoll,view)
{
  doPopup(400, 450, "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 go_to(url)
{
	window.location=url;
}


// video player

var videoWnd;

function videoplayer(publication_id, width, height)
{ 
  if (videoWnd != null && !videoWnd.closed && videoWnd.focus) videoWnd.focus();

  xs = width + 500;
  ys = height + 208;
  if (xs > window.screen.width-20) xs = window.screen.width-20;
  if (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;
  
  videoWnd = window.open("http://gameplayer.se/videoplayer_loading.php?publication_id=" + publication_id, "gptv", "scrollbars=no,menubar=no,width="+xs+",height="+ys+",resizable=yes,toolbar=no,location=no,status=no,titlebar=no" + coords);
  videoWnd.focus();
}


// Paper show

function UpdatePaper() 
{
	var current_div = getHtmlElement('current_div');
	var level = getHtmlElement('level');
	var special = getHtmlElement('special');
	
   switch (document.getElementById('current_div').value)
   {
     case 'level':
	    if (special != null)
	    {
	    	current_div.value = 'special';
	    	level.style.display = 'none';
	     	special.style.display = 'block';
	    }
     break;
     case 'special':

     	if (level != null)
     	{
     		current_div.value = 'level';
	     	special.style.display = 'none';
	     	level.style.display = 'block';
	    }
     break;
     default:
     	if (level != null)
     	{
     		current_div.value = 'level';
	     	if (special != null) { special.style.display = 'none'; }
	     	level.style.display = 'block';
	    }
	    else if (special != null)
     	{
     		current_div.value = 'special';
     		if (level != null) { level.style.display = 'none'; }
	     	special.style.display = 'block';
	    }
     break;
   }
   
   setTimeout("UpdatePaper()", 10000);
}

function StartPaperShow() 
{
   setTimeout("UpdatePaper()", 10);
}
