/***************************************
*
* functions for home page image slider
* @author Mike Johnson
*
***************************************/

$(document).ready(function () {
    var items = $('.itemScroller');
    var scrollerInited = false;
    //items.css({ 'opacity': '0'});
    var firstImage = $(".itemScroller .slideWrap a:first-child img");
    
    firstImage.load(function () {
        if (!scrollerInited) {
            scrollerInited = true;
            for (var i = 0; i < items.length; i++) {
                var scroll = new itemScroller(items[i]);
                scroll.init();
            };
        }
        //items.css({ 'opacity': '1'});
    });
    firstImage.attr("src", firstImage.attr("src"));
    //    var items = $('.itemScrollerHorizontal');
    //    items.css({ 'opacity': '0' });
    //    setTimeout(function () {
    //        for (var i = 0; i < items.length; i++) {
    //            var scroll = new itemScrollerHorizontal(items[i]);
    //            scroll.init();
    //        };
    //        items.css({ 'opacity': '1' });
    //    }, 500);
});



function itemScroller(item) {

    this.content = item;

    this.init = function init() {

        var margin = 15;
        var items = $(this.content).find('.slideWrap a');
        var itemWidth = $(items).find('img').outerWidth(true) + margin;
        var itemHolder = $(this.content).find('.slideWrap');
        var extra = items.length * itemWidth - $(itemHolder).width();
        var maxHeight = 0;
        var height = $(items).outerHeight(true);

        items.each(function () {
            height = $(this).height();
            maxHeight = (height > maxHeight) ? height : maxHeight;
        });

        $(itemHolder).css({ 'height': maxHeight });

        for (var i = 0; i < items.length; i++) {
            $(items[i]).css({ "position": "absolute", "left": i * itemWidth });
        }

        if (extra > 0) {

            $(this.content).prepend('<a href="#" class="scrollerLeft"></a>')
                           .append('<a href="#" class="scrollerRight"></a><div class="clear"></div>');
            $(this.content).find('.scrollerLeft, .scrollerRight').css({ 'float': 'left', 'height': $(items).outerHeight(true) });
            $(itemHolder).css({ 'float': 'left' });

            $(this.content).find('.scrollerRight').click(function (e) {

                var slideDist = parseInt($(itemHolder).width() / itemWidth) * itemWidth;
                var left = parseInt($(items[0]).css('left'));
                var L = (extra + left > slideDist) ? -slideDist : -(extra + left);

                e.preventDefault();

                if (left + extra > 0) {
                    for (var i = 0; i < items.length; i++) {
                        $(items[i]).animate({ left: parseInt($(items[i]).css('left')) + L }, 1000);
                    }
                }
            });

            $(this.content).find('.scrollerLeft').click(function (e) {

                var slideDist = parseInt($(itemHolder).width() / itemWidth) * itemWidth;
                var left = parseInt($(items[0]).css('left'));
                var L = (left + slideDist < 0) ? slideDist : -left;

                e.preventDefault();

                for (var i = 0; i < items.length; i++) {
                    $(items[i]).animate({ left: parseInt($(items[i]).css('left')) + L }, 1000);
                }

            });

        }

    };
}

//function itemScrollerHorizontal(item) {

//    this.content = item;

//    this.init = function init() {

//        var margin = 15;
//        var items = $(this.content).find('.slideWrap a');
//        var itemHeight = $(items).find('img').outerHeight(true) + margin;
//        var itemHolder = $(this.content).find('.slideWrap');
//        var extra = items.length * itemHeight - $(itemHolder).height();
//        var maxWidth = 0;
//        var width = $(items).outerWidth(true);

//        items.each(function () {
//            height = $(this).height();
//            maxWidth = (width > maxWidth) ? width : maxWidth;
//        });

//        $(itemHolder).css({ 'height': maxWidth });

//        for (var i = 0; i < items.length; i++) {
//            $(items[i]).css({ "position": "absolute", "top": i * itemHeight });
//        }

//        if (extra > 0) {

//            $(this.content).prepend('<a href="#" class="scrollerTop"></a>')
//                           .append('<a href="#" class="scrollerBottom"></a><div class="clear"></div>');
//            $(this.content).find('.scrollerTop, .scrollerBottom').css({ 'display': 'block', 'width': $(items).outerWidth(true) });
//            $(itemHolder).css({ 'display': 'block' });

//            $(this.content).find('.scrollerBottom').click(function (e) {

//                var slideDist = parseInt($(itemHolder).height() / itemHeight) * itemHeight;
//                var top = parseInt($(items[0]).css('top'));
//                var L = (extra + top > slideDist) ? -slideDist : -(extra + top);

//                e.preventDefault();

//                if (top + extra > 0) {
//                    for (var i = 0; i < items.length; i++) {
//                        $(items[i]).animate({ top: parseInt($(items[i]).css('top')) + L }, 1000);
//                    }
//                }
//            });

//            $(this.content).find('.scrollerTop').click(function (e) {

//                var slideDist = parseInt($(itemHolder).height() / itemHeight) * itemHeight;
//                var top = parseInt($(items[0]).css('top'));
//                var L = (top + slideDist < 0) ? slideDist : -top;

//                e.preventDefault();

//                for (var i = 0; i < items.length; i++) {
//                    $(items[i]).animate({ top: parseInt($(items[i]).css('top')) + L }, 1000);
//                }

//            });

//        }

//    };
//}

