﻿var Steps =
{
    _container: '#stepbox'
    ,_cookieName: '.Wizard'
    ,_count: 3
    ,_state:
    [
        {
            name: 'step1'
            ,state: false
            ,defaultValue: 'Επιλογή Αριθμού'
            ,handler: function(box, step, args)
            {
                var values = args.split(';');
                var value = (step.state ? values[0] : step.defaultValue);
                box.find('a:first')
                    .attr('title', value)
                    .css('cursor', 'pointer')
                    .find('span:first')
                    .addClass('Tahoma17').html(value);
            }
        }
        ,
        {
            name: 'step2'
            ,state: false
            ,handler: null
        }
        ,
        {
            name: 'step3'
            ,state: false
            ,doneValue: 'Επαρκές υπόλοιπο'
            ,errorValue: 'Μη επαρκές υπόλοιπο'
            ,defaultValue: 'Αγορά πόντων'
            ,doneHref: 'javascript:'
            ,errorHref: '/MyViva/BuyPoints.aspx'
            ,handler: function(box, step, args)
            {
                // check here
                var balance = Balance.get_currentBalance();
                var products = Steps.get_products();
                
                var selection = args;
                
                // An den mporei na agorasei to epilegmeno tote to
                if(products != null && balance != null)
                {
                    var isvalid = false;
                    
                    Steps._modifyCookie(selection);
                    
                    if(args == 1 && balance > products.year || args == 2 && balance > products.monthly)
                        isvalid = true;
                        
                    box.attr('class', (isvalid ? 'done' : 'error'));
                    box.find('a:first')
                        .attr('title', (isvalid ? step.doneValue : step.errorValue ))
                        .attr('href', (isvalid ? step.doneHref : step.errorHref))
                        .css('cursor', (isvalid ? 'default':'pointer'))
                        .find('span:first')
                        .html((isvalid ? step.doneValue : step.errorValue ));
                }
                else
                {
                    var message = step.defaultValue;
                    box.find('a:first')
                        .attr('title', message)
                        .attr('href', message)
                        .css('cursor','default')
                        .find('span:first')
                        .html(message);
                }
            }
        }
        ,{name: 'step4', state: false, handler: null}
    ]
    ,_products: null
    
    ,initialize: function()
    {
        if(Balance != null)
        {
            Balance.balanceChanged = function()
            {
                var cookie = getCookie(Steps._cookieName);
                if(cookie != null)
                {
                    var parts = cookie.split('||');
                    if(parts.length == 3 && Steps.get_products() != null)
                        Steps.set_stepStatus(3, true , parseInt(parts[1]));
                }
            }
        }
    }
    
    ,get_products: function(){return this._products;}
    ,set_products: function(value){this._products = value;}
    
    ,set_stepStatus: function(step, value, args)
    {
        if (value != null)
        {
            this._state[step - 1].state = value;
        
            // Raise event
            this.stepValueChanged(step, args);
        }
    }
    
    ,get_stepStatus: function(step)
    {
        return this._state[step - 1].state;
    }
    
    ,set_stepHandler: function(step, handler)
    {
        this._state[step - 1].handler = handler;
    }
    
    ,stepValueChanged: function(step, args)
    {
        var st = this._state[step - 1];
        
        var box = $(this._container).find('li').eq(step - 1);
        box.attr('class', (st.state ? 'done' : ''));
        
        if(st.handler != null)
            st.handler(box, st, args);
    }
    
    ,_modifyCookie: function(value)
    {
        var cookie = getCookie(Steps._cookieName);
        if(cookie != null)
        {
            var parts = cookie.split('||');
            var value = new Array(parts[0], parts[1], value);
            setCookie(Steps._cookieName, value.join('||'));
        }
    }
}

// Initialize
Steps.initialize();
