var GALLERY_ANIMATION_DELAY = 5000;

function init_front_end_admin() {
        $('.admin-change-link').each(function () {
                        $(this).hover(function () { $(this).addClass("hover"); },
                                                    function () { 
                                                            var el = $(this);
                                                            el.removeClass("hover");
                                                            /* eye candy */
                                                            el.addClass("hover-out");
                                                            setTimeout(function () {
                                                                            el.removeClass("hover-out");
                                                                    }, 500); 
                                                    });
                });
}

function init_autolabels() {
        /// not fully tested port to jquery
        function _set_input_default(input, initial_text) {
                if (!input || (input.value && input.value!=initial_text)) return;
                var is_password_input = input.type == "password";
                                
                function _display_label(input) {
                        input.value = initial_text; 
                        if (is_password_input) input.type = "text";
                        $(input).addClass("autolabel-inactive");
                }
                function _undisplay_label(input) {
                        input.value = ''; 
                        if (is_password_input) input.type = "password";
                        $(input).removeClass("autolabel-inactive");                                                                                                             
                                
                }
                                
                function _on_focus(ev) {
                        if (this.value == initial_text) 
                                _undisplay_label(this);
                        $(this).removeClass("autolabel-inactive"); 
                }
                function _on_blur(ev) {
                        if (this.value == '') { 
                                _display_label(this);
                        }
                }
                                
                $(input).focus(_on_focus);
                $(input).blur(_on_blur);
                _display_label(input);
                function _reset() {
                        if (input.value==initial_text) {
                                input.value = '';
                        }
                }
                $(input.form).submit(_reset);
        }

        $(".autolabel").each(function() {
                        var input = this;
                        var label = input.title;
                        input.title = '';
                        _set_input_default(input, label);
                });
}


function init_gallery() {
        var gallery = $('#gallery-slider');
        if (!gallery) return;
        var bar = $('.scrollbar', gallery);
        if (!bar) return;
        var scrollPane = $('#gallery-slider-container', gallery);
        var scrollContent = $('#gallery', gallery);
        var scrollBar = bar.slider({
            slide:function(e, ui){
                                var sl = Math.round( ui.value / 100 * ( scrollPane.attr("scrollWidth") - scrollPane.width() ));
                                scrollPane.scrollLeft(sl);
                                stop_animation();
                        }
                });

        var main_picture_holder = $('.gallery-main-picture-holder');
        var main_picture = $('#gallery-main-picture');

        if (!main_picture) return;

        var thumbs = $('A.gallery-thumbnail', gallery);
        
        var timer_animation;
        
        function stop_animation() {
            clearTimeout(timer_animation);
        }
        
        function animation() { 
            timer_animation = setTimeout(function() { _next_main_picture(main_picture); animation();} , GALLERY_ANIMATION_DELAY )
        };

        function _get_index(a) {
            for (var i=0;i<thumbs.length;i++) {
                //console.log(i, thumbs[i].href, main_picture[0].src, thumbs[i].href == main_picture.src);
                if ((thumbs[i].href) == (main_picture[0].src))
                return i;
            }
        }
        
        function _next_main_picture(main_picture) {
                var i = _get_index(main_picture);
                var newi = (i+1)%thumbs.length;
                var val = (newi*100/(thumbs.length-1));
                _change_main_picture(thumbs[newi]);
                bar.slider("value", val);
                scrollPane.scrollLeft(Math.round( val / 100 * ( scrollPane.attr("scrollWidth") - scrollPane.width() )));
        }
        
        function _init_main_picture(main_picture) {
            
                main_picture.click(function () {
                                _next_main_picture(this);
                                stop_animation();
                        });
                main_picture.css("cursor", "pointer");
                main_picture.attr("title", "Cliquer pour passer à la photo suivante");
        }

        function _change_main_picture(thumb) {
            var img = new Image();
            img.src = thumb.href;
            main_picture.fadeOut(500, function () { this.src = ''; $(this).remove();});
            main_picture.css("position", "absolute");
            main_picture_holder.css({'height':main_picture.attr("height")});
            $(img).load(function () {
                            $(img).css("position", "absolute");
                            main_picture_holder.append(img);
                            $(img).hide();
                            $(img).fadeIn(2000);
                            $(main_picture_holder).animate({'height':img.height, 'width':650});
                            main_picture = $(img);
                            _init_main_picture(main_picture);
                    });
        }

        _init_main_picture(main_picture);
        
        thumbs.click(function() {
                        _change_main_picture(this);
                        stop_animation();
                        return false;
                });
        animation();
    
}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

    function _ga_track(page) {
        try {
            _gat._getTracker("UA-18165556-1")._trackPageview(page);
        }
        catch(err){}
    }

$(document).ready(function(){
                init_front_end_admin();
                init_autolabels();
                init_gallery();
		$('select.selectbox').selectbox({effect: "fade"});


        //quote mechanism
        var is_sent_page = window.location.href.search("quote_sent")>-1
        if ($('._quote-container').css("display")=="block" && is_sent_page) {
            _ga_track("/quote/sent/");
        }
		$('._quote-toggle').click(function () { 
                $('._quote-container').toggle();
                $('._quote-center').center();
                if (!is_sent_page) {
                    if ($('._quote-container').css("display")=="block") {
                        _ga_track("/quote/show/");
                    } else {
                        _ga_track("/quote/close/");
                    }
                }
            });
        });


