// -----------------------------------------------------------
// this function accepts a media id and gets the data for this
// video from the database and populates the update form
// -----------------------------------------------------------
function GetGridForGenre(genre_id,limit,start,page, media_id)
{
if (limit == '')
{
	limit = 20;
}

if (start == '')
{
	start = 0;
} 

if ((genre_id.length==0) && (media_id.length ==0))
  {
  	return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url = '/cgi-bin/yourphotos.pl';
url=url+"?id="+genre_id+'&limit='+limit+'&start='+start+'&page='+page;


// -----------------------------------------------
// The three lines below load the bottom grid
// -----------------------------------------------
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

// -----------------------------------------------
// We check to see if the media id has a hash. If 
// it does, we remove it alone with everyting after it
// -----------------------------------------------
/*
   var mySplitResult = new Array();

	var mySplitResult = media_id.split("#");
	if ((mySplitResult[0] != '') && (mySplitResult[0] != false))
	{
		media_id = mySplitResult[0]
	}
*/

if (media_id == false)
{
	media_id = '';
}

// -----------------------------------------------
// The line below loads the top image by calling 
// the LoadFirstImage routine
// The line below does not beed to be called in if
// the user passed in a media_id. 
// Need to write a new script like yourphotos.pl
// that collects the data for ONE media and and also
// sets up the top frame for one photo. Then using json
// we can make a javascript call to substitute the html.
// -----------------------------------------------
window.location.hash="green_nav_grid";
setTimeout('LoadFirstImage('+media_id+');',3000);
}

// -----------------------------------------------------------
function GetVmixDataForMediaID(media_id)
{
xmlHttp2=GetXmlHttpObjectSingle();
if (xmlHttp2==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url = '/cgi-bin/directimagelink.pl';
url=url+'?media_id='+media_id;

// -----------------------------------------------
// The three lines below load the bottom grid
// -----------------------------------------------
//alert('url is: ' + url);
xmlHttp2.onreadystatechange=stateChangedSingle;
xmlHttp2.open("GET",url,true);
xmlHttp2.send(null);
window2.location.hash="green_nav_grid";
}
// -----------------------------------------------------------
function LoadFirstImage(my_media_id)
{
var media_id = '';

if (!my_media_id)
{
	// this function loads the first image in the bottom grid.
	// if there is no media_id, it takes the first media_id as 
	// the top image.
	//alert('media id is ' + media_id);
	 media_id = $('#grid ul li:first').attr('id');
	//alert('media id is ' + media_id);
	var url = document.getElementById(media_id + '_image_url').innerHTML;
	var prev = document.getElementById(media_id + '_prev').innerHTML;
	var next = document.getElementById(media_id + '_next').innerHTML;
	var genre_id = document.getElementById('current_genre').innerHTML;

	loadUserImage(media_id,url,next,prev,genre_id);

} else {
	// we call the perl script on the server to retrieve the data for
	// one photo and then substitute this data in the top photo div
	media_id = my_media_id;
	GetVmixDataForMediaID(media_id);
}

}

// ------------------------------------

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 

if (xmlHttp.responseText != '')
{
	mydata = xmlHttp.responseText;
  	document.getElementById("grid").innerHTML=mydata;
	//return false;
} else {
	alert('there was an error');
  	document.getElementById("grid").innerHTML="<b>There was an error loading the data.";
} // if
} // if
} 

// -----------------------------------------------------------

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
if (!xmlHttp)
{ alert('Error initializing XMLHttpRequest'); }
return xmlHttp;
}

// ------------------------------------------------------

function loadUserImage(media_id,url,next,prev,genre_id){

var p_prev = prev + "_prev";
var p_next = prev + "_next";
var p_jpeg = prev + "_image_url";

var n_prev = next + "_prev";
var n_next = next + "_next";
var n_jpeg = next + "_image_url";

var prev_link = "PREV";
var next_link = "NEXT";

var page_id =  genre_id + "_pages";
var pages = document.getElementById(page_id).innerHTML;

var title_id =  media_id + '_title';
var desc_id =  media_id + '_desc';
var author_id =  media_id + '_author';

var title = document.getElementById(title_id).innerHTML;
var author = document.getElementById(author_id).innerHTML;
var description = document.getElementById(desc_id).innerHTML;

if (author != '')
{ author = 'By ' + author; }

// set up the prev link
if (prev != '')
{
prev_link = "<b><a href=\"#\" onclick=\"javascript:loadUserImage('"+prev+"','"+document.getElementById(p_jpeg).innerHTML + "','" + document.getElementById(p_next).innerHTML + "','" + document.getElementById(p_prev).innerHTML + "','" + genre_id + "');\">&lt;PREV</a>&nbsp;&nbsp;</b>";
} else {
prev_link = document.getElementById('prev_set_photos').innerHTML;
}

// set up the next link
if (next != '')
{
next_link = "&nbsp;&nbsp;<b><a href=\"#\" onclick=\"javascript:loadUserImage('"+next+"','"+document.getElementById(n_jpeg).innerHTML + "','" + document.getElementById(n_next).innerHTML + "','" + document.getElementById(n_prev).innerHTML + "','" + genre_id + "');\">NEXT &gt;</a></b>";
} else {
next_link = document.getElementById('next_set_photos').innerHTML;
}
document.getElementById("user_submitted_top_photo").innerHTML = '<h2>' +title +"</h2>"+author+"<br><br>"+prev_link + " " + next_link + "<br><br><img src=\"" + url + "\"><br><div id=userphotodesc style='width:400px;margin-bottom:10px;' ><br>" + description + "<br><br>" + prev_link + " " + next_link + "<br><br></div>";
}
// -----------------------------------------------------------
// This fuction fills in the top area with the html for the media_id
// that was submitted in the query string of the url. We go to vmix,
// get the data, format it, and the display it in the top half of the 
// page in this function.
// -----------------------------------------------------------

function stateChangedSingle()
{
if (xmlHttp2.readyState==4)
{

if (xmlHttp2.responseText != '')
{
        mydata = xmlHttp2.responseText;
        document.getElementById("user_submitted_top_photo").innerHTML=mydata;
        return false;
} else {
        alert('there was an error');
        document.getElementById("user_submitted_top_photo").innerHTML="<b>There was an error loading the data.";
} // if
} // if
}

// -----------------------------------------------------------

function GetXmlHttpObjectSingle()
{
var xmlHttp2=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp2=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
if (!xmlHttp2)
{ alert('Error initializing XMLHttpRequest'); }
return xmlHttp2;
}

// ------------------------------------------------------

//GetGridForGenre('00004595'); 
/*
$(function() {
        $('a#all_header').trigger('click');        
});			
*/

// ------------------------------------------------------
