/*******************************************************************************/
/*							DISPLAY MANAGEMENT								   */
/*******************************************************************************/
var CImages = new Array();
var ImagesBeforeTable = 2
var countimages = 0;
var LblFlyover
var i = 0;
var MacSystem ='';
var DisplayPopup ='False';
var ButtonsPerImage = 3;

var DetailPage = '';
var ContainerEParam = '';
var ThumbType = '';
var CartType = '';
var AbleToEditAlbum = false;
var AbleToEditPicture = false;

var LinkNextPage = '';
var LinkPreviousPage = '';
var LinkFirstPage = '';
var LinkEndPage = '';

var LinkToEditPage = '';
var UserLogged = false;
var NbColumns = 3;
var ImgSize = 'TR6';

var ERestr = '';

var Xoffset = -50; 
var Yoffset = 25; 

var nav, old, iex = (document.all), yyy=-1000;

var FlyOverBackColor = '';
var ShowCaption = true;



function DocImage(tEParam, tDocYear, tSourceName, tCreatorName, tDO_Custom_4, tCaptionShort,tCaptionLong, tPrintableTitle, tBox1, tBox2, tCart, tRestr, tPictureRestricted, tAlbum, tDO1, tDO2, tDO3, tRestrictions, tEmbargo, tHighRes, tFilePath, tItemNumber, tCounter, tCanEditPic, tDocType, tPicWidth, tPicHeight, tMotherCreator_Name, tMotherDO_Custom_3, tMotherDO_Custom_6, tMotherCopyright, tMotherCompany, tAuthor_Rights)
{
	this.EParam = tEParam;
	if (tDocYear.length >6){tDocYear = tDocYear.substring(3,7);}
		this.DocYear = tDocYear;
		this.SourceName = tSourceName;
		this.CreatorName = tCreatorName;
		this.DO_Custom_4 = tDO_Custom_4;
		this.CaptionShort = tCaptionShort;
		this.CaptionLong = tCaptionLong;
		this.PrintableTitle = tPrintableTitle;
		this.Box1 = tBox1;
		this.Box2 = tBox2;
		this.Cart = tCart;
		this.Album = tAlbum;
		this.Restriction = tRestr;
		this.PictureRestricted = tPictureRestricted;
		this.DO_Custom_1 = tDO1;
		this.DO_Custom_2 = tDO2;
		this.DO_Custom_3 = tDO3;
		this.Restrictions = tRestrictions;
		this.Embargo = tEmbargo;
		this.HighRes = tHighRes;
		this.FilePath = tFilePath;
		this.ItemNumber = tItemNumber;
		this.Counter = tCounter;
		this.CanEditPic = tCanEditPic;
		this.DocType = tDocType;
		this.PicWidth = tPicWidth;
		this.PicHeight = tPicHeight;
		this.MotherCreator_Name = tMotherCreator_Name;
		this.MotherDO_Custom_3 = tMotherDO_Custom_3;
		this.MotherDO_Custom_6 = tMotherDO_Custom_6;
		this.MotherCopyright = tMotherCopyright;
		this.MotherCompany = tMotherCompany;
		this.Author_Rights = tAuthor_Rights;
}

function SwitchImageSelection(tEParam, tCategory, tCartType, tAction, tAlbRecordID, tDO1, tDO2, tDO3)
{
	tUrl = ''
	tUrl += 'InvisibleToolPage.aspx?Category=' + tCategory + '&Action=' + tAction + '&CartType=' + tCartType;
	tUrl += '&DO1=' + tDO1 + '&DO2=' + tDO2 + '&DO3=' + tDO3;
	tUrl += '&E=' + tEParam + '&TopID=' + tAlbRecordID;
	image1 = new Image();
	image1.src = tUrl;
}

function Navigation_ie()
{
	if (event.keyCode ==34){if (LinkNextPage != ''){document.location.href = LinkNextPage}}	
	if (event.keyCode ==33){if (LinkPreviousPage != ''){document.location.href = LinkPreviousPage}}	
	if (event.keyCode ==36){if (LinkFirstPage != ''){document.location.href = LinkFirstPage}}	
	if (event.keyCode ==35){if (LinkEndPage != ''){document.location.href = LinkEndPage}}	
}

