﻿//ItemPeque
// This JavaScript file defines the object "ItemPeque"
ItemPeque = function(ID, IDnum, Parent, XLocation, YLocation, Titulo, Scale, imagen, sText, sVideo, sBackground_Image, sIcon,sBigBackground_Image,sBigIcon,sBigText,sLinkLabel0,sLink0,sLinkLabel1,sLink1,sLinkLabel2,sLink2,sImage)
{
    this._ID = ID + "_";
    this._idnum = IDnum;
    this._parent = Parent;
    this._XLocation = XLocation;
    this._YLocation = YLocation;
    this._Titulo = Titulo;
	this._Icon = sIcon; 
    this._Text = sText;
    this._BigText = sBigText;
	this._Background_Image = sBackground_Image;
    this._host = this._parent.getHost();
    this._Scale = Scale;
    this._Imagen = imagen;
    this._Video = sVideo;
    this._BigIcon = sBigIcon;
	this._BigBackground_Image = sBigBackground_Image;
	this._LinkLabel0 = sLinkLabel0;
	this._Link0 = sLink0;
	this._LinkLabel1 = sLinkLabel1;
	this._Link1 = sLink1;
	this._LinkLabel2 = sLinkLabel2;
	this._Link2 = sLink2;
	this._Image = sImage;
    // The first step is to retrive the XAML content for the "box"
    this.StartXamlDownload();
}

ItemPeque.prototype =
{
    _findNameByXamlID : function(nameInXamlFile)
    {
        return this._parent.findName(this._getIdFor(nameInXamlFile));
    },
    
    _getIdFor : function(nameInXamlFile)
    {
        return this._ID + nameInXamlFile;
    },

    StartXamlDownload : function()
    {
        // A Silverlight "downloader" object is used to retrieve the "box.xaml" file that contains
        // the XAML for the "box"
        // A delegate is created that will call the "XamlDownloadCompleted" method when the
        // download is completed
        var xamlDownloader = this._host.createObject("downloader");
        xamlDownloader.open("GET", "ItemPeque.xaml");
        xamlDownloader.addEventListener("completed", Silverlight.createDelegate(this, this.XamlDownloadCompleted));
        xamlDownloader.send();
    },

    XamlDownloadCompleted : function(sender, eventArgs)
    {
        // The download of "box.xaml" has been completed
        // "sender.ResponseText" contains the contents of the xaml control
        var originalXaml = sender.ResponseText;

        // In order to avoid name collisions, the name of each "box" object will be replaced
        // with a name that begins with the ID that was passed in the object constructor
        originalXaml = originalXaml.replace(/Name="/g, "Name=\"" + this._ID);

        // The "box" will now be added to the main Canvas
        // "plugin" is a reference to the Silverlight control on the html page
        var plugin = sender.getHost();
        var newElement = plugin.content.createFromXaml(originalXaml);
        var nommm = newElement.Name;
        
        // The altered "box.xaml" is used to create a XAML object
        //var otroxaml = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'	x:Name='cuadradito'	Width='52' Height='47' Background='#FFEA8080'></Canvas>";
        //var newElement = plugin.content.createFromXaml(otroxaml)
         
        // "rootCanvas" is a reference to the main Silverlight Canvas
        
        var rootCanvas = plugin.content.findName("mainCanvas");
        
        // The XML object is added to the root Canvas
        //rootCanvas.children.add(newElement);
        main.children.add(newElement);

        // Now that the "box" has been added to the main Canvas
        // the "BoxTitle" will be altered and the "box" position will be set
        this._setControlReferences();
		
    },

    _setControlReferences : function()
    {
        // This method sets the "Title" and the "ItemPeque" position
        this._BoxTitle = this._findNameByXamlID("Titulo");
        //Asignamos la fuente!
        this._BoxTitle.setFontSource(downloader);
        this._BoxTitle.fontFamily = sFontName;//sFontName; //"Segoe UI";  
        
        //this._BoxTitle.text = this._Titulo; //this._XLocation.toString();

        // Now we adjust the scale
        this._BaseScale = this._findNameByXamlID("BaseScale");
        this._BaseScale.ScaleX = this._Scale; //this._XLocation.toString();
        this._BaseScale.ScaleY = this._Scale; //this._XLocation.toString();

        //Add the image        
        this._CtlImagen = this._findNameByXamlID("Thumb");
        this._CtlImagen.Source = this._Imagen; //this._XLocation.toString();
        
		this._CtlBackGroundImage = this._findNameByXamlID("ItemBackGround");
        this._CtlBackGroundImage.Source = this._Background_Image; //this._XLocation.toString();
		
		this._CtlIconImage = this._findNameByXamlID("Icon");
        this._CtlIconImage.Source = this._Icon;  //this._XLocation.toString();
		
		this._BoxText = this._findNameByXamlID("Text");
        //Asignamos la fuente!
        this._BoxText.setFontSource(downloader);
        this._BoxText.fontFamily = sFontName;//sFontName; //"Segoe UI";  
		this._BoxText.text = this._Text;
        //Add the image from the downloader object
        //this._CtlImagen.setSource(downloader, Elem_Thumbs_Download[this._idnum]);  
        
            
        //Add the image Reflection       
        this._CtlImagen = this._findNameByXamlID("ThumbR");
        this._CtlImagen.Source = this._Imagen; //this._XLocation.toString();

        this._box = this._findNameByXamlID("ItemPeque");
        this._box["Canvas.Left"] = this._XLocation;
        this._box["Canvas.Top"] = this._YLocation;

		
        //Añadimos los manejadores de eventos
        //this._box.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
        //xamlDownloader.addEventListener("completed", Silverlight.createDelegate(this, this.XamlDownloadCompleted));
        //MouseEnter="ItemMouseEnter" MouseLeave="ItemMouseLeave"
        this._box.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.ItemMouseEnter));
        this._box.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.ItemMouseLeave));
        this._box.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.itemRelease));

        //Y lanzamos la animación para que aparezca el elemento
        var AnimItemPeque_Appear = this._findNameByXamlID( "ItemPeque_Appear" );
        AnimItemPeque_Appear.Begin();

       //Si es el último elemento, iniciamos el carrusel... 
       if (this._ID == "010_"){
           AfterItemsBuilt();
       }
    },
       

