(function($) {
    
    var W = this.window, D = W.document, DE = D.documentElement;
    
    if(!DE || !D.getElementById || !D.getElementsByTagName || !D.createElement) {
        return;
    }
    
    DE.id = 'js';
    
    // clear fields onfocus / restore on blur
    var reTextFieldTypes = /(text|password)/i;
    var reEmpty = /^\s*$/;
    
    var clearTextFieldValue = function() {
        if(this.value == this.defaultValue) {
            this.value = '';
        }
    };
    
    var restoreTextFieldValue = function() {
        if(reEmpty.test(this.value)) {
            this.value = this.defaultValue;
        }
    };
    
    $.fn.clearTextFields = function() {
        this.each(function(i, elm) {
            if(elm.nodeName.toLowerCase() == 'input' && reTextFieldTypes.test(elm.type)) {
                $(elm).focus(clearTextFieldValue).blur(restoreTextFieldValue);
            } else {
                $('input[type=text], input[type=password]', elm).focus(clearTextFieldValue).blur(restoreTextFieldValue);
            }
        });
    };
    
    // manage dropdowns on main navigation
    var triggerL1, triggerL2;
    
    var mouseOverNav = function(e) {
        var expand = $(this).find('div.expand');
        if(expand.length) {
            expand.show();
            triggerL1 = expand.prev().addClass('active');
        } else {
            expand = $(this).find('div.expandSub');
            if(expand.length) {
                expand.show();
                triggerL2 = expand.prev().addClass('active');
            }
        }
    };
    
    var mouseOutNav = function(e) {
        var expand = $(this).find('div.expand');
        if(expand && expand.length) {
            expand.hide();
            if(triggerL1 && triggerL1.length) {
                triggerL1.removeClass('active');
            }
        } else {
            expand = $(this).find('div.expandSub');
            if(expand.length) {
                expand.hide();
                if(triggerL2 && triggerL2.length) {
                    triggerL2.removeClass('active');
                }
            }
        }
    };
    
    // manage dropdowns on contextual navigation
    
    // create and manage toolbar (change font size, printâ€¦)
    var currentFontSize = 0, clsFontSize = ['normal', 'large', 'big'];
    
    var setFontSize = function() {
        $('#page').get(0).className = clsFontSize[currentFontSize];
        $.cookie('fontSize', currentFontSize);
    };
    
    var openSendToFriend = function(link) {
        W.open(link, 'sendToFriend', 'menubar=yes, toolbar=no, location=yes, scrollbars=yes, resizable=yes, status=no, width=450, height=500');
    };
    
    var createTools = function() {
        $(
            '<div id="tools">' +
               '<ul>' +
                  '<li></li>' +
				   '<li></li>' +
                    '<li></li>' +
                    '<li></li>' +
                '</ul>' +
            '</div>'
        ).prependTo('#main div.content').click(function(e) {
            e.preventDefault();
            var src = e.target.src;
            if(src) {
                if(src.indexOf(l10n.url.tools.print) > -1) {
                    W.print();
                } else if(src.indexOf(l10n.url.tools.down) > -1) {
                    currentFontSize = currentFontSize === 0 ? 0 : currentFontSize - 1;
                    setFontSize();
                } else if(src.indexOf(l10n.url.tools.up) > -1) {
                    currentFontSize = currentFontSize == 2 ? 2 : currentFontSize + 1;
                    setFontSize();
                } else if(src.indexOf(l10n.url.tools.down) > -1) {
                    openSendToFriend(e.target.parentNode);
                }
            }
        });
    };
    
    // main content accordions
    var manageAccordions = function(e) {
        e.preventDefault();
        var t = $(this);
        if(t.hasClass('on')) {
            t.next().slideUp(500, function() {
                t.removeClass('on');
            });
        } else {
            t.next().slideDown(500, function() {
                t.addClass('on');
            });
        }
    };
    
    // main content tabs
    var activeTab, activeTabContent;
    
    var manageTabs = function(e) {
        e.preventDefault();
        if(e.target.nodeName.toLowerCase() == 'a') {
            var id = e.target.href.replace(/^[^#]*/, '');
            if(D.getElementById(id.substring(1))) {
                if(activeTab && activeTab.length && activeTabContent && activeTabContent.length) {
                    activeTab.removeClass('on');
                    activeTabContent.removeClass('on');
                }
                activeTab = $(e.target.parentNode).addClass('on');
                activeTabContent = $(id).addClass('on');
            }
        }
    };
    
    $(document).ready(function() {
        $('#query').clearTextFields();
        
        $('#mainNav li.on li').hover(mouseOverNav, mouseOutNav);
        
        createTools();
        
        var fontSize = parseInt($.cookie('fontSize'), 10);
        if(fontSize) {
            currentFontSize = fontSize;
            $('#page').get(0).className = clsFontSize[fontSize];
        }
        
        var accordion = $('#main div.accordion');
        if(accordion.length) {
            var h3 = accordion.find('h3').wrapInner('<a href="#"></a>').click(manageAccordions).each(function(i, el) {
                var t = $(el);
                if(t.hasClass('on')) {
                    t.next().show();
                }
            });
        }
        
        var tabs = $('#main div.tabs');
        if(tabs.length) {
            activeTab = $('#main ul.tabsListing li.on');
            activeTabContent = $('#main div.tabContent.on');
            tabs.find('ul.tabsListing').click(manageTabs);
        }
    });
    
})(jQuery);