﻿function Portfolio(name)
{
    this.Name = name;
    this.AddressElement = null;
    this.CompanyElement = null;
    this.CreationElement = null;
    this.DescriptionElement = null;
    this.IndustryElement = null;
    this.InfosElement = null;    
    this.ItemsCount = 0;
    this.Loading = true;
    this.PreviewElement = null;
    this.ScrollDownElement = null;
    this.ScrollUpElement = null;
    this.SelectedWebsite = null;
    this.ThumbnailsElement = null;
    this.Websites = new Array();  
    
    this.isMSIE = document.all?true:false;
}

Portfolio.prototype.AddWebsite = function(imgThumbnail, company, preview, industry, url, creation, description, sortOrder)
{
    var idx, m;
    
    imgThumbnail.alt = imgThumbnail.alt.replace(';', '\n');
    imgThumbnail.alt = imgThumbnail.alt.replace(';', '\n');
    
    idx = this.Websites.length;    
    this.Websites[idx] = imgThumbnail;
    this.Websites[idx].Company = company;
    this.Websites[idx].Preview = preview;
    this.Websites[idx].Industry = industry;
    this.Websites[idx].Url = url;
    this.Websites[idx].Creation = creation;
    this.Websites[idx].Description = description;

    //imgThumbnail.LoadingFx = new Spry.Effect.FadeSquish(imgThumbnail.id, {duration:1000,from:'0%',to:'100%'});
    //imgThumbnail.SelectionFx = new Spry.Effect.Fade(imgThumbnail.id, {duration:1000,from:'100%',to:'25%',toggle:true});
    
    /*if (sortOrder == 1)
    {
        this.SelectedWebsite = this.Websites[idx];
        this.SelectWebsiteFx.start();
    }       
    else
    {
        imgThumbnail.LoadingFx.addNextEffect(imgThumbnail.SelectionFx);   
    }*/
    
    m = sortOrder*500;

    //setTimeout(imgThumbnail.id + '.LoadingFx.start();', m);
    this.ThumbnailsElement.scrollTop = 0;
}

Portfolio.prototype.HideWait = function()
{
    //this.LoadingElement.fx.start();
    this.ScrollDownElement.fx.start();
    this.ScrollUpElement.fx.start();
    
    this.Loading = false;
}

Portfolio.prototype.Load = function()
{    
    var portfolio;
    
    portfolio = this;   
    function SetInfo() { portfolio.SetInfo(); }
    //this.SelectWebsiteFx = new TransitionEffect(this.PreviewElement.id, this.InfosElement.id, this.CompanyElement.id, {duration:500,from:'100%',to:'0%',toggle:false,finish:SetInfo});   
        
    if (!this.isMSIE) 
    {
        this.ThumbnailsElement.onmousemove = this.SetMouseY;
        //this.FlowersElement.onmousemove = this.SetMouseY;
    }
    
    //this.LoadingElement.fx = new Spry.Effect.Fade(this.LoadingElement.id, {duration:1000,from:'100%',to:'0%'});
    this.ScrollDownElement.fx = new Spry.Effect.Fade(this.ScrollDownElement.id, {duration:1000,from:'0%',to:'100%'});
    this.ScrollUpElement.fx = new Spry.Effect.Fade(this.ScrollUpElement.id, {duration:1000,from:'0%',to:'100%'});

    setTimeout(this.Name + '.HideWait();', this.ItemsCount*5);
}

Portfolio.prototype.StartScrollDown = function(px)
{    
    if (!this.Loading && !this.ThumbnailsElement.ScrollingDown && !this.ThumbnailsElement.ScrollingUp)
    {
        if (px == undefined) 
        {
            this.ThumbnailsElement.oldScrollTop = this.ThumbnailsElement.scrollTop;
            px = 1;           
        }
        
        if ((this.ThumbnailsElement.scrollTop - this.ThumbnailsElement.oldScrollTop) < 180 && (this.ThumbnailsElement.scrollHeight-this.ThumbnailsElement.offsetHeight-this.ThumbnailsElement.scrollTop) > 0 && !this.ThumbnailsElement.ScrollingUp)
        {       
            this.ThumbnailsElement.ScrollingDown = true;            
            this.ThumbnailsElement.scrollTop += px;

            if (this.ThumbnailsElement.scrollTop - this.ThumbnailsElement.oldScrollTop > 90 && px > 1) px--;
            else px++;
            
            setTimeout(this.Name + '.ScrollDown(' + px + ');', 25);        
        }
        else
        {    
            this.ThumbnailsElement.ScrollingDown = false;                                                  
        }
    }    
}

