/**
 *  Javascript for details page only.
 */

var _colormap = new Array();
    _colormap['Blue Green'] = 'BG';
    _colormap['Black'] = 'BK';
    _colormap['Blue'] = 'BL';
    _colormap['Brown'] = 'BR';
    _colormap['Burgundy'] = 'BY';
    _colormap['Cocoa'] = 'CA';
    _colormap['China Blue'] = 'CB';
    _colormap['Cadet Blue (5194)'] = 'CD';
    _colormap['Cadet Blue'] = 'CD';
    _colormap['Canary'] = 'CN';
    _colormap['Cobalt'] = 'CO';
    _colormap['Caribe'] = 'CR';
    _colormap['Chardonnay'] = 'CH';
    _colormap['Christmas'] = 'CS';
    _colormap['Dark Blue'] = 'DB';
    _colormap['Dark Ocean'] = 'DC';
    _colormap['Dark Green'] = 'DG';
    _colormap['Dark Jade'] = 'DJ';
    _colormap['Dark Leaf'] = 'DL';
    _colormap['Dark Golden'] = 'DN';
    _colormap['Dark Oak'] = 'DO';
    _colormap['Dark Brown (5184)'] = 'DR';
    _colormap['Dark Lavender'] = 'DV';
    _colormap['Emerald'] = 'EM';
    _colormap['Forest'] = 'FT';
    _colormap['Gold Salmon'] = 'GD';
    _colormap['Green Gold'] = 'GG';
    _colormap['Golden'] = 'GL';
    _colormap['Green Oak'] = 'GO';
    _colormap['Green'] = 'GR';
    _colormap['Green Salmon'] = 'GS';
    _colormap['Indigo'] = 'IN';
    _colormap['Jade'] = 'JD';
    _colormap['Light Blue'] = 'LB';
    _colormap['Lichen'] = 'LC';
    _colormap['Lime'] = 'LM';
    _colormap['Light Oak'] = 'LO';
    _colormap['Light Sand (5143)'] = 'LS';
    _colormap['Light Sand'] = 'LS';
    _colormap['Lavender'] = 'LV';
    _colormap['Marine'] = 'MA';
    _colormap['Merlot'] = 'MT';
    _colormap['Midnight'] = 'MD';
    _colormap['Mint'] = 'MI';
    _colormap['Ocean'] = 'OC';
    _colormap['Oak'] = 'OK';
    _colormap['Olive'] = 'OV';
    _colormap['Pale Blue'] = 'PB';
    _colormap['Pacific Blue'] = 'PC';
    _colormap['Pale Green'] = 'PG';
    _colormap['Pale Purple'] = 'PP';
    _colormap['Pale Rose'] = 'PR';
    _colormap['Pale Salmon'] = 'PS';
    _colormap['Red'] = 'RD';
    _colormap['Retro Lime'] = 'RL';
    _colormap['Parrot'] = 'RR';
    _colormap['Peppermint'] = 'PT';
    _colormap['Russet'] = 'RT';
    _colormap['Sage'] = 'SA';
    _colormap['Sky Blue'] = 'SB';
    _colormap['Sand'] = 'SD';
    _colormap['Salmon'] = 'SL';
    _colormap['Sunset'] = 'SN';
    _colormap['Spruce'] = 'SP';
    _colormap['Spring'] = 'SR';
    _colormap['Turquoise Cobalt'] = 'TC';
    _colormap['Turquoise Gold'] = 'TD';
    _colormap['Turquoise Green'] = 'TG';
    _colormap['Tangerine'] = 'TN';
    _colormap['Turquoise'] = 'TU';
    _colormap['Violet'] = 'VI';
    _colormap['Warm Blue'] = 'WB';
    _colormap['Wheat'] = 'WH';
    _colormap['White Salmon'] = 'WS';
    _colormap['White'] = 'WT';
    
var _counter = 1;

function getLiteralColor(_code)
{
    for (var i in _colormap) {
        if (_colormap[i] == _code) {
            return i;
        }
    }
    
    return undefined;
}

var _output = '';   // multipurpose variable, clean it when you're finished.
    
