﻿function SwitchPic(_container, width, height, textHeight)
{
    this.container = (typeof (_container) == 'string') ? document.getElementById(_container) : _container;

    this.width = width || 400;
    this.height = height || 300;
    this.textHeight = textHeight || 18;

    this.flashPath = '/flashs/switchPic.swf';

    this.pics = [];
    this.links = [];
    this.texts = [];

    this.addPic = function (pic, link, text)
    {
        this.pics.push(pic);
        this.links.push(link || "");
        this.texts.push(text || "");
    }

    this.show = function ()
    {
        var _oString = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + this.width + '" height="' + (this.height + this.textHeight) + '">';
        _oString += '<param name="allowScriptAccess" value="sameDomain"><param name="movie" value="' + this.flashPath + '"><param name=wmode value=transparent><param name="quality" value="high">';
        _oString += '<param name="menu" value="false"><param name=wmode value="opaque">';
        _oString += '<param name="FlashVars" value="pics=' + this.pics.join('|') + '&links=' + this.links.join('|') + '&texts=' + this.texts.join('|') + '&borderwidth=' + this.width + '&borderheight=' + this.height + '&textheight=' + this.textHeight + '">';
        _oString += '<embed src="' + this.flashPath + '" wmode="opaque" FlashVars="pics=' + this.pics.join('|') + '&links=' + this.links.join('|') + '&texts=' + this.texts.join('|') + '&borderwidth=' + this.width + '&borderheight=' + this.height + '&textheight=' + this.textHeight + '" menu="false" bgcolor="#DADADA" quality="high" width="' + this.width + '" height="' + (this.height + this.textHeight) + '" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
        _oString += '</object>';
        this.container.innerHTML = _oString;
    }
}



/**
*glide.layerGlide((oEventCont,oSlider,sSingleSize,sec,fSpeed,point);
*@param auto type:bolean 是否自动滑动 当值是true的时候 为自动滑动
*@param oEventCont type:object 包含事件点击对象的容器
*@param oSlider type:object 滑动对象
*@param sSingleSize type:number 滑动对象里单个元素的尺寸（width或者height）  尺寸是有point 决定
*@param second type:number 自动滑动的延迟时间  单位/秒
*@param fSpeed type:float   速率 取值在0.05--1之间 当取值是1时  没有滑动效果
*@param point type:string   left or top
*/
var glide = new function ()
{
    function $id(id) { return document.getElementById(id); };
    this.layerGlide = function (auto, oEventCont, oSlider, sSingleSize, second, fSpeed, point)
    {
        var oSubLi = $id(oEventCont).getElementsByTagName('li');
        var interval, timeout, oslideRange;
        var time = 1;
        var speed = fSpeed
        var sum = oSubLi.length;
        var a = 0;
        var delay = second * 1000;
        var setValLeft = function (s)
        {
            return function ()
            {
                oslideRange = Math.abs(parseInt($id(oSlider).style[point]));
                $id(oSlider).style[point] = -Math.floor(oslideRange + (parseInt(s * sSingleSize) - oslideRange) * speed) + 'px';
                if (oslideRange == [(sSingleSize * s)])
                {
                    clearInterval(interval);
                    a = s;
                }
            }
        };
        var setValRight = function (s)
        {
            return function ()
            {
                oslideRange = Math.abs(parseInt($id(oSlider).style[point]));
                $id(oSlider).style[point] = -Math.ceil(oslideRange + (parseInt(s * sSingleSize) - oslideRange) * speed) + 'px';
                if (oslideRange == [(sSingleSize * s)])
                {
                    clearInterval(interval);
                    a = s;
                }
            }
        }

        function autoGlide()
        {
            for (var c = 0; c < sum; c++) { oSubLi[c].className = ''; };
            clearTimeout(interval);
            if (a == (parseInt(sum) - 1))
            {
                for (var c = 0; c < sum; c++) { oSubLi[c].className = ''; };
                a = 0;
                oSubLi[a].className = "active";
                interval = setInterval(setValLeft(a), time);
                timeout = setTimeout(autoGlide, delay);
            } else
            {
                a++;
                oSubLi[a].className = "active";
                interval = setInterval(setValRight(a), time);
                timeout = setTimeout(autoGlide, delay);
            }
        }

        if (auto) { timeout = setTimeout(autoGlide, delay); };
        for (var i = 0; i < sum; i++)
        {
            oSubLi[i].onmouseover = (function (i)
            {
                return function ()
                {
                    for (var c = 0; c < sum; c++) { oSubLi[c].className = ''; };
                    clearTimeout(timeout);
                    clearInterval(interval);
                    oSubLi[i].className = "active";
                    if (Math.abs(parseInt($id(oSlider).style[point])) > [(sSingleSize * i)])
                    {
                        interval = setInterval(setValLeft(i), time);
                        this.onmouseout = function () { if (auto) { timeout = setTimeout(autoGlide, delay); }; };
                    } else if (Math.abs(parseInt($id(oSlider).style[point])) < [(sSingleSize * i)])
                    {
                        interval = setInterval(setValRight(i), time);
                        this.onmouseout = function () { if (auto) { timeout = setTimeout(autoGlide, delay); }; };
                    }
                }
            })(i)
        }
    }
}
