﻿
window.addEvent('domready', function() { Products.setup(); });

var Map = {
    initialize: function() {
    var myLatlng = new google.maps.LatLng($('ctl00_ctl00_PageContent_SectionHeader_mapLat').get('value') * 1, $('ctl00_ctl00_PageContent_SectionHeader_mapLong').get('value') * 1);
        var myOptions = {
        zoom: $('ctl00_ctl00_PageContent_SectionHeader_mapZoom').get('value') * 1,
            mapTypeControl: false,
            navigationControl: false,
            scaleControl:false,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        }
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        ;

    },
    loadScript: function() {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=Map.initialize";
        document.body.appendChild(script);
    }
}

var Products = {
    ReqId: 0,
    CurrentThumb: 0,
    setup: function() {

        if ($('priceSummary') != null) {
            Products.GetPrice();
            $$('.option,#variant,.quantity').addEvent('change', Products.GetPrice);
        }

        if ($$('.dohide').length > 0) {
            (function() { $$('.dohide').morph({ 'opacity': '0' }); }).delay(10000);
            (function() { $$('.dohide').morph({ 'height': '0', 'padding': '0' }); }).delay(10500);
        }

        if ($('map_canvas') != null) {
            Map.loadScript();
        }

        var isNumberKey = function(e) {
            if (e.code > 31 && (e.code < 48 || e.code > 57) && (e.code < 96 || e.code > 105))
                return false;
            return true;
        }

        var isBackSpaceKey = function(e) {
            return e.code == 8;
        }

        var isArrowKey = function(e) {
            return e.code >= 37 && e.code <= 40;
        }

        
        $$('.extraquantity').each(function(y) {
            y.addEvent('keydown', function(e) {
                if (!isNumberKey(e) && !isBackSpaceKey(e) && !isArrowKey(e))
                    e.stop();

            })
        });

        $$('.extraquantity').addEvent('keyup', Products.GetPrice);

    },
    GetPrice: function(url) {


        Products.ReqId++;
        $('priceSummary').addClass('hidden');
        var nv = $('numvars').value * 1;

        var variants = new Array();

        if ($$('.addform .productinfo').length > 0) {

            for (var i = 1; i <= nv; i++) {

                var row = $$('.addform .productinfo')[i - 1];

                var opts = row.getElements('.option');
                var options = new Array();
                opts.each(function(y) {
                    options.include({ 'ID': y.get('rel'), 'Value': y.get('type') == 'checkbox' ? y.checked : y.value });
                });

                var qty = row.getElement('.quantity').get('value') * 1;
                var eq = row.getElement('.extraquantity');
                if (qty == -1) {
                    eq.removeClass('hidden');
                    eq.focus();

                    qty = eq.get('value') * 1;
                }
                else {
                    eq.addClass('hidden');
                }
                variants.include({ 'ID': row.getElement('.var').value, 'Val': i, 'Options': options, 'Qty': qty });

            }
            var post = { 'ID': Products.ReqId, 'Variants': variants, 'URL': url };

            var req = new Request.JSON({ url: 'price', onSuccess: Products.PriceDone, onFailure: function(x) { alert(x); } });
            req.send("json=" + JSON.encode(post));


        }
    },
    PriceDone: function(r) {

        if (r.OK && r.ID == Products.ReqId) {
            r.Variants.each(function(y) {
                var row = $$('.addform .productinfo')[y.Val - 1];


                if (y.InStock) {
                    row.getElement('.priceInfo span').set('text', '£' + y.Price);
                }
                else {
                    row.getElement('.priceInfo span').set('text', 'Out of Stock');
                }

                if (y.GrindDescription.length > 0) {
                    row.getElement('.grind span').set('text', y.GrindDescription);
                }

            });

            $('priceSummary').removeClass('hidden');

            if (r.OK && r.InStock) {
                $('addbasket').removeClass('hidden');
            }
            else {
                $('addbasket').addClass('hidden');
            }
        }
    }
}
