﻿var flashup,flashdown;

$.ready(function () {

    noticePlay=function () {

        var ads=$("headerAds").getElementsByTagName("A");
        var curr=0,stop=false,len=ads.length,w=220,h=50,up=true;
        $("headerAds").on("mousemove",function () {
            stop=true;
        }).on("mouseout",function () {
            stop=false;
        });


        var flashLinks=$("flash").getElementsByTagName("A");
        var flashPics=$("flash").getElementsByTagName("IMG");
        var flashNums=$("flashIndex").getElementsByTagName("LI");
        var flashLength=flashLinks.length;

        var currentFlash=0;
        var isStop=false;
        var flashHeight=flashLinks[0].offsetHeight;

        function changeFlash(oldIndex,newIndex) {
            if(oldIndex==newIndex)
                return;

            var opt={
                element: flashLinks[oldIndex],
                startTime: 0,
                endTime: 10,
                topStart: 0,
                topEnd: flashHeight* -1
            };

            var opt1={
                element: flashLinks[newIndex],
                startTime: 0,
                endTime: 10,
                topStart: flashHeight,
                topEnd: 0
            };

            if(newIndex>oldIndex) {
                opt.topStart=0;
                opt.topEnd=flashHeight* -1;
                opt1.topStart=flashHeight;
                opt1.topEnd=0;
            } else {
                opt.topStart=0;
                opt.topEnd=flashHeight;
                opt1.topStart=flashHeight* -1;
                opt1.topEnd=0;
            }

            $(opt1.element).css({ top: flashHeight+"px",display: "block" });
            currentFlash=newIndex;

            $.gradual([opt,opt1],function () {
                flashNums[oldIndex].className="";
                flashNums[newIndex].className="cur";
                $(opt.element).css({ display: "none" });
            });
        };

        flashup=function () {
            changeFlash(currentFlash,currentFlash==0?flashLength-1:currentFlash-1);
        };
        flashdown=function () {
            changeFlash(currentFlash,currentFlash==flashLength-1?0:currentFlash+1);
        };

        $("flash").on("mousemove",function () {
            isStop=true;
        }).on("mouseout",function () {
            isStop=false;
        });

        $.each(flashNums,function (item,i) {
            $(item).on("mousemove",function () {
                changeFlash(currentFlash,i);
                isStop=true;
            }).on("mouseout",function () {
                isStop=false;
            });
        });

        function start() {
            var opts=[];

            if(ads&&ads.length) {
                if(!stop) {

                    var a=$(ads[curr]);
                    curr=curr==len-1?0:(curr+1);
                    var b=$(ads[curr]);
                    var c,d;
                    if(up) {
                        c={
                            topStart: 0,
                            topEnd: -1*h,
                            startTime: 0,
                            endTime: 10
                        };
                        d={
                            topStart: h,
                            topEnd: 0,
                            startTime: 0,
                            endTime: 10,
                            onstart: function () {
                                b.css("top:"+h+"px;left:0px;display:block");
                            }
                        };
                    } else {
                        c={
                            leftStart: 0,
                            leftEnd: -1*w,
                            startTime: 0,
                            endTime: 10
                        };
                        d={
                            leftStart: w,
                            leftEnd: 0,
                            startTime: 0,
                            endTime: 10,
                            onstart: function () {
                                b.css("top:0px;left:"+w+"px;display:block");
                            }
                        };
                    }
                    c.element=a;
                    d.element=b;
                    //up=!up;

                    opts.push(c);
                    opts.push(d);
                }
            }

            if(!isStop)
                changeFlash(currentFlash,currentFlash==flashLength-1?0:currentFlash+1);

            $.gradual(opts);

            window.setTimeout(start,6000);
        };

        return start;
    };

    (function () {


        var elems=$("intro").getElementsByTagName("A");
        if(elems.length!=0) {
            var curr=0;
            var width=$("intro").offsetWidth;
            var changing=false;
            var introStop=false;
            function rotate(isNext) {
                if(!changing) {
                    var flag=isNext?-1:1;
                    changing=true;
                    var next=curr==elems.length-1?0:(curr+1);
                    var a={
                        element: elems[curr],
                        startTime: 0,
                        endTime: 10,
                        leftStart: 0,
                        leftEnd: flag*width
                    };
                    var b={
                        element: elems[next],
                        startTime: 0,
                        endTime: 10,
                        leftStart: -1*flag*width,
                        leftEnd: 0,
                        onstart: function () {
                            $(elems[next]).css("display:block");
                        }
                    };
                    $.gradual([a,b],function () {
                        $(elems[curr]).css("display:none");
                        changing=false;
                        curr=next;
                    });
                }
            };
            function introStart() {
                if(!introStop) {
                    rotate(true);
                }
                window.setTimeout(introStart,5000);
            };
            window.setTimeout(introStart,5000);

            function showBtn() {
                $("introPrev").css("display:block");
                $("introNext").css("display:block");
            };
            $("introPrev").on("click",function () { rotate(false); })
                .on("mousemove",function () { introStop=true;showBtn(); })
                .on("mouseout",function () { introStop=false; });
            $("introNext").on("click",function () { rotate(true); })
                .on("mousemove",function () { introStop=true;showBtn(); })
                .on("mouseout",function () { introStop=false; });
            $("intro").on("mousemove",function () {
                introStop=true;
                showBtn();
            }).on("mouseout",function () {
                introStop=false;
                $("introPrev").css("display:none");
                $("introNext").css("display:none");
            });
        }

    })();

    (function () {
        var ideas=$("corporPic").getElementsByTagName("A");
        var curr=0;
        $("corporPrev").css({ opacity: 0.4,cursor: "default" });
        if(ideas.length<=5) {
            $("corporNext").css({ opacity: 0.4,cursor: "default" });
        } else {
            var width=145;
            $("corporPic").css({ width: width*ideas.length+"px" });

            var changing=false;
            function rotate(isNext) {
                if(!changing) {
                    changing=true;
                    var a={
                        element: $("corporPic"),
                        startTime: 0,
                        endTime: 10
                    };
                    if(isNext) {
                        a.leftStart= -1*curr*width;
                        a.leftEnd= -1*width*(curr+1);
                    } else {
                        a.leftStart= -1*curr*width;
                        a.leftEnd= -1*width*(curr-1);
                    }
                    $.gradual([a],function () {
                        if(isNext) {
                            curr++;

                        } else {
                            curr--;
                        }
                        if(curr==0)
                            $("corporPrev").css({ opacity: 0.4,cursor: "default" });
                        else
                            $("corporPrev").cssText("");
                        if(curr==ideas.length-5)
                            $("corporNext").css({ opacity: 0.4,cursor: "default" });
                        else
                            $("corporNext").cssText("");
                        changing=false;
                    });
                }
            };

            $("corporPrev").on("click",function () {
                if(curr>0) {
                    rotate(false);
                }
            });
            $("corporNext").on("click",function () {
                if(curr<ideas.length-5) {
                    rotate(true);
                }
            });
        }
    })()

});



var cur_ao={};
function showAO(name,num) {

    if(typeof cur_ao[name]=="undefined")
        cur_ao[name]=0;

    var old_ao=cur_ao[name];
    if(old_ao==num)
        return;

    var old_tit=$(name+"_"+old_ao);
    var old_c=$(name+"_c_"+old_ao);

    var currTit=$(name+"_"+num);
    var curr=$(name+"_c_"+num);
    var height=old_c.offsetHeight-1;
    old_tit.css("");
    currTit.css("cur");

    var a={
        element: old_c,
        startTime: 0,
        endTime: 10,
        heightStart: height,
        heightEnd: 0
    };
    var b={
        element: curr,
        startTime: 0,
        endTime: 10,
        heightStart: 0,
        heightEnd: height,
        onstart: function () {
            curr.css("display:block;height:0px;");
        }
    };

    $.gradual([a,b],function () {
        curr.css("display:block;height:"+height+"px;");
        old_c.css("display:none;height:0px;");
        cur_ao[name]=num;
    });
};


