$(function(){
    if($('.lightbox .container')){
        $('.lightbox .container').customScroll({
            lineWidth: 10
        });
        initLightHover();
    }
});

function initLightHover(){
    $('.button > a').mouseenter(function(){
        $(this).find('em').fadeOut();
    }).mouseleave(function(){
        $(this).find('em').fadeIn();
    });

}

/*--- mouse wheel function ---*/
(function($) {
var types = ['DOMMouseScroll', 'mousewheel'];
$.event.special.mousewheel = {
    setup: function() {
        if ( this.addEventListener )
            for ( var i=types.length; i; )
                this.addEventListener( types[--i], handler, false );
        else
            this.onmousewheel = handler;
    },
    teardown: function() {
        if ( this.removeEventListener )
            for ( var i=types.length; i; )
                this.removeEventListener( types[--i], handler, false );
        else
            this.onmousewheel = null;
    }
};
$.fn.extend({
    mousewheel: function(fn) {
        return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
    },
    unmousewheel: function(fn) {
        return this.unbind("mousewheel", fn);
    }
});
function handler(event) {
    var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
    event = $.event.fix(event || window.event);
    event.type = "mousewheel";
    if ( event.wheelDelta ) delta = event.wheelDelta/120;
    if ( event.detail     ) delta = -event.detail/3;
    // Add events and delta to the front of the arguments
    args.unshift(event, delta);
    return $.event.handle.apply(this, args);
}
})(jQuery);
/*--- function custom scroll ---*/
jQuery.fn.customScroll = function(_options) {
var _options = jQuery.extend({
    lineWidth: 10
}, _options);
return this.each(function(){
    var _box = jQuery(this);
    if(_box.is(':visible')){
        if(_box.children('.scroll-content').length == 0){
            var line_w = _options.lineWidth;
            /*--- init part ---*/
            var scrollBar = jQuery('<div class="scroll-bar"><div class="scroll-up"></div><div class="scroll-line"><div class="scroll-slider"><div></div></div></div><div class="scroll-down"></div></div>');
            _box.wrapInner('<div class="scroll-content"></div>').append(scrollBar);
            var scrollContent = _box.children('.scroll-content');
            var scrollSlider = scrollBar.find('.scroll-slider');
            var scrollSliderH = scrollSlider.parent();
            var scrollUp = scrollBar.find('.scroll-up');
            var scrollDown = scrollBar.find('.scroll-down');
            /*--- different variables ---*/
            var box_h = _box.height();
            var box_w = _box.width();
            var slider_h = 0;
            var slider_f = 0;
            var cont_h = scrollContent.height();
            var _f = false;
            var _f1 = false;
            var _f2 = true;
            var _t1, _t2, _s1, _s2;
            /*--- set styles ---*/
            _box.css({
                position: 'relative',
                overflow: 'hidden',
                width: box_w,
                height: box_h
            });
            scrollContent.css({
                position: 'absolute',
                top: 0,
                left: line_w,
                zIndex: 1,
                width: box_w - line_w,
                height: 'auto'
            });
            scrollBar.css({
                position: 'absolute',
                top: 0,
                left: 0, //box_w - line_w,
                zIndex:2,
                width: line_w,
                height: box_h,
                overflow: 'hidden'
            });
            scrollUp.css({
                width: line_w,
                height: line_w,
                overflow: 'hidden',
                cursor: 'pointer'
            });
            scrollDown.css({
                width: line_w,
                height: line_w,
                overflow: 'hidden',
                cursor: 'pointer'
            });
            slider_h = scrollBar.height();
            if(scrollUp.is(':visible')) slider_h -= scrollUp.height();
            if(scrollDown.is(':visible')) slider_h -= scrollDown.height();
            scrollSliderH.css({
                position: 'relative',
                width: line_w,
                height: slider_h,
                overflow: 'hidden'
            });
            slider_h = 0;
            scrollSlider.css({
                position: 'absolute',
                top: 0,
                left: 0,
                width: line_w,
                height: slider_h,
                overflow: 'hidden',
                cursor: 'pointer'
            });
            box_h = _box.height();
            cont_h = scrollContent.height();
            if(box_h < cont_h){
                _f = true;
                slider_h = Math.round(box_h/cont_h*scrollSliderH.height());
                if(slider_h < 5) slider_h = 5;
                scrollSlider.height(slider_h).find('> .center').height(slider_h-8);
                slider_h = scrollSlider.height();
                slider_f = (cont_h - box_h)/(scrollSliderH.height() - scrollSlider.height());
                _s1 = (scrollSliderH.height() - scrollSlider.height())/20;
                _s2 = (scrollSliderH.height() - scrollSlider.height())/3;
            }
            else{
                _f = false;
                scrollBar.hide();
                scrollContent.css({width: _box.width(), top: 0, left:0});
            }
            var _top = 0;
            /*--- element's events ---*/
            scrollUp.mousedown(function(){
                _top -= _s1;
                scrollCont();
                _t1 = setTimeout(function(){
                    _t2 = setInterval(function(){
                        _top -= 4/slider_f;
                        scrollCont();
                    }, 20);
                }, 500);
            }).mouseup(function(){
                if(_t1) clearTimeout(_t1);
                if(_t2) clearInterval(_t2);
            }).mouseleave(function(){
                if(_t1) clearTimeout(_t1);
                if(_t2) clearInterval(_t2);
            });
            scrollDown.mousedown(function(){
                _top += _s1;
                scrollCont();
                _t1 = setTimeout(function(){
                    _t2 = setInterval(function(){
                        _top += 4/slider_f;
                        scrollCont();
                    }, 20);
                }, 500);
            }).mouseup(function(){
                if(_t1) clearTimeout(_t1);
                if(_t2) clearInterval(_t2);
            }).mouseleave(function(){
                if(_t1) clearTimeout(_t1);
                if(_t2) clearInterval(_t2);
            });
            scrollSliderH.click(function(e){
                if(_f2){
                    if(scrollSlider.offset().top + slider_h < e.pageY){
                        _top += _s2;
                    }
                    else if(scrollSlider.offset().top > e.pageY){
                        _top -= _s2;
                    }
                    scrollCont();
                }
                else{
                    _f2 = true;
                }
            });
            var t_y = 0;
            scrollSlider.mousedown(function(e){
                _box.css({'-moz-user-select':'none', '-khtml-user-select': 'none', 'user-select':'none'});
                t_y = e.pageY - $(this).position().top;
                _f1 = true;
            }).mouseup(function(){
                _f1 = false;
                _box.css({'-moz-user-select':'', '-khtml-user-select': '', 'user-select':''});
            });
            $('body').mousemove(function(e){
                if(_f1){
                     _f2 = false;
                     _top = e.pageY - t_y;
                     scrollCont();
                }
            }).mouseup(function(){
                _f1 = false;
                _box.css({'-moz-user-select':'', '-khtml-user-select': '', 'user-select':''});
            });
            document.body.onselectstart = function(){if(_f1) return false;}
            _box.bind('mousewheel', function(event, delta){
                if(_f){
                    _top -=delta*_s1;
                    scrollCont();
                    if((_top > 0) && (_top+slider_h < scrollSliderH.height())) return false;
                }
            });
            function scrollCont(){
                if(_top < 0) _top = 0;
                else if(_top+slider_h > scrollSliderH.height()) _top = scrollSliderH.height() - slider_h;
                scrollSlider.css('top', _top);
                scrollContent.css('top', -_top*slider_f);
            }
            this.scrollResize = function(){
                box_h = _box.height();
                cont_h = scrollContent.height();
                if(box_h < cont_h){
                    _f = true;
                    scrollBar.show();
                    scrollContent.width(box_w - line_w);
                    slider_h = Math.round(box_h/cont_h*scrollSliderH.height());
                    if(slider_h < 5) slider_h = 5;
                    scrollSlider.height(slider_h);
                    slider_h = scrollSlider.height();
                    slider_f = (cont_h - box_h)/(scrollSliderH.height() - scrollSlider.height());
                    if(cont_h + scrollContent.position().top < box_h) scrollContent.css('top', -(cont_h - box_h));
                    _top = - scrollContent.position().top/slider_f;
                    scrollSlider.css('top', _top);
                    _s1 = (scrollSliderH.height() - scrollSlider.height())/20;
                    _s2 = (scrollSliderH.height() - scrollSlider.height())/3;
                }
                else{
                    _f = false;
                    scrollBar.hide();
                    scrollContent.css({width: _box.width(), top: 0, left:0});
                }
            }
            /*
            setInterval(function(){
                if(_box.is(':visible') && cont_h != scrollContent.height()) _box.get(0).scrollResize();
            }, 200);
            */
        }
        else{
            this.scrollResize();
        }
    }
});
}

