//Function to open new windows
function openWin(URL, Width, Height)
{

aWindow=window.open(URL,"PopUp","width="+Width+",height="+Height+",resizable=yes,scrollbars=yes,left=100,top=60");
    
    aWindow.resizeTo(Width, Height);
    
    aWindow.focus();
    if(aWindow.opener == null)
    {
        aWindow.opener = window;
        aWindow.opener.name = "opener";
    }

}

//Function to pop up windows for images.
function ImgPop(zeTitle, zeDescription, zeCopyright, zeSource, level)
{
    popTitle = zeTitle;
	popDescription = zeDescription;
    popCopyright = zeCopyright;
    popSource = zeSource;
	popUrl = "";

    if (popDescription == "description")
        popDescription = "";
    
    width=650;
    height=750;

    //If resolution is 640x480
    if(screen.width < 800)
        height=350;
    //If resolution is 800x600
    else if(screen.width < 1024)
        height=500;

    for(i=1; i<=level; i++)
	{
		popUrl = popUrl + "../";
	}

	popUrl = popUrl + "inc/popup.html";
//	confirm("The Url is " + popUrl);
	openWin(popUrl,width,height);
}

function DisplayImage(level, file_path, image_name, image_type, image_title, image_description, image_copyright, float_position)
{
	var image_url = '';
	var single_quot = "'";
	var image_code = '';
    for(i=1; i<=level; i++)
	{
		image_url += "../";
	}
	image_code += '<a href="javascript:ImgPop(' + single_quot + image_title + single_quot + ', ' + single_quot + image_description + single_quot + ', ' + single_quot + image_copyright + single_quot + ', ' + single_quot + '../' + file_path + image_name + '_det.' + image_type + single_quot + ', ' + level + ');">';
	image_code += '<img src="' + image_url + file_path + image_name + '_thu.' + image_type + '" class="' + float_position + '" alt="' + image_description + '" title="' + image_title + '" /></a>';
	document.write(image_code);
}

function DisplayVideo(file_path, video_name, video_type, video_title, video_description, copyright)
{
	var video_code = '';
	video_code  = '<!--<video><videotitle>-->';
	video_code += '<h3>'+video_title+'</h3>';
	video_code += '<!--</videotitle>-->';
	if ((video_type == 'MP4') || (video_type == 'mp4'))
	{
		video_code += '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ';
		video_code += '	codebase="http://www.apple.com/qtactivex/qtplugin.cab" ';
		video_code += '	width="320" height="256"> ';
		video_code += '	<param name="src" value="' + file_path + video_name + '.mp4" /> ';
		video_code += '	<param name="autoplay" value="true" /> ';
		video_code += '	<param name="controller" value="true" /> ';
		video_code += '	<object data="' + file_path + video_name  + '.mp4" ';
		video_code += '  		type="video/mp4" ';
		video_code += '  		width="320" height="256"> ';
		video_code += '		<param name="autoplay" value="true" /> ';
		video_code += '		<param name="controller" value="true" /> ';
		video_code += '	</object> ';
		video_code += '</object> ';
		video_code += '<!-- <a href="' + file_path + video_name + '.mp4"></a> -->';
	} else if ((video_type == 'WVX') || (video_type == 'wvx'))
	{
		video_code +=  '<object data="' + file_path + video_name + '.wvx" type="video/x-ms-wvx"';
		video_code +=  '	width="320" height="256">';
		video_code +=  '	<param name="src" value="' + file_path + video_name + '.wvx">';
		video_code +=  '</object>';
	}
	else
	{
		alert('The video type <' + video_type + '> has not been setup.');
	}
	video_code += '<!-- <a href="'+file_path+video_name+'.'+video_type+'"></a> -->';
	video_code += '<!--<videodesc>-->';
	video_code += '<p>'+video_description+'</p>';
	video_code += '<p>'+copyright+'</p>';
	video_code += '<!--</videodesc>-->';
	video_code += '<!--</video>-->';
	document.write(video_code);
}