Portfolio.prototype.ScrollDown = function(px)
{    
    if (!this.Loading)
    {
        if (px == undefined) 
        {
            this.ThumbnailsElement.oldScrollTop = this.ThumbnailsElement.scrollTop;
            px = 1;           
        }
        
        if ((this.ThumbnailsElement.scrollTop - this.ThumbnailsElement.oldScrollTop) < 180 && (this.ThumbnailsElement.scrollHeight-this.ThumbnailsElement.offsetHeight-this.ThumbnailsElement.scrollTop) > 0 && !this.ThumbnailsElement.ScrollingUp)
        {       
            this.ThumbnailsElement.ScrollingDown = true;            
            this.ThumbnailsElement.scrollTop += px;

            if (this.ThumbnailsElement.scrollTop - this.ThumbnailsElement.oldScrollTop > 90 && px > 1) px--;
            else px++;
            
            setTimeout(this.Name + '.ScrollDown(' + px + ');', 25);        
        }
        else
        {    
            this.ThumbnailsElement.ScrollingDown = false;                                                  
        }
    }    
}

Portfolio.prototype.StartScrollUp = function(px)
{
    if (!this.Loading && !this.ThumbnailsElement.ScrollingDown && !this.ThumbnailsElement.ScrollingUp)
    {
        if (px == undefined) 
        {
            this.ThumbnailsElement.oldScrollTop = this.ThumbnailsElement.scrollTop;
            px = 1;
        }
                    
        if ((this.ThumbnailsElement.oldScrollTop - this.ThumbnailsElement.scrollTop) < 180 && this.ThumbnailsElement.scrollTop > 0 && !this.ThumbnailsElement.ScrollingDown)
        {
            this.ThumbnailsElement.ScrollingUp = true;                        
            this.ThumbnailsElement.scrollTop -= px;
            
            if (this.ThumbnailsElement.oldScrollTop - this.ThumbnailsElement.scrollTop > 90 && px > 1) px--;
            else px++;
            
            setTimeout(this.Name + '.ScrollUp(' + px + ');', 25);
        }
        else
        {                                
            this.ThumbnailsElement.ScrollingUp = false;     
        }        
    }
}

Portfolio.prototype.ScrollUp = function(px)
{    
    if (!this.Loading)
    {
        if (px == undefined) 
        {
            this.ThumbnailsElement.oldScrollTop = this.ThumbnailsElement.scrollTop;
            px = 1;
        }
                    
        if ((this.ThumbnailsElement.oldScrollTop - this.ThumbnailsElement.scrollTop) < 180 && this.ThumbnailsElement.scrollTop > 0 && !this.ThumbnailsElement.ScrollingDown)
        {
            this.ThumbnailsElement.ScrollingUp = true;                        
            this.ThumbnailsElement.scrollTop -= px;
            
            if (this.ThumbnailsElement.oldScrollTop - this.ThumbnailsElement.scrollTop > 90 && px > 1) px--;
            else px++;
            
            setTimeout(this.Name + '.ScrollUp(' + px + ');', 25);
        }
        else
        {                                
            this.ThumbnailsElement.ScrollingUp = false;     
        }        
    }
}

Portfolio.prototype.SelectWebsite = function()
{
    var y, website, previous;
        
    if (this.isMSIE) y = event.clientY + document.body.scrollTop;
    else y = document.MouseY;
       
    y += this.ThumbnailsElement.scrollTop;
       
    previous = this.SelectedWebsite;
       
    for (idx=0; idx<this.Websites.length; idx++)
    {
        website = this.Websites[idx];
        
        if (y >= website.offsetTop && y <= (website.offsetTop + website.offsetHeight))
        {            
            if (website != this.SelectedWebsite && website.PreviewLoaded)
            {
                this.SelectedWebsite = website;
                this.SelectWebsiteFx.start();
                website.SelectionFx.start();
                idx = this.Websites.length+10;
            }
        }
    }
    
    if (this.SelectedWebsite != previous) previous.SelectionFx.start();
}

Portfolio.prototype.SetMouseY = function(e)
{
    document.MouseY = e.pageY;
}

Portfolio.prototype.SetInfo = function()
{
    this.CompanyElement.innerHTML = this.SelectedWebsite.Company;
    this.PreviewElement.src = this.SelectedWebsite.Preview;
    this.IndustryElement.innerHTML = this.SelectedWebsite.Industry;
    this.AddressElement.innerHTML = this.SelectedWebsite.Url;
    this.AddressElement.href = this.SelectedWebsite.Url;
    this.CreationElement.innerHTML = this.SelectedWebsite.Creation;
    this.DescriptionElement.innerHTML = this.SelectedWebsite.Description;        
}

Portfolio.prototype.constructor = Portfolio; 