function showHideFieldText(field, text)
{
        var shField = $(field);
        if (shField) {
            if (shField.attr("value") === "") {
                shField.attr("value", text);
            }
            shField.focus(function() {
                    if($(this).attr("value") == text) $(this).attr("value", "");
            });
            shField.blur(function() {
                    if($(this).attr("value") == "") $(this).attr("value", text);
            });
        }
}

jQuery(function($){

        showHideFieldText("#searchkeywords", "Search Keyword(s)");
        showHideFieldText("#searchkeywords-header", "Search Keyword(s)");
        showHideFieldText("input[value=Enter your email address]",
            "Enter your email address");

        // Set initial state for sidebar widgets
        $("#sidebar ul.bar li.initial-open").addClass('active');
        $("#sidebar ul.bar li.initial-closed").removeClass('active');
        if (logged_in) {
            $('#sidebar ul.bar li.initial-closed-new').addClass('active');
            $('#sidebar ul.bar li.initial-open-new').removeClass('active');
        } else {
            $('#sidebar ul.bar li.initial-closed-new').removeClass('active');
            $('#sidebar ul.bar li.initial-open-new').addClass('active');
        }

        // Expand/collapse for the sidebar widgets
        $("#sidebar ul.bar>li>a").click(function (e) {
            e.preventDefault();
            var clicked_li = $(this).parent();
            if (clicked_li.hasClass('active')) {
                clicked_li.children('.block').slideUp('fast',
                    function() {
                        clicked_li.removeClass("active");
                    });
            } else {
                clicked_li.children('.block').slideDown('fast',
                    function() {
                        clicked_li.addClass("active");
                    });
            }
        });


        // Expand/collapse for sidebar menu
        $("#sidebar ul.navbar li").each(function() {
            if ($(this).find('li').length > 1) {
                if ($(this).find('.active').length||$(this).hasClass('active')) {
                    $(this).prepend('<a class="nav-close" href="#">Close</a>');
                } else {
                    $(this).children('ul').hide();
                    $(this).prepend('<a class="nav-open" href="#">Open</a>');
                }
            }
        });
        $('#sidebar a.nav-close').live('click', function(e) {
            e.preventDefault();
            if (!$.browser.msie) { //IE doesn't do the sliding correctly.
                $(this).siblings('ul').slideUp();
            } else {
                $(this).siblings('ul').fadeOut();
            }
            $(this).text('Open').removeClass('nav-close').addClass('nav-open');
        });
        $('#sidebar a.nav-open').live('click', function(e) {
            e.preventDefault();
            if (!$.browser.msie) { //IE doesn't do the sliding correctly.
                $(this).siblings('ul').slideDown();
            } else {
                $(this).siblings('ul').fadeIn();
            }
            $(this).text('Close').removeClass('nav-open').addClass('nav-close');
        });

});

