var load;
var images = new Array();

function open_image_viewer(game_id, image_id, type_id, pub_id, title_id, console_id)
{
	if (load != null && !load.closed && load.close)
		load.close();

	width = 700;
	height = 575;
	screenX = (window.screen.availWidth - width)/2;

	load = window.open('image_viewer.php?game_id=' + game_id + '&image_id=' + image_id + '&type_id=' + type_id + '&pub_id=' + pub_id + '&title_id=' + title_id + '&console_id=' + console_id,'','scrollbars=yes,menubar=no,height=' + height + ',width=' + width + ',left=' + screenX + ',top=20,resizable=yes,toolbar=no,location=no,status=no');

	if (window.focus)
		load.focus();
}


function prevClick()
{
	if (curr_img_index > 1)
	{
		curr_img_index--;
	}
	else
	{
		curr_img_index = images.length;
	}
	
	document.getElementById('image_holder').src = '/images/' + images[curr_img_index-1];
	document.getElementById('image_holder').title = titles[curr_img_index-1];
	document.getElementById('img_nr').innerHTML = curr_img_index;
}


function nextClick()
{
	if (curr_img_index < images.length)
	{
		curr_img_index++;
	}
	else
	{
		curr_img_index = 1;
	}
	
	document.getElementById('image_holder').src = '/images/' + images[curr_img_index-1];
	document.getElementById('image_holder').title = titles[curr_img_index-1];
	document.getElementById('img_nr').innerHTML = curr_img_index;
}


function doGameVote(id, vote)
{
	if (window.confirm('Är du säker på att ge ' + vote + ' i betyg?'))
	{
		window.location='/game_info.php?game_id=' + id + '&action=vote_score&user_score=' + vote;
	}
}


// Display a image infront of the page with dark transparent background
// The function takes a index in img_arr a array of file paths, img_arr is required
function display_screenshots(index)
{
	// Show the complete div everything is inside
	var con = document.getElementById('container');
	con.style.display = 'block';
	
	// The number of images to be able to scroll through
	var max = img_arr.length;
	
	// Indexes for the previus and nex images
	var prev = index - 1;
	var next = index + 1;
	
	// Check boundries of the array and start from the other end if a endpoint is active
	if (index == 0)
	{
		prev = max - 1;
	}

  if (index == (max - 1))
	{
		next = 0;
	}
	
	// Edit the background so it covers the whole html area
  var div = document.getElementById('screenshot_bg');
  div.style.width = screen.width - 21; // The whole width - 21 pixels so we don't get a scrollbar 
  div.style.height = document.getElementById('bottom_anchor').offsetTop + 12; // Get the anchor "bottom" and add 12 pixels to it's position to get the whole height
  
  // Load the correct image into the image holder
  var img = document.getElementById('screenshot');
  img.src = '/images/' + img_arr[index];
  
  // Generate a correct string
  var str = (index + 1) + ' / ' + max;
  
  // Print the string on the page
  var txt = document.getElementById('textDiv');
  txt.innerHTML = str;
  
  // Get the previus button and enable a link to the previus image
  var pBut = document.getElementById('prevButton');
  pBut.href = 'javascript:display_screenshots('+prev+');';
  
  // Get the next button and enable a link to the next image
  var nBut = document.getElementById('nextButton');
  nBut.href = 'javascript:display_screenshots('+next+');';
  
  // Change the width of the button table to match the image size
  setTimeout('setBW()', 500);
  setTimeout('setAb()', 500);
}

function display_forum_screenshots(arr, index)
{
	// Store the array
	images = arr;
		
	// Show the complete div everything is inside
	var con = document.getElementById('container');
	con.style.display = 'block';
	
	// The number of images to be able to scroll through
	var max = arr.length;
	
	// Indexes for the previus and nex images
	var prev = index - 1;
	var next = index + 1;
	
	// Check boundries of the array and start from the other end if a endpoint is active
	if (index == 0)
	{
		prev = max - 1;
	}
	
	if (index == (max - 1))
	{
		next = 0;
	}
	
	// Edit the background so it covers the whole html area
  var div = document.getElementById('screenshot_bg');
  div.style.width = screen.width - 21; // The whole width - 21 pixels so we don't get a scrollbar 
  div.style.height = document.getElementById('bottom_anchor').offsetTop + 12; // Get the anchor "bottom" and add 12 pixels to it's position to get the whole height
  
  // Load the correct image into the image holder
  var img = document.getElementById('screenshot');
  img.src = '/files/' + images[index];
  if (img.width > 900)
  {
  	img.width = 900;
  }
  
  // Generate a correct string
  var str = (index + 1) + ' / ' + max;
  
  // Print the string on the page
  var txt = document.getElementById('textDiv');
  txt.innerHTML = str;
  
  // Get the previus button and enable a link to the previus image
  var pBut = document.getElementById('prevButton');
  pBut.href = 'javascript:display_forum_screenshots(images, '+prev+');';
  
  // Get the next button and enable a link to the next image
  var nBut = document.getElementById('nextButton');
  nBut.href = 'javascript:display_forum_screenshots(images, '+next+');';
  
  // Change the width of the button table to match the image size
  setTimeout('setBW()', 500);
  setTimeout('setAb()', 500);
}

function display_screenshot(file)
{
	// Show the complete div everything is inside
	var con = document.getElementById('container');
	con.style.display = 'block';
	
	// Edit the background so it covers the whole html area
  var div = document.getElementById('screenshot_bg');
  div.style.width = screen.width - 21; // The whole width - 21 pixels so we don't get a scrollbar 
  div.style.height = document.getElementById('bottom_anchor').offsetTop + 12; // Get the anchor "bottom" and add 12 pixels to it's position to get the whole height
  
  // Load the correct image into the image holder
  var img = document.getElementById('screenshot');
  img.src = '/images/' + file;
  if (img.width > 900)
  {
  	img.width = 900;
  }
  
  // Change the width of the button table to match the image size
  setTimeout('setAb()', 500);
}

// Sets the width of the button table to fit the width of the image
function setBW()
{
	var img = document.getElementById('screenshot');
	var but = document.getElementById('buttonTable');
	
	// If large image
	if (img.width >= 640)
	{
  	but.width = img.width;
  }
  else
  {
  	// If small image
  	but.width = 680;
  }
}

// Sets the position of the absolute div to the center of the screen
function setAb()
{
	var img = document.getElementById('screenshot');
	var rel = document.getElementById('relativeDiv');
	var abs = document.getElementById('absoluteDiv');
	
	// If large image
	if (img.width >= 640)
	{
		rel.style.width = img.width;
  	abs.style.left = document.body.scrollLeft + (document.body.clientWidth / 2) - (img.width / 2) - 7;
  }
  else
  {
  	// If small image
  	rel.style.width = 680;
  	abs.style.left = document.body.scrollLeft + (document.body.clientWidth / 2) - (680 / 2) - 10;
  }
  
  // Set the position to a the golden ratio (kindof)
  abs.style.top = document.body.scrollTop + document.body.clientHeight / 18;
}