if (!window.UntitledProject1)
	window.UntitledProject1 = {};

UntitledProject1.Page = function() 
{
}

UntitledProject1.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
	}
}

//To do progressive loading you need to have the
//images in script.  These have to be images that
//exist on the same server and must use *relative*
//urls (image/foo.png or /images/foo.png are okay)
var imageArray = new Array();
imageArray[0] = "/media/gay_080313.jpg";
imageArray[1] = "/media/Howard_080313.jpg";
imageArray[2] = "/media/mcgrady_080313.jpg";
imageArray[3] = "/media/Okur_080313.jpg";
imageArray[4] = "/media/Bibby_080313.jpg";
imageArray[5] = "/media/carter_080313.jpg";
imageArray[6] = "/media/Childress_080313.jpg";
imageArray[7] = "/media/george_080313.jpg";
imageArray[8] = "/media/Horford_080313.jpg";
imageArray[9] = "/media/perkins_080313.jpg";
imageArray[10] = "/media/Snyder_080313.jpg";
var totalNumberOfPhotos = imageArray.length; //this needs to be either the array length or changed based on the # of photos in the gallery

var photos = function()
{
    var index = 1;
    var thumbIndex = 1; //keep track of the top thumbnail
    var last = totalNumberOfPhotos;
    
    this.current = function()
    {
        if(index > totalNumberOfPhotos)
        {
            index = 1;
        }
        return index;
    }

    this.topThumb = function()
    {
        return thumbIndex;
    }
    
    this.incrementThumb = function()
    {
        if(thumbIndex < (totalNumberOfPhotos + 1))
        {
            thumbIndex = (thumbIndex + 1);
            return thumbIndex;
        }
        else
        {
            return thumbIndex;
        }
    }
    
    this.decrementThumb = function()
    {
        if(thumbIndex > 1)
        {
            thumbIndex = (thumbIndex - 1);
            return thumbIndex;       
        }
        else
        {
            return thumbIndex;
        }
    }
    
    this.setPhoto = function(num)
    {
        last = index;
        index = num;
    }
    
    this.last = function()
    {
        return last;
    }   
    
    this.increment = function()
    {
        last = index;
        if(index > totalNumberOfPhotos)
        {
            index = 1;
        }
        else
        {
            index = (index + 1);
        }
    }
    
    this.decrement = function()
    {
        last = index;
        if(index < 2) //1 or less
        {
            index = totalNumberOfPhotos;
        }
        else
        {
            index = (index - 1);
        }
    }
}

var myPhotos = new photos();
var slideshow = true;
var waitingAnimation = null;
var waitingPhoto = null;
var inital = true;

function myLoad(sender, eventArgs)
{
    var token = sender.addEventListener("keyDown", onKeyDown);
    SilverlightControl1 = sender.getHost();
    SilverlightControl1.content.onFullScreenChange = onFullScreenChanged;
    updateLayout(SilverlightControl1.content.actualWidth, SilverlightControl1.content.actualHeight);
    
    SilverlightControl1.content.findName("min_ani_timeline").Begin();

    for(x=1;(x<totalNumberOfPhotos+1);x++)
    {
    var thumb = SilverlightControl1.content.findName("thumb" + x);
    thumb.AddEventListener("MouseEnter", onMouseEnter);
    thumb.AddEventListener("MouseLeave", onMouseLeave);
    thumb.AddEventListener("MouseLeftButtonDown", changePhoto);
    }
    
    var minimizedOverlay = SilverlightControl1.content.findName("Minimized_Overlay");
    minimizedOverlay.AddEventListener("MouseLeftButtonDown", globalMouseDown);

    var pbl = SilverlightControl1.content.findName("PlayButtonLayer");
    pbl.AddEventListener("MouseLeftButtonDown", globalMouseDown);
    
    var thumbSidecar = SilverlightControl1.content.findName("Thumbnail_Sidecar");
    thumbSidecar.AddEventListener("MouseLeftButtonDown", sideCarControl);
    var hiddenThumbSidecar = SilverlightControl1.content.findName("Thumbnail_Sidecar_Hidden");
    hiddenThumbSidecar.AddEventListener("MouseLeftButtonDown", sideCarControl);

    var previous = SilverlightControl1.content.findName("Previous");
    previous.AddEventListener("MouseLeftButtonDown", changePhoto);
    var next = SilverlightControl1.content.findName("Next");
    next.AddEventListener("MouseLeftButtonDown", changePhoto);
    
    var infoHide = SilverlightControl1.content.findName("Info_Hidden");
    infoHide.AddEventListener("MouseLeftButtonDown", infoWindowControl);
    var infoWindow = SilverlightControl1.content.findName("InfoWindow");
    infoWindow.AddEventListener("MouseLeftButtonDown", infoWindowControl);
    
    var arrowUp = SilverlightControl1.content.findName("Arrow_Up");
    arrowUp.AddEventListener("MouseLeftButtonDown", arrowControl);

    var arrowDown = SilverlightControl1.content.findName("Arrow_Down");
    arrowDown.AddEventListener("MouseLeftButtonDown", arrowControl);
    
    var titleText = SilverlightControl1.content.findName("Title");
    titleText.AddEventListener("MouseLeftButtonDown", slideShowControl);
    
    
    
    var closeButton = SilverlightControl1.content.findName("Close_X");
    closeButton.AddEventListener("MouseLeftButtonDown", closeControl);
    var closeText = SilverlightControl1.content.findName("CLOSE_Text");
    closeText.AddEventListener("MouseLeftButtonDown", closeControl);
    
    SilverlightControl1.content.findName("thumb1_border").stroke = "#FFED174C"

    progressiveDownload(0);
}

