var news = 0;
var trailer = 0;
var news_status = 'minimized';
var trailer_status = 'minimized';
var trailer_clicked = 0;
var div;
var olddata = '';
var quote;
var displayPos;
var displayStop;
var displayText = 'Willkommen bei GRanks.de!';

function AjaxError(data, ioArgs) 
  {
  // alert('Error when retrieving data from the server!');
  }
  
function grStatusCallback(data)
  {
  dojo.publish('grStatus', [{message: data, type: 'error', duration: 5000}]);
  }

function grSetValue(param, value, db)
  {
  dojo.xhrGet({url: 'ajax/setvalue.php?param=' + param + '&value=' + value + '&db=' + db});      
  }

function grSubjectSelectChild(parent)
  {
  var results = parent.id.match(/subject_(\d+)_(\d+)/i);  

  InitGrid(results[2]);
  hid = results[2];

  dojo.xhrGet({url: 'ajax/getrrs.php?sid=' + results[1] + '&hid=' + results[2] + '&ccode=' + ccode, load: RRCallback});    
  AjaxGetQuote(results[1], results[2]);

  var franchise = dijit.byId('gr_franchise');
  franchise.setHref('/ajax/franchise_gfx.php?subjectid=' + results[1] + '&hid=' + results[2]);
  franchise.connect();

  console.dir(franchise);
  }

function RRCallback(data, ioArgs)
  {
  var info = data.split(';;');
  
  dojo.byId('gamerank_hardware').innerHTML = '(' + info[0] + ')';
  
  if (info[1] > 0)
    dojo.byId('gamerank_overall').innerHTML = info[1];
  else
    dojo.byId('gamerank_overall').innerHTML = '?';

  if (info[2] > 0)
    dojo.byId('gamerank_overall_genre').innerHTML = info[2];
  else
    dojo.byId('gamerank_overall_genre').innerHTML = '?';

  dojo.byId('gamerank_overall_counter').innerHTML = info[3];
  dojo.byId('gamerank_overall_genre_counter').innerHTML = info[4];

  if (info[5] > 0)
    dojo.byId('rating_overall').innerHTML = info[5] + '%';
  else
    dojo.byId('rating_overall').innerHTML = '-';

  if (info[6] > 0)
    dojo.byId('rating_locale').innerHTML = info[6] + '%';
  else
    dojo.byId('rating_locale').innerHTML = '-';

  dojo.byId('infobox_packshot').style.backgroundImage = 'url(' + info[7] + ')';

  if (info[8])
    dojo.byId('infobox_link').href = info[8];

  if (info[9] != '0')
    {
    dojo.byId('infobox_price').innerHTML = info[9];
    dojo.byId('infobox_price').style.display = 'block';
    }

	dojo.byId('subject_genre').innerHTML     = info[10];
  
  if (info[11].length > 0)
    dojo.byId('subject_developer').innerHTML = '<a href="/developer.php?did=' + info[11] + '"><img src="/print.php?id=00' + info[11] + '&fs=12" alt="Developer" style="margin-left: -1px;"/></a>';
  else
    dojo.byId('subject_developer').innerHTML = '';

  if (info[12].length > 0)
    dojo.byId('subject_publisher').innerHTML = '<a href="/publisher.php?pid=' + info[12] + '"><img src="/print.php?id=00' + info[12] + '&fs=12" alt="Publisher" style="margin-left: -1px;"/></a>';
  else
    dojo.byId('subject_publisher').innerHTML = '';

	if (info[14] != '')
	  dojo.byId('subject_age').innerHTML = '<img src="/images/ages/' + info[14] + '.png" align="right" />';
  else
		dojo.byId('subject_age').innerHTML = '';

  FormatDate(info[13], 'subject_release');

  if (info[16] == 1)
    {
    if (info[17] == 1)
		  dojo.byId('subject_icon_own').innerHTML = '<a href="#" onClick="AddSubject(sid, hid, \'own\', 0); return false;"><img src="/images/icons/gr_own_1.png" border="0" /></a>';
    else
		  dojo.byId('subject_icon_own').innerHTML = '<a href="#" onClick="AddSubject(sid, hid, \'own\', 1); return false;"><img src="/images/icons/gr_own_0.png" border="0" /></a>';

    if (info[18] == 1)
		  dojo.byId('subject_icon_watch').innerHTML = '<a href="#" onClick="AddSubject(sid, hid, \'watch\', 0); return false;"><img src="/images/icons/gr_watch_1.png" border="0" /></a>';
    else
		  dojo.byId('subject_icon_watch').innerHTML = '<a href="#" onClick="AddSubject(sid, hid, \'watch\', 1); return false;"><img src="/images/icons/gr_watch_0.png" border="0" /></a>';
    }
  }

