﻿// News rotation

var rotatorsArray = new Array();

function InitNews(uniqueID, newsType, filters, newsCount, newsPosition, imageSize)
{
    var newsRotator = new RotateNews();
    newsRotator.uniqueID = uniqueID;
    newsRotator.newsType = newsType == 'MotosQuads' ? 'Motos' : newsType;
    newsRotator.filters = filters;
    newsRotator.newsPosition = newsPosition;
    newsRotator.newsCount = newsCount;
    newsRotator.imageSize = imageSize;
    newsRotator.viewName = newsType;
    
    rotatorsArray[uniqueID] = newsRotator;
    newsRotator.go();
}

function RotateRightNews(uniqueID)
{
    var rotator = rotatorsArray[uniqueID];
    
    if (rotator.canMoveRight)
    {
        rotator.newsPosition = rotator.newsPosition + rotator.newsCount;
        rotator.go();                    
    }
}

function RotateLeftNews(uniqueID)
{
    var rotator = rotatorsArray[uniqueID];
    
    // Here we change position before display
    if (rotator.canMoveLeft)
    {
        rotator.newsPosition = rotator.newsPosition - rotator.newsCount;
        rotator.go();            
    }
}

function RotateNews() {     
    this.uniqueID = '';   
    this.newsType = '';
    this.newsPosition;
    this.newsCount = 0;
    this.filters = null;
    this.imageSize = ''
    this.canMoveLeft = false;
    this.canMoveRight = true;
    this.lastID = 0;
    this.viewName = null;
    
    
    // Main callback function
    this.callback = function(res) {
        if (FinishAJAXMethod(res))
        {            
            var table = res.value.Tables[0];
            var cutText; 
            var imageSource;
            var sufix;
            var href;
            
            for (var i=0; i< this.newsCount; i++)
            {
                sufix = '_' + this.uniqueID + i;
                
                // Check if we have enought data
                if (i < table.Rows.length)
                {    
                    // Check if news repeat
                    if (this.lastID == table.Rows[i]['Id'])
                    {   
                        this.canMoveRight = false;
                    }
                    else
                    {                    
                        //SlideOut(0, this.uniqueID + 'previewImageHolder' + i, "Data/" + GetImageSource(this.newsType) + "/" + table.Rows[i]['Path']);
                              
                        // recambios have different header ... dani can do the same for noticias
                        if (table.Rows[i]['Marca'] == null)
                        {                        
                            //cutText = table.Rows[i]['Titulo'].substring(0, 25);    
                            cutText = table.Rows[i]['Titulo'];
                            href = '/' + table.Rows[i]['UrlTitulo'] + '/';
                        }
                        else
                        {
                            cutText =                         
                                table.Rows[i]['Marca'] + ' ' +
                                table.Rows[i]['Modelo'];
                                
                            href = '/' + table.Rows[i]['Marca'] + '/' + table.Rows[i]['Modelo'] + '/';
                            
                            // append version
                            if (table.Rows[i]['VersionUrl'] != null)
                            {
                                href += table.Rows[i]['VersionUrl'] + '/';
                            }
                        }
                        
                        // finish url
                        href += table.Rows[i]['Id'];
                        
                        // Change image
                        if (this.newsType == 'Quads')
                        {
                            $('Image' + sufix).src = "Data/Motos/TN_" + this.imageSize + "/" + table.Rows[i]['Path'];   
                        }
                        else
                        {
                            $('Image' + sufix).src = "Data/" + this.newsType + "/TN_" + this.imageSize + "/" + table.Rows[i]['Path'];
                        }
                               
                        // Set values
                        if (table.Rows[i]['Entrada'])
                        {                        
                            $('Header' + sufix).innerHTML = cutText;
                            switch (this.newsType)
                            {
                                case 'Noticias':
                                    $('Header' + sufix).href = $('Url' + sufix).href = $('UrlImg' + sufix).href = 'sector' + href;
                                    break;
                                case 'NoticiasDeporte':
                                    if (table.Rows[i]['NoticiaTipoId'] == 0)
                                    {
                                        $('Header' + sufix).href = $('Url' + sufix).href = $('UrlImg' + sufix).href = 'deportescoches' + href;
                                    }
                                    else
                                    {
                                        $('Header' + sufix).href = $('Url' + sufix).href = $('UrlImg' + sufix).href = 'deportesmotos' + href;
                                    }
                                    break;
                            }
                            $('Entrada' + sufix).innerHTML = CheckSize(table.Rows[i]['Entrada'], 200);
                        }
                        else
                        {
                            $('Header' + sufix).innerHTML = CheckSize(cutText, 25);
                            
                            // SetUrl
                            if (table.Rows[i]['Categoria'] != null && table.Rows[i]['Categoria'] == '1')
                            {
                                $('Url' + sufix).href = 'vehiculosindustriales' + href;
                                $('UrlImg' + sufix).href = 'vehiculosindustriales' + href;
                            }
                            else if(this.newsType == 'Motos' && table.Rows[i]['Categoria'] != null && table.Rows[i]['Categoria'] == 'QUAD')
                            {
                                $('Url' + sufix).href = 'quads' + href;
                                $('UrlImg' + sufix).href = 'quads' + href;
                            }
                            else
                            {
                                $('Url' + sufix).href = this.newsType + href;
                                $('UrlImg' + sufix).href = this.newsType + href;    
                            }
                        }
                        
                        if (table.Rows[i]['AnyoMatriculacion'] != null)
                        {
                            $('Year' + sufix).innerHTML = 'Año ' + table.Rows[i]['AnyoMatriculacion'];
                        }
                        // For fecha...
                        else if (table.Rows[i]['Tipo'])
                        {
                            $('Year' + sufix).innerHTML = table.Rows[i]['Tipo'];
                        }
                        else if (table.Rows[i]['FechaPublicacionText'])
                        {
                            $('Year' + sufix).innerHTML = table.Rows[i]['FechaPublicacionText'];
                        }
                        
                        if (table.Rows[i]['Precio'])
                        {
                            $('Precio' + sufix).innerHTML =
                                table.Rows[i]['Precio'] == '0' ? 'Consultar precio' : (table.Rows[i]['Precio'] + '€');
                                
                            if (table.Rows[i]['Precio'] != '0')
                            {
                                $('Cuota' + sufix).innerHTML = 'Desde ' + (Math.round(table.Rows[i]['Precio'] / 60 * 1.08)) + '€ al mes';
                            }
                            else $('Cuota' + sufix).innerHTML = '&nbsp;';
                        }
                                                                       
                        // Set header text
                        if (this.filters != null && this.filters != '' && !table.Rows[i]['Entrada'])
                        {
                            if (res.value.Tables[1].Rows[0]['Total'] < 0)
                            {
                                GetControl(this.uniqueID, 'CaruselHeader', '').innerHTML = GetControlValue(this.uniqueID, 'DefaultHeaderText', '');
                            }
                            else
                            {
                                GetControl(this.uniqueID, 'CaruselHeader', '').innerHTML = GetControlValue(this.uniqueID, 'HeaderText', '');
                            }
                        }                                                                               
                        
                        // Show control - because sometimes we hide the control if there aren't enough records
                        if (GetControl(this.uniqueID, 'Carusel' + i, '') != null)
                        {
                            GetControl(this.uniqueID, 'Carusel' + i, '').style.display = '';
                        }
                        
                        // Set values
                        this.lastID = table.Rows[i]['Id'];
                        this.canMoveRight = true;
                    }
                    
                }
                // If not .. dont't allow to overflow
                else
                {
                    this.canMoveRight = false;
                    
                    // Hide remaining carusels if can't display enough
                    if (GetControl(this.uniqueID, 'Carusel' + i, '') != null)
                    {
                        GetControl(this.uniqueID, 'Carusel' + i, '').style.display = 'none';
                    }
                }
                
                // Hide 'loading ...' message
                $('Hider' + sufix).style.display = 'none';
            }
            
            // Check movement left
            if (this.newsPosition > 0) 
                this.canMoveLeft = true;
            else this.canMoveLeft = false;            
        }    
    };
    this.go = function() {    
        // Show 'loading ...' message;
        for (var i=0; i< this.newsCount; i++)
        {
            $('Hider_' + this.uniqueID + i).style.display = '';
        }
        
        // Call callback function
        var res = Controls_CarPreviewControl.GetNews(this.viewName, this.filters, this.newsPosition, this.newsCount, this.callback.bind(this));
        // var res = Controls_CarPreviewControl.GetNews(this.newsType, this.filters, this.newsPosition, this.newsCount, this.callback.bind(this));
    };
} 

function CheckSize(text, size)
{
    if (text == null)
    {
        return '';
    }
    
    if (text.length > size)
    {
        return text.substring(0, size) + ' ...';
    }
    return text;
}

function HideNews()
{
    
}

function SlideOut(state, objId, src)
{    
    state -= 10;
    var obj = $(objId);
    var pos = state + 'px 0px';
    
    if (obj.clientWidth > (-state)) 
    {
        
      obj.style.backgroundPosition = pos;     
      window.setTimeout("SlideOut(" + state + ",'" + objId + "','" + src + "')", 10);
    }  
    else
    {
        obj.style.backgroundImage = "url('" + src + "')";
        SlideIn(obj.clientWidth, objId)
    }
}

function SlideIn(state, objId)
{
    state -= 10;
    var obj = $(objId);
    var pos = state + 'px 0px';
    
    if (state >= 0) 
    {
      if (state < 10)
      {
        state = 0;
      }
      obj.style.backgroundPosition = pos;     
      window.setTimeout("SlideIn(" + state + ",'" + objId + "')", 10);
    } 
}