function slideShowControl()
{
       if(!slideshow)
        {
            sideCarControl();
            slideshow = true;
            startSlideshow();
        }
        else
        {
            slideshow = false;
        }
}

function progressiveDownload(x)
{
        var downloader = SilverlightControl1.CreateObject("downloader");
        downloader.AddEventListener("Completed", onDownloadCompleted);
        downloader.Open("GET", imageArray[x]);
        downloader.Send();
}

function findImageInArray(array, name)
{
    for(x=0;x<array.length;x++)
    {
        if(name == array[x])
        return (x + 1); //we're 1 based in the application but to use array.length for the images we're 0 based in JS
    }
}


function onDownloadCompleted(sender)
{
    if(inital == true)
    {
        SilverlightControl1.content.findName("Transition").visibility = "Collapsed";
        inital = false;
    }
    var photo = sender.uri;
    var imageNumber = findImageInArray(imageArray,photo);
    var image = SilverlightControl1.content.findName("Large_Image" + imageNumber + "_1");
    if(image.source == "")
    {
        image.source = photo;
        if(imageNumber == waitingPhoto)
        {
            triggerAnimation();
        }
    }
    else
    {
        alert("abort: already has a source! " + image.name);
    }
    if(imageNumber<(totalNumberOfPhotos))//currently we load one after another, could do a mass downloading or load all in .zip file
    {
      progressiveDownload(imageNumber);
      //fake network traffic
//    setTimeout('progressiveDownload(' + imageNumber + ')',5000);
    }
    
}

function pragIncrease()
{
    foo = new Object()
    foo.name = "Arrow_Up"
    arrowControl(foo);

}

function pragDecrease() //H key
{
    foo = new Object()
    foo.name = "Arrow_Down"
    arrowControl(foo);
//    SilverlightControl1.content.findName("thumb" + myPhotos.last() + "_gradient").Visibility = "Collapsed";
//    SilverlightControl1.content.findName("thumb" + myPhotos.current() + "_gradient").Visibility = "Visible";
}


function opacityFadeComplete(sender)
{

    if(slideshow)
    {
        SilverlightControl1.content.findName("Pause").Begin();
    }
    else
    {

    }
}

function pauseComplete()
{
    if(slideshow)
    {
        startSlideshow();
    }
    else
    {
    
    }
}


