$(function () {
    $('.homeslide').slidetabs();
    $('.homelogos').slidetabs({
        speed: 8000
    });
    $('.latestnews').slidenews({
        speed: 3000
    });
    $('.bluelist').click(function () {
        var url = $(this).find('a').attr('href');
        window.location.href = url;

    });

});

(function ($) {

    $.fn.extend({
        slidenews: function (options) {
			 var myoptions = $.extend({
                    speed: 5000
                }, options);
				
            return this.each(function () {
                var box = $(this);
                box.newsrepeater(myoptions);
                box.prevnext(myoptions);
				box.overmouse(myoptions);
			});
        }, 
		overmouse: function (options) {
                return this.each(function () {
					var box =$(this);
                    box.children('ul').hover(
                    function () {
                        window.clearTimeout(box.data('timeout'));
                    }, function () {
                        box.data('timeout', window.setTimeout(function () {
                            box.newsrepeater(options);
                        }, options.speed));
                    });
                });
            
        },
        //Function for repeating news
        newsrepeater: function (options) {
            return this.each(function () {
                var box = $(this);
                var list = box.find('li');

                
                var currentelement = (box.find('li.current').length > 0) ? box.find('li.current') : box.find('li:first');
                var nextelement = (currentelement.next().length > 0) ? currentelement.next() : box.find('li:first');

                if ($(currentelement).is(':not(:animated)')) {
                    if (currentelement.hasClass('current') == true) {
                        nextelement.css({
                            top: list.height()
                        });
                        nextelement.show();

                        nextelement.addClass('fade');

                        nextelement.stop(true, true).animate({
                            top: 0
                        }, 500);

                        currentelement.stop(true, true).animate({
                            top: -list.height()
                        }, 500, function () {
                            currentelement.hide();
                            currentelement.removeClass('current');
                            nextelement.addClass('current');
                            nextelement.removeClass('fade');

                        });



                    } else {
                        currentelement.addClass('current');
                    };
                };
                box.data('timeout', window.setTimeout(function () {
                    box.newsrepeater(options);
					}, options.speed));
				
                

            });
        },

        //add options for Prev next buttons
        prevnext: function (options) {
            return this.each(function () {
                var box = $(this);
                var list = box.find('li');
                var myoptions = $.extend({
                    speed: 5000
                }, options);

                box.find('h2:first a').click(function (e) {
                    e.preventDefault();

                    var action = $(this).attr('class');

                    window.clearTimeout(box.data('timeout'));

                    var currentelement = (box.find('li.current').length > 0) ? box.find('li.current') : box.find('li:first');
                    var prevelement = (currentelement.prev().length > 0) ? currentelement.prev() : box.find('li:last');
                    var nextelement = (currentelement.next().length > 0) ? currentelement.next() : box.find('li:first');

                    var actionelement = (action == 'prevnews') ? prevelement : nextelement;
                    if (box.find('li.current').is(':not(:animated)')) {
                        if (action == 'prevnews') actionelement.css({
                            top: -list.height()
                        });
                        else actionelement.css({
                            top: list.height()
                        });
                        actionelement.show();

                        actionelement.addClass('fade');

                        var currenttop = (action == 'prevnews') ? list.height() : -list.height();

                        actionelement.stop(true, true).animate({
                            top: 0
                        }, 500);


                        currentelement.stop(true, true).animate({
                            top: currenttop
                        }, 500, function () {
                            currentelement.hide();
                            currentelement.removeClass('current');
                            actionelement.addClass('current');
                            actionelement.removeClass('fade');
							box.data('timeout', window.setTimeout(function () {
                                box.newsrepeater(options);
                            }, options.speed));
						
                        });
                    };

                });

            });
        },

        // This function add missed elements and run slideshow		
        slidetabs: function (options) {
            $(this).each(function () {
                var box = $(this);

                var myoptions = $.extend({
                    speed: 5000
                }, options);

                box.append('<p class="numbers"></p><p class="caption" ></p>');

                var a = 1;
                var images = box.children('.slidein');
                images.children('img').each(function () {
                    box.children('.numbers').append('<span>' + a + '</span>')
                    a++;
                });

                $(this).repeater(myoptions);
                $(this).currentclick(myoptions);
            });

        },

        // This function, create slideshow, and repeat every time
        repeater: function (options) {
            $(this).each(function () {
                var box = $(this);
                var images = box.children('div:first');

                var pagination = box.children('.numbers');
                if (pagination.children('.current').length > 0) {

                    var current = pagination.children('.current');
                    current.removeClass('current');
                    var next = current.next();

                    if (next.length === 0) next = pagination.children('span:first');

                    next.addClass('current');

                    var currentelement = pagination.children('span').index(current);
                    var nextelement = pagination.children('span').index(next);

                    var currentimage = images.children("img:eq(" + currentelement + ")");
                    var nextimage = images.children("img:eq(" + nextelement + ")");

                    nextimage.addClass('fade');
                    var caption = nextimage.attr('alt');
                    currentimage.removeClass('fade');

                    nextimage.stop(true, true).fadeIn('slow', function () {
                        currentimage.hide();
                        box.children('.caption').html(caption);

                    });
                } else {
                    pagination.children('span:first').addClass('current')
                    images.children('img:first').addClass('fade')
                    images.children('img:first').stop(true, true).fadeIn('fast');

                    var caption = images.children('img:first').attr('alt');
                    box.children('.caption').html(caption);

                }
                box.data('timeout', window.setTimeout(function () {
                    box.repeater(options);
                }, options.speed));


            });
        },

        // This function activate show current slide by clicking on buttons 
        currentclick: function (options) {
            $(this).each(function () {
                var box = $(this);
                var clickable = box.children('.numbers');
                clickable.children('span').click(function () {

                    var element = $(this);
                    if (!element.hasClass('current')) {
                        window.clearTimeout(box.data('timeout'));
                        clickable.children('.current').removeClass('current');
                        element.addClass('current');

                        var clickedindex = element.index();
                        var image = box.children('div:first');
                        var hided = image.children(".fade");
                        hided.removeClass('fade');

                        var faded = image.children("img:eq(" + clickedindex + ")");
                        faded.addClass('fade');

                        var caption = faded.attr('alt');

                        faded.stop(true, true).fadeIn('slow', function () {
                            hided.hide();
                            box.children('.caption').html(caption);
                            box.data('timeout', window.setTimeout(function () {
                                box.repeater(options);
                            }, options.speed));

                        });
                    }
                });
            });
        }
    });
})(jQuery)
