$(document).ready(function() {

	$(".selected").prev("h4").text("puppies");
	


    var $$ = function(param) {
        var node = $(param)[0];
        var id = $.data(node);
        $.cache[id] = $.cache[id] || {};
        $.cache[id].node = node;
        return $.cache[id];
    };

    OnPageLoad();
    $('.productShow .alternates a.thumbnail img').click(function(event) {
        var thumbSrc, thumbPath, image, imageSrc, imageQueryString, newSrc, fadeWrapper, i;
        try {
            thumbSrc = $(this).attr('src');
            thumbPath = thumbSrc.substring(0, thumbSrc.indexOf('?'));
            image = $('.productShow .left.column .image img');
            fadeWrapper = $('.productShow .left.column .image .fadeWrapper');
            imageSrc = image.attr('src');
            imageQueryString = imageSrc.substring(imageSrc.indexOf('?'));
            newSrc = thumbPath + imageQueryString;

            // preload the image while we fade out
            i = new Image();
            i.src = newSrc;

            fadeWrapper.fadeOut('normal', function() {
                image.attr('src', newSrc);
                image.load(function() {
                    fadeWrapper.fadeIn('fast');
                });
            });
            return false;
        } catch (e) {
/*             console.debug(e); */
            return false;
        }
    });
    
    $('.dropdowntrigger').mouseenter(function(){
    	$(this).children('.dropdownlist').show();
    	$$(this).hovered = true;
    }).each(function(index) {
        var self = this;
        $(this).mouseleave(function(){
    	    $$(self).hovered = false;
        	setTimeout(function() {
        	    if(!$$(self).hovered) {
        	        $(self).children('.dropdownlist').hide();
        	    }
        	}, 600);
        });
    });

    setupProductReviews();

    $('div.breadcrumb ul li').not('.last').click(function() {
        window.location.href = $(this).find('a').attr('href');
    });
});

function OnPageLoad() {
    $('.hideForAjax').hide();
    $('.showForAjax').removeClass('showForAjax');
    setupProductCompare();
}

function ScrollToElement(theElement) {
    var selectedPosX = 0;
    var selectedPosY = 0;
    while (theElement != null) {
        selectedPosX += theElement.offsetLeft;
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }
    window.scrollTo(selectedPosX, selectedPosY);
}

function ParseFloatOrDefault(stringval, defaultVal) {
    var f = parseFloat(stringval);
    if (isNaN(f)) return defaultVal;
    return f;
}

//use 0 for height and width to use size of inline container
function ThickboxInlineUrl(inlineID, isModal,height,width) {
    var div = $('#' + inlineID);
    if (height === undefined || height == 0) height = div.height();
    if (width === undefined || width == 0) width = div.width();
    return '#TB_inline?height=' + height.toString() + '&width=' + width.toString() + '&inlineId=' + inlineID + '&modal=' + isModal.toString();
}

$.fn.dropdown = function(triggerElement) {
    try {
        var isopen = false;
        var trigger = $(triggerElement);
        var div = $(this);
        div.append('<!--[if lte IE 6]><iframe></iframe><![endif]-->');
        div.addClass('selectFree');
        var offset = trigger.offset();
        div.hide().remove().css('top', offset.top + trigger.height()).css('left', offset.left - div.width()).css('position', 'absolute');
        $('body').append(div);
        $('body').click(function() {
            if (isopen) {
                $(div).slideUp('fast');
                isopen = false;
            }
            return true;
        });
        $(window).resize(function() {
            var offset = trigger.offset();
            div.css('top', offset.top + trigger.height()).css('left', offset.left - div.width());
        });
        trigger.click(function() {
            if (isopen) {
                $(div).slideUp('fast');
                isopen = false;
            }
            else {
                var offset = trigger.offset();
                div.css('top', offset.top + trigger.height()).css('left', offset.left - div.width());
                $(div).slideDown('fast');
                isopen = true;
            }
            return false;
        });
    }
    catch (e) { }
};

function setupProductReviews() {
}