function changePhoto(sender)
{
    if(slideshow == true)
    {
        slideshow = false;
    }
    var prevPhotoAni = SilverlightControl1.content.findName("opacityFade" + myPhotos.last());
    var prevPhoto = SilverlightControl1.content.findName("Large_Image" + myPhotos.last());
    prevPhotoAni.Stop();
    prevPhoto["Canvas.zIndex"] = 0;
    prevPhoto.opacity = 1;
    
    var name;
    var last = myPhotos.current();
    
    if(sender.name == "Previous")
    {
        myPhotos.decrement();
        SilverlightControl1.content.findName("Next").visibility = "Visible";
        SilverlightControl1.content.findName("thumb" + myPhotos.last() + "_gradient").Visibility = "Collapsed";
        SilverlightControl1.content.findName("thumb" + myPhotos.current() + "_gradient").Visibility = "Visible";
        if(myPhotos.current() == 1)
        {
            SilverlightControl1.content.findName("Previous_Disabled").visibility = "Visible";
            SilverlightControl1.content.findName("Previous").visibility = "Collapsed";
        }
        pragIncrease(); //arrow
    }
    else if(sender.name == "Next")
    {
        myPhotos.increment();
        SilverlightControl1.content.findName("Previous").visibility = "Visible";
        SilverlightControl1.content.findName("Previous_Disabled").visibility = "Collapsed";

        if(myPhotos.current() == totalNumberOfPhotos)
        {
            SilverlightControl1.content.findName("Next_Disabled").visibility = "Visible";
            SilverlightControl1.content.findName("Next").visibility = "Collapsed";
        }
        pragDecrease(); //arrow
    }
    else
    {   
        var thumbnail = sender.name.substr(5);
        thumbnail = parseInt(thumbnail);
        myPhotos.setPhoto(thumbnail);
        if(myPhotos.current() == 1)
        {
            SilverlightControl1.content.findName("Previous").visibility = "Collapsed";
            SilverlightControl1.content.findName("Previous_Disabled").visibility = "Visible";
        }
        else if(myPhotos.current() == totalNumberOfPhotos)
        {
            SilverlightControl1.content.findName("Next").visibility = "Collapsed";
            SilverlightControl1.content.findName("Next_Disabled").visibility = "Visible";

        }
        else
        {
            SilverlightControl1.content.findName("Next_Disabled").visibility = "Collapsed";
            SilverlightControl1.content.findName("Previous_Disabled").visibility = "Collapsed";
            SilverlightControl1.content.findName("Next").visibility = "Visible";
            SilverlightControl1.content.findName("Previous").visibility = "Visible";
        }
    }

    var borderName;
    var border;

    var currentPhotoName = "Large_Image" + last;
    var currentPhoto = SilverlightControl1.content.findName(currentPhotoName);

    var nextPhotoName = "Large_Image" + myPhotos.current();
    var nextPhoto = SilverlightControl1.content.findName(nextPhotoName);
    

    var currentThumbName = "thumb" + myPhotos.current() + "_border";
    var currentThumb = SilverlightControl1.content.findName(currentThumbName);
    
    var currentCaptionName = "Info_Text_Description" + myPhotos.current();
    var currentCaption = SilverlightControl1.content.findName(currentCaptionName);
    
    clearThumbBorder();
    clearCaptions();
    clearThumbAni();

    currentThumb.stroke = "#FFED174C"
    currentCaption.visibility = "Visible";

    currentPhoto["Canvas.zIndex"] = 3;
    nextPhoto["Canvas.zIndex"] = 2;
    
    var cZ = currentPhoto["Canvas.zIndex"];
    var nZ = nextPhoto["Canvas.zIndex"];

//    alert("current: " + currentPhotoName + " " + " currentZindex " + cZ + " nextphotoname: " + nextPhotoName + " next photoz " + nZ);

    //these both get put into a globals because we're going to call tiggerAnimation 
    //on each progressive image load to check if we can transition

    waitingPhoto = myPhotos.current();
    waitingAnimation = last;
    triggerAnimation();
    
}

function triggerAnimation()
{

    if((waitingAnimation != null) && (waitingPhoto != waitingAnimation))
    {
        var last = waitingAnimation;
        var nextPhotoSourceName = "Large_Image" + waitingPhoto + "_1";
        var nextPhotoSource = SilverlightControl1.content.findName(nextPhotoSourceName);
        if(nextPhotoSource.source == "")
        {
            SilverlightControl1.content.findName("Transition").visibility = "Visible";
        }
        else
        {
            SilverlightControl1.content.findName("Transition").visibility = "Collapsed";
            var opacityAnimation = SilverlightControl1.content.findName("opacityFade" + last);
            opacityAnimation.Begin();
            waitingAnimation = null;
        }
    }
}

function clearThumbBorder()
{
    for(x=1;x<=totalNumberOfPhotos;x++)
    {
        borderName = "thumb" + x + "_border";
        border = SilverlightControl1.content.findName(borderName);
        border.stroke = "#FFFFFFFF";
    
    }
}

function clearThumbAni()
{
    for(x=1;x<=totalNumberOfPhotos;x++)
    {
        aniName = "thumb" + x + "_ani";
        ani = SilverlightControl1.content.findName(aniName);
        ani.Stop();
    }
}


function clearCaptions()
{
    for(x=1;x<=totalNumberOfPhotos;x++)
    {
        captionName = "Info_Text_Description" + x;
        caption = SilverlightControl1.content.findName(captionName);
        caption.visibility = "Collapsed";
    
    }
}


