var translations = {
    hide: 'Hide',
    view: 'View',
    open: 'Open'
};
$.fn.slider = function(options) {
    function get(v, d) {
        return typeof v == 'undefined' ? d : v;
    }

    options = get(options, {});

    return this.each(function() {
        var $slider = $(this).find('ul');
        var elementWidth = $slider.find('li:eq(0)').outerWidth();

        var index = get(options.index, 0); // index of active element;
        var destination = get(options.destination, 0); // destination x coordinate of slider list;
        var step = get(options.step, 1); // value added or subtracted to index;
        var ease = get(options.ease, 0.3); // adds easing effect to animation;
        var auto = get(options.auto, 5000); // auto slide interval;
        var stopClick = get(options.stopClick, false); // stop after first interaction;
        var minCount = get(options.minCount, 3); // minimal number of items after which arrows are displayed;

        var autoInterval = undefined;
        var animInterval = undefined;

        var animate = function() {
            if (typeof animInterval == 'undefined')
                animInterval = setInterval(animate, 33);

            var destination = index * elementWidth;
            var x = parseInt($slider.css('margin-left'), 10);

            if (x == destination) {
                clearInterval(animInterval);
                animInterval = undefined;
                return;
            }

            var dx = destination - x;

            x = x + Math.ceil(Math.abs(dx) * ease) * (dx >= 0 ? 1 : -1);

            if (x < -elementWidth) {
                x += elementWidth;
                index++;
                $slider.find('li:first').appendTo($slider);
            } else if (x > 0) {
                x -= elementWidth;
                index--;
                $slider.find('li:last').prependTo($slider);
            }

            $slider.css('margin-left', x + 'px');
        };

        var intervalInit = function() {
            autoInterval = setInterval(function() {
               index -= step;
               animate();
            }, auto);
        };

        if (auto)
            intervalInit();

        var count = $slider.find('li').length;
        if (count > minCount) {
            $('<a class="slider-arrow-left" href="#">left</a>').click(function(e) {
                e.preventDefault();
                index += step;
                animate();
                clearInterval(autoInterval);
                if (auto && !stopClick)
                    intervalInit();
            }).appendTo($(this).parent());

            $('<a class="slider-arrow-right" href="#">right</a>').click(function(e) {
                e.preventDefault();
                index -= step;
                animate();
                clearInterval(autoInterval);
                if (auto && !stopClick)
                    intervalInit();
            }).appendTo($(this).parent());
        }

        $slider.find('li').hover(
            function(e) {
                e.preventDefault();
                clearInterval(autoInterval);
            },
            function(e) {
                e.preventDefault();
                if (!stopClick && auto)
                    intervalInit();
            }
        );
    });
};

//homepage big menu - slider
$('.front .slider').each(function() {
    var $slider = $(this);
    $slider.resetSlider = function() {
        $slider.find('div.default').show().siblings('div').hide();
        $slider.find('ul a').removeClass('active');
    }
    $slider.find('ul a').click(function(e){
        e.preventDefault();
    }).hover(function(e){
        e.preventDefault();
        $(this).addClass('active').parent().siblings('li').find('a').removeClass('active');
        $slider.find('div.' + $(this).parent().attr('class')).show().siblings('div').hide();
    });
    $slider.hover(
        function(){
            
        },function(){
            $slider.resetSlider();
        }
    );
});

//motivation slider
$('.motivation-section .slider, .dashboard .section-goals-slider .slider').slider({
     'index': 0,
     'destination': 0,
     'step': 1,
     'ease': 0.3,
     'auto': 0,
     'stopClick': false,
     'minCount': 5
 });

//canvas reflection
 //$(".motivation-section .slider img").reflect({height: 0.25});
// if ($(".motivation-section .slider img").length)
   //  $(".motivation-section .slider img").reflect({ height: 0.25 });
     


//clean form fields on focus
$('input:text.default, textarea.default').each(function() {
     this.defaultText = this.value;
     $(this)
             .focus(function() {if (this.value == this.defaultText) this.value = '';})
             .blur(function() {if (this.value == '' && this.defaultText) this.value = this.defaultText;});
});

//remove message entry button
//$('.dashboard .messages-section a.remove').click(function(e) {
//    e.preventDefault();
//    $(this).parents('dd').slideUp(300, function(){
//        if (!$(this).parents('.section').find('dd:visible').length) {
//            $(this).parents('.section').find('dd.empty-msg').slideDown(300);
//        }
//        $(this).remove();
//    });
//    });

//autosubmit forms
$('.dashboard .myclients-intro form select, .dashboard .clients-section td.buttons select, .dashboard .clients-section .title select').change(function(){
    $(this).parents('form').submit();
});

