﻿/// <reference path="jquery-1.4.2.min.js" />
/// <reference path="script.js" />

$.fn.overAndOut = function () {
    $(this).hover(function () { $(this).addClass('over'); }, function () { $(this).removeClass('over') });
}


var SHOP = new Object();


SHOP.applyView = function () {
}



SHOP.expandNode = function (selector) {
    $(selector).each(function () {
        var hasChildren = false;
        $(this).children('ul').each(function () {
            hasChildren = true;
        });
        if (hasChildren) {
            $(this).children('div').each(function () {
                if (!$(this).hasClass('expanded')) {
                    $(this).addClass('expanded');
                    $(this.parentNode).children('ul').each(function () {
                        $(this).show();
                    });
                }
            });
        }
    });
}

SHOP.defineClick = function (selector) {
    $(selector).each(function () {
        var hasChild = false;
        $(this).children('ul').each(function () {
            hasChild = true;
        });

        $(this).overAndOut();
        $(this).children('div').each(function () {
            $(this).overAndOut();
        });

        if (hasChild) {
            $(this).children('div').children('div.toggle').click(function () {
                $(this).overAndOut();
                if ($(this.parentNode).hasClass('expanded')) {
                    $(this.parentNode).removeClass('expanded');
                    $(this.parentNode.parentNode).children('ul').each(function () {
                        $(this).hide();
                    });
                } else {
                    SHOP.expandNode(this.parentNode.parentNode);
                }
            });
        }
    });
}

SHOP.initializeChildren = function (selector) {
    $(selector).children('ul').each(function () {

        var expanded = false;

        $(this.parentNode).children('div').each(function () {
            if ($(this).hasClass('expanded')) {
                expanded = true;
            }
        });

        if (!expanded) {
            this.style.display = 'none';
        }
        $(this).overAndOut();
        $(this).children('li').each(function () {
            SHOP.initializeChildren(this);
            SHOP.defineClick(this);
        });
    });
}

SHOP.applyBehavior = function () {
    $('#shopgroupmenu').each(function () {
        $(this).children('ul').children('li').each(function () {
            $(this).overAndOut();

            SHOP.initializeChildren(this);
            SHOP.defineClick(this);
        });
    });
}

SHOP.openGroup = function (element, id) {
    $(element).each(function () {
        SHOP.expandNode(this.parentNode.parentNode);

        var categorypageid = document.getElementById('__isshopcategorypage');

        if (categorypageid == null) {
            // jump to the page with the category viewer
            document.location = 'shop.aspx?shopgroupid=' + id + '&ts=' + (new Date().getTime());
        } else {
            var cb = new Object();
            cb.Callback = function () {
            }
            GNscript.GnaxControls.Run('Shop.ShowCategory', id, cb, false);
        }
    });
}


$(document).ready(function () {
    SHOP.applyView();
    SHOP.applyBehavior();
});


