﻿if (!window.iconsHero) window.iconsHero = {};

iconsHero.Base = function() {
    this.control = null;
    this.rootCanvas = null;
    this.root = null;
    this.frontIcon = 1;
    this.autoRotate = 8000;
}

iconsHero.Base.prototype = {
    dispose : function() { this.root = null; },
    initialize : function(control, userContext, rootElement) {
		this.control = control;
		this.data = null;
		this.root = this.control.content;
		this.rootCanvas = this.root.findName("rootCanvas");
		
		this.rootCanvas.findName("loadRun").addEventListener("Completed", Silverlight.createDelegate(this, this.getAssets));       

		this.rootCanvas.findName("preloader_fadeout").addEventListener("Completed", Silverlight.createDelegate(this, this.initialImage));
		this.rootCanvas.findName("preloader_fadeout").addEventListener("Completed", Silverlight.createDelegate(this, this.fadeFrame));
		this.rootCanvas.findName("preloader_fadeout").addEventListener("Completed", Silverlight.createDelegate(this, this.arrows_in));
		
		
		this.registerButton(this.rootCanvas.findName("flip_right_click"), this.rOverRight, this.rOutRight, this.nextIcon);
		this.registerButton(this.rootCanvas.findName("flip_left_click"), this.rOverLeft, this.rOutLeft, this.previousIcon);
		this.registerButton(this.rootCanvas.findName("click_gallery"), this.rOverViewBio, this.rOutViewBio, this.viewBio);
		

    },
    registerButton : function(button, overAction, outAction, clickAction) {
        if (overAction != null)
            button.addEventListener("MouseEnter", Silverlight.createDelegate(this, overAction));
        if (outAction != null)
            button.addEventListener("MouseLeave", Silverlight.createDelegate(this, outAction));
        if (clickAction != null)
	        button.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, clickAction));
    },
    
    getAssets : function(sender, eventArgs)
    {
		this.rootCanvas.findName("preloader_animation").begin();
        var downloader = this.control.createObject("downloader");
        downloader.addEventListener("Completed", Silverlight.createDelegate(this, this.onCompleted));
        downloader.addEventListener("DownloadProgressChanged", Silverlight.createDelegate(this, this.preloader));
        downloader.open("GET", "/prophoto/icons/icons-hero/images/icons.deploy");
        downloader.send();
    },
    
    onCompleted : function(sender, eventArgs)
    {
        this.rootCanvas.findName("icon1_image").setSource(sender, "borges.jpg");
        this.rootCanvas.findName("icon2_image").setSource(sender, "cantrell.jpg");
        this.rootCanvas.findName("icon3_image").setSource(sender, "finlay.jpg");
        this.rootCanvas.findName("icon4_image").setSource(sender, "fitzmaurice.jpg");
        this.rootCanvas.findName("icon5_image").setSource(sender, "ghionis.jpg");
        this.rootCanvas.findName("icon6_image").setSource(sender, "hoffmann.jpg");
        this.rootCanvas.findName("icon7_image").setSource(sender, "krogh.jpg");
        this.rootCanvas.findName("icon8_image").setSource(sender, "read-miller.jpg");
        this.rootCanvas.findName("icon9_image").setSource(sender, "reggie.jpg");
        this.rootCanvas.findName("icon10_image").setSource(sender, "rosato.jpg");
        this.rootCanvas.findName("icon11_image").setSource(sender, "shaw.jpg");
        this.rootCanvas.findName("icon12_image").setSource(sender, "jordan-smith.jpg");
        this.rootCanvas.findName("icon13_image").setSource(sender, "wolfe.jpg");
    },
    
    preloader : function(sender, eventArgs) {
        var percentage = Math.floor(sender.downloadProgress * 100);
		if (percentage >= 100) this.rootCanvas.findName("preloader_fadeout").begin();
    },
    
    autoRotateReset : function(sender, eventArgs) {
		this.autoInterval = setInterval(Silverlight.createDelegate(this, this.nextIcon), this.autoRotate);
    },
   
    initialImage : function(sender, eventArgs) {
        this.rootCanvas.findName("icon1_fadein").begin();
        this.autoRotateReset();
        },
        
    fadeFrame : function(sender, eventArgs) {this.rootCanvas.findName("image_frame_fadein").begin();},   
        
    arrows_in : function(sender, eventArgs) {this.rootCanvas.findName("arrows_in").begin();},
    
    rOverRight : function(sender, eventArgs) {this.rootCanvas.findName("right_fadein").begin();},
    rOutRight : function(sender, eventArgs) {this.rootCanvas.findName("right_fadeout").begin();},
    
    rOverLeft : function(sender, eventArgs) {this.rootCanvas.findName("left_fadein").begin();},
    rOutLeft : function(sender, eventArgs) {this.rootCanvas.findName("left_fadeout").begin();},
    
    nextIcon : function(sender, eventArgs) {
        if (this.frontIcon == 13) { 
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("icon1_fadein").begin(); 
            this.rootCanvas.findName("icon13_fadeout").begin();
            this.frontIcon = 0;
        }
        else {
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("icon" + (this.frontIcon + 1) + "_fadein").begin();
            this.rootCanvas.findName("icon" + this.frontIcon + "_fadeout").begin();
        }
        this.frontIcon = this.frontIcon + 1;
        this.autoRotateReset();
    },
    
    previousIcon : function(sender, eventArgs) {
        if (this.frontIcon == 1) { 
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("icon13_fadein").begin(); 
            this.rootCanvas.findName("icon1_fadeout").begin();
            this.frontIcon = 14;
        }
        else {
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("icon" + (this.frontIcon - 1) + "_fadein").begin();
            this.rootCanvas.findName("icon" + this.frontIcon + "_fadeout").begin();
        }
        this.frontIcon = this.frontIcon - 1;
        this.autoRotateReset();
    },
    
    rOverViewBio : function(sender, eventArgs) {this.rootCanvas.findName("viewbio_fadein").begin();},
    rOutViewBio : function(sender, eventArgs) {this.rootCanvas.findName("viewbio_fadeout").begin();},
    viewBio : function(sender, eventArgs) {
        if (this.frontIcon == 1) {
            window.location = ('/prophoto/icons/borges/bio.aspx');
        }
        else if (this.frontIcon == 2) {
            window.location = ('/prophoto/icons/cantrell/bio.aspx');
        }
        else if (this.frontIcon == 3) {
            window.location = ('/prophoto/icons/finlay/bio.aspx');
        }
        else if (this.frontIcon == 4) {
            window.location = ('/prophoto/icons/fitzmaurice/bio.aspx');
        }
        else if (this.frontIcon == 5) {
            window.location = ('/prophoto/icons/ghionis/bio.aspx');
        }
        else if (this.frontIcon == 6) {
            window.location = ('/prophoto/icons/hoffmann/bio.aspx');
        }
        else if (this.frontIcon == 7) {
            window.location = ('/prophoto/icons/krogh/bio.aspx');
        }
        else if (this.frontIcon == 8) {
            window.location = ('/prophoto/icons/miller/bio.aspx');
        }
        else if (this.frontIcon == 9) {
            window.location = ('/prophoto/icons/reggie/bio.aspx');
        }
        else if (this.frontIcon == 10) {
            window.location = ('/prophoto/icons/rosato/bio.aspx');
        }
        else if (this.frontIcon == 11) {
            window.location = ('/prophoto/icons/shaw/bio.aspx');
        }
        else if (this.frontIcon == 12) {
            window.location = ('/prophoto/icons/smith/bio.aspx');
        }
        else if (this.frontIcon == 13) {
            window.location = ('/prophoto/icons/wolfe/bio.aspx');
        }
    
    }


}