////		Mouse Enter & Leave de los items
//function ItemMouseEnter(s,e) {
//	RollOverItem=true;	
//}
//function ItemMouseLeave(s,e) {
//	RollOverItem=false;
//}       
   ItemMouseEnter : function(sender, eventArgs){ 
        RollOverItem=true;
        //var anim = plugin.content.findName("ItemPeque_Glow_In"); // ItemPeque_Glow_Out Popup_Resize_In Popup_Resize_Out
        this._findNameByXamlID("Popup_Resize_In").Begin();
        this._findNameByXamlID("ItemPeque_Glow_Out").Begin();

		this._CtlBackGroundImage = this._findNameByXamlID("ItemBackGround");
		var icoBlickAnim = "";
        if(this._CtlBackGroundImage.Source=="images/itembg0.png") icoBlickAnim = "IcoOrangeBlick";
		if(this._CtlBackGroundImage.Source=="images/itembg1.png") icoBlickAnim = "IcoGreenBlick";
		if(this._CtlBackGroundImage.Source=="images/itembg2.png") icoBlickAnim = "IcoBlueBlick";
		
		var AnimOverItem = plugin.content.findName( icoBlickAnim );
        AnimOverItem.Begin();
		
		
   },

    ItemMouseLeave : function(sender, eventArgs){ 
        RollOverItem=false;
        //var anim = plugin.content.findName("ItemPeque_Glow_In"); // ItemPeque_Glow_Out Popup_Resize_In Popup_Resize_Out
        this._findNameByXamlID("Popup_Resize_Out").Begin();
        this._findNameByXamlID("ItemPeque_Glow_Out").Begin();
        
        var AnimOverItem = plugin.content.findName( "icoDefault" );
        AnimOverItem.Begin();
   },

       
   itemRelease : function(sender, eventArgs){ 
        var ItemPopUp = plugin.content.findName("Item_design");
        var ItemPopUpTrans = plugin.content.findName("STItem");
    

        var InitialScale = 1;
        ItemPopUpTrans.ScaleX = InitialScale;
        ItemPopUpTrans.ScaleY = InitialScale;

        var xLoc = this._XLocation;
        
        var CLeft = this._box["Canvas.Left"];
        var CTop = this._box["Canvas.Top"];      

        ItemPopUp["Canvas.Left"] = CLeft - 100; 
        ItemPopUp["Canvas.Top"] = CTop -170; 
        
        ItemPopUp.Visibility = "Visible";

        iItemClicked = this._idnum;

        var oTitulo = plugin.content.findName ('ItemTitulo');
        oTitulo.text = this._Titulo; 
        
        var oTexto = plugin.content.findName ('Texte');
		oTexto.setFontSource(downloader);
        oTexto.fontFamily = sFontName;
        oTexto.text = this._BigText;
		
		var oLabel0 = plugin.content.findName ('Label0');
		oLabel0.setFontSource(downloader);
        oLabel0.fontFamily = sFontName;
        oLabel0.text = this._LinkLabel0;
		
		var oLink0 = plugin.content.findName ('Link0');
		oLink0.setFontSource(downloader);
        oLink0.fontFamily = sFontName;
        oLink0.text = this._Link0;
	
		var oLabel1 = plugin.content.findName ('Label1');
		oLabel1.setFontSource(downloader);
        oLabel1.fontFamily = sFontName;
        oLabel1.text = this._LinkLabel1;
		
		var oLink1 = plugin.content.findName ('Link1');
		oLink1.setFontSource(downloader);
        oLink1.fontFamily = sFontName;
        oLink1.text = this._Link1;
		
		var oLabel2 = plugin.content.findName ('Label2');
		oLabel2.setFontSource(downloader);
        oLabel2.fontFamily = sFontName;
        oLabel2.text = this._LinkLabel2;
		
		var oLink2 = plugin.content.findName ('Link2');
		oLink2.setFontSource(downloader);
        oLink2.fontFamily = sFontName;
        oLink2.text = this._Link2;

		plugin.content.findName ('LinkImage0').Visibility="Visible";
		plugin.content.findName ('LinkImage1').Visibility="Visible";
		plugin.content.findName ('LinkImage2').Visibility="Visible";
					
		if(this._Link0 == "") plugin.content.findName ('LinkImage0').Visibility="Collapsed";
		if(this._Link1 == "") plugin.content.findName ('LinkImage1').Visibility="Collapsed";
		if(this._Link2 == "") plugin.content.findName ('LinkImage2').Visibility="Collapsed";

		//if(this._idnum==4)plugin.content.findName ('LinkImage2').Source = 'images/movie.png';
		//else plugin.content.findName ('LinkImage2').Source = 'images/link.png';

		var VideoContainer = plugin.content.findName ('ItemVideoContainer');
		var SlideShowContainer = plugin.content.findName ('ItemSlideShowContainer');
		
		if(this._Video != "")
		{
			VideoContainer.Visibility = "Visible";
			SlideShowContainer.Visibility = "Collapsed";
			 		videoconfiguration['file'] = this._Video;
					var ctr = new jeroenwijering.Controller(videoconfiguration);
					var vie = new jeroenwijering.View(videoconfiguration,ctr);
					videoControl = new jeroenwijering.Model(videoconfiguration,ctr,vie);
					ctr.startMVC(vie,videoControl);
						
					videoControl.video.Source = videoconfiguration['file'];
					videoControl.goPause(0);
		}
		else
		{
			VideoContainer.Visibility = "Collapsed";
			SlideShowContainer.Visibility = "Visible";
	        var ScreenShot = plugin.content.findName("ScreenShot");
            ScreenShot.Source = this._Image;
		
			var AnimSlideShow = plugin.content.findName( "SlideShow0" );
        	AnimSlideShow.Begin();
		}

        var AnimOpenItem = plugin.content.findName( "ItemPopUp_Open" );
        AnimOpenItem.Begin();
		
        //Y lanzamos el video...
        //var Video = plugin.content.findName("ItemVideo");
        //Video.Source = this._Video; //"Videos/Sample Video WC 1.wmv"
        
        var BigBoxBackground = plugin.content.findName("BigBoxBackground");
        BigBoxBackground.Source = this._BigBackground_Image;
        
        var BigBoxIcon = plugin.content.findName("BigBoxIcon");
        BigBoxIcon.Source = this._BigIcon;
        
        //Indicamos que el popup esta visible
        PopUpShown = true
   }
}