function ChangeSITab(id, sid)
  {
  var odiv = dojo.byId('hardware_' + oid);
  var div  = dojo.byId('hardware_' + id);

  var oht  = dojo.byId('grHardwareTab' + oid);
  var ht   = dojo.byId('grHardwareTab' + id);

  oht.className = 'grTab';
  ht.className  = 'grTabCurrent';
 
  odiv.style.display = 'none';
  div.style.opacity = 0;
  div.style.display = 'block';  
   
  dojo.fadeIn({ duration: 1000, node: div }).play();
  AjaxGetQuote(sid, id);
  
  oid = id;
  }

function FormatDate(timestamp, id, time)
  {
  node = dojo.byId(id);
  var timestring;

  if (timestamp > 0)
    {
    var mydate  = new Date(timestamp*1000);   

    if (!time)
      timestring = dojo.date.locale.format(mydate, {fullYear:1, formatLength:'short', selector:'date'});
    else
      timestring = dojo.date.locale.format(mydate, {fullYear:1, formatLength:'short'});
    }
  else
    timestring = '???';

  if (node)
    node.innerHTML = timestring;
  else
    return timestring;
  }

function MaximizeTrailer() 
  {
  var trailer = dojo.byId("grTrailer");
  var dummy   = dojo.byId("grTrailerDummy");

  if (trailer_status == 'minimized')
    {
    //trailer.style.opacity = '0';
    //dojo.anim(trailer, {opacity: 100}, 5000);    
    
    var a0 = dojo.anim(dummy,   {height: 466}, 500);    
    var a1 = dojo.anim(trailer, {height: 466}, 500);    
    var a2 = dojo.anim(trailer, {width: 480}, 500);    
    var a3 = dojo.anim(trailer, {left: 478}, 500);;

    var a4 = dojo.anim(trailer.childNodes[3].childNodes[1].childNodes[9], {width: 480}, 500);    
    var a5 = dojo.anim(trailer.childNodes[3].childNodes[1].childNodes[9], {height: 413}, 500);    

    //dojo.fx.chain([a0,a1,a2,a3,a4,a5]).play();
    trailer_status = 'maximized';
    }
  }

function MinimizeTrailer()
  {
  var trailer = dojo.byId("grTrailer");
  var dummy   = dojo.byId("grTrailerDummy");

  if (trailer_status == 'maximized')
    {
    var a0 = dojo.anim(dummy,   {height: 312}, 500);    
    var a1 = dojo.anim(trailer, {height: 312}, 500);    
    var a2 = dojo.anim(trailer, {width: 298}, 500);    
    var a3 = dojo.anim(trailer, {left: 660}, 500);;

    var a4 = dojo.anim(trailer.childNodes[3].childNodes[1].childNodes[9], {width: 298}, 500);    
    var a5 = dojo.anim(trailer.childNodes[3].childNodes[1].childNodes[9], {height: 258}, 500);    

    //dojo.fx.chain([a0,a1,a2,a3,a4,a5]).play();

    trailer_status = 'minimized';
    }  
  }
  
function MaximizeNews()
  {  
  if (!news)
    var news = dojo.byId("grNewsList");
  
  if (news_status == 'minimized')
    {
    //dojo.anim(news, { height: 400 }, 1000);    

    news.style.opacity = '0';
    news.style.height = '200';
    
    dojo.byId("grFurtherNews").style.display = 'block';
    dojo.query(".grNewsText").style("display","block");
    dojo.anim(news, {opacity: 100}, 5000);    
    dojo.anim(news, {height: 500}, 500);    
    news.style.overflow = 'scroll';
    news_status = 'maximized';
    }
  }

function heightCheck()
  {
  var content = dojo.byId("grContent");
  var struct  = dojo.byId("grBGStruct");
  var outer   = dojo.byId("grOuterFrame");

  outer.style.height   = 'auto';
  outer.style.overflow = 'visible';    

  var struct_coords = dojo.coords(struct);
  var outer_coords  = dojo.coords(outer);
  
  console.log(outer_coords['h']);
  
  if (struct_coords['h'] > outer_coords['h'])
    {
    var height = struct_coords['h'] + 'px';
    outer.style.height   = height;
    outer.style.overflow = 'hidden';
    }  
  }

function MinimizeNews()
  {
  if (!news)
    var news = dojo.byId("grNewsList");
    
  if (news_status == 'maximized')
    {
    news.style.opacity = '0';
    dojo.byId("grFurtherNews").style.display = 'none';
    dojo.query(".grNewsText").style("display","none");
    dojo.anim(news, { height: 200}, 500);    
    dojo.anim(news, {opacity: 100}, 5000);    
    news_status = 'minimized';
    news.style.overflow = 'hidden';
    }
  }

function loginCheck()
  {
  var id  = dojo.byId("login_id"); 
  var pwd = dojo.byId("login_pwd"); 

  if ((id) && (id.value == ''))
    id.value = std_login_id_value;

  if (pwd && (pwd.value == ''))
    pwd.value = std_login_pwd_value;
  }

function InitGR()
  {
  window.setTimeout("loginCheck()", 1000);    
  
  heightCheck();
  }
  
dojo.addOnLoad(InitGR);  