// Slider
var KW = KW || {};
KW.prevNext = {
    hovered: function () {
        $(this).siblings('strong').fadeToggle(50);
    },
    init: function ($parent, $link) {
        $parent.append('<strong style="display: none;"></strong>');
        $link.hover(KW.prevNext.hovered);
    }
};

var $slideshow = $('#feature');

KW.homeRotate = {
	init: function () {
        // On resize
        this.setWidths();
        $(window).resize(function () {
            KW.homeRotate.setWidths();
        });
        KW.homeRotate.$feature = $('#feature-container');
        KW.homeRotate.$pager = KW.homeRotate.$feature.find('.nav-feature-pager');
        KW.homeRotate.totalCount = KW.homeRotate.$pager.find('li').length;
        KW.homeRotate.itemWidth = KW.homeRotate.$feature.width();
        KW.homeRotate.$timer = KW.homeRotate.$pager.find('.timer');
        KW.homeRotate.$progress = KW.homeRotate.$feature.find('.status');
        KW.homeRotate.$prevNext = KW.homeRotate.$feature.find('#nav-feature');
        KW.homeRotate.rotateSpeed = 15000;
        KW.homeRotate.current = 'current';
        KW.homeRotate.stepWidth = Math.round(100 / (KW.homeRotate.totalCount - 1));
        KW.homeRotate.rotating = false;
        KW.homeRotate.rotateInterval = '';
        KW.homeRotate.$pager.find('a').bind({
            'click': function (e) {
                var $parent = $(this).parent();
                if (!$parent.hasClass(KW.homeRotate.current)) {
                    KW.homeRotate.advance($parent.prevAll().length, $parent);
                }
                e.preventDefault();
                e.stopPropagation();
            },
            'mouseenter': function () {
                $(this).children('.preview').css('opacity', '0').stop().animate({
                    'bottom': '16px',
                    'opacity': '0.5'
                }, 75).css('display', 'block').animate({
                    'bottom': '26px',
                    'opacity': '1.0'
                }, 150);
            },
            'mouseleave': function () {
                $(this).children('.preview').css({
                    'bottom': '10px',
                    'display': 'none'
                });
            }
        });
        KW.prevNext.init(KW.homeRotate.$prevNext.find('li'), KW.homeRotate.$prevNext.find('a'));
        KW.homeRotate.updatePrevNext(KW.homeRotate.$pager.find('li:first-child'));
        KW.homeRotate.$prevNext.find('a').bind({
            'click': function (e) {
                var $this = $(this),
                    $cur = KW.homeRotate.$pager.find('.current'),
                    $newCur;
                if ($this.parent().hasClass('prev')) {
                    $newCur = KW.homeRotate.getPrevious($cur);
                } else {
                    $newCur = KW.homeRotate.getNext($cur);
                }
                $newCur.children('a').click();
                e.preventDefault();
                e.stopPropagation();
            },
            mouseover: function () {
                $(this).siblings('strong').fadeIn(150);
            },
            mouseout: function () {
                $(this).siblings('strong').fadeOut(50);
            }
        });
        $(window).load(function () {
            KW.homeRotate.startRotate();
            KW.homeRotate.$feature.bind({
                mouseover: KW.homeRotate.stopRotate,
                mouseout: KW.homeRotate.startRotate
            });
        });
    },

	
	
	
	advance: function (pos, $newParent) {
        
		if (KW.homeRotate.rotating === false) {
            KW.homeRotate.rotating = true;

            var newPos = 0,
                $current = KW.homeRotate.$pager.find('.' + KW.homeRotate.current),
                currentPos = $current.prevAll().length;
            if (pos < KW.homeRotate.totalCount) {
				newPos = pos * KW.homeRotate.itemWidth;
                newPos = '-' + newPos;            
			}

            KW.homeRotate.$feature.find('#feature-wrapper').animate({
				left: newPos
            }, 800, 'easeInOutCirc');
		
			if (pos > currentPos) {
                KW.homeRotate.handlePassed(pos);
            }
			        			
			$current.removeClass(KW.homeRotate.current);
            KW.homeRotate.$progress.animate({
                'width': (pos * KW.homeRotate.stepWidth) - 0.01 + '%'
            }, 350, 'easeInOutCirc', function () {
                if (pos < currentPos) {
                    KW.homeRotate.handlePassed(pos);
                }
                $newParent.addClass(KW.homeRotate.current);
                KW.homeRotate.rotating = false;
            });
            KW.homeRotate.updatePrevNext($newParent);
        }
    },
	
	setWidths: function () {
		this.windowWidth = $(window).width();
		
        $homeSlideshow = $slideshow.find('#feature-wrapper');

        $homeSlideshow.
        width(this.windowWidth * KW.homeRotate.totalCount); //
		
        $homeSlideshow.find('.frame').
        width(this.windowWidth);

        // Align current slide to left        
		// $homeSlideshow.css({'margin-left':parseInt('-'+KW.homeRotate.windowWidth*1)});
    },
	
	
    autoRotate: function () {
        var $next = KW.homeRotate.$pager.find('.' + KW.homeRotate.current).next();
        if ($next.length !== 0) {
            $next.children('a').click();
        } else {
            KW.homeRotate.$pager.find('li:first-child a').click();
        }
    },
    getPrevious: function ($cur) {
        var $previous;
        if ($cur.prevAll().length === 0) {
            $previous = KW.homeRotate.$pager.find('li:last-child');
        } else {
            $previous = $cur.prev();
        }
        return $previous;
    },
    getNext: function ($cur) {
        var $next;
        if ($cur.nextAll().length === 0) {
            $next = KW.homeRotate.$pager.find('li:first-child');
        } else {
            $next = $cur.next();
        }
        return $next;
    },
    handlePassed: function (pos) {
        KW.homeRotate.$pager.find('.passed').removeClass('passed');
        KW.homeRotate.$pager.find('li:lt(' + pos + ')').addClass('passed');
    },
    startRotate: function () {
        KW.homeRotate.rotateInterval = setInterval(KW.homeRotate.autoRotate, KW.homeRotate.rotateSpeed);
    },
    stopRotate: function () {
        clearInterval(KW.homeRotate.rotateInterval);
    },
    updatePrevNext: function ($cur) {
        var $previous = KW.homeRotate.getPrevious($cur),
            $next = KW.homeRotate.getNext($cur);
        KW.homeRotate.$prevNext.find('.prev strong').text($previous.text());
        KW.homeRotate.$prevNext.find('.next strong').text($next.text());
    }
};

KW.homeRotate.init();


/*

$(function () {
    $("#feature-wrapper").css("margin-right", $(document).width());
    $(window).bind("resize", function () {
        $("#feature-wrapper").css("margin-right", $(document).width());
    });
});
*/
$(document).ready(function () {
    $('.news-content').each(function () {
        var param = $(this).attr('id');
        var $nav = $('<div class="pagination" id="holder_' + param + '"><\/div>').insertBefore('.paginator-placeholder');
        $(this).cycle({
            fx: 'scrollHorz',
            speed: 300,
            timeout: 56500,
            pager: $nav,
            next: '#next_' + param,
            prev: '#previous_' + param
        });
    });
    $(".map-fancybox").fancybox({
        'width': '75%',
        'height': '75%',
        'autoScale': false,
        'type': 'iframe'
    });

});
