(function($) {
    
//    if ($.browser.msie && $.browser.version == '6.0') {
        //$(".scroll-pane").jScrollPane();
        $('#slider').nivoSlider({
            effect:'fade',
            animSpeed:300,
            directionNav:false,
            directionNavHide:true,
            controlNavThumbs:true,
            controlNavThumbsSearch: '/image/',
            controlNavThumbsReplace: '/image/thumb/',
            pauseOnHover:true
        });
//    }
	
    $.fn.getMiId = function() {
        $mi =$(this).attr('id');
        return $mi.substring( $mi.indexOf('_') +1 );
    }

    $.app = {
        init: function() {
            $('.launcher').click(function(e) {
                e.preventDefault();
                $($(this).attr('href') + ' a').lightBox();
                $($(this).attr('href') + ' a:first').click();

            });
            $('.salon-services a').click(function(e) {
                e.preventDefault();


                $('.service-description > div').not($(this).attr('href')).slideUp();
                $($(this).attr('href')).slideToggle();

            });


            $(".scroll-pane").jScrollPane();
            $('#slider').nivoSlider({
                effect:'fade',
                animSpeed:300,
                directionNav:false,
                directionNavHide:true,
                controlNavThumbs:true,
                controlNavThumbsSearch: '/image/',
                controlNavThumbsReplace: '/image/thumb/',
                pauseOnHover:true
            });
//            $('form').submit(function(e) {
//                e.preventDefault();
//
//                $.app.load($(this).attr('action'), $(this).serialize(), {type: 'post'});
//            });
            $(".page-body a[target!=_blank]").click(function(e) {
                e.preventDefault();
                
                $mi = $('#menu .menu-child-list a[href='+$(this).attr('href')+']');
                if ($mi.length > 0) {
                    $mi.click();
                } else {
                    $.app.load($(this).attr('href'));
                }
            });
        },
        load: function(url, params, opts) {
            window.location = url;
            return;
        	if ($.browser.msie) {
        		window.location = url;
        		//alert('No uses internet explorer 6');
        		return;
        	}
        	
            $('#right-bar').addClass('loading');

            $('#loader').fadeOut(function() {

                $.ajax(
                    $.extend({
                        url: url,
                        data: params,
                        success: function(data) {
                            $('#loader').html(data);

                            url = url.replace(/(\.\.\/)* /, '');

                            window.location = '#'+url;

                            $('#loader').fadeIn(function() {
                                $.app.init();
                                $('#right-bar').removeClass('loading');

                            });
                        }
                    }, opts)
                );

            });

        }

    }




    $(function() {

    if (!($.browser.msie && $.browser.version == '6.0')) {
        $('#loader').hide();
    }

    $('#loader').show();
        $('#menu .menu-root > a')
            .mouseover(function(){
                $(this).stop().animate(
                    {backgroundPosition:"-30px 0"},
                    {duration:200});
            })
            .mouseout(function(){
                if ($(this).hasClass('active')) return;
                $(this).stop().animate(
                    {backgroundPosition:"-160px 0"},
                    {duration:200});
            })
            .click(function(e) {
                //e.preventDefault();
                if ($(this).hasClass('active')) return;

                $('.menu-child-list').fadeOut();

                if ($(this).parents('.menu-root').find('.menu-child-list').length == 0) {
//                    $.app.load($(this).attr('href'));
//                    document.title = $(this).attr('title');
                    return;
                }
                $(this).parents('.menu-root').find('.menu-child-list').fadeIn();
                e.preventDefault();

                $('#menu .menu-root > a.active').stop().animate(
                    {backgroundPosition:"-160px 0"},
                    {duration:200}).removeClass('active');

                $('#menu .menu-child-list a.active').stop().animate(
                    {backgroundPosition:"160px 0"},
                    {duration:200}).removeClass('active');

                $(this).addClass('active');
                
                $(this).parents('.menu-root').find('.menu-child-list .default > a').click();
                

            });

        $('#menu .menu-child-list a')
            .mouseover(function(){
                $(this).stop().animate(
                    {backgroundPosition:"0px 0"},
                    {duration:200})
            })
            .mouseout(function(){
                if ($(this).hasClass('active')) return;
                $(this).stop().animate(
                    {backgroundPosition:"160px 0"},
                    {duration:200})
            })
            .click(function(e) {
//                e.preventDefault();
//                if ($(this).hasClass('active')) return;
//
//                $.app.load($(this).attr('href'));
//                document.title = $(this).attr('title');
//
//                $('#menu .menu-child-list a.active').stop().animate(
//                    {backgroundPosition:"160px 0"},
//                    {duration:200}).removeClass('active');
//
//                $(this).stop().animate(
//                    {backgroundPosition:"0px 0"},
//                    {duration:200})
//
//                $(this).addClass('active');

            });
            
		if (!($.browser.msie && $.browser.version == '6.0')) {
	        if(window.location.hash.slice(1)) {
	            url = window.location.hash.slice(1);
	
	            $el = $('#menu .menu-child-list a[href='+url+']');
	
	            if ($el) {
	                if ($el.parents('.menu-root')) $el.parents('.menu-root').find('> a').click();
	                $el.click();
	            }
	        }
	
	        $('#footer a[lang=es]').attr('href', '/es');
	        $('#footer a[lang=ca]').attr('href', '/ca');
	        $('#footer a[lang=en]').attr('href', '/en');
	
	        $('#loader').fadeIn(function() {
	            $.app.init();
	        });
		}
    });
})(jQuery)
