(function($) {

    window.site = {

        init: function() {
            
            // init calls that run on page load...

            /* $('.image_popup').fancybox({
                //'titlePosition': 'outside',

                'scrolling': 'no',
                'centerOnScroll': false,
                'autoScale': false,
                'autoDimensions': false,
                'imageScale': 'true',
                'overlayColor': '#f5f5f5',
                'overlayOpacity': 0.60,
                'width': 980,
                'height': 601
            });
            */



            $("a.image_popup").fancybox({
                'overlayShow': true,
                'overlayOpacity': 0.7,
                'overlayColor': '#d9d9d9',
                'imageScale': 'true',
                'zoomOpacity': 'true'
            });










            $('span#filter_by_label').click(function(){
                $(this).toggleClass('active');
               $('div#artists_list').slideToggle('slow');
            });
            
            $('ul.topnav li.active a, .section-art-fairs ul.topnav li#news a').css('opacity',0.5);

            if(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) {
                $("body.section-home #slideshow").css({'height':'960px'});
                $("body.section-home .records_list ul li span.content").css('position', 'static');
            }

            // Slideshow - About Page

            if (h.element_exists('body.section-about div#slideshow')) {
                $('body.section-about div#slideshow').cycle({
                    fx: 'fade',
                    speed:  3000,
                    timeout: 3500
                });
            }
           
            // AJAX function for filtering store items
            
            $('div#artists_list ul li').click(function(){
                
                if( $(this).hasClass('selected') == false ){
                    
                    var pathname = window.location.pathname.toString();
                    var paths = pathname.split('/');
                    var artist_id = $(this).attr('id');
                    var artist_name = $(this).children().text();
                    var store_type = paths[paths.length-2];
                    var proxy_dir = paths[1];
                    var ajaxurl = '/store/ajax/'

                    $.post(ajaxurl, {artist_id:artist_id, artist_name:artist_name, store_type:store_type, proxy_dir:proxy_dir},
                        function(data) {
                            $('div.records_list').fadeOut('fast',
                                function() {
                                $('div.records_list').html(data).fadeIn('fast');
                                
                            });
                            $('.section-store .page_stats').fadeOut('fast');
                    });
                }

                $(this).addClass('selected');

                // remove selected class from other LI elements
                $(this).siblings().removeClass('selected');
                $(this).parent().siblings().children().removeClass('selected');
                
            });
        },

        // add site specific functions here

        artist_list_slideshow: {

            init: function() {


                //if(window.location.pathname.toString().slice(0,3) == '/l/'){
                //    $('.page-artists #artist_list_slideshow ul, .page-artists #artist_list_slideshow ul li').height(735).width(735);
                //}
                //else {
                //    $('.page-artists #artist_list_slideshow ul, .page-artists #artist_list_slideshow ul li').height(530).width(530);
                //}


                if (h.element_exists('#artist_list_slideshow') && !$('#artist_list_slideshow').hasClass('no-slideshow')) {
                    $('#artist_list_slideshow ul').cycle({
                        fx:     'fade',
                        speed:    100,
                        timeout:  7000,
                        pause:   0
                    });
                }
                //.page-artists #artist_list_slideshow li
            }
        },

        about_us_slideshow: function() {
            if (h.element_exists('body.section-about div#slideshow')) {
                if ($('body.section-about div#slideshow img').length == 1) {
                    $('body.section-about div#slideshow img').show();
                }
                else if ($('body.section-about div#slideshow img').length > 1) {
                    $('body.section-about div#slideshow img').hide();
                    $('body.section-about div#slideshow').cycle({
                        fx: 'fade',
                        speed:  3000,
                        timeout: 3500
                    });
                }
                
            }
        },

        // resize the columns on the exhibition pages
        resize_content: function() {
            var tallest_el = 0;
            $('.section-exhibitions .records_list > div').each(function(){
                var current_height = $(this).height();
                if(current_height > tallest_el){
                    tallest_el = current_height;
                }
            });
            $('.section-exhibitions .records_list > div').height(tallest_el);
        },
        
        proxy_dir: function() {
            $(window).resize(function(){
                var window_width = $(window).width();
                var window_threshold = 1380;
                var current_path = window.location.pathname.toString();
                var new_path;
                if (window_width > window_threshold) {
                    if (current_path.slice(0,3) != '/l/') {
                        new_path = '/l' + current_path;
                        window.location.replace(new_path);
                    }
                }
                else {
                    if (current_path.slice(0,3) == '/l/') {
                        new_path = current_path.slice(2);
                    window.location.replace(new_path);
                    }
                }
            }).resize();
        }

    };

    $(document).ready(function() {
        window.site.init();
        //window.site.resize_content();
        window.site.artist_list_slideshow.init();
        window.site.about_us_slideshow();
        window.site.proxy_dir();
    });


})(jQuery);
