﻿(function ($) {

    $.fn.shiftGallery = function (options) {
        var wpos = 0;
        var thumbWidth;
        var thumbHeight;
        var thumbSpace;
        var thumbNrPage;

        var totalWidth = 0;
        var totalHeight = 0;
        var realtotalWidth = 0;
        var realtotalHeight = 0;

        var currPage = 0;
        var currNr = 0;

        var stripArray = new Array();

        var defaultArrowSize = 20;


        var stripWidth = 150;
        var stripHeight = 150;

        var j = 0;
        var k = 0;

        var stripX = 0;
        var stripY = 0;

        var stripNrX = 5;
        var stripNrY = 5;

        var navSpace = 5;

        var transition = "curtainLeft"
        var transitionArray = [];
        var transitionTypeArray = [];
        var descriptionOpacityArray = [];

        var transitionTime = 1200;
        var stripDelay = 70;

        var busy = false;

        var lastIndex = 99;

        var aux = 0;
        var aux3 = 0;

        var isSlideshow = "off";
        var paused = false;
        //var slideshowTime = 5000;
        var slideshowTime;
        var tempCount = 0;

        var menuPosition = "down";
        var thumbInitialOpacity = 0.2;
        var darkTheme = "off";
        var basePath = "";

        var pagination = true;
        var piWidth = 11;
        var piHeight = 11;
        var piSpace = 10;

        var defaults = {
            thumbWidth: 140,
            thumbHeight: 100,
            thumbSpace: 5,
            thumbNrPage: 5,

            totalWidth: 0,
            totalHeight: 0,
            transition: "curtainLeft",
            transitionTime: 1200,
            stripDelay: 70,

            stripNrX: 5,
            stripNrY: 5,
            navSpace: 5,
            isSlideshow: "off",
            slideshowTime: 5,
            menuPosition: "down",
            darkTheme: "off",
            basePath: "",


            //just for backward compatibility ( now use totalWidth )
            imageWidth: 300,
            imageHeight: 300
        },
          options = $.extend(defaults, options);

        this.each(function () {
            if (pagination == true) {
                thumbWidth = piWidth;
                thumbHeight = piHeight;
                thumbSpace = piSpace;
                thumbNrPage = 20;
            }
            else {
                thumbWidth = options.thumbWidth;
                thumbHeight = options.thumbHeight;
                thumbSpace = options.thumbSpace;
                thumbNrPage = options.thumbNrPage;
            }

            if (options.totalWidth != 0) {
                totalWidth = options.totalWidth;
            }
            else {
                totalWidth = options.imageWidth;
            }

            if (options.totalHeight != 0) {
                totalHeight = options.totalHeight;
            }
            else {
                totalHeight = options.imageHeight;
            }
            imageWidth = options.imageWidth;

            transition = options.transition;

            transitionTime = options.transitionTime;
            stripDelay = options.stripDelay;

            stripNrX = options.stripNrX;
            stripNrY = options.stripNrY;
            navSpace = options.navSpace;

            isSlideshow = options.isSlideshow;
            slideshowTime = options.slideshowTime;

            menuPosition = options.menuPosition;
            darkTheme = options.darkTheme;
            basePath = options.basePath;


            jQuery(this).append('<div id="imgCon"></div>')

            if (menuPosition == 'right') {
                jQuery(this).css({
                    'overflow': 'hidden',
                    'width': totalWidth + navSpace + thumbWidth,
                    'height': totalHeight
                })
            }
            else {
                jQuery(this).css({
                    'overflow': 'hidden',
                    'width': totalWidth,
                    'height': totalHeight + navSpace + thumbWidth
                })
            }



            var nrChildren = jQuery(this).children().length;
            var imgCon = jQuery(this).find('#imgCon');

            if (transition == "random") {
                for (i = 0; i < nrChildren; i++) {
                    var randomNr = Math.floor(Math.random() * 9)


                    if (randomNr == 0) {
                        if (jQuery.browser.msie) {
                            transitionArray[i] = "curtainLeft";
                        }
                        else
                            transitionArray[i] = "slideFade";
                    }

                    if (randomNr == 1) {

                        if (jQuery.browser.msie) {
                            transitionArray[i] = "curtainLeft";
                        }
                        else
                            transitionArray[i] = "slideFade";
                    }

                    if (randomNr == 2)
                        transitionArray[i] = "curtainAlpha";
                    if (randomNr == 3)
                        transitionArray[i] = "curtainLeft";
                    if (randomNr == 4)
                        transitionArray[i] = "curtainSim";
                    if (randomNr == 5)
                        transitionArray[i] = "curtainShift";
                    if (randomNr == 6)
                        transitionArray[i] = "curtainAlternative";
                    if (randomNr == 7)
                        transitionArray[i] = "blocksDiagonal";
                    if (randomNr == 8)
                        transitionArray[i] = "blocksSlide";
                }
            }
            else {
                for (i = 0; i < nrChildren; i++)
                    transitionArray[i] = transition;
            }

            for (i = 0; i < nrChildren; i++) {
                if (transitionArray[i] == "blocksDiagonal" || transitionArray[i] == "blocksSlide") {
                    transitionTypeArray[i] = "cubes"
                }
                else {
                    transitionTypeArray[i] = "simple"
                }
            }

            imgCon.append('<div id="slider"></div>')

            var sliderCon = imgCon.find('#slider');

            for (i = 0; i < nrChildren - 1; i++) {
                jQuery(this).children().eq(0).appendTo(sliderCon)
            }

            var images = sliderCon.children();



            if (totalWidth === 0) {
                totalWidth = images.eq(0).width();
                realtotalWidth = totalWidth;
            }
            else {
                realtotalWidth = images.eq(0).width();
            }


            if (totalHeight === 0) {
                totalHeight = images.eq(0).height();
                realtotalHeight = totalHeight;
            }
            else {
                realtotalHeight = images.eq(0).height();
            }



            stripWidth = totalWidth / stripNrX;
            stripHeight = totalHeight / stripNrY;

            images.width(totalWidth)
            images.height(totalHeight)


            imgCon.css({
                'height': totalHeight,
                'width': totalWidth,
                'overflow': 'hidden'
            })



            sliderCon.css('position', 'relative')



            //NAVIGATION CONSTRUCTION *************
            jQuery(this).append('<div id="navCon"><div id="thumbSliderCon"><div id="thumbSlider"></div></div></div>');

            var navCon = jQuery(this).find('#navCon');
            var thumbSliderCon = jQuery(this).find('#thumbSliderCon');
            var thumbSlider = jQuery(this).find('#thumbSlider');

            if (menuPosition == "down") {

                navCon.css({
                    'width': totalWidth,
                    'height': thumbHeight + navSpace,
                    'overflow': 'hidden',
                    'text-align': 'center',
                    'position': 'relative',
                    'top': navSpace - 5
                })

                thumbSliderCon.css({
                    'width': thumbNrPage * (thumbWidth + thumbSpace),
                    'height': thumbHeight,
                    'overflow': 'hidden',
                    'position': 'relative',
                    'margin': 'auto'
                })
            }
            else {

                navCon.css({
                    'width': thumbWidth + navSpace,
                    'height': totalHeight,
                    'overflow': 'hidden',
                    'text-align': 'left',
                    'position': 'absolute',
                    'left': navSpace + totalWidth,
                    'top': 0
                })

                thumbSliderCon.css({
                    'width': thumbWidth,
                    'height': thumbNrPage * (thumbHeight + thumbSpace),
                    'overflow': 'hidden',
                    'position': 'relative',
                    'top': (totalHeight - (thumbNrPage * (thumbSpace + thumbHeight))) / 2,
                    'margin': 'auto'
                })
            }


            thumbSlider.css({
                'position': 'relative',
                'width': 'auto',
                'background-color': '#ddd'
            })


            var thumbPos = 0;

            if (thumbNrPage >= nrChildren - 1) {
                if (pagination == true) {
                    thumbPos += ((thumbNrPage - nrChildren + 1) * (thumbWidth + thumbSpace) + thumbSpace) * 0.5;
                }
                else {
                    thumbPos += (thumbNrPage - nrChildren + 1) * thumbWidth * 0.5;
                }
            }



            for (i = 0; i < images.length; i++) {
                //縮圖框內要顯示的資料
                if (pagination == true) {
                    if (i == currNr) {
                        thumbSlider.append("<img src='" + basePath + "en/images/content/main_top_banner/button/button.png'/>")
                    }
                    else {
                        thumbSlider.append("<img src='" + basePath + "en/images/content/main_top_banner/button/button_over.png'/>")
                    }
                } else {
                    if (images.eq(i).attr("data-thumb") != undefined) {
                        thumbSlider.append("<img src='" + images.eq(i).attr("data-thumb") + "'/>")
                    }
                    else {
                        thumbSlider.append(images.eq(i).clone());
                    }
                }


                //縮圖預設效果設定
                if (menuPosition == "down") {
                    //選單在下方時的縮圖設定
                    if (pagination == true) {
                        thumbSlider.children().eq(i).css({
                            'left': thumbPos,
                            'top': 0,
                            'position': 'absolute',
                            'cursor': 'pointer'
                        })
                    }
                    else {
                        thumbSlider.children().eq(i).css({
                            'left': thumbPos,
                            'top': 0,
                            'position': 'absolute',
                            'cursor': 'pointer',
                            'opacity': thumbInitialOpacity
                        })
                    }

                    thumbPos += thumbWidth + thumbSpace;
                }
                else {
                    //選單在左方時的縮圖設定
                    if (pagination == true) {
                        thumbSlider.children().eq(i).css({
                            'left': 0,
                            'top': thumbPos,
                            'position': 'absolute',
                            'cursor': 'pointer'
                        })
                    }
                    else {
                        thumbSlider.children().eq(i).css({
                            'left': 0,
                            'top': thumbPos,
                            'position': 'absolute',
                            'cursor': 'pointer',
                            'opacity': thumbInitialOpacity
                        })
                    }
                    thumbPos += thumbHeight + thumbSpace;
                }



                //縮圖Rollover設定
                thumbSlider.children().eq(i).mouseover(function (event) {
                    if (thumbSlider.children().index($(this)) == currNr) {
                        return;
                    }
                    jQuery(event.target).stop();
                    if (pagination == true) {
                        var newSrc = jQuery(event.target).attr("src").replace("button_over.png", "button.png");
                        $(this).attr("src", newSrc);
                    }
                    else {
                        jQuery(event.target).animate({
                            opacity: 1
                        }, 100)
                    }
                })

                //縮圖Rollout設定
                thumbSlider.children().eq(i).mouseout(function (event) {
                    if (thumbSlider.children().index($(this)) == currNr) {
                        return;
                    }
                    jQuery(event.target).stop();
                    if (pagination == true) {
                        var newSrc = jQuery(event.target).attr("src").replace("button.png", "button_over.png");
                        $(this).attr("src", newSrc);
                    }
                    else {
                        jQuery(event.target).animate({
                            opacity: thumbInitialOpacity
                        }, 400)
                    }
                })
            }

            //設定縮圖寬高
            thumbSlider.children().width(thumbWidth);
            thumbSlider.children().height(thumbHeight);



            //設定箭頭的圖檔來源
            if (thumbNrPage < nrChildren - 1) {
                if (darkTheme == "on")
                    navCon.append('<img src="' + basePath + 'img/barrow.png" id="arrowleft"/>')
                else
                    navCon.append('<img src="' + basePath + 'img/arrow.png" id="arrowleft"/>')

                if (jQuery.browser.msie) {
                    if (darkTheme == "on")
                        navCon.append('<img src="' + basePath + 'img/barrowie.png" id="arrowright"/>')
                    else
                        navCon.append('<img src="' + basePath + 'img/arrowie.png" id="arrowright"/>')
                }
                else {
                    if (darkTheme == "on")
                        navCon.append('<img src="' + basePath + 'img/barrow.png" id="arrowright" class="flipped"/>')
                    else
                        navCon.append('<img src="' + basePath + 'img/arrow.png" id="arrowright" class="flipped"/>')
                }
            }

            //箭頭大小設定
            var arrowSize = 0;

            if (navCon.find('#arrowleft').children().eq(0).width() === 0 || navCon.find('#arrowleft').children().eq(0).width() == null) {
                arrowSize = defaultArrowSize;
            }
            else {
                arrowSize = navCon.find('#arrowleft').children().eq(0).width();
            }




            if (menuPosition == "down") {
                //選單在下方時的箭頭位置和屬性設定
                aux = ((totalWidth - (thumbNrPage * (thumbWidth + thumbSpace))) / 4) - arrowSize / 2;
                navCon.find('#arrowleft').css({
                    'position': 'absolute',
                    'top': thumbHeight / 2 - arrowSize / 2,
                    'left': aux,
                    'cursor': 'default',
                    'opacity': '0.5'
                })

                navCon.find('#arrowright').css({
                    'position': 'absolute',
                    'top': thumbHeight / 2 - arrowSize / 2,
                    'right': aux,
                    'cursor': 'pointer',
                    'opacity': '0.5'
                })
            }
            else {
                //選單在左方時的箭頭位置和屬性設定
                aux = totalHeight - ((totalHeight - (thumbNrPage * (thumbHeight + thumbSpace))) / 4) - arrowSize / 2;
                navCon.find('#arrowleft').css({
                    'position': 'absolute',
                    'top': 0,
                    'left': thumbWidth * 0.5 - arrowSize * 0.5,
                    'cursor': 'default',
                    'opacity': '0.5'
                })

                navCon.find('#arrowright').css({
                    'position': 'absolute',
                    'top': aux,
                    'left': thumbWidth * 0.5 - arrowSize * 0.5,
                    'cursor': 'pointer',
                    'opacity': '0.5'
                })
            }



            //左箭頭點擊和狀態設定
            $(this).find('#arrowleft').mouseover(function () {
                if (currPage != 0) {
                    navCon.find('#arrowleft').css({
                        'opacity': '1'
                    })
                }
            });
            $(this).find('#arrowleft').mouseout(function () {
                navCon.find('#arrowleft').css({
                    'opacity': '0.5'
                })
            });
            $(this).find('#arrowleft').click(function () {
                gotoPage(currPage - 1);
            });

            //右箭頭點擊和狀態設定
            $(this).find('#arrowright').mouseover(function () {
                if (currPage <= Math.floor((nrChildren - 1) / thumbNrPage)) {
                    navCon.find('#arrowright').css({
                        'opacity': '1'
                    })
                }
            });
            $(this).find('#arrowright').mouseout(function () {
                navCon.find('#arrowright').css({
                    'opacity': '0.5'
                })
            });
            $(this).find('#arrowright').click(function () {
                gotoPage(currPage + 1);
            });




            function gotoPage(arg) {
                if (arg >= 0 && arg <= Math.floor((nrChildren - 1) / thumbNrPage)) {
                    currPage = arg;
                    thumbSlider.stop();
                }
                else {
                    return;
                }

                if (arg == 0) {
                    navCon.find('#arrowleft').css('cursor', 'default')
                    navCon.find('#arrowleft').css({
                        'opacity': '0.5'
                    })
                }
                else {
                    navCon.find('#arrowleft').css('cursor', 'pointer')
                }

                if (arg == Math.floor((nrChildren - 1) / thumbNrPage)) {
                    navCon.find('#arrowright').css({
                        'opacity': '0.5'
                    })
                    navCon.find('#arrowright').css('cursor', 'default')
                }
                else {
                    navCon.find('#arrowright').css('cursor', 'pointer')
                }

                var targetX = 0;

                if (menuPosition == "down") {
                    targetX = arg * thumbNrPage * -(thumbWidth + thumbSpace);

                    if (targetX > 0) {
                        targetX = 0;
                    }
                    if (targetX < ((nrChildren - 1) - thumbNrPage) * -(thumbWidth + thumbSpace)) {
                        targetX = ((nrChildren - 1) - thumbNrPage) * -(thumbWidth + thumbSpace);
                    }
                    thumbSlider.animate({
                        left: targetX
                    }, 500);
                }
                else {
                    targetX = arg * thumbNrPage * -(thumbHeight + thumbSpace);
                    if (targetX > 0) {
                        targetX = 0;
                    }
                    if (targetX < ((nrChildren - 1) - thumbNrPage) * -(thumbHeight + thumbSpace)) {
                        targetX = ((nrChildren - 1) - thumbNrPage) * -(thumbHeight + thumbSpace);
                    }
                    thumbSlider.animate({
                        top: targetX
                    }, 500);
                }
            }

            //點擊縮圖設定
            thumbSlider.children().click(function (event) {
                gotoImage(thumbSlider.children().index(this));
                //alert(thumbSlider.children().index(this));
                //alert(thumbSlider.children().index(this));
            })




            if (pagination == true) {
                var newSrc = thumbSlider.children().eq(currNr).attr("src").replace("button_over.png", "button.png");
                $(this).attr("src", newSrc);
            }
            else {
                thumbSlider.children().eq(currNr).animate({
                    opacity: 0.9
                }, 100)
            }
            //FUNCTION THAT HANDLES TRANSITIONS
            function gotoImage(arg) {

                if (currNr === arg) {
                    return;
                }
                if (busy == true) {
                    return;
                }
                busy = true;
                if ((stripArray[arg].length == 0 || stripArray[currNr].length == 0) && transitionArray[arg] != 'fade') {
                    transition = "slideFade"
                }
                else {
                    transition = transitionArray[arg];
                }
                //alert(stripArray[arg].length);
                images.eq(arg).css('z-index', lastIndex)
                lastIndex++;

                for (i = 0; i < images.length; i++) {
                    if (i == currNr || i == arg) {
                        images.eq(i).css('visibility', 'visible')
                    }
                    else {
                        images.eq(i).css('visibility', 'hidden')
                    }
                }

                //設定前一張縮圖效果
                if (pagination == true) {
                    var newSrc = thumbSlider.children().eq(currNr).attr("src").replace("button.png", "button_over.png");
                    thumbSlider.children().eq(currNr).attr("src", newSrc);
                }
                else {
                    thumbSlider.children().eq(currNr).animate({
                        opacity: thumbInitialOpacity
                    }, 100)
                }

                //*************TRANSITIONS***********

                //START FADE
                if (transition === "fade") {

                    images.eq(arg).animate({
                        left: 0,
                        opacity: 0
                    }, 0)

                    images.eq(arg).animate({
                        opacity: 1
                    }, transitionTime)
                    //if (images.children().eq(arg)[0].nodeName == "OBJECT") {
                    //alert(arg);
                    //alert("object--")
                    //callToActionscript("myFlashMovie" + arg);
                    //var flashName = "myFlashMovie" + arg;
                    //var flashMovie = (window[flashName]) ? window[flashName] : document[flashName];
                    //flashMovie.sendToActionscript();
                    //callFunction("myFlashMovie0");
                    //}
                    //alert(images.children().eq(arg)[0].nodeName);
                    //alert(images.eq(arg));
                    //alert("mda")
                    setTimeout(unBusy, transitionTime)

                }
                //END FADE

                //START SLIDEFADE
                if (transition === "slideFade") {
                    if (jQuery.browser.msie) {

                        images.eq(currNr).animate({
                            left: 0
                        }, 0);

                        images.eq(arg).animate({
                            left: totalWidth
                        }, 0);

                        images.eq(currNr).animate({
                            left: -totalWidth
                        }, 700);

                        images.eq(arg).animate({
                            left: 0
                        }, 700);

                        setTimeout(unBusy, transitionTime)
                    }
                    else {

                        images.eq(currNr).stop();
                        images.eq(arg).stop();

                        if (currNr < arg) {

                            images.eq(currNr).animate({
                                left: 0,
                                opacity: 1
                            }, 0);

                            images.eq(arg).animate({
                                left: totalWidth,
                                opacity: 0
                            }, 0);

                            images.eq(currNr).animate({
                                left: -totalWidth,
                                opacity: 0
                            }, 700);

                            images.eq(arg).animate({
                                left: 0,
                                opacity: 1
                            }, 700);

                        }
                        else {

                            images.eq(currNr).animate({
                                left: 0,
                                opacity: 1
                            }, 0);

                            images.eq(arg).animate({
                                left: -totalWidth,
                                opacity: 0
                            }, 0);

                            images.eq(currNr).animate({
                                left: totalWidth,
                                opacity: 0
                            }, 700);

                            images.eq(arg).animate({
                                left: 0,
                                opacity: 1
                            }, 700);

                        }

                        setTimeout(unBusy, transitionTime)
                    }
                }
                //END SLIDEFADE

                //START curtainAlpha
                if (transition === "curtainAlpha") {
                    if (images.eq(arg).css('opacity') == 1) {

                    }
                    else {
                        images.eq(arg).css({
                            'left': '0',
                            'opacity': '1'
                        })
                    }

                    images.eq(arg).css({
                        'left': '0'
                    })

                    for (i = 0; i < stripArray[arg].length; i++) {
                        stripArray[arg][i].animate({
                            opacity: 0
                        }, 0)
                        stripArray[arg][i].animate({
                            opacity: 1
                        }, transitionTime + 1.5 * i * stripDelay)
                    }
                    setTimeout(unBusy, transitionTime)
                }
                //END curtainAlpha

                //START curtainLeft
                if (transition === "curtainLeft") {

                    if (images.eq(arg).css('opacity') == 1) {

                    }
                    else {
                        images.eq(arg).css({
                            'left': '0',
                            'opacity': '1'
                        })
                    }
                    images.eq(arg).css({
                        'left': '0'
                    })
                    for (i = 0; i < stripArray[arg].length; i++) {
                        stripArray[arg][i].animate({
                            opacity: 0,
                            top: -totalWidth
                        }, 0)
                    }
                    i = 0;
                    function animateIt1() {
                        stripArray[arg][i].animate({
                            opacity: 0,
                            top: -totalWidth
                        }, 0)
                        stripArray[arg][i].animate({
                            opacity: 1,
                            top: 0
                        }, transitionTime)
                        i++;
                        if (i == stripArray[arg].length) {
                            clearInterval(inter);
                            setTimeout(unBusy, transitionTime)
                        }
                    }
                    animateIt1();
                    i = 1;
                    var inter = setInterval(function () {
                        animateIt1();
                    }, stripDelay)
                }
                //END curtainLeft

                //START curtainSim	
                if (transition === "curtainSim") {


                    if (images.eq(arg).css('opacity') == 1) {

                    }
                    else {
                        images.eq(arg).css({
                            'left': '0',
                            'opacity': '1'
                        })
                    }

                    images.eq(arg).css({
                        'left': '0'
                    })


                    for (i = 0; i < stripArray[arg].length; i++) {
                        stripArray[arg][i].animate({
                            opacity: 0,
                            top: -totalWidth
                        }, 0)
                    }

                    i = 0;
                    j = stripArray[arg].length - 1;
                    function animateIt2() {
                        stripArray[arg][i].animate({
                            opacity: 0,
                            top: -totalWidth
                        }, 0)

                        stripArray[arg][i].animate({
                            opacity: 1,
                            top: 0
                        }, transitionTime)



                        i++;


                        if (j > (stripArray[arg].length / 2)) {
                            stripArray[arg][j].animate({
                                opacity: 0,
                                top: -totalWidth
                            }, 0)

                            stripArray[arg][j].animate({
                                opacity: 1,
                                top: 0
                            }, transitionTime)



                            j--;

                        }

                        if (i >= (stripArray[arg].length / 2)) {
                            clearInterval(inter);
                            setTimeout(unBusy, transitionTime)
                        }

                    }



                    animateIt2();
                    i = 1;
                    j = j = stripArray[arg].length - 2;

                    var inter = setInterval(function () {

                        animateIt2();

                    }, stripDelay)


                }
                //END curtainSim

                //START CURTAINALTERNATIVE
                if (transition === "curtainAlternative") {


                    if (images.eq(arg).css('opacity') == 1) {

                    }
                    else {
                        images.eq(arg).css({
                            'left': '0',
                            'opacity': '1'
                        })
                    }

                    images.eq(arg).css({
                        'left': '0'
                    })


                    for (i = 0; i < stripArray[arg].length; i++) {
                        stripArray[arg][i].animate({
                            opacity: 0
                        }, 0)
                    }




                    i = 0;
                    j = stripArray[arg].length - 1;
                    function animateIt3() {
                        if (i % 2 == 0) {
                            stripArray[arg][i].animate({
                                opacity: 0,
                                top: -totalWidth
                            }, 0)
                        }
                        else {
                            stripArray[arg][i].animate({
                                opacity: 0,
                                top: totalWidth
                            }, 0)
                        }

                        stripArray[arg][i].animate({
                            opacity: 1,
                            top: 0
                        }, transitionTime)



                        i++;

                        if (j > (stripArray[arg].length / 2)) {
                            if (j % 2 == 0) {
                                stripArray[arg][j].animate({
                                    opacity: 0,
                                    top: -totalWidth
                                }, 0)
                            }
                            else {
                                stripArray[arg][j].animate({
                                    opacity: 0,
                                    top: totalWidth
                                }, 0)
                            }


                            stripArray[arg][j].animate({
                                opacity: 1,
                                top: 0
                            }, transitionTime)



                            j--;
                        }

                        if (i >= stripArray[arg].length / 2) {
                            clearInterval(inter);
                            setTimeout(unBusy, transitionTime)
                        }
                    }



                    animateIt3();
                    i = 1;
                    j = j = stripArray[arg].length - 2;

                    var inter = setInterval(function () {

                        animateIt3();

                    }, stripDelay)


                }
                //END CURTAINALTERNATIVE

                //START CURTAINSHIFT
                if (transition === "curtainShift") {


                    if (images.eq(arg).css('opacity') == 1) {

                    }
                    else {
                        images.eq(arg).css({
                            'left': '0',
                            'opacity': '1'
                        })
                    }

                    images.eq(arg).css({
                        'left': '0'
                    })


                    for (i = 0; i < stripArray[arg].length; i++) {
                        stripArray[arg][i].animate({
                            opacity: 0
                        }, 0)
                    }




                    i = 0;
                    j = stripArray[arg].length - 1;
                    function animateIt4() {
                        if (i % 2 == 0) {
                            stripArray[arg][i].animate({
                                opacity: 0,
                                left: stripWidth * (i + 1),
                                width: 1
                            }, 0)
                        }
                        else {
                            stripArray[arg][i].animate({
                                opacity: 0,
                                left: stripWidth * i,
                                width: 1
                            }, 0)
                        }

                        if (i % 2 == 0) {
                            stripArray[arg][i].animate({
                                opacity: 1,
                                left: stripWidth * (i),
                                width: stripWidth
                            }, transitionTime)
                        }
                        else {
                            stripArray[arg][i].animate({
                                opacity: 1,
                                width: stripWidth
                            }, transitionTime)
                        }



                        i++;

                        if (j > (stripArray[arg].length / 2)) {
                            if (j % 2 == 0) {
                                stripArray[arg][j].animate({
                                    opacity: 0,
                                    left: stripWidth * (j + 1),
                                    width: 1
                                }, 0)
                            }
                            else {
                                stripArray[arg][j].animate({
                                    opacity: 0,
                                    left: stripWidth * j,
                                    width: 1
                                }, 0)
                            }

                            if (j % 2 == 0) {
                                stripArray[arg][j].animate({
                                    opacity: 1,
                                    left: stripWidth * (j),
                                    width: stripWidth
                                }, transitionTime)
                            }
                            else {
                                stripArray[arg][j].animate({
                                    opacity: 1,
                                    width: stripWidth
                                }, transitionTime)
                            }

                            j--;
                        }

                        if (i >= stripArray[arg].length / 2) {
                            clearInterval(inter);
                            setTimeout(unBusy, transitionTime)
                        }

                    }



                    animateIt4();
                    i = 1;
                    j = j = stripArray[arg].length - 2;

                    var inter = setInterval(function () {

                        animateIt4();

                    }, stripDelay)


                }
                //END PSEUDOCURTAIN

                //SIMSTRIPS
                if (transition === "blocksDiagonal") {


                    if (images.eq(arg).css('opacity') == 1) {

                    }
                    else {
                        images.eq(arg).css({
                            'left': '0',
                            'opacity': '1'
                        })
                    }


                    stripNrY = options.stripNrY;


                    var q = 0;

                    busy = true;

                    images.eq(arg).css({
                        'left': '0'
                    })


                    for (i = 0; i < stripArray[arg].length; i++) {
                        for (j = 0; j < stripArray[arg][i].length; j++) {

                            stripArray[arg][i][j].animate({
                                opacity: 0
                            }, 0)
                        }

                    }
                    i = 0;
                    j = 0;
                    k = 0;
                    q = 0;

                    function animateIt5() {



                        function animateRight() {
                            if (k >= stripNrX) {
                                k = 0;
                                i++;
                            }


                            if (parseFloat($(stripArray[arg][i][k]).css('opacity')) == 0) {

                                jQuery(stripArray[arg][i][k]).animate({
                                    opacity: 1
                                }, transitionTime)
                            }
                            else {
                                k++;



                                if (i >= stripNrY && k >= stripNrX) {

                                }
                                else
                                    animateRight();
                            }
                        }


                        animateRight();

                        function animateDown() {

                            if (j >= stripNrY) {
                                j = 0;
                                q++;
                            }

                            if (parseFloat($(stripArray[arg][i][k]).css('opacity')) == 0) {
                                jQuery(stripArray[arg][j][q]).animate({
                                    opacity: 1
                                }, transitionTime)
                            }
                            else {
                                j++;



                                if (j >= stripNrY && q >= stripNrX) {

                                }
                                else
                                    animateDown();
                            }
                        }


                        animateDown();

                        //i++;
                        //j++;
                        if (i >= stripNrY - 1 && k >= stripNrX - 1) {
                            clearInterval(inter);
                            setTimeout(unBusy, transitionTime);
                        }
                    }

                    animateIt5();
                    animateIt5();

                    var inter = setInterval(function () {

                        animateIt5();

                    }, stripDelay)


                }
                //END blocksDiagonal

                //START blocksSlide
                if (transition === "blocksSlide") {
                    if (images.eq(arg).css('opacity') == 1) {
                    }
                    else {
                        images.eq(arg).css({
                            'left': '0',
                            'opacity': '1'
                        })
                    }

                    images.eq(arg).css({
                        'left': '0'
                    })

                    for (i = 0; i < stripArray[arg].length; i++) {
                        for (j = 0; j < stripArray[arg][i].length; j++) {
                            stripArray[arg][i][j].animate({
                                opacity: 0,
                                left: 0
                            }, 0)
                        }
                    }

                    for (i = 0; i < stripArray[arg].length; i++) {
                        for (j = 0; j < stripArray[arg][i].length; j++) {

                            stripArray[arg][i][j].animate({
                                opacity: 1,
                                left: j * stripWidth
                            }, transitionTime + i * 200)
                        }
                    }
                    setTimeout(unBusy, transitionTime);
                }
                //END blocksSlide

                //hide previous transitions
                images.eq(currNr).find("#descriptionBg").animate({
                    opacity: 0,
                    top: aux2
                }, 400)


                aux3 = parseFloat(images.eq(currNr).find(".descriptionText").css('top'))
                images.eq(currNr).find(".descriptionText").animate({
                    opacity: 0,
                    top: aux2
                }, 400)


                //正在播放縮圖效果設定
                currNr = arg;
                if (pagination == true) {
                    var newSrc = thumbSlider.children().eq(currNr).attr("src").replace("button_over.png", "button.png");
                    thumbSlider.children().eq(currNr).attr("src", newSrc);
                }
                else {
                    thumbSlider.children().eq(currNr).animate({
                        opacity: 0.9
                    }, 100)
                }

                aux2 = parseFloat(images.eq(currNr).find("#descriptionBg").css('top'))
                images.eq(currNr).find("#descriptionBg").animate({
                    opacity: 0,
                    top: aux2 - 10
                }, 0)


                aux3 = parseFloat(images.eq(currNr).find(".descriptionText").css('top'))
                images.eq(currNr).find(".descriptionText").animate({
                    opacity: 0,
                    top: aux2 - 10
                }, 0)

                tempCount = 0;

            }
            //END GOTOIMAGE

            function unBusy() {
                busy = false;
                if (images.eq(currNr).find("#descriptionBg").attr('style') != undefined) {
                    i++;
                    images.eq(currNr).find("#descriptionBg").stop();
                    images.eq(currNr).find(".descriptionText").stop();

                    images.eq(currNr).find("#descriptionBg").animate({
                        opacity: descriptionOpacityArray[currNr],
                        top: aux2
                    }, 500)

                    images.eq(currNr).find(".descriptionText").animate({
                        opacity: 1,
                        top: aux3
                    }, 500)
                }
            }
            //unBusy

            var descriptionBgDiv = "<div class='round-corners' id='descriptionBg' ";
            var descriptionTextDiv = "<div class='descriptionText'";


            //STRIPE CONSTRUCTION****************
            for (i = 0; i < images.length; i++) {

                descriptionBgDiv = "<div id='descriptionBg' ";
                descriptionTextDiv = "<div class='descriptionText'";

                //images.eq(i).wrap('<div class="item"></div>')
                var t = images.eq(i)[0].getAttribute("target"); 
                if (t == null) {
                    t = "";
                }

                images.eq(i).wrap('<a href="' + images.eq(i)[0].getAttribute("href") + '"' + ' target="' + t + '"' + ' ></a>')
                //alert(images.eq(i)[0].getAttribute('href'));
                //aux = images.eq(i).attr("src");
                aux = images.eq(i)[0].childNodes[0].getAttribute('src');
                descriptionOpacityArray[i] = Number(images.eq(i).attr("data-descriptionOpacity"))
                descriptionBgDiv += "style='position: absolute; left:" + images.eq(i).attr("data-descriptionX") + "px; top: " + images.eq(i).attr("data-descriptionY") + "px; z-index:98; width:" + images.eq(i).attr("data-descriptionWidth") + "px; height:" + images.eq(i).attr("data-descriptionHeight") + "px; background-color:" + images.eq(i).attr("data-descriptionBg") + "; opacity:" + images.eq(i).attr("data-descriptionOpacity") + "; filter:alpha(opacity=" + images.eq(i).attr("data-descriptionOpacity") * 100 + ");'></div>";
                descriptionTextDiv += "style='position: absolute; left:" + images.eq(i).attr("data-descriptionX") + "px; top: " + images.eq(i).attr("data-descriptionY") + "px; z-index:98; width:" + images.eq(i).attr("data-descriptionWidth") + "px; height:" + images.eq(i).attr("data-descriptionHeight") + "px;  z-index:99; color:" + images.eq(i).attr("data-descriptionTextColor") + "; text-align:" + images.eq(i).attr("data-descriptionTextAlign") + "; font-size:16;'>" + images.eq(i).attr("data-descriptionText") + "</div>";
                stripX = 0;
                stripY = 0;
                var auxArray = [];
                stripArray[i] = auxArray;

                if (images.eq(i)[0].nodeName != "A" || transitionArray[i] == "fade") {
                    sliderCon.children().eq(i).append(images.eq(i))
                    if (transitionArray[i] != "fade") {
                        transitionArray[i] = "slideFade";
                    }
                }
                else {
                    sliderCon.children().eq(i).empty();
                    function addArrayLine() {
                        k++;
                        var auxArray2 = [];
                        auxArray[k] = auxArray2;
                    }
                    k = 0;
                    if (transitionTypeArray[i] == "cubes") {
                        var auxArray2 = [];
                        auxArray[k] = auxArray2;
                        stripNrY = options.stripNrY;
                        stripHeight = totalHeight / stripNrY;
                    }
                    else {
                        stripHeight = totalHeight;
                        stripNrY = 1;
                    }

                    for (j = 0; j < stripNrX * stripNrY; j++) {
                        var aux2 = -stripX + "px " + -stripY + "px";
                        sliderCon.children().eq(i).append('<div class="strip"></div>');

                        if (transitionTypeArray[i] == "cubes") {
                            stripArray[i][k][(j - k * stripNrX)] = sliderCon.children().eq(i).children().eq(j);
                            if (i < 2) {
                                //console.log('j:',j)
                                //console.log("el e:",j-k*stripNrX)
                                //console.log(i,k,(j-k*stripNrX))
                                //console.log(stripArray[i][k][(j-k*stripNrX)])
                            }
                        }
                        else {
                            auxArray[j] = sliderCon.children().eq(i).children().eq(j)
                        }


                        sliderCon.children().eq(i).children().eq(j).css({
                            'background-position': aux2,
                            'width': stripWidth,
                            'height': stripHeight,
                            'position': 'absolute',
                            'left': stripX,
                            'top': stripY,
                            'background-image': 'url(' + aux + ')',
                            'background-repeat': 'no-repeat',
                            'z-index': j
                        })

                        stripX += stripWidth;

                        if (Math.ceil(stripX) >= totalWidth) {
                            stripY += stripHeight;
                            if (transitionTypeArray[i] == "cubes")
                                addArrayLine();
                            stripX = 0;
                        }
                    }
                }


                if (descriptionBgDiv.search("undefined") == -1) {
                    sliderCon.children().eq(i).append(descriptionBgDiv)
                }
                if (descriptionTextDiv.search("undefined") == -1) {
                    sliderCon.children().eq(i).append(descriptionTextDiv)
                }


                //sliderCon.children().eq(i).find('#descriptionBg').css('opacity',0.5)
            }




            images = sliderCon.children();

            var imgPos = 0;

            for (i = 0; i < images.length; i++) {
                images.eq(i).css({
                    'left': imgPos,
                    'top': 0,
                    'position': 'absolute'
                })
                //if(images.eq(i).find("#descriptionBg").attr('style')!=undefined)
                //descriptionOpacityArray[i]=Number(images.eq(i).attr('data-descriptionOpacity'))
                imgPos += totalWidth;
            }

            if (isSlideshow == "on") {
                intervalID = setInterval(checkTimer, 1000)
            }


            function checkTimer() {
                if (paused == false)
                    tempCount++;

                if (tempCount >= slideshowTime) {
                    gotoNext();
                }
            }
            //checkTimer

            function gotoNext() {
                if (currNr == nrChildren - 2)
                    gotoImage(0)
                else
                    gotoImage(currNr + 1)

                if (Math.floor(currNr / thumbNrPage) != currPage) {
                    gotoPage(Math.floor(currNr / thumbNrPage))
                }
            }
            //gotoNext


            $(this).mouseover(function () {
                paused = true;
            })

            $(this).mouseout(function () {
                paused = false;
            })

            return this;

        }); // end each

    } //shiftGallery
})(jQuery);
