var main =
{
    init: function()
    {
        // Home - Slider
        if ( $('#header #slider-wrapper').length )
        {
            $('#header #slider-content ul').bxSlider({
                auto: true,
                controls: false,
                pager: true,
                pageType: 'full'
            });
        }
        
        // Tooltips
        if ( $('#content .tooltip').length )
        {
            $('#content .tooltip').each(function() {
                var position_my = $(this).dataset('tooltip-position-my');
                var position_at = $(this).dataset('tooltip-position-at');
                
                $('#content .tooltip').qtip({
                    style: {
                        classes: 'ui-tooltip-rounded ui-tooltip-shadow'
                    },
                    position: {
                        my: position_my,
                        at: position_at,
                        adjust: {
                            x: 10
                        }
                    }
                });
            });
        }
        
        // Fancybox
        if ( $('.fancybox').length )
        {
            $('.fancybox').fancybox({
                'titleShow': true
            });
        }
        
        // Fancybox - popup notice
        if ( $('#popup-notice').length )
        {
            $('#popup-notice').fancybox({
                'width': 640,
                'height': 440,
                'autoDimensions': true,
                'showCloseButton': false,
                'centerOnScroll': true,
                'overlayColor': '#7f7f7f',
                'content': $('#popup-notice-content').html()
            }).trigger('click');
        }
        
        // Footer banners - opacity
        if ( $('#brands img').length )
        {
            $('#brands img').css('opacity', '0.6');
        }
        
        // 
//        // Desaturate footer banners
//        $('#brands img').css('opacity', '0.6').load(function() {
//            initImage(this);
//        });
        
        // Expandable texts
        if ( $('.expandable-text').length )
        {
            $('.expandable-text').each(function() {
                var slicePoint = $(this).data('slice-point');
                
                $(this).expander({
                    slicePoint: slicePoint
                });
            });
        }
        
        // Register: if doctor_id is selected (after POST) - put this value into the fake select
        var register_form = $('body.home-register #content #main #register');
        
        if (register_form.length && register_form.dataset('post') == 'yes') {
            var doctor_id = register_form.find('select[name=doctor_id] option:selected').val();
            var doctor_name = register_form.find('select[name=doctor_id] option[value='+doctor_id+']:first').text();
            register_form.find('.select span').text(doctor_name);
        }
        
        // Register: show tooltip when hover on input/select
        if (register_form.length) {
            $('body.home-register #content #main input, body.home-register #content #main .select').hover(
                function() {
                    $(this).next('a').trigger('mouseover');
                },
                function() {
                    $(this).next('a').trigger('mouseout');
                }
            );
        }
        
        // Subnagivation - close button
        $('#nav .box .content').each(function() {
            $(this).find('.title:first').append('<a class="close" href="#" title="Zamknij">[x]</a>');
        });
        
        main.events();
    },
    
    events: function()
    {
        // Show/hide nav box when hover on a menu item
        $('#nav > li').hover(
            function() {
                main.showNavBox( $(this) );
            },
            function() {
                main.hideNavBox( $(this) );
            }
        );
            
        // Hide nav box when click on close button
        $('#nav .box .close').click(function() {
            var parent = $(this).closest('li');
            
            main.hideNavBox(parent);
            parent.find('a').removeClass('hover');
            
            return false;
        });
        
        // Close notice
        $('.notice .close').click(function() {
            $(this).parent().fadeOut(800);
            return false;
        });
        
        // Scroll to the top
        $('a.scroll-top').click(function() {
            $('html').scrollTo('0px', 500);
            return false;
        });
        
        // Work schedule table
        if ( $('body.hospital-work_schedule .staff').length )
        {
            $('body.hospital-work_schedule .staff table').hide();
            
            $('body.hospital-work_schedule .staff .member a').click(function() {
                $(this).next('table').toggle();
                
                return false;
            });
        }
        
        // Register: doctor_id select (hover)
        $('body.home-register #content #main .select select').hover(
            function() {
                $(this).prev('span').addClass('focus');
            },
            function() {
                $(this).prev('span').removeClass('focus');
            }
        );
            
        // Register: doctor_id select (change)
        $('body.home-register #content #main .select select').change(function() {
            var doctor_id = $(this).val();
            var doctor_name = $(this).find('option[value='+doctor_id+']:first').text();
            $(this).prev('span').text(doctor_name);
        });
        
        // Clear input's value on focus
        $('.clear-on-focus').clear_on_focus();
        
        // Hospital Pricelist: expand categories
        if ( $('body.hospital-pricelist #content #main .categories').length ) {
            $('body.hospital-pricelist #content #main .categories a.name').click(function() {
                $(this).next('table').toggle();
                
                if ( $(this).next('table').is(':visible') )
                    $(this).closest('li').addClass('expanded');
                else
                    $(this).closest('li').removeClass('expanded');
                
                return false;
            });
        }
        
        // Show color vesion of footer banner on hover
        $('#brands img').hover(
            function() {
                var src = $(this).attr('src');
                var new_src = src.replace(/_bw/, '');
                $(this).attr('src', new_src);
                $(this).css('opacity', '1');
            },
            function() {
                var src = $(this).attr('src');
                var new_src = src.replace(/.png/, '_bw.png');
                $(this).attr('src', new_src);
                $(this).css('opacity', '0.6');
            }
        );
    },
    
    showNavBox: function(element)
    {
        element.closest('li').find('a').addClass('hover');
        element.find('.box').addClass('visible');        
    },
    
    hideNavBox: function(element)
    {
        element.closest('li').find('a').removeClass('hover');
        element.find('.box').removeClass('visible');
    },
    
    hideAllNavBoxes: function()
    {
        $('#nav .box').hide();
    }
}

$(function() {
    main.init();
})

//// Image desaturation
//var paircount = 0;
//
//function initImage(obj)
//{
//    obj.onload = null;
//    
//    var $newthis = $(obj);
//    
//    if ($.browser.msie)
//    {
//        $newthis = $newthis.desaturateImgFix();
//    }
//    
//    $newthis.addClass("pair_" + ++paircount);
//    
//    var $cloned = $newthis.clone().attr('id', '');
//    
//    $cloned.get(0).onload = null;
//    $cloned.insertAfter($newthis).addClass("color").hide();
//    $newthis = $newthis.desaturate();
//    $newthis.bind("mouseenter mouseleave", desevent)
//    $cloned.bind("mouseenter mouseleave", desevent);
//};
//
//function desevent(event) 
//{
//    var classString = new String($(this).attr('class'));
//    var pair = classString.match(/pair_\d+/);
//    $("."+pair).hide();
//    
//    if (event.type == 'mouseenter')
//        $("."+pair).css('opacity', 1).filter(".color").show();
//    
//    if (event.type == 'mouseleave')
//        $("."+pair).css('opacity', '0.6').filter(":not(.color)").show();
//}

// Clear input's value on focus
$.fn.clear_on_focus = function() {
    return this.focus(function() {
        if ( this.value == this.defaultValue ) {
            this.value = "";
            this.className += " filled";
        }
    }).blur(function() {
        if ( ! this.value.length ) {
            this.value = this.defaultValue;
        }
    });
}
