// JavaScript Document


/***
 * console.log fix for machines not running firebug.
 *
 */
if (typeof(console) == 'undefined') {
    var console = {
        log: function() {},
        info: function() {},
        warn: function() {},
        error: function() {},
        time: function() {}
    };
}
else if (typeof(console.log) == 'undefined') {
    console.log = function() {};
}

var WG = {};
WG.Cart =  function () {
    current = '';
    return {
            getCurrent : function () {
                return this.current;
            },

            setCurrent : function (current) {
                this.current = current;
            }
     };
}();



    window.addEvent('domready', function () {

         //get all image elements in product-left and assign them a grey background as default colour
        var images = $('product-left').getElements('img');
        images.each(function(el) {
            el.setStyle('background-color','#CCCCCC');
        });

        var swatch = $('color-swatch');
        if (swatch && RExt.Options.length) {
            $('static_color').setStyle('display','none');    // temporarily hide the static color swatch
            $('color-swatch').setStyle('height','130px');   // show the dynamic color swatch
            // assign a handler to the purchase button to ensure product colors are selected
            var purchase = $('purchase');
            purchase.addEvent('click', onPurchase.bindWithEvent(purchase));

            RExt.Options.each(function(color) {
               var img = document.createElement('div');
               img.setAttribute('id',color.name+':'+color.value);
               img.setAttribute('class','tip');
               img.setAttribute('title',color.name+' :: Color code is '+color.value);
               swatch.adopt(img);
               var el = $(color.name+':'+color.value);
               if (el) {
                  el.setStyles('float:left; margin:2px; width:48px; height:22px; border:1px solid #000; background-color:#'+color.value+';');
                  el.addEvent('click', onSwatch.bindWithEvent(el,color));
               }
            });
            var qtips = new Tips($$('.tip'));
        }
    });

    function onSwatch(ev, color) {
        var images = $('product-left').getElements('img') //get all image elements
        images.each(function(el) {
            el.setStyle('background-color','#'+color.value);
        });

        // make the slimbox background the selected swatch colour
        $('lbCenter').setStyle('background-color','#'+color.value);

        // save the selected color as the current color for use with the purchase button
        RExt.Selection = {name: color.name, value: color.value};
		//alert(color.name);
		document.getElementById('itemname5').value=color.name;
    }

    function onPurchase(ev) {
         var evt = new Event(ev);
         if (typeof RExt.Selection.name == 'undefined' || typeof RExt.Selection.value == 'undefined') {
             alert('You should select a color for this Product!');
             evt.preventDefault();
         } else {
             var id = getSelectedVariant();
             if (cart.length) {
                 var exists = false;
                 for (var i = 0, len = cart.length; i<len; i++) {
                     if (id === cart[i].variant_id) {
                         exists = true;
                         var list = Cookie.get(id); // get all the existing colors
                         var colors = Json.evaluate(list);
                         colors.push(RExt.Selection);
                         Cookie.set(id,Json.toString(colors),{path: '/'});
                     }
                 }
                 if (!exists) {
                     var color = [];
                     color.push(RExt.Selection);
                     Cookie.set(id,Json.toString(color),{path: '/'}); // no match in cart, write this one up
                 }
             } else {
                 var color = [];
                 color.push(RExt.Selection);
                 Cookie.set(id,Json.toString(color),{path: '/'}); // nothing in cart, write this one up
             }
         }
         //evt.preventDefault();
         //evt.stopPropagation();
    }

    function getSelectedVariant() {
      var v = RExt.Variants;
      for ( var i = 0, len = v.length; i < len; i++) {
          // this would work for radio buttons
          var chk = $('radio_'+v[i].id);
          if (chk && chk.checked) {
             return v[i].id;
          }

      }
      // this would work for dropdowns
      //var el = $('selected_option');
      //return el.value;
   }