$(document).ready(function(){
    
    $("#giftAmount").keypress(function(e) {
        
        if (e.which == 8 || e.which == 0) { // filter out delete/backspace
            return true;
        }
        
        var _allowed = ['0','1','2','3','4','5','6','7','8','9','.'];
        var _key = String.fromCharCode(e.which);
        var _fl = false;
        
        for (var _i in _allowed) {
            if (_key == _allowed[_i]) {
                _fl = true;
                break;
            }
        }
        
        if (!_fl) {
            return false;
        }
        
    });
    
    /** Gift Card Script **/
    if ($('#giftAmount').length) {
    
        $('#AddToCart').click(function() {
            if (parseFloat($('#giftAmount').val()) <= 0.00) {
                alert("You must enter an amount on the gift card");
                return false;
            }
            
            return true;
        });
    }
    
    var _isFiltered = false;
    
    if ($('select.optcolor').length && $('select.optcolor option').length > 2) {
        
        var _sku = $('input#productSKU').val();
        
        $('div.detailImage').css('height', '285px');
        
        /**
         *  Grabs all the thumbnails and places them on the display
         */
        $('select#'+$('select.optcolor').attr('id')+' option').each(function() {
            if (this.value > 0) {
                var _select = $.trim(this.text);
                
                if (_select in _colormap) {
                
                    var _cw = _colormap[_select];
                    var _file = _sku+'_'+String(_cw).toLowerCase();
                    _output += '<img colorID="'+$(this).val()+'" width="55" alt="'+_select+'" title="'+_select+'" src="cw3/assets/product_small/'+_file+'_sm.jpg" class="SwapableImage" cw="'+_cw+'" filename="'+_file+'"> &nbsp;';
                    $.preloadimages('cw3/assets/product_full/'+_file+'_med.jpg');
                }
            }
        });
        
        $('div.productImageSwapArea').html(_output);
        _output = '';
        
        /**
         *  Updates visibly larger images to reflect the click
         */
        $('img.SwapableImage').click(function(e) {
            
            // This one line updates color dropdown when thumb is selected
            //$('select#'+$('select.optcolor').attr('id')+' option[value="'+$(this).attr('colorID')+'"]').attr('selected', true);
            //$('select#'+$('select.optcolor').attr('id')).change();
            
            $('div.detailImage img#detailsImageVw').attr('src', 'cw3/assets/product_full/'+$(this).attr('filename')+'_med.jpg');
            var _file = 'cw3/assets/product_expanded/'+$(this).attr('filename')+'_lg.jpg';
            $('div.detailImage a#detailShowProductImage').attr('href', _file);
            $('div.detailImage a#detailShowProductImage').click(function() {
                return cwShowProductImage(_file,'Item');
            });
            
            if (e.clientX != undefined) {
                $('select.optcolor option[value="'+$(this).attr('colorID')+'"]').attr('selected', true);
                $('select.optcolor').change();
            }
            
        });
        
        
        /**
         *  Color Select onChange event, what it does is this, takes newly selected
         *  item from color dropdown, finds out the colorway (2 letter code) and auto
         *  clicks the thumb image to update it.
         */
        $('select#'+$('select.optcolor').attr('id')).change(function(e) {
            _counter++;
            
            if ($(this).val() != 0) {
                var _select = $.trim($('option:selected', this).text());
                if (_select in _colormap) {
                    var _cw = _colormap[_select];
                    $('img.SwapableImage[cw='+_cw+']').click();
                    
                    // small = sm 80x80 product_small
                    // thumb = th 130x130 product_thumb
                    // full = med 225x225  product_full
                    // expanded = lg 450x4500 product_expanded
                }
            } else {
                $('img.SwapableImage').eq(0).click();
            }

        });
        
        
        /**
         *  This bit of code is to determine upon first arrival which
         *  color option should be auto selected based on image that is 
         *  showing
         */
        var _s = $('#detailsImageVw').attr('src');
            _s = _s.split('/');
            _s = _s[_s.length-1];
            _s = _s.split('_');
            _s = String(_s[1]).toUpperCase();
            _s = getLiteralColor(_s);
           
        
         
        if (_s == undefined) {
            // don't do anything
        } else {
            
            if ($('#colorFamilies').length && $('#colorFamilies').val() != '') {
                
                var _t = $('#colorFamilies option:selected').text();
                
                $('select.optcolor option').each(function() {
                   if ($(this).text() == _t) {
                       _isFiltered = true;
                       $(this).attr('selected', 'selected');
                       $('select.optcolor').each(function() {
                            cwSetDependentList(this, 0);
                            $('select.optcolor').change();
                       });
                   }
                });
            } 
            
            if (_isFiltered == false) {
            
                $('select.optcolor option').each(function() {
                   if ($(this).text() == _s) {
                       $(this).attr('selected', 'selected');
                       $('select.optcolor').each(function() {
                            cwSetDependentList(this, 0);
                            $('select.optcolor').change();
                        });
                   }
                });
            }
        }
        
    } else {
    
    
        if ($('select.optcolor').length && $('select.optcolor option').length == 2) {
            if ($('#colorFamilies').length && $('#colorFamilies').val() != '') {
                
                var _t = $('#colorFamilies option:selected').text();
                
                $('select.optcolor option').each(function() {
                   if ($(this).text() == _t) {
                       _isFiltered = true;
                       $(this).attr('selected', 'selected');
                       $('select.optcolor').each(function() {
                            cwSetDependentList(this, 0);
                            $('select.optcolor').change();
                       });
                   }
                });
            } 
            
            if (_isFiltered == false) {
                $('select.optcolor option').eq(1).attr('selected', 'selected');
                $('select.optcolor').change();
            
            }
        } else {
        
            /** Selects First option in Frame Dropdown if frame option is showing and no color is
             */
            if (!$('select.optcolor').length && $('select.optframe').length) {
                $('select.optframe option').eq(1).attr('selected', 'selected');
                $('select.optframe').change();
            }
            
        }
        
    }
    
    
    
});
