var PG_SelectedThumbIndex = 0;
var PG_CountOfThumbIndex = 36;

function PG_GetThumbIndex(ID) {
    var index = 0;
    
    try {
        index = parseInt(ID.split("_")[1], 10);
    } catch (error) {
        // ignore it
    }

    return index;
}

function PG_SelectImage(ID) {
    var newIndex = PG_GetThumbIndex(ID);
    var oldThumb = document.getElementById("th_" + ZeroPadInteger(PG_SelectedThumbIndex));
    var newThumb = document.getElementById("th_" + ZeroPadInteger(newIndex));

    oldThumb.src = "/images/spacer.gif";
    newThumb.src = PG_RedBorderImage;

    document.getElementById("pg_largeimage").src = newThumb.name;
    document.getElementById("pg_largeimage").title = newThumb.title;
    
    var caption = document.getElementById("pg_largeimage_caption")
    if (caption)
		caption.innerHTML = newThumb.title;
    
    PG_SelectedThumbIndex = newIndex;

    return false;
}

function PG_NextIndex(index) {
    if (index >= (PG_CountOfThumbIndex - 1))
        index = 0;
    else
        index++;

    return index;
}

function PG_PreviousIndex(index) {
    if (index <= 0)
        index = PG_CountOfThumbIndex - 1;
    else
        index--;

    return index;
}

function PG_NextImage() {
    return PG_SelectImage("th_" + PG_NextIndex(PG_SelectedThumbIndex));
}

function PG_PreviousImage() {
    return PG_SelectImage("th_" + PG_PreviousIndex(PG_SelectedThumbIndex));
}

// begin js for use in gallery popups
var message="The entire content on this site is protected by copyright, trademark rights and database rights.\nNo reproduction without the consent of the relevant owner.";

function clickIE4(){
	if (event.button==2){
		return false;
	}
}

function clickNS4(e){
	if (document.layers||document.getElementById&&!document.all){
		if (e.which==2||e.which==3){
			alert(message);
		}
	}
}

function openImageWindow(imageSrc,gallery_mode,source)
{
	sContent="";
	sContent+="<html><head><title>The Official Formula 1 Website</title>\n";
	sContent+="<style>.popupCopyright {color: #9c9c9c; font-family: Arial, Helvetica, sans-serif; font-size: 9px; font-weight: normal; text-decoration: none}</style>\n";
	sContent+="<script>function specialInit(){window.resizeTo(document.images[0].width + 22,document.images[0].height + 115);}</script></head>\n";
	sContent+="<body topmargin='0' leftmargin='0' marginheight='0' marginwidth='0' onLoad='specialInit();'>\n";
	sContent+="<script>\n";
	sContent+="document.write('<div align=\\'center\\'>');\n";
	sContent+="document.write('<table cellpadding=\\'0\\' cellspacing=\\'0\\' border=\\'0\\'>');\n";
	sContent+="document.write('<tr height=\\'5\\'><td colspan=\\'3\\'></td></tr>');\n";
	sContent+="document.write('<tr><td colspan=\\'3\\'><img name=\\'popup\\' src=\\'"+imageSrc+"\\' galleryimg=\\'no\\'></td></tr>');\n";
	sContent+="document.write('<tr height=\\'2\\'><td colspan=\\'3\\'></td></tr>');\n";
	
	if (source == "GALLERY") {
		sContent+="document.write('<tr height=\\'33\\'><td bgcolor=\\'#636363\\'><img src=\\'/images/popup_images/bar_f1logo.gif\\' width=\\'65\\' height=\\'33\\'><img src=\\'/images/popup_images/bar_title_gal.gif\\' width=\\'61\\' height=\\'33\\'></td>');\n";
	} else {
		sContent+="document.write('<tr height=\\'33\\'><td bgcolor=\\'#636363\\'><img src=\\'/images/popup_images/bar_f1logo.gif\\' width=\\'65\\' height=\\'33\\'><img src=\\'/images/popup_images/bar_title_news.gif\\' width=\\'61\\' height=\\'33\\'></td>');\n";
	}
	
	sContent+="document.write('<td bgcolor=\\'#636363\\'><img id=\\'Formula1Referer\\' src=\\'http://images.formula1.com/tracker/" + imageSrc.substring(imageSrc.indexOf('/',7) + 1,imageSrc.length) + "?referer=(none)\\' width=\\'1\\' height=\\'1\\'></td><td bgcolor=\\'#636363\\' align=\\'right\\'></td></tr>');\n";
	sContent+="document.write('<tr height=\\'3\\'><td colspan=\\'3\\'></td></tr>');\n";
	if (gallery_mode=="POSTCARDS") {
		sContent+="document.write('<tr><td colspan=\\'3\\' align=\\'right\\' class=\\'popupCopyright\\'>&copy; No reproduction without permission.</td></tr>');\n";
	}
	else {
		sContent+="document.write('<tr><td colspan=\\'3\\' align=\\'right\\' class=\\'popupCopyright\\'>&copy; Formula One Administration Ltd. No reproduction without permission.</td></tr>');\n";
	}
	sContent+="document.write('</table>');\n";
	sContent+="document.write('</div>');\n";
	sContent+="</script></body></html>\n";

	var _parms = 'directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=yes';
	var _windowName = "PicViewer";
  
	winPopup=window.open("",_windowName,_parms);
	winPopup.document.open();
	winPopup.document.write(sContent);
	
	if (winPopup.document.layers){
		winPopup.document.captureEvents(Event.MOUSEDOWN);
		winPopup.document.onmousedown=clickNS4;
	}
	else if (winPopup.document.all&&!winPopup.document.getElementById){
		winPopup.document.onmousedown=clickIE4;
	}
	winPopup.document.oncontextmenu=new Function("return false");
	
	winPopup.document.close();
	winPopup.focus();
}