// Create action associate to the button
function MouseButtonAction(vCounter, tButtonName, tAction)
{	
	var AlbID = '';
	var tActionToDo = '';
	var tPosition = 0;
	var tCategory = 'CART';
	var tempcarttype = '';
	
	if (AbleToEditAlbum == true) { tPosition = tPosition + 1; }
	
	switch (tButtonName)
	{
		case 'Ico_Album_' :
			tempcarttype = 'Album';
			tPosition = tPosition + 0;
			tCategory = 'ALBUM';
			tONOFF=CImages[vCounter].Album;

			if (tAction == 'CLICK')
			{ 
				tONOFF = (tONOFF + 1 ) % 2; 
				CImages[vCounter].Album = tONOFF; 

				AlbID = ContainerEParam; 
			}
			break;
		case 'Ico_Box_A_Thumb_' :
			tempcarttype = 'Box1';
			tPosition = tPosition + 1;
			tONOFF=CImages[vCounter].Box1;
			if (tAction == 'CLICK') { tONOFF = (tONOFF + 1 ) % 2; CImages[ vCounter].Box1 = tONOFF; }

			break;
		case 'Ico_Box_B_Thumb_' :
			tempcarttype = 'Box2';
			tPosition = tPosition + 2;
			tONOFF=CImages[vCounter].Box2;
			if (tAction == 'CLICK') { tONOFF = (tONOFF + 1 ) % 2; CImages[vCounter].Box2 = tONOFF; }      
			
			break;
		case 'Ico_Cart_Thumb_' :
			tempcarttype = 'Cart';
			tPosition = tPosition + 3;
			tONOFF=CImages[vCounter].Cart;
			if (tAction == 'CLICK') { tONOFF = (tONOFF + 1 ) % 2; CImages[vCounter].Cart = tONOFF; }      

			break;
		case 'Ico_Edit_Thumb_' :
			tempcarttype = 'Edit';
			tPosition = tPosition + 4;
			tONOFF=0;
			if (tAction == 'CLICK') 
			{ 
				top.document.location.href= LinkToEditPage + '&E=' + CImages[vCounter].EParam ; 
			}      

			break;
		case 'Ico_Restr_Thumb_' :
			tempcarttype = 'Restriction';
			tButtonName = 'Ico_Restr_Thumb_'
			if (CImages[vCounter].PictureRestricted == 0)
			{ tButtonName = tButtonName + 'Off_' }
			else
			{ tButtonName = tButtonName + 'On_' }
			tPosition = tPosition + 5;
			tCategory = 'RESTRICTION';
			tONOFF=CImages[vCounter].Restriction;
			if (tAction == 'CLICK')
			{ 
				tONOFF = (tONOFF + 1 ) % 2; 
				CImages[vCounter].Restriction = tONOFF; 
				AlbID = ERestr; 
			}

			break;
		case 'Ico_Restr_Thumb_On_' :
			tempcarttype = 'Restriction';
			tButtonName = 'Ico_Restr_Thumb_'
			if (CImages[vCounter].PictureRestricted == 0)
			{ tButtonName = tButtonName + 'Off_' }
			else
			{ tButtonName = tButtonName + 'On_' }
			tPosition = tPosition + 5;
			tCategory = 'RESTRICTION';
			tONOFF=CImages[vCounter].Restriction;
			if (tAction == 'CLICK')
			{ 
				tONOFF = (tONOFF + 1 ) % 2; 
				CImages[vCounter].Restriction = tONOFF; 
				AlbID = ERestr; 
			}
			break;
		case 'Ico_Restr_Thumb_Off_' :
			tempcarttype = 'Restriction';
			tButtonName = 'Ico_Restr_Thumb_'
			if (CImages[vCounter].PictureRestricted == 0)
			{ tButtonName = tButtonName + 'Off_' }
			else
			{ tButtonName = tButtonName + 'On_' }
			tPosition = tPosition + 5;
			tCategory = 'RESTRICTION';
			tONOFF=CImages[vCounter].Restriction;
			if (tAction == 'CLICK')
			{ 
				tONOFF = (tONOFF + 1 ) % 2; 
				CImages[vCounter].Restriction = tONOFF; 
				AlbID = ERestr; 
			}
			break;
		default :
			alert('Bouton d action inconnu!' + tButtonName);
			break;
	}

	if (tAction == 'CLICK' && UserLogged == false)
	{
		top.document.location.href = 'Login.aspx';
	}
	else
	{
		if (tAction == 'CLICK')
		{
			if (tONOFF == 0) { tActionToDo = 'REMOVE'; } else { tActionToDo = 'ADD'; }
			SwitchImageSelection(CImages[vCounter].EParam,tCategory, tempcarttype, tActionToDo, AlbID, CImages[vCounter].DO_Custom_1, CImages[vCounter].DO_Custom_2, CImages[vCounter].DO_Custom_3);
		}
		
		if (tAction == 'OVER' && tONOFF == 0 )
		{ tFileName = '../images/' + tButtonName + 'over.gif'; }
		else
		{   
			if (tONOFF == 0 ) 
			{ tFileName = '../images/' + tButtonName + 'off.gif'; } 
			else 
			{ tFileName = '../images/' + tButtonName + 'on.gif'; }
		}
		tImagePosition = (CImages[vCounter].Counter * (ButtonsPerImage + 1 )  + ImagesBeforeTable) + tPosition;
		tFinalName = 'btn' + tImagePosition
		document.images[tFinalName].src = tFileName;
	}
}


	
// Create HTML text which show a button
function BuildButton(vCounter, tButtonName)
{
	var tIconWidth = 0;
	var tIconHeight = 0;
	var tPosition = 0;
	if (AbleToEditAlbum == true) { tPosition = tPosition + 1; }
	
	switch (tButtonName)
	{
		case 'Ico_Album_' :
			tPosition = tPosition + 0;
			tONOFF = CImages[vCounter].Album;
			tIconWidth = 48;
			tIconHeight = 24;
			LblFlyover = "Ajouter/Supprimer des images de la selection existante";
			break;
		case 'Ico_Box_A_Thumb_' :
			tPosition = tPosition + 1;
			tONOFF = CImages[vCounter].Box1;
			tIconWidth = 48;
			tIconHeight = 24;
			LblFlyover = "Ajouter/Supprimer des images du choix A";
			break;
		case 'Ico_Box_B_Thumb_' :
			tPosition = tPosition + 2;
			tONOFF = CImages[vCounter].Box2;     
			tIconWidth = 48;
			tIconHeight = 24;
			LblFlyover = "Ajouter/Supprimer des images du choix B";
			break;
		case 'Ico_Cart_Thumb_' :
			tPosition = tPosition + 3;
			tONOFF = CImages[vCounter].Cart;     
			tIconWidth = 55;
			tIconHeight = 24;
			LblFlyover = "Ajouter/Supprimer des images du panier";
			break;
		case 'Ico_Edit_Thumb_' :
			tPosition = tPosition + 4;
			tONOFF = 0;     
			tIconWidth = 35;
			tIconHeight = 24;
			LblFlyover = "Editer l'image";
			break;
		case 'Ico_Restr_Thumb_' :
			tPosition = tPosition + 5;
			tButtonName = 'Ico_Restr_Thumb_'
			if (CImages[vCounter].PictureRestricted == 0)
			{ tButtonName = tButtonName + 'Off_' }
			else
			{ tButtonName = tButtonName + 'On_' }
			tONOFF = CImages[vCounter].Restriction;    
			tIconWidth = 16;
			tIconHeight = 15;
			LblFlyover = "Assigner la restriction à l'image";
			break;
		case 'Ico_Restr_Thumb_On_' :
			tPosition = tPosition + 5;
			tButtonName = 'Ico_Restr_Thumb_'
			if (CImages[vCounter].PictureRestricted == 0)
			{ tButtonName = tButtonName + 'Off_' }
			else
			{ tButtonName = tButtonName + 'On_' }
			tONOFF = CImages[vCounter].Restriction;    
			tIconWidth = 16;
			tIconHeight = 15;
			LblFlyover = "Assigner la restriction à l'image";
			break;
		case 'Ico_Restr_Thumb_Off_' :
			tPosition = tPosition + 5;
			tButtonName = 'Ico_Restr_Thumb_'
			if (CImages[vCounter].PictureRestricted == 0)
			{ tButtonName = tButtonName + 'Off_' }
			else
			{ tButtonName = tButtonName + 'On_' }
			tONOFF = CImages[vCounter].Restriction;    
			tIconWidth = 16;
			tIconHeight = 15;
			LblFlyover = "Assigner la restriction à l'image";
			break;
		default :
			alert('Bouton d action inconnu!' + tButtonName);
			tIconWidth = 0;
			tIconHeight = 0;
			break;
	}

	if (tONOFF == 0)
	{ tFileName = '../images/' + tButtonName + 'off.gif'; } 
	else   
	{ tFileName = '../images/' + tButtonName + 'on.gif'; }

	tFileNameOver = '../images/' + tButtonName + 'over.gif';
	tReturn = '';
	tReturn += '<A HREF = "javascript:void(0)" ';
	tReturn += ' onMouseOver = MouseButtonAction(' + vCounter + ',"' + tButtonName + '","OVER") ';
	tReturn += ' onMouseOut = MouseButtonAction(' + vCounter + ',"' + tButtonName + '","NORMAL") '; 
	tReturn += ' onClick = "return false;" '; 
	tReturn += ' onMouseUp = MouseButtonAction(' + vCounter + ',"' + tButtonName + '","CLICK")> ';  

	tImagePosition = (CImages[vCounter].Counter * (ButtonsPerImage + 1 )  + ImagesBeforeTable) + tPosition;
	tFinalName = 'btn' + tImagePosition
	tReturn += '<IMG name="' + tFinalName + '" SRC = "' + tFileName + '" width="' + tIconWidth + '" height="' + tIconHeight + '" alt="' + LblFlyover + '" border="0"></a>';
	return tReturn;
}





	/*******************************************************************************/
	/*						Build Images										   */
	/*******************************************************************************/
	function ImageCell(vCounter, vHeight)
			{
				tReturn = "";
				tReturn += '<TABLE BORDER=0>';
				tReturn += '<TR><TD ';
				if (ImgSize == 'TRV')
					{ tReturn += 'WIDTH="100" '; }
				else
					{ tReturn += 'WIDTH="200" '; }
				tReturn += 'HEIGHT="' + vHeight + '" ';
				tReturn += 'align="center" valign="bottom">';
				switch (CImages[vCounter].DocType)
				{
					case "Album" :
						tReturn += '<A HREF="FramerT.aspx?V=CDocT&E=' + CImages[vCounter].EParam + '&DT=ALB" target="AlbumDetail" ';
						break;
					case "Story" :
						tReturn += '<A HREF="FramerT.aspx?V=CDocT&E=' + CImages[vCounter].EParam + '&DT=DOC" target="StoryDetail" ';
						break;
					default :
						tReturn += '<A HREF="' + DetailPage + '&Pic=' + CImages[vCounter].ItemNumber + '&SubE=' + CImages[vCounter].EParam + '" target=_top ';
						break;
				}
				if (DisplayPopup == 'True' && ShowCaption == true)
					{
					tReturn += ' onmouseout=ProcessImageOut()';
					tReturn += ' onmouseover=ProcessImageIn(' + vCounter + ') ';
					}
				tReturn += '><IMG border=0 name="ThumbNail' + vCounter + '" src="' + CImages[vCounter].FilePath + '" OnError="this.src=\'../images/scannotfound' + ImgSize + '.jpg\';"'
				if (CImages[vCounter].PicWidth > 0) tReturn += ' width="' + CImages[vCounter].PicWidth + '" ';
				if (CImages[vCounter].PicHeight > 0) tReturn += ' height="' + CImages[vCounter].PicHeight + '" ';
				tReturn += '>';
				tReturn += '</A>';				

			if (NbColumns == 1)
				{
					tReturn += '</TD><TD vAlign="bottom" align="left" width="500">';
					tReturn += getpopupcontent(vCounter, ImageCaptionsFields);
					var viewIcon = true;
				
				if (viewIcon == true)
				{
					if (AbleToEditAlbum == true) 
					{ 
						tReturn += BuildButton(vCounter, 'Ico_Album_'); 
					}
					tReturn += BuildButton(vCounter, 'Ico_Box_A_Thumb_');
					tReturn += BuildButton(vCounter, 'Ico_Box_B_Thumb_');
					tReturn += BuildButton(vCounter, 'Ico_Cart_Thumb_');
					if (CImages[vCounter].CanEditPic == 1 && AbleToEditPicture == true) 
					{ 
						tReturn += BuildButton(vCounter, 'Ico_Edit_Thumb_');
					}
					if (ERestr != '') 
					{ 
						tReturn += BuildButton(vCounter, 'Ico_Restr_Thumb_');
					}
				}
				}
			else
				{
				tReturn += '</TD><TD vAlign="bottom" align="left">';
				var viewIcon = true;
				
				if (viewIcon == true)
				{
					if (AbleToEditAlbum == true) 
					{ 
						tReturn += BuildButton(vCounter, 'Ico_Album_')+'<br>'; 
					}
					tReturn += BuildButton(vCounter, 'Ico_Box_A_Thumb_')+'<br>';
					tReturn += BuildButton(vCounter, 'Ico_Box_B_Thumb_')+'<br>';
					tReturn += BuildButton(vCounter, 'Ico_Cart_Thumb_')+'<br>';
					if (CImages[vCounter].CanEditPic == 1 && AbleToEditPicture == true) 
					{ 
						tReturn += BuildButton(vCounter, 'Ico_Edit_Thumb_')+'<br>';
					}
					if (ERestr != '') 
					{ 
						tReturn += BuildButton(vCounter, 'Ico_Restr_Thumb_');
					}
				}
				if (DisplayPopup == 'False' && ShowCaption == true)
				{
					tReturn += '</td>';	
					tReturn += '</tr><tr>';				
					tReturn += '<TD colspan="2" align="left" valign="top">';
					tReturn += getpopupcontent(vCounter, ImageCaptionsFields);
				}
				}
				tReturn += '</TD></TR>';
				tReturn += '</TABLE>';
				//alert(tReturn);
				return(tReturn);
			}



	function popupFix(ImageYear,Source,Caption,PrintableTitle,Restrictions,Embargo,HighRes,PictureRestricted)
	{
		var content = '<table ';
		if (NbColumns == 1)
			{ 
			content += 'width="500" '; }
		else
			{
			if (ImgSize == 'TRV')
				{ content += 'width="155" '; }
			else
				{ content += 'width="260" '; }
			}
		content += 'border="0" cellspacing="3"><tr><td class="ThumbMacCaption" align="left" valign="top"><b>' + Source + '</b></td></tr>';
		
	if (Caption != '' || PrintableTitle != '')
		{ 
		
		content += "<tr><td class='ThumbMacCaption' align='left' valign='top'>" + PrintableTitle;
		
		if (PrintableTitle != '' && Caption != '') content += "<br>";
			
		textCaption = new String(Caption);
		if (textCaption.length > 120) content += textCaption.slice(0,120) + " ..."; else content += textCaption;
			
		content += "</td></tr>";
		}
		
		if (PictureRestricted == 1)
			{ 
			content += "<tr><td class='ThumbMacWarning' align='left' valign='top'>";
			content += "Restriction";
			content += "</td></tr>";
			}
		
		if (HighRes == 1)
			{ 
			content += "<tr><td class='ThumbMacWarning' align='left' valign='top'>Haute Définition</td></tr>"; 
			}
		content += "</table>";
		return content;
		}
	