function DisplayAudio(file_path, audio_name, audio_type, audio_title, audio_description, copyright)
{
	var audio_code = '';
	audio_code  = '<!--<audio><audiotitle>-->';
	audio_code += '<h3>'+audio_title+'</h3>';
	audio_code += '<!--</audiotitle>-->';
	if (window.ActiveXObject)
	{
		audio_code += '<object classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" width="260" height="45" align="middle">';
		audio_code += '<param name="src" value='+ file_path+audio_name+'.'+audio_type +' />';
		audio_code += '<param name= "autoplay" value= "true" />';
		//the embed is in here because of old browser functionality
		audio_code += '<embed src='+ file_path+audio_name+'.'+audio_type +' autoplay="true" width="260" height= "45" align= "middle" bgcolor="E7EADF" pluginspage="http://www.apple.com/quicktime/download/">';
		audio_code += '</embed>';
		audio_code += '</object>';
	}
	else
	{
		document.write('<object type="audio/x-mpeg" data='+ file_path+audio_name+'.'+audio_type +' width="260" height="45">\n');
        document.write('<param name="autoplay" value="true" />\n');
        document.write('<param name="controller" value="true" />\n');
        document.write('</object>\n');
	}

	audio_code += '<!-- <a href="'+file_path+audio_name+'.'+audio_type+'"></a> -->';
	audio_code += '<!--<audiodesc>-->';
	audio_code += '<p>'+audio_description+'</p>';
	audio_code += '<p>Accession Number: '+audio_name+'</p>';
	audio_code += '<p>'+copyright+'</p>';
	audio_code += '<!--</audiodesc>-->';
	audio_code += '<!--</audio>-->';
	document.write(audio_code);
}

// The following code uses the AJAX technology
var xmlHttp
var hostaddress="http://www.albertasource.ca/abestonians/";
var select_counter;
var image_counter=0;
var A_accession_number = [];
var A_position = [];
var A_level = [];
/*
function DisplayContent(url)
{
	confirm(hostaddress+url);
	aWindow=window.open(hostaddress+url,"PopUp",resizable=yes,scrollbars=yes,left=100,top=60");
    aWindow.focus();
    if(aWindow.opener == null)
    {
        aWindow.opener = window;
        aWindow.opener.name = "opener";
    }
}
*/
function showGroupCategory(group_category_id)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	url+="/administration/getgroupcategory.php"
	url+="?group_category_id="+group_category_id
	url+="&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChangedGroup 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
function stateChangedGroup() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("txtcategory").innerHTML=xmlHttp.responseText 
	} 
}

function showSubCategory(category_id)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url=hostaddress;
	url+="/administration/getsubcategory.php"
	url+="?category_id="+category_id
	url+="&sid="+Math.random()	
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("txtsubcategory").innerHTML=xmlHttp.responseText 
	} 
}

function SetupImage(accession_number, position, level)
{
	A_accession_number[image_counter]=accession_number;
	A_position[image_counter]=position;
	A_level[image_counter]=level;
	image_counter++;
}

function ProcessImages()
{
	if (image_counter > 0)
	{
		image_counter=0;
		DisplayImages();
	}
}

function DisplayImages()
{
	accession_number=A_accession_number[image_counter];	
	position=A_position[image_counter];	
	level=A_level[image_counter];	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url=hostaddress;
	url+="administration/getimageinfo.php";
	url+="?accession_number="+accession_number;
	url+="&image_position="+position;
	url+="&image_level="+level;
	url+="&sid="+Math.random();
	xmlHttp.onreadystatechange=foundedImageInfo; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function foundedImageInfo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		info=xmlHttp.responseText;
		field_name = A_accession_number[image_counter];
		document.getElementById(field_name).innerHTML=xmlHttp.responseText;
		if (image_counter < A_accession_number.length-1)
		{
			image_counter++;
			DisplayImages();
		}
	}
}

function SetupVideo(accession_number)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url=hostaddress;
	url+="administration/getvideoinfo.php";
	url+="?accession_number="+accession_number;
	url+="&sid="+Math.random();
	xmlHttp.onreadystatechange=foundedVideoInfo; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function foundedVideoInfo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		document.getElementById("VideoInfo").innerHTML=xmlHttp.responseText;
	}
}

