﻿document.addEvent('domready', function() { COOK.Setup(); });
var COOK = {
    Setup: function() {
        new Dropdown();
        Search.Initialize();
    }
}

var Search = {
    SID: 0,
    LID: 0,
    Timer: null,
    Initialize: function() {
        var s = $('search');
        if (s != null) {
            s.addEvent('focus', function(x) {
                if (s.get('value') == 'Product Search')
                    s.set('value', '');
                $('searchresults').removeClass('hidden');
            });
            s.addEvent('blur', function(x) {
                var d = function() {
                    $('searchresults').addClass('hidden');
                };
                d.delay(200);
                if (s.get('value') == '')
                    s.set('value', 'Product Search');
            });
            s.addEvent('keyup', function(x) {

                if (Search.Timer != null)
                    $clear(Search.Timer);

                Search.Timer = Search.DoSearch.delay(200);


            });
        }
    },
    DoSearch: function() {
        Search.SID++;
        var more = $('searchresults').getElement('.more');
        if (more != null) {
            more.set('text', 'Searching....');
            var im = new Element('img', { 'src': '/images/ajax-loader(1).gif' });
            im.inject(more);
        }
        var req = new Request.JSON({ url: '/search/', onSuccess: Search.SearchDone, onFailure: function(x) { } });
        req.send("search=" + escape($('search').get('value')) + "&ajax=1&sid=" + Search.SID);
        _gaq.push(['_trackPageview', '/search/?search=' + escape($('search').get('value'))]);
    },
    SearchDone: function(x) {
        if (x.SID >= Search.LID) {
            Search.LID = x.SID;
            var sr = $('searchresults');
            sr.empty();
            var el = new Element('ul');
            el.inject(sr);
            x.Results.each(function(y) {
                var li = new Element('li');
                li.inject(el);
                var a = new Element('a');
                a.inject(li);
                var j = new Element('img', { 'src': y.ImageURL, 'height': 60, 'width': 60 });
                j.inject(a);
                var s = new Element('span');
                s.inject(a);
                s.set('text', unescape(y.Name));
                a.set('href', y.PageURL);
            });

            if (x.NumResults > 5) {
                var a = new Element('a');
                a.inject(sr);

                a.set('href', "/search/?search=" + $('search').get('value'));
                a.set('text', "More Results >");
                a.addClass('more').addClass('topborder');
            }
            if (x.NumResults == 0) {
                var a = new Element('a');
                a.inject(sr);

                a.set('href', "/search/?search=" + $('search').get('value'));
                a.set('text', "No results found");
                a.addClass('more');
            }
        }
    }
};


var Dropdown = new Class({
    DoClose: false,
    initialize: function() {

        this.Dropdowns = $$('.dropdown li');
        $$('.dropdown a').addEvent('mouseenter', function(x) { this.DoClose = false; $clear(this.timeout); });

        var y = this.OpenMenu.bind(this);
        var x = this.StartClose.bind(this);
        this.Dropdowns.each(function(e) {
            var e2 = e.getElement('a.top')
            if (e2 != null) {
                e2.addEvent('mouseenter', y.pass(e2));
            }
            //e.addEvent('mouseleave', x.pass(e));
        })
        $$('.dropdown div.level0').addEvent('mouseleave', function(e) {
            $$('.dropdown div.level0').each(function(i) {
                i.fade(0);
                i.getParent().getElements('a').removeClass('open');
            });
        });
    },
    OpenMenu: function(e) {

        $clear(this.timeout);

        var parent = e.getParent().getParent().getParent();
        parent.getElements('a').each(function(k) {

            if (k.hasClass('open') && k != e) {
                k.removeClass('open');
                k.getParent().getElements('div.level0').each(function(i) {
                    i.setStyle('display', 'none');
                });
            }
        }
                );

        if (!e.hasClass('open')) {
            e.addClass('open');


            if (e.getParent().getChildren().length > 1) {

                var children = e.getParent().getChildren()[1];
                children.setOpacity(0);
                children.setStyles({ 'display': 'block' });
                children.fade(1);


                var left, top;
                //console.log(e.getPosition().x);

                left = e.getPosition(parent).x;
                top = e.getPosition(parent).y + e.getSize().y;

                /*var nw = e.getSize().x;
                if (nw < 150)
                nw = 150;
                children.setStyle('width', nw + 'px');
                */

                children.setStyle('top', top + 'px');
                children.setStyle('left', left + 'px');

            }
        }
    },
    StartClose: function(e) {
        this.DoClose = true;
        console.log('startclose');
        this.timeout = this.CloseMenu.delay(500);
    },
    CloseMenu: function() {
        $clear(this.timeout);
    
        if (this.DoClose) {
            $$('.dropdown div.level0').each(function(i) {
                i.fade(0);
                i.getParent().getElements('a').removeClass('open');
            });
        }
    }
});