// Some logic to make the check boxes in the search box work.
jQuery(function($) {
    function update_text_bar() {
        var models = $('form.search input[type="checkbox"][value!="Entire Site"][checked]');
        if (models.length) {
            var names = $.map(models, function(item, index) {
                return $(item).attr('value');
            });
            $("#model-text").text(names.join(', '));
        } else {
            $("#model-text").text('Entire Site');
        }
    };
    $('form.search input[type="checkbox"][value="Entire Site"]').click(
        function(e) {
            if ($(this).attr('checked')) {
                $('form.search input[type="checkbox"][value!="Entire Site"]'
                    ).removeAttr('checked');
            } else {
                e.preventDefault();
            }
            update_text_bar();
        }
    );
    $('form.search input[type="checkbox"][value!="Entire Site"]').click(
        function(e) {
            if ($(this).attr('checked')) {
                $('form.search input[type="checkbox"][value="Entire Site"]'
                    ).removeAttr('checked')
                var model_options_checked = $('form.search input[type="checkbox"][value!="Entire Site"][checked]');
                var all_model_options = $('form.search input[type="checkbox"][value!="Entire Site"]');
                if (model_options_checked.length == all_model_options.length) {
                    $('form.search input[type="checkbox"]').removeAttr(
                        'checked');
                    $('form.search input[type="checkbox"][value="Entire Site"]').attr('checked', 'checked');
                }
            } else {
                if (!$('form.search input[type="checkbox"][value!="Entire Site"][checked]').length) {
                    $('form.search input[type="checkbox"][value="Entire Site"]'
                        ).attr('checked', 'checked');
                }
            }
            update_text_bar();
        }
    );
});

// Top/bottom sliding link panels
jQuery(function($) {
    $('div.add-box a.add-box-button').click(function(e) {
        e.preventDefault();
        $(this).parents('div.add-box').children('div.panel-block'
            ).slideToggle(function() {
                $(this).parent().toggleClass('open');
            }
        );
    });
});

