﻿if (!window.discover_vista) window.discover_vista = {};

discover_vista.Base = function() {
    this.control = null;
    this.rootCanvas = null;
    this.root = null;
    this.frontPic = 3;
    this.autoPicFlip = 15000; 
}

discover_vista.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.registerButton(this.rootCanvas.findName("image_three"), this.rOverImageThree, this.rOutImageThree, this.clickImageThree);
		this.registerButton(this.rootCanvas.findName("image_two"), this.rOverImageTwo, this.rOutImageTwo, this.clickImageTwo);
		this.registerButton(this.rootCanvas.findName("image_one"), this.rOverImageOne, this.rOutImageOne, this.clickImageOne);
		
		this.registerButton(this.rootCanvas.findName("button_one"), this.rOverButtonOne, this.rOutButtonOne, this.clickImageOne);
		this.registerButton(this.rootCanvas.findName("button_two"), this.rOverButtonTwo, this.rOutButtonTwo, this.clickImageTwo);
		this.registerButton(this.rootCanvas.findName("button_three"), this.rOverButtonThree, this.rOutButtonThree, this.clickImageThree);
		
		this.rootCanvas.findName("loadRun").addEventListener("Completed", Silverlight.createDelegate(this, this.flipPics));
		this.rootCanvas.findName("images_in").addEventListener("Completed", Silverlight.createDelegate(this, this.firstText));
		
//		this.fileAssets = new Array();
//		this.fileDownloader("silverlight/Segoe.deploy", this.pushFont);
//		this.fileDownloader("silverlight/SegoeLight.deploy", this.pushFont);

        WindowsSite.Services.DiscoverVista.GetXamlContent( "windows/windows-vista/discover/DiscoverSilverlight.xml", Silverlight.createDelegate(this, this.contentRecieved), Silverlight.createDelegate(this, this.contentError) );
    },
    
    contentRecieved : function ( result ) {
        var data = eval("(" + result + ")");
                
        this.rootCanvas.findName("image_one").Source = data.images[0];
        this.rootCanvas.findName("image_two").Source = data.images[1];
        this.rootCanvas.findName("image_three").Source = data.images[2];
        
        var textOneXaml = this.control.content.createFromXaml(data.text[0]);
        var textTwoXaml = this.control.content.createFromXaml(data.text[1]);
        var textThreeXaml = this.control.content.createFromXaml(data.text[2]);
        
        this.rootCanvas.findName("text_one").children.insert(0, textOneXaml);
        this.rootCanvas.findName("text_two").children.insert(0, textTwoXaml);
        this.rootCanvas.findName("text_three").children.insert(0, textThreeXaml);
        
        this.formatText("text_one");
        this.formatText("text_two");
        this.formatText("text_three");
       
        this.rootCanvas.findName("images_in").begin();
        this.getSegoe();
    },
    
//   pushFont : function(sender) {
//	    this.fileAssets.push({ name: "fontSegoe", file: sender, resource: null, type: "font" });
//	    this.setFontText();
//	},
//    
//    setFontText : function(featureObject, setFontStyle) {
//		this.attachFont(this.rootCanvas.findName("one_paragraph_one"), null, "fontSegoe", "Segoe", setFontStyle);
//		this.attachFont(this.rootCanvas.findName("one_paragraph_two"), null, "fontSegoe", "Segoe", setFontStyle);
//		this.attachFont(this.rootCanvas.findName("name_one"), null, "fontSegoe", "Segoe", setFontStyle);
//	},
//	

