// Easy News Plus - jQuery plugin for News Slide by Michael Lo
// http://www.ezjquery.com
// Copyright (c) 2008 Michael Lo
// Dual licensed under the MIT and GPL licenses.
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html
// free for anyone like Jquery. Enjoy!
(function ($) {
    jQuery.extend({
        init_plus: function (option) {
            option = $.extend({
                firstname: "",
                secondname: "",
                thirdname: "",
                fourthname: "",
                playingtitle: "Now Playing:",
                nexttitle: "Next News:",
                prevtitle: "Prev News:",
                newsspeed: 6000,
                isauto: 1,
                imagedir: "",
                newscountname: "",
                disablenewscount: false
            }, option);
            var firstname = option.firstname;
            var secondname = option.secondname;
            var thirdname = option.thirdname;
            var fourthname = option.fourthname;
            var newsspeed = option.newsspeed;
            var isauto = option.isauto;
            var playingtitle = option.playingtitle;
            var nexttitle = option.nexttitle;
            var prevtitle = option.prevtitle;
            var imagedir = option.imagedir;
            var newscountname = option.newscountname;
            var disablenewscount = option.disablenewscount;
            if (newscountname) {
                var news_sp = 1
            }
            if (disablenewscount === true) {
                var news_dis = 1
            }
            var myprevimg = $('#news_prev').attr('src');
            if (!myprevimg) {
                myprevimg = imagedir + 'prev.gif'
            }
            var mynextimg = $('#news_next').attr('src');
            if (!mynextimg) {
                mynextimg = imagedir + 'next.gif'
            }
            var mypauseimg = $('#news_pause').attr('src');
            if (!mypauseimg) {
                mypauseimg = imagedir + 'pause.gif'
            }
            var myprevimg0 = $('#news_prev0').attr('src');
            if (!myprevimg0) {
                myprevimg0 = imagedir + 'galeria_noticias/botones/prev0.gif'
            }
            var mynextimg0 = $('#news_next0').attr('src');
            if (!mynextimg0) {
                mynextimg0 = imagedir + 'galeria_noticias/botones/next0.gif'
            }
            var mypauseimg0 = $('#news_pause0').attr('src');
            if (!mypauseimg0) {
                mypauseimg0 = imagedir + 'galeria_noticias/botones/pause0.gif'
            }
            var activechk, activechkmore, mysize, myfirst, myfirst_explain, active, timer;
            mysize = $('#' + firstname + ' .news_style').size();
            myfirst = $('#' + firstname + ' .news_style').eq(0).html();
            myfirst_explain = $('#' + firstname + ' .news_style').eq(1).attr('rel');
            active = 0;
            $('#' + secondname).append('<table border=0 id=mm' + firstname + ' class=news_move cellspacing="0" cellpadding="0"><tr id=insidetr' + secondname + '></tr></table>');
            $('#insidetr' + secondname).append('<td class=mytable id=' + secondname + active + '>' + myfirst + '</td>');
            if (news_sp === 1 && news_dis != 1) {
                $('#' + newscountname).html('1/' + mysize)
            }
            if (news_sp != 1 && news_dis != 1) {
                $('#' + thirdname).html('&nbsp;&nbsp;' + playingtitle + '1/' + mysize + '&nbsp;&nbsp;')
            }
            $('#' + thirdname).append(nexttitle + myfirst_explain);
            $('#' + fourthname + ' #news_next').click(function () {
                clearTimeout(timer);
                $(this).attr({
                    src: mynextimg0
                });
                $('#' + fourthname + ' #news_prev').attr({
                    src: myprevimg
                });
                $('#' + fourthname + ' #news_pause').attr({
                    src: mypauseimg
                });
                var need_to_delete = '#' + secondname + active;
                active = active + 1;
                if (active == mysize) {
                    active = 0
                }
                var mynum = active + 1;
                var nextnum = mynum;
                var temp = $('#' + firstname + ' .news_style').eq(active).html();
                if (nextnum == mysize) {
                    nextnum = 0
                }
                var mynow_explain = $('#' + firstname + ' .news_style').eq(nextnum).attr('rel');
                $('#insidetr' + secondname).append('<td class=mytable id=' + secondname + active + '>' + temp + '</td>');
                if (news_sp === 1 && news_dis != 1) {
                    $('#' + newscountname).html(mynum + '/' + mysize);
                    $('#' + thirdname).html(nexttitle + mynow_explain)
                }
                if (news_sp != 1 && news_dis != 1) {
                    $('#' + thirdname).empty().html('&nbsp;&nbsp;' + playingtitle + '' + mynum + '/' + mysize + '&nbsp;&nbsp;');
                    $('#' + thirdname).append(nexttitle + mynow_explain)
                }
                if (news_dis === 1) {
                    $('#' + thirdname).html(nexttitle + mynow_explain)
                }
                var whatdist = $('#' + secondname + active).css("width");
                whatdist = parseInt(whatdist, 10);
                $('#mm' + firstname).animate({
                    left: -whatdist
                }, 1000, function () {
                    $(need_to_delete).remove();
                    $('#mm' + firstname).css({
                        left: '0'
                    })
                });
                if (isauto == 1) {
                    timer = setTimeout(autonext, newsspeed, active)
                }
            });
            $('#' + fourthname + ' #news_prev').click(function () {
                clearTimeout(timer);
                $(this).attr({
                    src: myprevimg0
                });
                $('#' + fourthname + ' #news_next').attr({
                    src: mynextimg
                });
                $('#' + fourthname + ' #news_pause').attr({
                    src: mypauseimg
                });
                var need_to_delete = '#' + secondname + active;
                active = active - 1;
                if (active < 0) {
                    active = mysize - 1
                }
                var mynum = active + 1;
                var myprevnum = mynum - 2;
                if (myprevnum < 0) {
                    myprevnum = mysize - 1
                }
                var temp = $('#' + firstname + ' .news_style').eq(active).html();
                var mynow_explain = $('#' + firstname + ' .news_style').eq(myprevnum).attr('rel');
                $('#insidetr' + secondname).prepend('<td class=mytable id=' + secondname + active + '>' + temp + '</td>');
                var whatdist = $('#' + secondname + active).css("width");
                whatdist = parseInt(whatdist, 10);
                $('#mm' + firstname).css({
                    left: -whatdist
                });
                if (news_sp === 1 && news_dis != 1) {
                    $('#' + newscountname).html(mynum + '/' + mysize);
                    $('#' + thirdname).html(prevtitle + mynow_explain)
                }
                if (news_sp != 1 && news_dis != 1) {
                    $('#' + thirdname).empty().html('&nbsp;&nbsp;' + playingtitle + '' + mynum + '/' + mysize + '&nbsp;&nbsp;');
                    $('#' + thirdname).append(prevtitle + mynow_explain)
                }
                if (news_dis === 1) {
                    $('#' + thirdname).html(prevtitle + mynow_explain)
                }
                $('#mm' + firstname).animate({
                    left: 0
                }, 1000, function () {
                    $(need_to_delete).remove()
                });
                if (isauto == 1) {
                    timer = setTimeout(autoprev, newsspeed, active)
                }
            });
            $('#' + fourthname + ' #news_pause').click(function () {
                $(this).attr({
                    src: mypauseimg0
                });
                $('#' + fourthname + ' #news_next').attr({
                    src: mynextimg
                });
                $('#' + fourthname + ' #news_prev').attr({
                    src: myprevimg
                });
                clearTimeout(timer)
            });
            var _st = window.setTimeout;
            window.setTimeout = function (fRef, mDelay) {
                if (typeof fRef == 'function') {
                    var argu = Array.prototype.slice.call(arguments, 2);
                    var f = (function () {
                        fRef.apply(null, argu)
                    });
                    return _st(f, mDelay)
                }
                return _st(fRef, mDelay)
            };

            function autonext(q) {
                if (!q) {
                    q = 0
                }
                myend = $('#' + firstname + ' .news_hide_style').size();
                myend = myend - 1;
                if (q >= myend) {
                    q = 0
                }
                $('#' + fourthname + ' #news_next').eq(q).click();
                q = q + 1
            }
            function autoprev(q) {
                if (!q) {
                    q = 0
                }
                myend = $(".news_hide_style").size();
                myend = myend - 1;
                if (q >= myend) {
                    q = 0
                }
                $('#' + fourthname + ' #news_prev').eq(q).click();
                q = q + 1
            }
            if (isauto == 1) {
                timer = setTimeout(autonext, newsspeed, 1)
            }
        }
    })
})(jQuery);
