var prods = {};
var prodAry = new Array();

$(document).ready(function () {
    $("ul.sf-menu").superfish({ autoArrows: false, dropShadows: false });

    $('[rel=warning]').click(function (e) {

        if (confirm('Gift cards are not covered by the 25% discount! If you add a gift card to your cart your 25% discount will be nullified.') == false) {
        e.preventDefault()
        }

    })


    $('#mainNav li a').mouseenter(function () { $(this).css('color', '#5a451a'); Cufon.refresh(); });
    $('#mainNav li a').mouseleave(function () { $(this).css('color', '#2c220d'); Cufon.refresh(); });

    $('#mainNav li ul li a').mouseenter(function () { $(this).css('color', '#cbb677'); Cufon.refresh(); });
    $('#mainNav li ul li a').mouseleave(function () { $(this).css('color', '#f1e5c7'); Cufon.refresh(); });

    $('#footerNav li a').mouseenter(function () { $(this).css('color', '#38afe3'); Cufon.refresh(); });
    $('#footerNav li a').mouseleave(function () { $(this).css('color', '#FFF'); Cufon.refresh(); });

    $('#questionsColumn').height(parseInt($('#resultsColumn').height()) - 64);

    $('#mainNav li').hover(function () {
        var liWidths = 0;
        $(this).children().children('li').each(function (i, val) {
            $(this).evenIfHidden(function (element) { liWidths += parseInt(element.width()) + parseInt(element.css('padding-left')) + parseInt(element.css('padding-right')) + 20; });
        });

        $(this).children('ul').css('width', liWidths);

        if (liWidths > 0) {
            var mainNavPos = $('#mainNavContainer').position();
            var mainNav = $('#mainNavContainer').width() + mainNavPos.left;
            var myPos = $(this).position();

            //console.log(myPos.left + liWidths);
            if ((myPos.left + liWidths) > mainNav) {
                var pos = $(this).children('ul').position();
                var widthDiff = $(this).children('ul').width() - liWidths;
                var myWidth = $(this).children('ul').width();

                //console.log(mainNav - liWidths);

                //if (myWidth + pos.left <= $('#mainNav').width()) {
                //alert('yep');
                //$(this).children('ul').css('left', mainNav - liWidths + 65);

                //console.log($.browser.version);
                if ($.browser.name == 'msie' && parseInt($.browser.version) < 7) {
                    //pos = $(this).children('ul').position();
                    //alert(pos.left);
                    $(this).children('ul').css('left', mainNav - liWidths - 160);
                }
                //}
            } else {
                $(this).children('ul').css('left', '12px');
            }

            //console.log(myPos.left + " - " + liWidths);

            //            if (myPos.left > 400) { //Every menu item on the right side of the screen will display its drop down on the right.
            //                var pos = $(this).children('ul').position();
            //                var widthDiff = $(this).children('ul').width() - liWidths;
            //                var myWidth = $(this).children('ul').width();

            //                if (myWidth + pos.left <= $('#mainNav').width()) {
            //                    //alert('yep');
            //                    $(this).children('ul').css('left', pos.left + widthDiff);
            //                    if ($.browser.name == 'msie' && parseInt($.browser.version) < 7) {
            //                        //pos = $(this).children('ul').position();
            //                        //alert(pos.left);
            //                        $(this).children('ul').css('left', pos.left + widthDiff - 160);
            //                    }
            //                }
            //            }
        }
        //$(this).child().css('color','#6d8baa');
    }, function () {

    });

    $('.openMap').colorbox({ width: 600, height: "90%", iframe: true });

    $('#detailImage').click(function () {
        var imgSrc = $('#detailImage img').attr('src');
        var imgRoot = imgSrc.replace('std', 'max');
        imgSrc = "<div style='text-align:center;'><img src='" + imgRoot + "'/></div>";
        $.fn.colorbox({ width: 550, height: 550, open: true, html: imgSrc });
        //$.fn.colorbox.resize();
    });

    $('#ddlSizes').change(function () {
        if ($(this).val() != 0) {
            getVals($(this).val());
        }
    });

    getVals($('#ddlSizes').val()); //To get the value of the drop down if the page is refreshed

    $('.btnRemove').click(function () {
        var $tdParent = $(this).parent();
        var $sibs = $tdParent.siblings();
        $sibs.parent().fadeOut('fast');
    }); //opens a friendly reminder
});

