﻿;(function($) {
    var defaults = {
        interval: 1000  //0 不替换 其它为毫秒数时间间隔。
        , objName: ".royalPG"
        , imageContainer: ".royalPG_slidshow"
        , descContainer: ".royalPG_desc"
        , itemHoverClass: "highlight"
        , itemSelClass: "hover"
        , thumbObj: ".royalPG_thumb"
        , itemListAlign: "right" //left|center|right
        , isSclItem: true
        //-----------------------------
        , sclSize: 4
        , sclDelay: 300
        , sclStart: 0
        , sclDriection: "H" //H|V
        , sclObj: ".royalPG_list"
        , sclItem: ".royalPG_item"
        , sclBtnprev: ".royalPG_btnprev"
        , sclBtnnext: ".royalPG_btnnext"
        , itemShowTxt: false
    };

    $.fn.royalPG = function(o) {
        this.each(function() {
            new $.royalPG(this, o);
        });
    };

    $.royalPG = function(e, o) {
        this.obj = $(e);
        this.options = $.extend({}, defaults, o || {});

        this.timer = null;
        this.data = []; //图片对象集合
        //this.currentImage = 1;
        //this.currentTabs = 0;

        this.param = {
            sclItemW: 100
            , sclItemH: 100
            , sclItemP: 1
            , sclItemM: 5
            , sclContainW: 0
            , sclContainH: 0
            , sclWidth: 112
            , sclHeight: 112
            , currentImg: 1
            , currentItem: 0
        }

        var self = this;

        if ($.browser.safari) {
            $(window).bind('load', function() { self.init(); });
        } else {
            this.init();
        }
    };

    var $rp = $.royalPG;

    $rp.fn = $rp.prototype = {
        verson: '1.0.0'
        , author: 'royal zhou'
    };

    $rp.fn.extend = $rp.extend = $.extend;

    $rp.fn.extend({
        buildData: function(listItem) {
            var title = "";
            var url = "";
            var bpic = "";
            var spic = "";
            var desc = "";
            var top = 0;
            var left = 0;
            var width = 200;
            var height = 200;

            var $li = (typeof listItem === "string") ? $(listItem) : listItem;
            var $a = $li.find("a");
            var $imgs = $a.find("img");
            var $span = $li.find("span");
            var $div = $li.find("div");

            title = $a.attr("title");
            url = $a.attr("href");
            spic = $imgs.eq(0).attr("src");
            bpic = $imgs.eq(1).attr("src");
            desc = $div.html();

            var pos = $span.html().split(",");
            for (var i = 0; i < pos.length; i++) {
                var c = pos[i].substring(0, 1);
                switch (c.toUpperCase()) {
                    case "T": top = pos[i].substring(1, pos[i].length); break;
                    case "L": left = pos[i].substring(1, pos[i].length); break;
                    case "H": height = pos[i].substring(1, pos[i].length); break;
                    case "W": width = pos[i].substring(1, pos[i].length); break;
                }
            }
            $a.removeAttr("href");
            $a.css("background-image", "url(" + spic + ")");
            if (this.options.tabsShowTxt) $a.append("<b>" + title + "</b>");
            $imgs.remove();
            $span.remove();
            $div.remove();

            var imageData = {
                title: title
                , url: url
                , bpic: bpic
                , spic: spic
                , desc: desc
                , top: top
                , left: left
                , width: width
                , height: height
            }

            this.data.push(imageData);
        },

        init: function() {
            var othis = this;
            this.obj.find(othis.options.sclItem).each(function(index) {
                othis.buildData($(this));
                othis.param.sclItemW = $(this).css("width").replace("px", "");
                othis.param.sclItemH = $(this).css("height").replace("px", "");
                if (othis.options.sclDriection == "H") {
                    othis.param.sclItemP = $(this).css("padding-left").replace("px", "");
                    othis.param.sclItemM = $(this).css("margin-left").replace("px", "");
                }
                else {
                    othis.param.sclItemP = $(this).css("padding-top").replace("px", "");
                    othis.param.sclItemM = $(this).css("margin-top").replace("px", "");
                }

                if (othis.options.isSclItem) {
                    if (othis.options.sclDriection == "H") {
                        othis.param.sclContainW = parseInt(othis.param.sclContainW) + parseInt(othis.param.sclItemW) + 2 * (parseInt(othis.param.sclItemP) + parseInt(othis.param.sclItemM));
                    }
                    else {
                        othis.param.sclContainH = parseInt(othis.param.sclContainH) + parseInt(othis.param.sclItemH) + 2 * (parseInt(othis.param.sclItemP)) + parseInt(othis.param.sclItemM);
                    }

                    $(this).click(function(e) {
                        othis.param.currentItem = index;
                        othis.play(index);
                    });

                    $(this).hover(function(e) { $(this).addClass(othis.options.itemSelClass); }, function(e) { $(this).removeClass(othis.options.itemSelClass); })
                }
                else {
                    $(this).mouseenter(function(e) {
                        othis.param.currentItem = index;
                        othis.play(index);
                    });
                }
            });
            //-----------------------------------------------
            var $sclObj = $(this.options.sclObj);
            if (othis.options.isSclItem) {
                if (this.options.sclDriection == "H") {
                    this.param.sclContainH = parseInt(this.param.sclItemH) + 2 * (parseInt(this.param.sclItemP) + parseInt(this.param.sclItemM));
                    this.param.sclWidth = parseInt(this.options.sclSize) * (parseInt(this.param.sclItemW) + 2 * (parseInt(this.param.sclItemP) + parseInt(this.param.sclItemM)));
                    this.param.sclHeight = 0;
                }
                else {
                    this.param.sclContainH += parseInt(this.param.sclItemM);
                    this.param.sclContainW = parseInt(this.param.sclItemW) + 2 * (parseInt(this.param.sclItemP) + parseInt(this.param.sclItemM));
                    this.param.sclHeight = parseInt(this.options.sclSize) * (parseInt(this.param.sclItemH) + 2 * (parseInt(this.param.sclItemP)) + parseInt(this.param.sclItemM));
                    this.param.sclWidth = 0;
                }

                this.obj.css("width", this.param.sclContainW + "px").css("height", this.param.sclContainH + "px");
                if (this.options.sclDriection == "H") {
                    //if ($sclObj.width() >= this.obj.width()) {
                    if (parseInt($sclObj.css("width").replace("px", "")) >= parseInt(this.obj.css("width").replace("px", ""))) {
                        this.disabledBtn(this.options.sclBtnprev);
                        this.disabledBtn(this.options.sclBtnnext);
                    }
                    else {
                        this.scrollTo(parseInt(this.options.sclStart));
                    }
                }
                else {
                    //if ($sclObj.height() >= this.obj.height()) {
                    if (parseInt($sclObj.css("height").replace("px", "")) >= parseInt(this.obj.css("height").replace("px", ""))) {
                        this.disabledBtn(this.options.sclBtnprev);
                        this.disabledBtn(this.options.sclBtnnext);
                    }
                    else {
                        this.scrollTo(parseInt(this.options.sclStart));
                    }
                }

            }
            else {
                //init sclobjwidth and height
                var tmpWidth = this.data.length * (parseInt(othis.param.sclItemW) + 2 * (parseInt(othis.param.sclItemP) + parseInt(othis.param.sclItemM))) + 2 * parseInt(othis.param.sclItemM)
                $sclObj.css("width", tmpWidth + "px");
                //alert(tmpWidth);
                var tmpTotalW = $(this.options.thumbObj).width();
                if (tmpWidth >= tmpTotalW) { }
                else {
                    switch (this.options.itemListAlign) {
                        case "left":
                            $sclObj.css("padding-left", parseInt(othis.param.sclItemM) + "px");
                            break;
                        case "center":
                            $sclObj.css("padding-left", Math.ceil((tmpTotalW - tmpWidth) / 2) + "px");
                            break;
                        case "right":
                            $sclObj.css("padding-left", tmpTotalW - tmpWidth + "px");
                            break;
                    }
                }


            }
            //-----------------------------------------------
            if (this.options.interval == 0) {
                this.play(this.options.sclStart);
            }
            else {
                $(this.options.objName).hover(function() { othis.stop() }, function() { othis.autoplay(); });
                this.param.currentItem = this.options.sclStart;
                this.autoplay();
            }


        },

        disabledBtn: function(btnObj) {
            //alert("end" + btnObj); 
            $btnObj = $(btnObj + " > a");
            if ($btnObj.hasClass("disabled")) return;
            $btnObj.unbind("click");
            $btnObj.addClass("disabled");

        },

        enabledBtn: function(btnObj, isprev) {
            var othis = this
            //alert("enabled" + btnObj); 
            $btnObj = $(btnObj + " > a");
            if (!$btnObj.hasClass("disabled")) return;
            $btnObj.bind("click", function() { othis.scroll(isprev); });
            $btnObj.removeClass("disabled");
        },

        chkScroll: function(index) {
            var $sclObj = $(this.options.sclObj);
            if (this.options.sclDriection == "H") {
                var sclWidthU = index * (parseInt(this.param.sclItemW) + 2 * (parseInt(this.param.sclItemP) + parseInt(this.param.sclItemM)));
                var sclWidthD = sclWidthU - $sclObj.width() + (parseInt(this.param.sclItemW) + 2 * (parseInt(this.param.sclItemP) + parseInt(this.param.sclItemM)));
                var currentSclW = $sclObj.scrollLeft();
                if (currentSclW < sclWidthD || currentSclW > sclWidthU) {
                    return true;
                }
                return false;
            }
            else {
                var sclHeightU = index * (parseInt(this.param.sclItemH) + 2 * (parseInt(this.param.sclItemP) + parseInt(this.param.sclItemM)));
                var sclHeightD = sclHeightU - $sclObj.height() + (parseInt(this.param.sclItemH) + 2 * (parseInt(this.param.sclItemP)) + parseInt(this.param.sclItemM));
                var currentSclH = $sclObj.scrollTop();
                if (currentSclH < sclHeightD || currentSclH > sclHeightU) {
                    return true;
                }
                return false;
            }
        },

        scrollTo: function(index) {
            var $sclObj = $(this.options.sclObj);
            if (this.options.sclDriection == "H") {
                var sclObjW = parseInt($sclObj.css("width").replace("px", ""));
                var objW = parseInt(this.obj.css("width").replace("px", ""));
                //if ($sclObj.width() >= this.obj.width()) {
                if (sclObjW >= objW) {
                    this.disabledBtn(this.options.sclBtnprev);
                    this.disabledBtn(this.options.sclBtnnext);
                    $sclObj.scrollLeft(0);
                }
                else {
                    var sclWidth = index * (parseInt(this.param.sclItemW) + 2 * (parseInt(this.param.sclItemP) + parseInt(this.param.sclItemM)));
                    if (sclWidth + sclObjW >= objW) {
                        sclWidth = objW - sclObjW;
                        this.disabledBtn(this.options.sclBtnnext);
                    }
                    else {
                        this.enabledBtn(this.options.sclBtnnext, false);
                    }
                    if (sclWidth == 0) {
                        this.disabledBtn(this.options.sclBtnprev);
                    }
                    else {
                        this.enabledBtn(this.options.sclBtnprev, true);
                    }
                    //$sclObj.scrollLeft(0);
                    //alert($sclObj.scrollLeft());
                    $sclObj.animate({ scrollLeft: sclWidth + "px" }, this.options.sclDelay);

                }
            }
            else {
                var sclObjH = parseInt($sclObj.css("height").replace("px", ""));
                var objH = parseInt(this.obj.css("height").replace("px", ""));
                if (sclObjH >= objH) {
                    this.disabledBtn(this.options.sclBtnprev);
                    this.disabledBtn(this.options.sclBtnnext);
                    $sclObj.scrollTop(0);
                }
                else {
                    var sclHeight = index * (parseInt(this.param.sclItemH) + 2 * (parseInt(this.param.sclItemP)) + parseInt(this.param.sclItemM));
                    if (sclHeight + sclObjH >= objH) {
                        sclHeight = objH - sclObjH;
                        this.disabledBtn(this.options.sclBtnnext);
                    }
                    else {
                        this.enabledBtn(this.options.sclBtnnext, false);
                    }
                    if (sclHeight == 0) {
                        this.disabledBtn(this.options.sclBtnprev);
                    }
                    else {
                        this.enabledBtn(this.options.sclBtnprev, true);
                    }
                    //$sclObj.scrollTop(sclHeight);
                    $sclObj.animate({ scrollTop: sclHeight + "px" }, this.options.sclDelay);

                }
            }
        },

        scroll: function(isPrev) {
            var $sclObj = $(this.options.sclObj);
            if (this.options.sclDriection == "H") {
                var sclObjW = parseInt($sclObj.css("width").replace("px", ""));
                var objW = parseInt(this.obj.css("width").replace("px", ""));
                if (sclObjW >= objW) {
                    this.disabledBtn(this.options.sclBtnprev);
                    this.disabledBtn(this.options.sclBtnnext);
                    $sclObj.scrollLeft(0);
                }
                else {
                    var sclWidth = parseInt(this.param.sclWidth);
                    sclWidth = (isPrev ? 0 - sclWidth : sclWidth);
                    //alert(sclWidth);
                    if (sclWidth + sclObjW + $sclObj.scrollLeft() >= objW) {
                        sclWidth = objW - sclObjW - $sclObj.scrollLeft();
                        this.disabledBtn(this.options.sclBtnnext);
                    }
                    else {
                        this.enabledBtn(this.options.sclBtnnext, false);
                    }
                    if (sclWidth + $sclObj.scrollLeft() <= 0) {
                        sclWidth = 0 - $sclObj.scrollLeft();
                        this.disabledBtn(this.options.sclBtnprev);
                    }
                    else {
                        this.enabledBtn(this.options.sclBtnprev, true);
                    }
                    $sclObj.animate({ scrollLeft: "+=" + sclWidth }, this.options.sclDelay);

                }
            }
            else {
                var sclObjH = parseInt($sclObj.css("height").replace("px", ""));
                var objH = parseInt(this.obj.css("height").replace("px", ""));
                if (sclObjH >= objH) {
                    this.disabledBtn(this.options.sclBtnprev);
                    this.disabledBtn(this.options.sclBtnnext);
                    $sclObj.scrollTop(0);
                }
                else {
                    var sclHeight = parseInt(this.param.sclHeight);
                    sclHeight = (isPrev ? 0 - sclHeight : sclHeight);
                    if (sclHeight + sclObjH + $sclObj.scrollTop() >= objH) {
                        sclHeight = objH - sclObjH - $sclObj.scrollTop();
                        this.disabledBtn(this.options.sclBtnnext);
                    }
                    else {
                        this.enabledBtn(this.options.sclBtnnext, false);
                    }
                    if (sclHeight + $sclObj.scrollTop() <= 0) {
                        sclHeight = 0 - $sclObj.scrollTop();
                        this.disabledBtn(this.options.sclBtnprev);
                    }
                    else {
                        this.enabledBtn(this.options.sclBtnprev, true);
                    }
                    $sclObj.animate({ scrollTop: "+=" + sclHeight }, this.options.sclDelay);
                }
            }
        },

        play: function(index) {
            //alert(index);
            //scroll thumb
            if (this.options.isSclItem && this.chkScroll(index)) {
                this.scrollTo(index);
            }
            if (this.obj.find(this.options.sclItem).eq(index).hasClass(this.options.itemHoverClass)) return;
            //this.obj.find(this.options.sclItem).eq(index).find("img").attr("src", this.data[index].spic);
            //this.obj.find("li." + this.options.itemHoverClass).find("img").attr("src", "images/rptabsTrans.gif");
            this.obj.find("." + this.options.itemHoverClass).removeClass(this.options.itemHoverClass);
            this.obj.find(this.options.sclItem).eq(index).addClass(this.options.itemHoverClass);
            var $desc = $(this.options.descContainer);
            var $descbg = $(this.options.descContainer + "bg");
            $desc.hide();
            $descbg.hide();
            $descbg.css("top", parseInt(this.data[index].top) + "px");
            $descbg.css("left", parseInt(this.data[index].left) + "px");
            $descbg.css("width", parseInt(this.data[index].width) + "px");
            $descbg.css("height", parseInt(this.data[index].height) + "px");
            $desc.css("top", (parseInt(this.data[index].top)) + "px");
            $desc.css("left", (parseInt(this.data[index].left)) + "px");
            $desc.css("width", Math.ceil(parseInt(this.data[index].width) * 0.96 - 20) + "px");
            $desc.css("height", Math.ceil(parseInt(this.data[index].height) * 0.96 - 20) + "px");
            $desc.html(this.data[index].desc);
            var $slidshow = $(this.options.imageContainer);
            $slidshow.find("img").stop(false, true);
            var $img = $slidshow.find("img").eq(this.param.currentImg);
            $img.attr("src", this.data[index].bpic).fadeIn(520, function() { $descbg.fadeTo(300, 0.7, function() { $desc.fadeIn(100).end(); }).end(); }).end();
            this.param.currentImg = 1 - this.param.currentImg;
            var $img = $slidshow.find("img").eq(this.param.currentImg);
            $img.fadeOut(500).end();
        },

        autoplay: function() {
            this.play(this.param.currentItem);
            this.param.currentItem = (this.param.currentItem + 1) % this.data.length;
            var self = this;
            var delay = this.options.interval + 1200;
            this.timer = setTimeout(function() { self.autoplay(); }, delay);
        },

        stop: function() {
            if (this.timer == null) {
                return;
            }
            window.clearTimeout(this.timer);
            this.timer = null;
        }


    });

})(jQuery);
