$(document).ready(function(){
    $('.s2_form_widget').hide();

    //-------DISPLAY MORE COMMENTS AND ADD A COMMENT-------//
    $('.bottom-border-wrapper .text-georgia').click(function(){
        id = $(this).attr('rel');
        $('.bottom-border-wrapper .toggle'+id).toggle();
    });
    $('.comment-form-ctn .text-georgia').click(function(){
        id = $(this).attr('rel');
        $('.comment-form-ctn .toggle'+id).toggle();
    });
    $('.more-comments-btn a').click(function(){
        id = $(this).attr('rel');
        if(id == 0){
            $('.comment-hide').toggle();
            $('.a-more-comments').toggle();
        }else{
            $('.comment-hide').each(function(){
                if(id == $(this).attr('rel')){
                    $('.comment-hide'+id).toggle();
                    $('.more'+id).toggle();
                }
            });
        }
    });
    // --------------------------------------------------//


    //Blog list title
    if(current_lng == 'fr')
        $('#related-links-box .top-box').html('<h4 class="text-white text-dinbold caps">Liste de Blogues</h4>');

    //-------Eye Diseases Scroll To-------//
    anchor = window.location.hash;
    if(anchor){
        anchor  = anchor.substring(1,anchor.length);
        $.scrollTo('#ctn-'+anchor, '10');
    }

    $('.list-categories').click(function(){
        $.scrollTo('#ctn-'+this.id, '10');
    });
    $('.top-ankor-link').click(function(){
        $.scrollTo('#breadcrumbs', '10');
    });
    // --------------------------------------------------//


    // ----------------- Quiz Tabs --------------------- //
    $('.tabbed-box .header-tabbed-box a').live('click',function() { // bind click event to link
        $('.tabbed-box .content-tabbed-box').removeClass('selected');
        $('#'+$(this).attr('rel')).addClass('selected');
        $('.tabbed-box .header-tabbed-box .header-btn').removeClass('selected');
        $(this).parent().addClass('selected');
        resize_header_tabbed_box_header_btn();
    });

    function resize_header_tabbed_box_header_btn(){
        // left-bg and right-bg resize
        $('.tabbed-box .header-tabbed-box a .middle-ctn').each(function(){
            $('.left-bg,.right-bg',this).css({height:$(this).find('.middle-content').outerHeight()});
        });
        // Ajustement de la hauteur des header-btn
        $('.tabbed-box .header-tabbed-box .header-btn').each(function(){
            var header_btn_height = $(this).find('.middle-ctn').height()+6;
            var header_tabbed_box_height = $('.tabbed-box .header-tabbed-box').height();
            var top_offset = header_tabbed_box_height - header_btn_height;
            $(this).css({top:top_offset});
        });

    }
    // --------------------------------------------------//


    // ----------------- FAQ TOGGLE --------------------- //
        $('.faq-question-title').click(function(){
            if(this.style.color == '' || this.style.color == 'rgb(0, 102, 142)')
                this.style.color = '#000000';
            else
                this.style.color = '#00668E';
           $('#'+$(this).attr('rel')).toggle();
        });
    // --------------------------------------------------//


    // ----------------- Datepicker --------------------- //
    $('#reminder_self_date').datepicker({
        showOn: "button",
        buttonImage: "http://www.opto.ca/openyoureyes/wp-content/themes/opto/assets/img/reminder/calendar.jpg",
        buttonImageOnly: true,
        dateFormat: 'yy-mm-dd'
    });
    // -------------------------------------------------- //
    // ----------------- Datepicker --------------------- //
    $('#reminder_rel_date').datepicker({
        showOn: "button",
        buttonImage: "http://www.opto.ca/openyoureyes/wp-content/themes/opto/assets/img/reminder/calendar.jpg",
        buttonImageOnly: true,
        dateFormat: 'yy-mm-dd'
    });
    // -------------------------------------------------- //


    $('a').live('click',function(e){
        if($(this).attr('href')=="#")
            e.preventDefault();
    });

    $('#text-size-btn').live('click',function(e){
        if($(this).attr('rel') == '12') {
            $('body').css({'font-size':14});
            $(this).attr('rel','14');
        }
        else if($(this).attr('rel') == '14') {
            $('body').css({'font-size':16});
            $(this).attr('rel','16');
        }
        else if($(this).attr('rel') == '16') {
            $('body').css({'font-size':12});
            $(this).attr('rel','12');
        }
        resize_handle();
    });

    // ----------------- Reminders --------------------- //
    $('.reminder-block a').live('click',function(e){
        if($('.thanks-msg').length > 0){
            $('.thanks-msg').addClass('hide');
        }
        $reminderChoice= this.rel;
        $('#reminder').removeClass();
        $('#reminder').addClass($reminderChoice);
        displayReminder();
    });

    if($('#reminder').exists()) {
        var v = $('input.dearfield').val();
        $('#reminder').removeClass();
        $('#reminder').addClass(v);
        displayReminder();
    }

    function displayReminder(){
        if($('#reminder').hasClass('yearly'))
        {
            $('#block-yearly').removeClass('not-selected-dot-right').addClass('selected');

            $('#block-dear').removeClass('selected').addClass('not-selected-dot-left');

            $('#block-yearly a').removeClass('right-triangle').addClass('bottom-triangle');
            $('#block-dear a').removeClass('bottom-triangle').addClass('right-triangle');

            $('#reminder-yearly-ctn').removeClass('hide');
            $('#reminder-dear-ctn').addClass('hide');
        }
        else if($('#reminder').hasClass('dear'))
        {
            $('#block-yearly').removeClass('selected').addClass('not-selected-dot-right');

            $('#block-dear').removeClass('not-selected-dot-left').addClass('selected');

            $('#block-dear a').removeClass('right-triangle').addClass('bottom-triangle');
            $('#block-yearly a').removeClass('bottom-triangle').addClass('right-triangle');

            $('#reminder-dear-ctn').removeClass('hide');
            $('#reminder-yearly-ctn').addClass('hide');
        }
    }
    // -------------------------------------------------- //

    $('.main-nav > li').mouseenter(function(){
        $(' > .over-background',this).addClass('hover');
        if($(this).find('.submenu-ctn').exists())
            $(this).find('.submenu-ctn').show();
    });

    $('.main-nav > li').mouseleave(function(){
        $(' > .over-background',this).removeClass('hover');
        if($(this).find('.submenu-ctn').exists())
            $(this).find('.submenu-ctn').hide();
    });

    $('input:text').each(function(){
        if($(this).val() == '' ||  $(this).val() == $(this).attr('placeholder')) {
            $(this).val($(this).attr('placeholder')).css('color','#999');
        }
        $(this).click(function(){
            if ($(this).val() == '' ||  $(this).val() == $(this).attr('placeholder')){
                $(this).val('').css('color','#000');
            }
        });
        $(this).blur(function(){
            if ($(this).val() == '' ||  $(this).val() == $(this).attr('placeholder')){
                $(this).val($(this).attr('placeholder')).css('color','#999');
            }
        });
    });

    resize_handle();

    function resize_handle() {
        $('.main-nav > li').each(function(){
            $(this).find('> .over-background').css({width:$(this).width()});
        });
        resize_header_tabbed_box_header_btn();
    }
});


function validate_find_opto(){
    var val     =   $('#input-postal-code').val();
    if(val != 'Your postal code' && val != 'Votre code postal' && val != ''){
        $('#find-opto-form').submit();
    }else{
        return false;
    }
}

function validate_email(){
    var reg             =   new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
    var val             =   $('#subscribe-answer-form #s2email').val();
    if(val == 'Enter email address...' || val == 'Entrez votre courriel...'){
        $('#subscribe-answer-form #s2email').css('background-color', '#FF0000');
        return false;
    }else{
        if(reg.test(val)){
            $('#subscribe-answer-form #s2email').css('background-color', '#fff');
            $('#subscribe-answer-form').submit();
        }else{
            $('#subscribe-answer-form #s2email').css('background-color', '#FF0000');
            return false;
        }
    }
}