function SetupAudio(accession_number)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url=hostaddress;
	url+="administration/getaudioinfo.php";
	url+="?accession_number="+accession_number;
	url+="&sid="+Math.random();
	xmlHttp.onreadystatechange=foundedAudioInfo; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function foundedAudioInfo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		document.getElementById("AudioInfo").innerHTML=xmlHttp.responseText;
	}
}

function LoadPageContent(Content_Filename)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url=hostaddress;
	url+="administration/getpagecontent.php";
	url+="?content_filename="+Content_Filename;
	url+="&sid="+Math.random();
	xmlHttp.onreadystatechange=foundedPageInfo; 
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function foundedPageInfo() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		info=xmlHttp.responseText;
		document.getElementById("PageContent").innerHTML=xmlHttp.responseText;
		found_scripts=info.split('</script>');
		for (i=0; i<found_scripts.length-1; i++)
		{
			start_location=found_scripts[i].search(/javascript:/);			
			check_script = found_scripts[i].substr(start_location+11);
			script_name = check_script.split('(');
			if (script_name[0] == "SetupImage")
			{
				// We need to parse out the parameters needed to call the SetupImage function
				check_script = check_script.substr(script_name[0].length+1,check_script.length-(script_name[0].length));
				// Each of the parameters will be separated by commas
				check_script = check_script.replace(');',',');
				// Find the location of the first comma and get the first parameter
				comma_pos = check_script.indexOf(",");
				first_parameter = check_script.substr(0,comma_pos);
				check_script = check_script.substr(comma_pos+1);
				// Find the location of the first comma and get the second parameter
				comma_pos = check_script.indexOf(",");
				second_parameter = check_script.substr(0,comma_pos);
				check_script = check_script.substr(comma_pos+1);
				// Find the location of the first comma and get the third parameter
				comma_pos = check_script.indexOf(",");
				third_parameter = check_script.substr(0,comma_pos);
				// remove all of the quotes(') from the first two parameters
				first_parameter = first_parameter.replace("'","");
				first_parameter = first_parameter.replace("'","");
				second_parameter = second_parameter.replace("'","");
				second_parameter = second_parameter.replace("'","");
				// know that we have all of the parameters we can call the SetupImage function
				SetupImage(first_parameter,second_parameter,third_parameter);
			}
			else if (script_name[0] == "SetupVideo")
			{
				// We need to parse out the parameter needed to call the SetupVideo function
				check_script = check_script.substr(script_name[0].length+1,check_script.length-(script_name[0].length));
				// remove all of the quotes(')
				first_parameter = check_script.replace("');","");
				first_parameter = first_parameter.replace("'","");
				SetupVideo(first_parameter);
			}
			else if (script_name[0] == "SetupAudio")
			{
				// We need to parse out the parameter needed to call the SetupAudio function
				check_script = check_script.substr(script_name[0].length+1,check_script.length-(script_name[0].length));
				// remove all of the quotes(')
				first_parameter = check_script.replace("');","");
				first_parameter = first_parameter.replace("'","");
				SetupAudio(first_parameter);
			}
			else if (script_name[0] == "ProcessImages")
			{
				ProcessImages();
			}
		}
	}
}

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");
		}
	}
	return xmlHttp;
}
function InsertAudio(AudioNameAndPath)
{
	if (window.ActiveXObject)
	{
		document.write('<object classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" width="260" height="45" align="middle">\n');
		document.write('<param name="src" value='+ AudioNameAndPath +' />\n');
		document.write('<param name= "autoplay" value= "true" />\n');
		//the embed is in here because of old browser functionality
		document.write('<embed src='+ AudioNameAndPath +' autoplay="true" width="260" height= "45" align= "middle" bgcolor="E7EADF" pluginspage="http://www.apple.com/quicktime/download/">\n');
		document.write('</embed>\n');
		document.write('</object>\n');
	}
	else
	{
		document.write('<object type="audio/x-mpeg" data='+ AudioNameAndPath +' width="260" height="45">\n');
        document.write('<param name="autoplay" value="true" />\n');
        document.write('<param name="controller" value="true" />\n');
        document.write('</object>\n');
	}
}