(function($) {

    window.app = {
        checkEmail: {
            check: function(which) {

                if ($(which).val() != "") {
                    var str=$(which).val();
                    var at="@";
                    var dot=".";
                    var lat=str.indexOf(at);
                    var lstr=str.length;
                    var ldot=str.indexOf(dot);
                    
                    if (str.indexOf(at)==-1){
                       return false;
                    }

                    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
                       return false;
                    }

                    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
                        return false;
                    }

                    if (str.indexOf(at,(lat+1))!=-1){
                        return false;
                    }

                    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
                        return false;
                    }

                    if (str.indexOf(dot,(lat+2))==-1){
                        return false;
                    }

                    if (str.indexOf(" ")!=-1){
                        return false;
                    }
                    
                    return true;
                }

                else {
                    return false;
                }
            }
        }
    }

    window.galleries = {

        init: function() {
            // add any init calls here to run on page load...

        },

        // add functions here...

        slideshow: {

            init: function() {

                if (h.element_exists('#slideshow')) {
                    $('#slideshow ul').cycle({
                        fx:     'fade',
                        speed:    1200,
                        timeout:  7000,
                        pause:   0
                    });
                    $('#slideshow.paused')
                        .each(function () {
                            pausePlay();
                        })
                    ;
                }
            }

        },

        artist_list_slideshow: {

            init: function() {
                if (h.element_exists('#artist_list_slideshow') && !$('#artist_list_slideshow').hasClass('no-slideshow')) {
                    $('#artist_list_slideshow ul').cycle({
                        fx:     'fade',
                        speed:    400,
                        timeout:  7000,
                        pause:   0,
                        before: function(cSlide, nSlide, options) {
                            $('#artist_list_slideshow_nav a').removeClass('active');
                            link_id = nSlide.id.replace('artist_list_slideshow_', 'artist_list_');
                            $('#artist_list_slideshow_nav #' + link_id).addClass('active');
                        },
                        after: function(cSlide, nSlide, options) {
                            link_id = nSlide.id.replace('artist_list_slideshow_', 'artist_list_');
                            $('#artist_list_slideshow_nav #' + link_id).addClass('active');
                        }
                    });

                    if ($('#artist_list_slideshow.slideshow_pause').size() > 0) {
                        $('#artist_list_slideshow ul').cycle('pause');
                    }

                    $('#artist_list_slideshow_nav a').mouseover(function () {
                        $('#artist_list_slideshow ul').cycle('pause');
                        $('#artist_list_slideshow ul').cycle(parseInt($(this).attr('id').split('artist_list_item')[1] - 1));
                        return false;
                    });

                }

            }

        },

        image_gallery: {

            init: function() {

                var first_load = true

                if (h.element_exists('#ig_slideshow')) {
                    
                    $('#ig_slideshow')
                        .cycle({
                            fx:       'fade',
                            speed:    1200,
                            timeout:  12000,
                            pause:    0,
                            before:   function(currSlideElement, nextSlideElement, options, forwardFlag) {
                                $('#ig_slideshow_caption').html($(this).attr('rel').replace(/\n/g,''));
                                if ($('#ig_slideshow_controller_count').size() > 0) {
                                    $('#ig_slideshow_controller_count').html(options.nextSlide + 1 + ' of ' + options.slideCount);
                                }
                            },
                            after:   function(currSlideElement, nextSlideElement, options, forwardFlag) {
                                if ($('#ig_slideshow_controller_count').size() > 0) {
                                    $('#ig_slideshow_controller_count').html(options.currSlide + 1 + ' of ' + options.slideCount);
                                }
                            }
                        })
                        .each(function () {
                            if ($('.artwork_video_link', this).size() > 0) {
                                var artwork_video_object = $('.artwork_video_object', this);
                                $('.artwork_video_link', this).click(function () {
                                    $(this).hide();
                                    artwork_video_object.html($(this).attr('rel'));
                                    return false;
                                });
                            }
                        });

                    $('#ig_slideshow_thumbnails a').click(function () {
                        $('#ig_slideshow .artwork_video_object').html('');
                        $('#ig_slideshow .artwork_video_link').show();
                        $('#ig_slideshow').cycle('pause');
                        if ($(this).attr('rel')) {
                            $('#ig_slideshow').cycle(parseInt($(this).attr('rel')));
                        }
                        return false;
                    });

                    $('#ig_slideshow_thumbnails').cycle({
                        fx:      'scrollHorz',
                        speed:    500,
                        timeout:  1000
                    });
                    $('#ig_slideshow_thumbnails').cycle('pause');
                    $('#ig_slideshow_thumbnails_prev a', window.parent.document).click(function () {
                        $('#ig_slideshow_thumbnails').cycle('prev');
                        return false;
                    });
                    $('#ig_slideshow_thumbnails_next a', window.parent.document).click(function () {
                        $('#ig_slideshow_thumbnails').cycle('next');
                        return false;
                    });

                    $('#ig_slideshow_controller').each(function() {
                        $('#ig_slideshow_controller_prev a').click(function() {
                            $('#ig_slideshow').cycle('pause').cycle('prev');
                            return false;
                        });
                        $('#ig_slideshow_controller_next a').click(function() {
                            $('#ig_slideshow').cycle('pause').cycle('next');
                            return false;
                        });
                    });

                }

            }

        },

        image_popup: {

            init: function() {

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

            }

        },

        quicksearch: {

           init: function() {

                $('#quicksearch_field').click(function() {
                    if ($('#quicksearch_field').val() == 'Search...') {
                        $('#quicksearch_field').val('');
                    }
                    $('#quicksearch_field').addClass('active');
                });
    
                $('#quicksearch_btn').click(function() {
                    if (!$('#quicksearch_field').val()) {
                        h.alert('You have not entered a search term!');
                        $('#quicksearch_field').select();
                    } else {
                        $('#quicksearch_form')[0].submit()
                    }
                });

           }

        },

        publications: {
            init: function() {
                //$(".publications_show_samples").click(function() {
                    $("a.fancybox_gallery").fancybox();
                //    $("a.fancybox_gallery#sample_image_1").click();
                //})
            }
        },

        artist: {
            init: function() {
                window.galleries.artist.enquire.init();
            },
            
            enquire: {
                init: function() {
                    if ($('#artist_enquire_form.errorOccurred').size() != 0) {
                            h.alert('Error: Some of the information entered was missing or incorrect.');
                    } else if ($('#artist_enquire_form.captchaError').size() != 0) {
                            h.alert('Error: The text entered did not match the image. Please try again.');
                    }
                },

                check_form: function() {
                        if ($('#f_name').val()=='') {
                                h.alert('Please enter your name.');
                                return false
                        } else if (window.app.checkEmail.check('#f_email') == false) {
                                h.alert('Please enter a valid email address.');
                                return false
                        } else if ($('#f_message').val()=='') {
                                h.alert('Please enter a message.');
                                return false
                        } else if ($('#captcha_answer').val()=='') {
                                h.alert('Ooops! The text entered did not match the image. Please try again.');
                                return false
                        }

                        return true;

                },

                submit: function() {

                    if (window.galleries.artist.enquire.check_form()) {
                        document.artist_enquire_form.submit();
                    }

                }
            }
        },

        contact_form: {

            init: function(which) {
                    $('#contact_form .link a').click(function () {
                            if (window.galleries.contact_form.doOnSubmit()) {
                                    $('#contact_form').submit();
                            }
                            return false;
                    });

                    if ($('#contact_form.errorOccurred').size() != 0) {
                            window.location = '#contact_form';
                            h.alert('Please fill in the required information.');
                    } else if ($('#contact_form.captchaError').size() != 0) {
                            window.location = '#contact_form';
                            h.alert('The verification text did not match the image.');
                    }
            },

            doOnSubmit: function() {

                    if ($('#f_name').val()=='') {
                            h.alert('Please enter your name.');
                            return false
                    } else if ($('#f_email').val()=='') {
                            h.alert('Please enter your email address.');
                            return false
                    } else if ($('#f_email').val()!='' && window.app.checkEmail.check('#f_email') == false) {
                            h.alert('Please enter a valid email address.');
                            return false
                    } else if ($('#f_phone').val()=='') {
                            h.alert('Please enter your phone number.');
                            return false
                    } else if ($('#f_occupation').val()=='') {
                            h.alert('Please enter your occupation.');
                            return false
                    } else if ($('#f_organisation').val()=='') {
                            h.alert('Please enter your occupation.');
                            return false
                    } else if ($('#f_address').val()=='') {
                            h.alert('Please enter your address.');
                            return false
                    } else if ($('#f_message').val()=='') {
                            h.alert('Please enter your message.');
                            return false
                    }

                    return true;

            }

        }


    };

    $(document).ready(function() {

        window.galleries.init();
        window.galleries.slideshow.init();
        window.galleries.image_gallery.init();
        window.galleries.artist_list_slideshow.init();
        window.galleries.image_popup.init();
        window.galleries.publications.init();
        window.galleries.quicksearch.init();
        window.galleries.artist.init();
        window.galleries.contact_form.init();

        // Fix for IE6-7 image link problem..
        // Image links in record lists are unclickable in IE6/7
        if (navigator.userAgent.indexOf('MSIE 7') > -1 || navigator.userAgent.indexOf('MSIE 6') > -1) {
            
            $('div.records_list a span.image img').click(function() {
                var parent_a = $(this).parents('a');
                if (!parent_a.hasClass('image_popup')) {
                    window.location.href = parent_a.attr('href');
                }
                
            })

        }

    });


})(jQuery);