function startSlideshow()
{
    var currentThumbAnimation;
    var currentThumb;
    var lastPhoto;
    var currentPhoto;
    var nextPhoto;
    var lastPhotoName;
    var currentPhotoName;
    var nextPhotoName;
    var opacityAnimation;
    var currentCaption;


    lastPhotoName = "Large_Image" + myPhotos.last();
    currentPhotoName = "Large_Image" + myPhotos.current();

    opacityAnimation = SilverlightControl1.content.findName("opacityFade" + myPhotos.current());
    
    myPhotos.increment();
    currentThumb = SilverlightControl1.content.findName("thumb" + myPhotos.current() + "_border");
    currentThumbAnimation = SilverlightControl1.content.findName("thumb" + myPhotos.current() + "_ani");
    currentCaption = SilverlightControl1.content.findName("Info_Text_Description" + myPhotos.current());
    clearCaptions();
    clearThumbBorder();
    clearThumbAni();
    currentThumb.stroke = "#FFED174C";
    currentCaption.visibility = "Visible";
    currentThumbAnimation.Begin();
    
    nextPhotoName = "Large_Image" + myPhotos.current();

    lastPhoto = SilverlightControl1.content.findName(lastPhotoName);
    currentPhoto = SilverlightControl1.content.findName(currentPhotoName);
    nextPhoto = SilverlightControl1.content.findName(nextPhotoName);

    lastPhoto["Canvas.zIndex"] = 0;
    lastPhoto.opacity = 1;
    currentPhoto["Canvas.zIndex"] = 3;
    nextPhoto["Canvas.zIndex"] = 2;
    
    opacityAnimation.Begin();        
    
}

function closeControl(sender)
{
    sender.GetHost().Content.FullScreen = false;
}



function arrowControl(sender)
{
    var thumbs = SilverlightControl1.content.findName("Thumbs");
    var thumbsPosition = thumbs["Canvas.Top"];
    var upAni = SilverlightControl1.content.findName("upAnimation");
    var downAni = SilverlightControl1.content.findName("downAnimation");

    if (sender.name == "Arrow_Down" && thumbsPosition > -530) //(-1*((totalNumberOfPhotos - 1) * 106))
    {
        thumbsPosition = thumbsPosition - 106;
        upAni.To = thumbsPosition;
        SilverlightControl1.content.findName("slideUp").Begin();
        myPhotos.incrementThumb();
        gradientControl();
    }
    else if(sender.name == "Arrow_Up" && thumbsPosition < 0)
    {
        thumbsPosition = thumbsPosition + 106;
        upAni.To = thumbsPosition;
        SilverlightControl1.content.findName("slideUp").Begin();
        myPhotos.decrementThumb();
        gradientControl();
    }
}


function infoWindowControl()
{
    
    var infoHideWindowAnimation = SilverlightControl1.content.findName("slideInfoWindowHideAnimation");
    var infoShowWindowAnimation = SilverlightControl1.content.findName("slideInfoWindowShowAnimation");
    var infoShow = SilverlightControl1.content.findName("InfoWindow");    
    var infoHide = SilverlightControl1.content.findName("Info_Hidden");
        
    if(infoShow.visibility == "Visible")
    {
        infoHideWindowAnimation.Begin();
    }
    else
    {
        infoShow.visibility = "Visible"
        infoShowWindowAnimation.Begin();
    }
}

function onslideInfoWindowHideAnimationComplete(sender)
{
    //trigger
    var infoHide = SilverlightControl1.content.findName("Info_Hidden");
    var infoShow = SilverlightControl1.content.findName("InfoWindow");
    var infoAnimation = SilverlightControl1.content.findName("infoOpacityFade");
    

    if(infoShow.visibility == "Visible")
    {
        infoShow.visibility = "Collapsed";
        infoHide.opacity = 0;
        infoHide.visibility = "Visible";
        infoAnimation.Begin();
        
    }
    else 
    {
        infoShow.visibility = "Visible";
        infoHide.visibility = "Collapsed";
    }
}


function sideCarControl()
{
    var thumbnails = SilverlightControl1.content.findName("Thumbnails");
    var sidecar = SilverlightControl1.content.findName("Thumbnail_Sidecar_Hidden");
    var animation;
    
    if(thumbnails.visibility == "Visible")
    {
    animation = SilverlightControl1.content.findName("slideThumbsAnimationHide");
    animation.Begin();
    }
    else
    {
    sidecar.visibility = "Collapsed";
    thumbnails.visibility = "Visible";
    animation = SilverlightControl1.content.findName("slideThumbsAnimationShow");
    animation.Begin();
    }
}