//custom select fields
$('select.custom').each(function() {
    var $select = $(this);
    var $newSelect = $('<div class="select"></div>');
    var $selectHeader = $('<p></p>');
    var $options = $select.find('option');
    var $list = $('<ul></ul>');
    var $listContainer = $('<div class="slist"></div>');
    $options.each(function(index) {
        if (index == 0) {
            $selectHeader.text($(this).text());
            $selectHeader.appendTo($newSelect);
        }
        var $newOption = $('<li></li>');
        $newOption.data('value', $(this).attr('value'));
        $newOption.text($(this).text());
        $newOption.appendTo($list);
    });
   $list.appendTo($listContainer);
   $listContainer.appendTo($newSelect);
   $newSelect.insertAfter($select);
   $select.hide();

   $selectHeader.click(function(e) {
       e.stopPropagation();
       var currentState = $listContainer.is(":visible");
       $('.select .slist').hide();
       $listContainer.toggle(!currentState);
   });

   $list.find('li').click(function(e) {
       $selectHeader.text($(this).text());
       $select.val($(this).data('value'));
       $listContainer.hide();
       //$(this).parents('form').submit();
   });
   $('body').click(function(e) {
       $listContainer.hide();
   });
});

//select fix for ie
if ($.browser.msie && $.browser.version == 7) {
    var zIndexNumber = 1000;
    $('.select .slist').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });
    zIndexNumber = 500;
    $('.select').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });
}

// dashboard submenu
$('.dashboard .submenu a').click(function(e) {
    if ($(this).siblings('ul').length) {
        e.preventDefault();
        $(this).siblings('ul').slideToggle('fast', function(){
            $(this).parent().toggleClass('open')
        });
    }
});



////remove goal entry button
//$('.dashboard  .current-goals-section a.remove').click(function(e) {
//    e.preventDefault();
//    $(this).parents('dd').slideUp(300, function() {
//        if (!$(this).parents('.section').find('dd:visible').length) {
//            $(this).parents('.section').find('dd.empty-msg').slideDown(300);
//        }
//        $(this).remove();
//    });

//});


//comments accordion
$('.dashboard  .comments a.button-hide').click(function(e) {
    e.preventDefault();
    var $button = $(this);
    $(this).parents('dd').find('h3').slideToggle();
    $(this).parents('dd').find('.section-content>div').slideToggle(300, function() {
        $button.find('span').text($(this).is(':visible') ? translations.hide : translations.view);
    }).parents('dd').siblings('dd:not(.empty-msg)').find('.section-content>div').slideUp(300, function() {
        $(this).parent().find('h3').show();
        $(this).parents('dd').find('a.button-hide span').text(translations.view);
    });
});


//current goals accordion
$('.dashboard  .current-goals-section a.button-hide').click(function(e) {
    e.preventDefault();
    var $button = $(this);

    //$(this).parents('dd').find('h3').slideToggle(); //Due to requirement to avoid hiding the title while toggling 

    $(this).parents('dd').find('.section-content>div').slideToggle(300, function() {
        $button.find('span').text($(this).is(':visible') ? translations.hide : translations.view);
    }).parents('dd').siblings('dd:not(.empty-msg)').find('.section-content>div').slideUp(300, function() {
        $(this).parent().find('h3').show();
        $(this).parents('dd').find('a.button-hide span').text(translations.view);
    });
});

//for my clients

$('.dashboard  .current-goals-section a.button-hide-clients').click(function(e) {
    e.preventDefault();
    var $button = $(this);
    //$button.className = "edit";

    //$(this).parents('dd').find('h3').slideToggle(); //Due to requirement to avoid hiding the title while toggling

    $(this).parents('dd').find('.section-content>div').slideToggle(300, function() {
        // $button.className = ($(this).is(':visible') ? 'edit button-hide-clients' : 'edit button-hide-clients');
        $button.removeClass(($(this).is(':visible') ? 'clone button-hide-clients' : 'clone2 button-hide-clients'));
        $button.addClass(($(this).is(':visible') ? 'clone2 button-hide-clients' : 'clone button-hide-clients'));
        //$button.find('span').text($(this).is(':visible') ? translations.hide : translations.view);
    }).parents('dd').siblings('dd:not(.empty-msg)').find('.section-content>div').slideUp(300, function() {
    $(this).parent().find('h3').show();
    var $button2 = $(this).parents('dd').find('a.button-hide-clients');
        $button2.removeClass('clone2 button-hide-clients');
        $button2.addClass('clone button-hide-clients');
    });
});

//toogle yellow blocks
$('.block-yellow-medium .block-head a.toggle').click(function(e) {
    e.preventDefault();
    var $button = $(this);
    $(this).parents('.block-yellow-medium').find('.block-content').slideToggle(300, function() {
        $button.text($(this).is(':visible') ? translations.hide : translations.open);
        $(this).parents('.block-yellow-medium').toggleClass('open');
    });
});


//toogle goals in blocks
$('.dashboard .section-goals-list h3 a').click(function(e) {
    e.preventDefault();
    $(this).parents('dd').find('.desc').slideToggle(300);
});



//fix for opera issue with sticky footer
if ($.browser.opera) {
    setTimeout(function() {
        $('body').css('overflow', 'hidden');
        setTimeout(function() {
            $('body').css('overflow', 'auto');
        }, 5);
    }, 1);
}