//	
//	
//	attachFont : function(textObject, textValue, fontSource, fontName, setFontStyle) {
//				alert(this.getAsset(fontName));
//		if (setFontStyle) {
//			textObject.setFontSource(this.getAsset(fontName));
//			textObject["FontFamily"] = fontName;
//		}
//		if (textValue != null){
//		    textObject["Text"] = textValue;
//		}
//	},
//	fileDownloader : function(fileName, callback, progress) {
//		var downloader = this.control.createObject("downloader");
//		downloader.addEventListener("Completed", Silverlight.createDelegate(this, callback));
//		if (progress != null)
//			downloader.addEventListener("DownloadProgressChanged", Silverlight.createDelegate(this, progress));
//		downloader.open("GET", fileName);
//		downloader.send();
//	},
//	getAsset : function(assetName) {
//		for (var i = 0; i < this.fileAssets.length; i++)
//			if (this.fileAssets[i].name == assetName) return this.fileAssets[i].resource;
//		return null;
//	},

	getSegoe : function(sender, eventArgs)
    {
        var downloader = this.control.createObject("downloader");
        downloader.addEventListener("Completed", this.onCompleted);
        downloader.open("GET", "/windows/windows-vista/discover/silverlight/Segoe.deploy");
        downloader.send();
    },

	onCompleted : function(sender, eventArgs)
    {
        var myTextBlock = sender.findName("three_paragraph_one");
        myTextBlock.setFontSource(sender);
        myTextBlock.fontFamily="Segoe";        
    },

	
    
    formatText : function(canvasID) {
        var canvas = this.rootCanvas.findName(canvasID).Children.GetItem(0);
        var offset = 25;
        for (var i=0; i < canvas.Children.Count; i++) {
            var text = canvas.Children.GetItem(i);
            text["Canvas.Top"] = offset;
            offset += ( text.ActualHeight + 10 );
        }
    },
    
    contentError : function ( result ) {
        alert("error:" + result);
    },
    
    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));
    },
    
    autoRotateReset : function(sender, eventArgs) {
		this.autoInterval = setInterval(Silverlight.createDelegate(this, this.autoClick), this.autoPicFlip);
    },
    
    autoClick : function(sender, eventArgs) {
        if  (this.frontPic == 1) { this.clickImageTwo() }
        else if  (this.frontPic == 2) { this.clickImageThree() }
        else if  (this.frontPic == 3) { this.clickImageOne() }
    },
    
    rOverButtonOne : function(sender, eventArgs) {this.rootCanvas.findName("button_one_tofifty").begin();},
    rOutButtonOne : function(sender, eventArgs) {this.rootCanvas.findName("button_one_tohundred").begin();},
    rOverButtonTwo : function(sender, eventArgs) {this.rootCanvas.findName("button_two_tofifty").begin();},
    rOutButtonTwo : function(sender, eventArgs) {this.rootCanvas.findName("button_two_tohundred").begin();},
    rOverButtonThree : function(sender, eventArgs) {this.rootCanvas.findName("button_three_tofifty").begin();},
    rOutButtonThree : function(sender, eventArgs) {this.rootCanvas.findName("button_three_tohundred").begin();},

    
//    rOverImageThree : function(sender, eventArgs) { this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 15; },
//    rOutImageThree: function(sender, eventArgs) {
//        if (this.frontPic == 1) {
//                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 7;
//               }
//        else if (this.frontPic == 2) {
//                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 5;
//               }
//        else if (this.frontPic == 3) {
//                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 10;
//               }
//    },
    clickImageThree: function(sender, eventArgs) { 
        this.rootCanvas.findName("image_one_back").begin();
        this.rootCanvas.findName("image_two_back").begin();
        this.rootCanvas.findName("image_three_rotate").begin();
        this.rootCanvas.findName("text_three_in").begin();
            if  (this.frontPic == 1) {
                clearInterval(this.autoInterval);
                this.rootCanvas.findName("text_one_out").begin();
                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 7;
                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 5;
                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 10;
                this.rootCanvas.findName("button_three")["Fill"] = '#FF196297';
                this.rootCanvas.findName("button_one")["Fill"] = '#FFBBBBBB';
            }
            else if (this.frontPic == 2) {
                clearInterval(this.autoInterval);
                this.rootCanvas.findName("text_two_out").begin();
                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 5;
                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 7;
                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 10;
                this.rootCanvas.findName("button_three")["Fill"] = '#FF196297';
                this.rootCanvas.findName("button_two")["Fill"] = '#FFBBBBBB';
            } 
        this.frontPic = 3; 
        this.autoRotateReset();
    },