function displayPriceSize(size, price) {
    $('.detailSize').html(size);
    $('.detailPrice').html(price);
    $('.detailSize').css('display','block');
    $('.detailPrice').css('display', 'block');
}

function buildObjects(fileName, swatchName, prodName, prodDescr, id, price, size, isSwatch) {
    prods = { _fileName: fileName, _swatchName: swatchName, _prodName: prodName, _prodDescr: prodDescr, _id: id, _price: price, _size:size, _isSwatch:isSwatch };
    prodAry.push(prods);
}

function getVals(id) {
    if($('#ddlSizes').width() > 200){
        $('.detailAddToCart').css('padding-left', '0px');
        $('.detailAddToCart').css('clear', 'left');
        $('.detailAddToCart').css('padding-top', '10px');
    }
    var aryLength = prodAry.length;
    if (aryLength > 0) {
        for (var i = 0; i <= aryLength - 1; i++) {
            if (prodAry[i]._id == id) {
                $('#detailImage img').attr('src', '/uploads/std' + prodAry[i]._fileName);
                $('.prodName').html(prodAry[i]._prodName);
                $('.prodDescr').html(prodAry[i]._prodDescr);

                if (prodAry[i]._isSwatch) {
                    $('#ddlSizes option').each(function () {
                        if ($(this).val() == prodAry[i]._id) {
                            $(this).attr('selected', 'selected');
                        }
                    })

                    //console.log($('#swatchName').css('left'));
                    var swWidth = $('#swatchName').width();
                    $('#swatchName').animate({ left: '-=' + swWidth + 'px' }, 300, function () { $('#swatchName').html(prodAry[i]._swatchName); });
                    $('#swatchName').animate({ left: 10 + 'px' }, 700);
                }

                //  original up-down box slider gizmo
                 $('.detailPrice').slideUp('normal',function () {$('.detailPrice').html(prodAry[i]._price); });
                 $('.detailPrice').slideDown();

                // new price slider gizmo


                return false;
            }
        }
    } else {
        if ($('.detailPrice').html() !=  null) {
            if ($('.detailPrice').html().length < 1) {
                $('.detailPrice').html($('#hidPrice').val());
            }
        }
        if ($('#ddlSizes').css('display') == null) {
            $('.detailAddToCart').css('padding-left', '0px');
        }
    }
}

function openCB(myText, w, h) {
    setTimeout(function () { $.fn.colorbox({ width: w, height: h, open: true, html: myText }); }, 900);
    setTimeout(function () { $.fn.colorbox.close(); }, 6000);
} 

function openIFrame(myURL, w, h) {
    $.fn.colorbox({ width: w, height: h, open: true, iframe: true, href: myURL });
    
}

jQuery.fn.evenIfHidden = function (callback) {

    return this.each(function () {
        var self = $(this);
        var styleBackups = [];

        var hiddenElements = self.parents().andSelf().filter(':hidden');

        if (!hiddenElements.length) {
            callback(self);
            return true; //continue the loop
        }

        hiddenElements.each(function () {
            var style = $(this).attr('style');
            style = typeof style == 'undefined' ? '' : style;
            styleBackups.push(style);
            $(this).attr('style', style + ' display: block !important;');
        });

        hiddenElements.eq(0).css('left', -10000);

        callback(self);

        hiddenElements.each(function () {
            $(this).attr('style', styleBackups.shift());
        });

    });
};