function onSlideThumbsAnimationHideComplete()
{
    var thumbnails = SilverlightControl1.content.findName("Thumbnails");
    if(thumbnails.visibility == "Visible")
    {
        thumbnails.visibility = "Collapsed";
        SilverlightControl1.content.findName("Thumbnail_Sidecar_Hidden").visibility = "Visible";
    }
}

function onMouseEnter(sender)
{
    var targetAni = sender.name + "_ani";
    SilverlightControl1.content.findName(targetAni).Begin();
    
}

function onMouseLeave(sender)
{
    var targetAni = sender.name + "_ani";
    SilverlightControl1.content.findName(targetAni).Stop();
}

function collapseGradients()
{
    for(x=1;(x<totalNumberOfPhotos+1);x++)
    {
        var gradient = SilverlightControl1.content.findName("thumb" + x + "_gradient");
        gradient.visibility = "Collapsed";
    
    }
}

function gradientControl(direction)
{
    collapseGradients();
    var top = myPhotos.topThumb();
    //6th photo

    if((top + 6) > (totalNumberOfPhotos))
    {
    }
    else
    {
        var bottom = top + 5;
        var bottomGradient = SilverlightControl1.content.findName("thumb" + bottom + "_gradient");
        var bottomGradientAngle = SilverlightControl1.content.findName("thumb" + bottom + "_gradient_angle");
        bottomGradientAngle.Angle = 0;
        bottomGradient.Visibility = "Visible";    
    
    }

    if(top < totalNumberOfPhotos)
    {
        var topGradient = SilverlightControl1.content.findName("thumb" + top + "_gradient");
        var topGradientAngle = SilverlightControl1.content.findName("thumb" + top + "_gradient_angle");
        topGradientAngle.Angle = -180;
        topGradient.Visibility = "Visible";
    }


    
}


function onFullScreenChanged(sender, eventArgs)
{
    if(slideshow == true)
    {
        slideshow = false;
    }
    var minimizedView = SilverlightControl1.content.findName("Min_Screen");
    var fullScreen = SilverlightControl1.content.findName("Full_Screen");

    if(minimizedView.visibility == "Visible")
    {
        minimizedView.visibility = "Collapsed";
        fullScreen.visibility = "Visible";
        updateLayout(SilverlightControl1.content.actualWidth, SilverlightControl1.content.actualHeight);
        slideShowControl();
    }
    else
    {
        minimizedView.visibility = "Visible";
        fullScreen.visibility = "Collapsed"

    }   

    // Do layout resizing of the app whenever the FullScreen property changes.
    
}

function updateLayout(width, height)
{
    // Perform layout tasks based on width and height.
    if(width != 0 && height != 0)
    {
    for(x=1;(x<totalNumberOfPhotos+1);x++)
    {
        scaleLayout("Large_Image" + x, width, height);
        scaleLayout("Large_Image" + x + "_1", width, height);
        scaleLayout("background_" + x, width, height);
    }    
        scaleLayout("Page", width, height);
        scaleLayout("Background", width, height);
        scaleLayout("Header_Background", width, 42);
        scaleLayout("Transition", width, height);
        shiftLayout("Close_X", 20, width-38);
        shiftLayout("CLOSE_Text", 20, width-86);
        shiftLayout("InfoWindow", height-114, width-596);
        shiftLayout("Prev_Next", height-34, width-143);
        shiftLayout("Info_Hidden", height-97, width-26);
    }
}

function scaleLayout(item, width, height)
{
    var target = SilverlightControl1.content.findName(item);
    target.width = width;
    target.height = height;
    
}

function shiftLayout(item, top, left)
{
    var target = SilverlightControl1.content.findName(item);
    target["Canvas.Top"] = top;
    target["Canvas.Left"] = left;

}

function onKeyDown(sender, keyEventArgs)
{
//enter full screen mode with "F" key
    if (keyEventArgs.Key == 35)
    sender.GetHost().Content.FullScreen = true;
    else if(keyEventArgs.Key == 48) //S Key
    {
        if(!slideshow)
        {
            slideshow = true;
            startSlideshow();
        }
        else
        {
            slideshow = false;
        }
    }
 }

function globalMouseDown(sender)
{
    sender.GetHost().Content.FullScreen = true;
}