/*******************************************************************************/
/*				POPUP MANAGEMENT       			       */
/*******************************************************************************/

function ProcessImageOut()
{killpopup();}
	
function ProcessImageIn(tPosition)
{ 
	//popup(CImages[tPosition].DocYear ,CImages[tPosition].SourceName ,CImages[tPosition].CaptionLong, CImages[tPosition].PrintableTitle,CImages[tPosition].Restrictions,CImages[tPosition].Embargo,CImages[tPosition].HighRes,CImages[tPosition].PictureRestricted);
	popup(tPosition);
}

function popup(tpos)
{
	var content = getpopupcontent(tpos, ImagePopupFields);

	if(old)
	{alert(msg); return;} 
	else
	{
		yyy=Yoffset;
		if(nav) 
		{ skn.document.write(content); skn.document.close(); skn.visibility="visible"; skn.width="200px"; }
		if(iex) 
		{ document.all("dek").innerHTML=content; skn.visibility="visible"; }
	}
}

function getpopupcontent(tpos,ttextmodel)
{
	var content = ttextmodel;
	content = fillmetatext(content, "CaptionLong", CImages[tpos].CaptionLong);
	content = fillmetatext(content, "Author", CImages[tpos].CreatorName);
	content = fillmetatext(content, "Source", CImages[tpos].SourceName);
	content = fillmetatext(content, "DocYear", CImages[tpos].DocYear);
	content = fillmetatext(content, "PrintableTitle", CImages[tpos].PrintableTitle);
	content = fillmetatext(content, "Restrictions", CImages[tpos].Restrictions);
	content = fillmetatext(content, "Embargo", CImages[tpos].Embargo);
	content = fillmetatext(content, "HighResolution", CImages[tpos].HighRes);
	content = fillmetatext(content, "PictureRestricted", CImages[tpos].PictureRestricted);
	content = fillmetatext(content, "DO_Custom_4", CImages[tpos].DO_Custom_4);
	content = fillmetatext(content, "DO_Custom_1", CImages[tpos].DO_Custom_1);
	content = fillmetatext(content, "MotherCreator_Name", CImages[tpos].MotherCreator_Name);
	content = fillmetatext(content, "MotherDO_Custom_3", CImages[tpos].MotherDO_Custom_3);
	content = fillmetatext(content, "MotherDO_Custom_6", CImages[tpos].MotherDO_Custom_6);
	content = fillmetatext(content, "MotherCopyright", CImages[tpos].MotherCopyright);
	content = fillmetatext(content, "MotherCreator_Name", CImages[tpos].MotherCreator_Name);
	content = fillmetatext(content, "MotherCompany", CImages[tpos].MotherCompany);
	content = fillmetatext(content, "Author_Rights", CImages[tpos].Author_Rights);
	return content;
}

