﻿atalaInitClientScript("OnPageInit();");

// #REGION THUMBNAILS
// This area allows us to easily scroll through the thumbnails
var ScrollInterval;
var xPos = 0;
var _thumbWidth = 138;
function scrollright() {

    clearInterval(ScrollInterval);
    ScrollInterval = setInterval("moveright()", 25);
}

function moveright() {

    if(xPos > -_thumbWidth * (WebThumbnailViewer1.getCount()-4)) {
        
        xPos = xPos - 50;
        WebThumbnailViewer1.setScrollPosition(new atalaPoint(xPos,0));
    }
}

function scrollleft() {

    clearInterval(ScrollInterval);
    ScrollInterval = setInterval("moveleft()", 25);
}

function moveleft() {

    if(xPos < 0) { 
        
        xPos = xPos + 50;
        WebThumbnailViewer1.setScrollPosition(new atalaPoint(xPos,0));
    }
}

// This makes sure that we are seeing exactly 4 full thumbnails when the user stops scrolling
function snap() {

    clearInterval(ScrollInterval);
    var numThumbs = Math.round(xPos / _thumbWidth);
    
    xPos = numThumbs * _thumbWidth;
    WebThumbnailViewer1.setScrollPosition(new atalaPoint(xPos,0));
}

function EnsureVisible() {

    var selected = WebThumbnailViewer1.getSelectedIndex();
    xPos = -selected * 138;
    WebThumbnailViewer1.setScrollPosition(new atalaPoint(xPos, 0));
}

// Increments and decrements the zoom level of the image viewer
function Zoom(inOut){

    WebAnnotationViewer1.setAutoZoom(AutoZoomMode.None);
    var z = WebAnnotationViewer1.getZoom();
    var zp = WebAnnotationViewer1.getZoomInOutPercentage();
	
    z += inOut * z * zp / 100;
    WebAnnotationViewer1.setZoom(z);
}

// if the user moves the mouse, we need to show the controls and thumbs
var HideAllTimeout;
function showAll() {

    clearTimeout(HideAllTimeout);
    HideAllTimeout = setTimeout("hideAll();", "2000");
    
    WebThumbnailViewer1.setVisibility("visible");
    document.getElementById("thumbs").style.visibility = "visible";
    document.getElementById("controls").style.visibility = "visible";
}

// if they keep their mouse still for 2 whole seconds, we hide the controls
function hideAll() {

    clearTimeout(HideAllTimeout);
    
    WebThumbnailViewer1.setVisibility("hidden");
    document.getElementById("thumbs").style.visibility = "hidden";
    document.getElementById("controls").style.visibility = "hidden";
}

function Download(filelocation) {

    window.location = "download.aspx?url=" + escape(filelocation);
}

function OnPageInit() {

    // set up the image viewer
    WebAnnotationViewer1.MouseDown = function() { NoThumbs = true; };
    
    // this tracks when the mouse moves
    WebAnnotationViewer1.MouseMove = showAll;
    
    // if the user is over either of the controls, we need to make sure that 
    // we don't get hide them until after they move their mouse away
    document.getElementById("thumbs").onmouseover = function() { clearTimeout(HideAllTimeout) };
    document.getElementById("controls").onmouseover = function() { clearTimeout(HideAllTimeout) };
    
    // this is a trick to grab a handle on the web image viewer's iframe. 
    // we want to watch it's mousemove since webimageviewer's only fires when you're over the image.
    // if the user zooms out too far, you wouldn't be able to get the controls back without this piece.
    var iframes = document.getElementById("WebAnnotationViewer1").getElementsByTagName("iframe");
    iframe = iframes[0].contentWindow || iframes[0].contentDocument;
    if(iframe.document)
        iframe = iframe.document;
    iframe.onmousemove = showAll;
    
    // first thing we should do is show users that controls do exist on the page. they'll 
    // automatically hide themselves if the user does not move their mouse.
    showAll();
    
    EnsureVisible();
    
    FixPngs();
}

//#region PNG Transparency

// fixes all images on the page with the .png extension if needed
// (for IE 5.5 and 6.0 PNG transparency problem)
function FixPngs(){
	if (NeedsPngFix()){
		for (i = 0; i < document.images.length; i++){
			var s = document.images[i].src;
			if (s.indexOf('.png') > 0)
				FixPng(s, document.images[i]);
		}
	}
}

// replaces the original img object's src attribute with a spacer.gif
// and adds the alpha image loader filter with the original img.src
// (for IE 5.5 and 6.0 PNG transparency problem)
function FixPng(u, d){
	if (NeedsPngFix()){
		d.src = 'images/spacer.gif';
		d.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + u + "', sizingMethod='scale')";
	}
}

// checks if the browser is IE, and determines if it's a version that needs
// to fix the alpha transparency of PNG images
function NeedsPngFix(){
	return (navigator.userAgent.indexOf('MSIE') > 0 && navigator.userAgent.indexOf('MSIE 7.0') < 0)
}

//#endregion