//    rOverImageTwo : function(sender, eventArgs) { this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 15; },
//    rOutImageTwo: function(sender, eventArgs) {
//        if (this.frontPic == 1) {
//                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 5;
//               }
//        else if (this.frontPic == 2) {
//                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 10;
//               }
//        else if (this.frontPic == 3) {
//                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 7;
//               }
//    },
    clickImageTwo: function(sender, eventArgs) { 
        this.rootCanvas.findName("image_one_back").begin();
        this.rootCanvas.findName("image_two_rotate").begin();
        this.rootCanvas.findName("image_three_back").begin();
        this.rootCanvas.findName("text_two_in").begin();
            if  (this.frontPic == 1) {
                clearInterval(this.autoInterval);
                this.rootCanvas.findName("text_one_out").begin();
                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 7;
                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 10;
                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 5;
                this.rootCanvas.findName("button_two")["Fill"] = '#FF196297';
                this.rootCanvas.findName("button_one")["Fill"] = '#FFBBBBBB';
            }
            else if (this.frontPic == 3) {
                clearInterval(this.autoInterval);
                this.rootCanvas.findName("text_three_out").begin();
                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 5;
                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 10;
                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 7;
                this.rootCanvas.findName("button_two")["Fill"] = '#FF196297';
                this.rootCanvas.findName("button_three")["Fill"] = '#FFBBBBBB';
            }
        this.frontPic = 2; 
        this.autoRotateReset();
    },
    
//    rOverImageOne : function(sender, eventArgs) { this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 15; },
//    rOutImageOne: function(sender, eventArgs) {
//        if (this.frontPic == 1) {
//                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 10;
//               }
//        else if (this.frontPic == 2) {
//                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 7;
//               }
//        else if (this.frontPic == 3) {
//                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 5;
//               }
//    },
    clickImageOne: function(sender, eventArgs) { 
        this.rootCanvas.findName("image_one_rotate").begin();
        this.rootCanvas.findName("image_two_back").begin();
        this.rootCanvas.findName("image_three_back").begin(); 
        this.rootCanvas.findName("text_one_in").begin();
            if  (this.frontPic == 2) {
                clearInterval(this.autoInterval);
                this.rootCanvas.findName("text_two_out").begin();
                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 10;
                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 7;
                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 5;
                this.rootCanvas.findName("button_one")["Fill"] = '#FF196297';
                this.rootCanvas.findName("button_two")["Fill"] = '#FFBBBBBB';
            }
            else if (this.frontPic == 3) {
                clearInterval(this.autoInterval);
                this.rootCanvas.findName("text_three_out").begin();
                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 10;
                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 5;
                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 7;
                this.rootCanvas.findName("button_one")["Fill"] = '#FF196297';
                this.rootCanvas.findName("button_three")["Fill"] = '#FFBBBBBB';
            }
        this.frontPic = 1;
        this.autoRotateReset(); 
    },
    
    flipPics : function(sender, eventArgs) {
        this.rootCanvas.findName("images_in").begin();
    },
    
    firstText : function(sender, eventArgs) {
        this.rootCanvas.findName("text_three_in").begin();
        this.rootCanvas.findName("navbuttons_in").begin();
        this.rootCanvas.findName("image_three_rotate_slow").begin();
        this.rootCanvas.findName("button_three")["Fill"] = '#FF196297';
        this.autoRotateReset();
    }
//    setZindex : function(sender, eventArgs) {
//        if (this.frontPic == 1) {
//                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 10;
//                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 5;
//                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 7;
//               }
//        else if (this.frontPic == 2) {
//                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 7;
//                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 10;
//                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 5;
//               }
//        else if (this.frontPic == 3) {
//                this.rootCanvas.findName("image_one")["Canvas.ZIndex"] = 5;
//                this.rootCanvas.findName("image_two")["Canvas.ZIndex"] = 7;
//                this.rootCanvas.findName("image_three")["Canvas.ZIndex"] = 10;
//               }
//    }  
       
 

}
