if (typeof(HR) == 'undefined') {
    HR = {

    };
}

HR.pricing = {
    calculator:function() {
	var self = this;
        
        this.cost = {
            update:function() {
                var yearlyCost = this.get(365);
                
                $('#price-yearly').html(
                    HR.format.GBP_readable(yearlyCost)
                );

                $('#price-monthly').html(
                    HR.format.GBP_readable(yearlyCost / 12)
                );
            },
            
            storage:function(period) {
                return storage.price.get() * storage.amount.get() * (period/HR.date.daysInMonth());
            },
            
            bandwidth:function() {
                return bandwidth.price.get() * bandwidth.amount.get() * 12;
            },
            
            get:function(period) {
                return this.storage(period) + this.bandwidth();
            }

        };

        this.update = {
            all:function(fromUrl) {
                storage.amount.set(null, fromUrl);
                bandwidth.amount.set(null, fromUrl);
                storage.amount.incrementor.setAmount(storage.amount.get());
                bandwidth.amount.incrementor.setAmount(bandwidth.amount.get());
            }
        };

        var usageEntity = function(type) {
            if (!(type == 'storage' || type == 'bandwidth')) {
                return false;
            }

            var amount = function(type) {
                if (!(type == 'storage' || type == 'bandwidth')) {
                    return false;
                }

                var fieldName = type+'Amount';

                this.get = function() {
                    return $('#'+fieldName).val();
                };

                this.set = function(amount, fromUrl) {
                    if (HR.validate.isNumeric(amount) === false) {
                        if (typeof(fromUrl) != 'boolean' || fromUrl !== true) {
                            return false;
                        }
                        
                        amount = HR.location.hash.get(fieldName);                        
                        if (HR.validate.isNumeric(amount) === false) {
                            amount = this.get();
                        }
                    }

                    HR.location.hash.set(fieldName, amount);
                    $('#'+fieldName).val(amount);
                    self.cost.update();

                    return true;
                };
                
                this.incrementor = new HR.incrementor({
                    initialAmount:this.get(),
                    minimum:0,
                    maximum:10000
                });
            };

            var price = function(type) {
                if (!(type == 'storage' || type == 'bandwidth')) {
                    return false;
                }

                var fieldName = type+'Price';

                this.get = function() {
                    return $('#'+fieldName).attr('class');
                };
            };

            this.amount = new amount(type);
            this.price = new price(type);
        };

        var storage = new usageEntity('storage');
        var bandwidth = new usageEntity('bandwidth');
        
        this.initialise = function() {
            if ((window.location+"").indexOf("basic") != -1) {
                return true;
            }

            var storageTickCallback = function() {
                storage.amount.set(storage.amount.incrementor.amount());
            };

            var bandwidthTickCallback = function() {
                bandwidth.amount.set(bandwidth.amount.incrementor.amount());
            };
            
            var startStorageIncrementation = function(delta, direction) {
                storage.amount.incrementor.start({
                    direction:direction,
                    delta:delta,
                    callback:storageTickCallback,
                    callbackInterval:tickInterval
                });
            };
            
            var stopStorageIncrementation = function() {
                storage.amount.incrementor.stop();
                storage.amount.set(storage.amount.get());
                storage.amount.incrementor.setAmount(storage.amount.get());
            };

            var startBandwidthIncrementation = function(delta, direction) {
                bandwidth.amount.incrementor.start({
                    direction:direction,
                    delta:delta,
                    callback:bandwidthTickCallback,
                    callbackInterval:tickInterval
                });
            };

            var stopBandwidthIncrementation = function() {
                bandwidth.amount.incrementor.stop();
                bandwidth.amount.set(bandwidth.amount.get());
                bandwidth.amount.incrementor.setAmount(bandwidth.amount.get());
            };

            $('#storageinput').append(
                $('<span class="bigarrows" />')
                .append(
                    $('<a id="storage-increase" href="#" class="bigarrow bigarrow-up" />').click(function(){
                        return false;
                    }).mouseup(function(){
                        stopStorageIncrementation();
                    }).mousedown(function(){
                        startStorageIncrementation(1, 'up');
                    }).mouseout(function(){
                        stopStorageIncrementation();
                    }))
               .append(
                    $('<a id="storage-decrease" href="#" class="bigarrow bigarrow-down" />').click(function(){
                        return false;
                    }).mouseup(function(){
                        stopStorageIncrementation();
                    }).mousedown(function(){
                        startStorageIncrementation(1, 'down');
                    }).mouseout(function(){
                        stopStorageIncrementation();
                    })
                )
            );

            $('#bandwidthinput').append(
                $('<span class="bigarrows" />')
                .append(
                    $('<a id="bandwidth-increase" href="#" class="bigarrow bigarrow-up" />').click(function(){
                        return false;
                    }).mouseup(function(){
                        stopBandwidthIncrementation();
                    }).mousedown(function(){
                        startBandwidthIncrementation(1, 'up');
                    }).mouseout(function(){
                        stopBandwidthIncrementation();
                    }))
               .append(
                    $('<a id="bandwidth-decrease" href="#" class="bigarrow bigarrow-down" />').click(function(){
                        return false;
                    }).mouseup(function(){
                        stopBandwidthIncrementation();
                    }).mousedown(function(){
                        startBandwidthIncrementation(1, 'down');
                    }).mouseout(function(){
                        stopBandwidthIncrementation();
                    })
                )
            );

            var tickInterval = 50;

            $('#storageAmount').change(function() {
                storage.amount.set(storage.amount.get());
            }).keyup(function(){
                stopStorageIncrementation();
            }).keydown(function(e){
                switch (e.keyCode) {
                case 33: // Page up
                    startStorageIncrementation(10, 'up');
                    break;

                case 34: // Page down
                    startStorageIncrementation(10, 'down');
                    break;

                case 38: // Up arrow
                    startStorageIncrementation(1, 'up');
                    break;

                case 40:  // Down arrow
                    startStorageIncrementation(1, 'down');
                    break;

                default:
                    break;
                }
            });

            $('#bandwidthAmount').change(function() {
                bandwidth.amount.set(bandwidth.amount.get());
            }).keyup(function(){
                stopBandwidthIncrementation();
            }).keydown(function(e){
                switch (e.keyCode) {
                case 33: // Page up
                    startBandwidthIncrementation(10, 'up');
                    break;

                case 34: // Page down
                    startBandwidthIncrementation(10, 'down');
                    break;

                case 38: // Up arrow
                    startBandwidthIncrementation(1, 'up');
                    break;

                case 40:  // Down arrow
                    startBandwidthIncrementation(1, 'down');
                    break;

                default:
                    break;
                }
            });

            $('#calculate').hide();
            $('#calculateForm').submit(function(){
                return false;
            });

            $('#typicalCosts a').click(function(){
                var storageAmount = HR.url.query.value('storageAmount', $(this).attr('href'));
                var bandwidthAmount = HR.url.query.value('bandwidthAmount', $(this).attr('href'));
                var priceTransitionDelay = ($("#price-monthly:in-viewport").length === 0) ? 500 : 200;

                HR.window.scrollTo('#calculator', 400, 'swing');

		window.setTimeout(function() {
                    storage.amount.set(storageAmount);
                    bandwidth.amount.set(bandwidthAmount);
                    self.update.all();
		}, priceTransitionDelay);

                return false;
            });

            this.update.all(true);
            return true;
        };
    }
};

HR.initialise.pricing = function() {
    var calculator = new HR.pricing.calculator();
    calculator.initialise();
};