jQuery.fn.setupBase = function(base, options) {
    for (extra in base) {
        var self = this;
        if (base[extra] instanceof Array) {
            for (var i = 0; i < base[extra].length; i++)
                base[extra][i].call(self, options);
        } else {
            base[extra].call(self, options);
        }
    }
};
var openFilters = [];
jQuery(function($) {

    var $$ = function(param) {
        var node = $(param)[0];
        var id = $.data(node);
        $.cache[id] = $.cache[id] || {};
        $.cache[id].node = node;
        return $.cache[id];
    };

    $.fn.menu = function(options) {
        options = options || {};
        this.setupBase(options.base || $.fn.menu.base, options);

        // Initialize
        this.each(function() {
            var menu = $(this);
            $$(menu).menus = $();

            $(this).children('li').mouseenter(function(event) {
                var li = $(this).closest('li');
                menu.trigger('menu:enter', [li]);
            });

            $(this).children('li').mouseleave(function(event) {
                var li = $(this).closest('li');
                menu.trigger('menu:leave', [li]);
            });

            $(this).children('li').each(function() {
                var ul = $(this).children('ul');
                $$(menu).menus = $$(menu).menus.add(ul);
            });

            menu.trigger('menu:initialize');

        });

        return this;
    };

    $.fn.menu.base = {
        initialize: [function(options) {
            this.bind('menu:initialize', function() {
                $(this).children('li').each(function() {
                    var ul = $(this).children('ul');
                    ul.hide('fast');
                });
            });
        } ],
        enter: [function(options) {
            this.bind('menu:enter', function(e, li) {
                li.children('a,span').addClass('open');
                li.addClass('hover');
                var ul = li.children('ul');
                ul.show('fast');
            });
        } ],
        leave: [function(options) {
            this.bind('menu:leave', function(e, li) {
                li.removeClass('hover');
                li.children('a,span').removeClass('open');
                li.children('ul').stop(true, true).hide('fast');
            });
        } ]
    };

    $.delayedMenuBase = $.extend({}, $.fn.menu.base);

    $.delayedMenuBase['leave'] = [function(options) {
        this.bind('menu:leave', function(e, li) {
            li.removeClass('hover');
            window.setTimeout(function() {
                if (!li.hasClass('hover')) {
                    li.children('a,span').removeClass('open');
                    li.children('ul').stop(true, true).hide('fast');
                }
            }, 300);
        });
    } ];

    $.fn.filters = function(leaveOpen) {
        var self = this;

        this.find('ul h4').mouseenter(function(event) {
            var ul = $(this).closest('ul');
            if (ul.find('input:checked').length == 0) {
                $(this).addClass('hover');
            }
        }).mouseleave(function(event) {
            $(this).removeClass('hover');
        });

        var transition = function(clicked, animate) {
            $(clicked).removeClass('closed').find('ul').slideDown(0);
            self.children('ul.filterHeading').each(function(i) {
                if ($(clicked).index(this) == -1) {
                    if ($(this).find('input:checked').length == 0) {
                        var uls = $(this).addClass('closed').find('ul');
                        self.find('input:checkbox').css('display', 'none');
                        animate ? uls.slideUp(0) : uls.hide();
                    }
                }
            });
        };

        leaveOpen = leaveOpen || [this.find('ul.filterHeading')[0]];

        /* if (this.length > 0) transition(leaveOpen, false); */

        this.find('ul.filterHeading ul').addClass('enhanced');
        this.find('ul.enhanced li').each(function() {
            if ($(this).find('input:checked').length > 0) {
                $(this).addClass('selected puppies');
            }
        });

        this.find('ul.enhanced li').click(function(event) {
            var li = $(this).closest('li');

            if ($(li).hasClass('disabled')) {
                return false;
            }

            // Turn off all other filters
            $('ul.enhanced li').removeClass('selected');
            $('ul.enhanced li').find('input:checkbox').each(function() {
                this.checked = false;
            });

            $(li).toggleClass('selected');
            var selected = $(this).hasClass('selected');
            $(li).find('input:checkbox').each(function() {
                this.checked = selected;
            });
            openFilters = $.map($('ul.filterHeading').get(), function(n, i) {
                if (!$(n).hasClass('closed')) {
                    return i;
                }
            });

            if (!$.browser.msie && !$.browser.version.substr(0, 1) < 7) {
                $('ul.enhanced:visible').fadeTo('fast', 0.3).addClass('disabled');
            }
            $('#filterform').submit();
            return false;
        });

        this.find('ul.filterHeading h4').click(function(event) {
            if ($(this).hasClass('selectedFilter')) {
                $(this).parent().find('input:hidden').remove();
                $('form#filterform').submit();
                return true;
            }
            else if ($(this).next('ul').is(':visible')) {
                transition([], true);
            } else {
                transition($(this).closest('ul')[0], true);
            }
            return false;
        });

    };
});

$(document).ready(function() {

    $('.sectionNavigation').menu();
    $('.sectionNavigation > li:last > a, .sectionNavigation > li:last > span').css({ border: 'none' });
    initGallery();
});

var initGallery = function(leaveOpen) {

    $('form#filterform').filters(leaveOpen);

    if ($('.gallery').size > 0) {
        $('.gallery .small:nth-child(4n)').css('border-right', 'none');
        $('.gallery .large:nth-child(3n)').css('border-right', 'none');
    }

//    setTimeout(function() {
//        $('.productListing select#SelectedSort').selectbox();
//        $('.productListing .jquery-selectbox-item').click(function(event) {
//            $('#filterform').append('<input type="hidden" name="SelectedSort" value="' + $('.productListing select#SelectedSort').val() + '"/>');
//            $('#filterform').submit();
//        });
//    }, 1);

    // compare functionality

    $('.productListing div.details p.compare label').html('<a href="#" title="Compare checked items">compare</a>');
    $('.productListing div.details p.compare input').click(function(event) {

        var compareLimit = 3;

        if (this.checked) {
            if ($('.productListing div.details p.compare input:checked').length >= compareLimit) {
                $('.productListing div.details p.compare input').each(function() {
                    if (!this.checked) this.disabled = true;
                });
            }
        } else {
            $('.productListing div.details p.compare input:disabled').each(function() {
                this.disabled = false;
            });
        }
    });
    $('.productListing div.details p.compare label a').click(function(event) {

        var compareMinimum = 1;

        var checked = $('.productListing div.details p.compare input:checked');
        if (checked.length <= compareMinimum) {
            alert('You must check at least one item to compare items!');
            return false;
        }
        var ids = $.map(checked.get(), function(element, index) {
            return '&compareID=' + $(element).next('input:hidden').val();
        });
        var width = (205 * ids.length) + 1;
        tb_show(null, FormUrlForAjax($('#compareform')) + '&height=496&width=' + (width).toString() + ids.join(), false, 'tb_Black');
        return false;
    });

};

$.fn.productVideo = function(videoKey, height, width, autoplay) {
   var id = $(this).attr('id');

   var params = { allowScriptAccess: 'always', allowFullScreen: 'true' };
   var atts = { id: id + '-player' };
   
   var url = 'http://www.youtube.com/v/' + videoKey + '?autoplay=' + (autoplay ? 1 : 0) + '&enablejsapi=1&version=3&playerapiid=' + id + '-player';
   swfobject.embedSWF(url, id, width, height, '8', null, null, params, atts);
};
   //Youtube player calls this function when it loads.
function onYouTubePlayerReady(playerid) { };