function fillmetatext(value_origin, value_tochange, value_new)
{
	var value_result = '';
	var tempstring = '';

	if (value_origin.indexOf('<' + value_tochange + '>') == -1 || value_origin.indexOf('</' + value_tochange + '>') == -1)
	{
		value_result = value_origin;
		value_result = value_result.replace('##' + value_tochange + '##', value_new)
		//return value_origin;
	}
	else
	{
		if (value_new.length == 0)
		{
			tempstring = new String('</' + value_tochange + '>');
			value_result = value_origin.substring(0, value_origin.indexOf('<' + value_tochange + '>')) + value_origin.substring(value_origin.indexOf('</' + value_tochange + '>') + tempstring.length, value_origin.length);
		}
		else
		{
			value_result = value_origin;
			value_result = value_result.replace('<' + value_tochange + '>', '');
			value_result = value_result.replace('</' + value_tochange + '>', '');
			value_result = value_result.replace('##' + value_tochange + '##', value_new)
		}
		//return eval(value_result);
	}
	while (value_result.search('%20') != -1)
	{
		value_result = value_result.replace('%20', ' ');
	}
	return value_result;
}


function get_mouse(e)
{
var x = (nav)?e.pageX:event.x + document.body.scrollLeft; skn.left = x + Xoffset;
var y = (nav)?e.pageY:event.y + document.body.scrollTop; skn.top = y + yyy;

}

function killpopup()
{
	if(!old){ yyy=-1000; skn.visibility = "hidden"; }
}

	function PrintWindow(tUrl,tTitre)
	{
		if (tTitre == 'Print' && navigator.userAgent.indexOf('Mac') != -1) 
		{ var win = window.open(tUrl,tTitre, "scrollbars=1,width=780,height=610,resizable=1,menubar=1,toolbar=1"); }
		else
		{ var win = window.open(tUrl,tTitre, "scrollbars=1,width=780,height=610,resizable=1"); }
	}

	function EmailWindow(tUrl,tTitre)
	{
		if (tTitre == 'Print' && navigator.userAgent.indexOf('Mac') != -1) 
		{ var win = window.open(tUrl,tTitre, "scrollbars=1,width=537,height=700,resizable=1,menubar=1,toolbar=1"); }
		else
		{ var win = window.open(tUrl,tTitre, "scrollbars=1,width=537,height=600,resizable=1"); }
	}
	



/*******************************************************************************/
/*				POPUP MANAGEMENT       			       */
/*******************************************************************************/


// ajout de thibaut pour la sauvegarde du search
function ShowAction(tUrl)
{
var win = window.open(tUrl,"PrintWindow", "scrollbars=0,width=500,height=200,resizable=0");
}


