﻿/*图片自适应大小*/
function resizeImage(img, maxWidth, maxHeight, callback) {
    if (img.nodeName != "IMG") {
        return;
    }

    var image = new Image();
    image.src = img.src;
    if (img.complete) {
        if (image.width > maxWidth || image.height > maxHeight) {
            if (image.width / maxWidth > image.height / maxHeight) {
                img.style.width = maxWidth + "px";
            } else {
                img.style.height = maxHeight + "px";
            }
        }
        if (typeof callback == "function") {
            callback(img);
        }
    } else {
        image.onload = function() {
            if (this.width > maxWidth || this.height > maxHeight) {
                if (this.width / maxWidth > this.height / maxHeight) {
                    img.style.width = maxWidth + "px";
                } else {
                    img.style.height = maxHeight + "px";
                }
            }
            if (typeof callback == "function") {
                callback(img);
            }
        }
        image.onload();
    }
}

$(function() {
    var items = $("#slider .slide-item");
    var timer = 0, index = 0;
    var count = items.length;
    if (count > 0) {
        items.eq(0).fadeIn(1000);
        var handle = function() {
            index = (index >= (count - 1)) ? 0 : ++index;
            $("#silder_num li").eq(index).trigger("click");
        };
        for (var i = 1; i <= count; i++) {
            var li = $("<li></li>");
            li.html("<span>" + i + "</span>");
            if (i == 1) li.addClass("current");
            li.bind({
                click: function(i, li) {
                    return function() {
                        index = (i >= count) ? 0 : i;
                        li.addClass("current").siblings().removeClass("current");
                        items.filter(":visible").fadeOut(500, function() {
                            $(this).parent().children().eq(index).fadeIn(1000);
                        });
                    };
                } (i - 1, li),
                mouseover: function(i, li) {
                    return function() {
                        clearInterval(timer);
                        index = (i >= count) ? 0 : i;
                        li.addClass("current").siblings().removeClass("current");
                        items.eq(index).show().siblings().hide();
                    };
                } (i, li),
                mouseout: function() {
                    timer = setInterval(handle, 6000);
                }
            });
            $("#silder_num").append(li);
        }
        timer = setInterval(handle, 6000);
    }
});

$(document).ready(function() {
    var height = 0;
    var banners = [];
    $("#slider .silder-player a img").each(function() {
        var img = new Image();
        img.src = this.src;
        banners.push(img);
    });
    if (banners.length > 0) {
        var setHeight = function() {
            var result = true;
            $(banners).each(function() {
                if (this.complete) {
                    height = this.height > height ? this.height : height;
                } else {
                    result = false;
                    return result;
                }
            });
            if (result) {
                $("#slider").css("height", height + "px");
            }
            return result;
        }

        var timer = window.setInterval(function() {
            if (setHeight()) {
                banners = undefined;
                clearInterval(timer);
            }
        }, 100);
    }
});

String.prototype.format = function() {
    var txt = this;
    i = arguments.length;
    while (i-- >= 0) {
        txt = txt.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return txt;
}
