﻿var ApplicationsGallery = {
    currentSlideshow: null,
    imagesDisplayAr: null,
    creditsDisplayAr: null,
    rotateImagesSetTimeout: 0,
    selectImageIdSetTimeout: 0,
    firstSetTimeout: 0,
    lastItem: 0,
    timer: 9000,
    pauseSlide: false,
    lightboxObj: null,
    searchItems: function() {
        var newSearch = $("GallerySearchStr").value;
        var oldSearch = location.href.substring(location.href.indexOf("search=") + 7);
        var newLocationHref = "";
        if (location.href.indexOf("tab=" + $("CurrentTabCodeHidden").value) > -1)
            newLocationHref = location.href;
        else
            newLocationHref = location.href + "?tab=" + $("CurrentTabCodeHidden").value;
        if ((newSearch != "") && (newSearch != oldSearch)) {
            if (location.href.indexOf("search=") > -1) location.href = newLocationHref.substring(0, newLocationHref.indexOf("search=") + 7) + newSearch;
            else location.href = newLocationHref + "&search=" + newSearch;
        }
    },
    showVideo: function(path, title) {
        if ($("lightbox-shadow")) $("lightbox-shadow").setStyle({ width: '480px', height: '410px' });
        if ($("lightbox-back")) $("lightbox-back").setStyle({ width: '480px', height: '410px' }).innerHTML = $("lightbox-back").innerHTML;

        ApplicationsGallery.lightboxObj = new lightbox($("lightboxAll"), path, true, title, null, 480, 360);
    },
    showGallery: function(id) {
        if (!$("GalleryHolder1")) return;
        var startImage = "";
        var galleryUrl = SiteParams.SiteUrl + "Flash/Gallery.swf?stageWidth=634&xmlPath=" + escape(SiteParams.SiteUrl + "AGalleryData.ashx?l=" + SiteParams.Language + "&id=" + id) + startImage;
        addFlashInside("GalleryHolder1", galleryUrl, "pageGallery", 634, 330, "#000000", "opaque");
    },
    showSlideShow: function(id, imgIndex, title, imgCount) {
        if ($("lightbox-shadow")) $("lightbox-shadow").setStyle({ width: '675px', height: '548px' });
        if ($("lightbox-back")) $("lightbox-back").setStyle({ width: '675px', height: '548px' });

        if (ApplicationsGallery.currentSlideshow != id) {//setup new gallery
            $("imagesCount").innerHTML = imgCount;
            $("slideshow-items").innerHTML = $("largeImages-" + id).innerHTML;
            ApplicationsGallery.imagesDisplayAr = $("slideshow-items").getElementsByTagName("div");
            ApplicationsGallery.creditsDisplayAr = $("slideshow-items").getElementsByTagName("span");
            ApplicationsGallery.lastItem = ApplicationsGallery.imagesDisplayAr.length - 1;
            ApplicationsGallery.currentSlideshow = id;
        }

        $("imageIndex").innerHTML = imgIndex + 1;
        $("title").innerHTML = title;
        ApplicationsGallery.imagesArCurrentIndex = imgIndex;
        $("start").setStyle({ display: 'none' });
        $("pause").setStyle({ display: 'block' });

        //set up curr item visibility
        for (var i = 0; i < (ApplicationsGallery.lastItem + 1); i++) {
            if (i == ApplicationsGallery.imagesArCurrentIndex) {
                $(ApplicationsGallery.imagesDisplayAr[i]).setStyle({ display: 'block' });
                $(ApplicationsGallery.creditsDisplayAr[i]).setStyle({ display: 'block' });
            }
            else {
                $(ApplicationsGallery.imagesDisplayAr[i]).setStyle({ display: 'none' });
                $(ApplicationsGallery.creditsDisplayAr[i]).setStyle({ display: 'none' });
            }
        }

        ApplicationsGallery.lightboxObj = new lightbox($("lightboxAll"), $("slideshow"), false);

        ApplicationsGallery.startFeaturedRotate();
    },
    closeLightBox: function() {
        if ($("slideshow-wrap")) $("slideshow-wrap").insert($("slideshow"));
        if (ApplicationsGallery.lightboxObj) ApplicationsGallery.lightboxObj.deactivate();
    },
    goNext: function() {
        var goToPage = (ApplicationsGallery.imagesArCurrentIndex == ApplicationsGallery.lastItem) ? 0 : ApplicationsGallery.imagesArCurrentIndex + 1;
        ApplicationsGallery.selectImageObj(goToPage);
    },
    goBack: function() {
        var goToPage = (ApplicationsGallery.imagesArCurrentIndex == 0) ? ApplicationsGallery.lastItem : ApplicationsGallery.imagesArCurrentIndex - 1;
        ApplicationsGallery.selectImageObj(goToPage);
    },
    pause: function() {
        if (!ApplicationsGallery.pauseSlide) {
            clearTimeout(ApplicationsGallery.firstSetTimeout);
            clearTimeout(ApplicationsGallery.rotateImagesSetTimeout);
            clearTimeout(ApplicationsGallery.selectImageIdSetTimeout);
            ApplicationsGallery.pauseSlide = true
            $("start").setStyle({ display: 'block' });
            $("pause").setStyle({ display: 'none' });
        }
        else {
            ApplicationsGallery.pauseSlide = false;
            ApplicationsGallery.goNext();
            $("pause").setStyle({ display: 'block' });
            $("start").setStyle({ display: 'none' });

        }
    },
    switchImages: function(displayId) {
        for (var i = 0; i < (ApplicationsGallery.lastItem + 1); i++) {
            //		if (i==displayId) $(ApplicationsGallery.imagesDisplayAr[displayId]).appear({duration:3});
            //		else $(ApplicationsGallery.imagesDisplayAr[i]).fade();
            if (i == displayId) {
                $(ApplicationsGallery.imagesDisplayAr[displayId]).setStyle({ display: 'block' });
                $(ApplicationsGallery.creditsDisplayAr[displayId]).setStyle({ display: 'block' });
            }
            else {
                $(ApplicationsGallery.imagesDisplayAr[i]).setStyle({ display: 'none' });
                $(ApplicationsGallery.creditsDisplayAr[i]).setStyle({ display: 'none' });
            }
        }

        $("imageIndex").innerHTML = (displayId + 1);
    },
    selectImageObj: function(imageObj) {
        clearTimeout(ApplicationsGallery.firstSetTimeout);
        clearTimeout(ApplicationsGallery.rotateImagesSetTimeout);
        clearTimeout(ApplicationsGallery.selectImageIdSetTimeout);
        ApplicationsGallery.imagesArCurrentIndex = imageObj;
        ApplicationsGallery.switchImages(imageObj);
        if (!ApplicationsGallery.pauseSlide) ApplicationsGallery.selectImageIdSetTimeout = setTimeout('ApplicationsGallery.rotateImages(' + ApplicationsGallery.timer + ')', ApplicationsGallery.timer);
    },
    rotateImages: function(timeOutToNextChange) {
        var imagesArNextIndex = (ApplicationsGallery.imagesArCurrentIndex == ApplicationsGallery.lastItem) ? 0 : ApplicationsGallery.imagesArCurrentIndex + 1;
        ApplicationsGallery.switchImages(imagesArNextIndex);
        ApplicationsGallery.imagesArCurrentIndex = imagesArNextIndex;
        ApplicationsGallery.rotateImagesSetTimeout = setTimeout('ApplicationsGallery.rotateImages(' + ApplicationsGallery.timer + ')', timeOutToNextChange);
    },
    startFeaturedRotate: function() {
        ApplicationsGallery.firstSetTimeout = setTimeout('ApplicationsGallery.rotateImages(' + ApplicationsGallery.timer + ')', ApplicationsGallery.timer);
        return true;
    },
    sendToNotebook: function() {
        var current = ApplicationsGallery.imagesDisplayAr[ApplicationsGallery.imagesArCurrentIndex];
        if (current.id != "") {
            var id = current.id.split('_')[1];
            Notebook.AddAGalleryImage(id.toString(), false, false, '');
        }
        ApplicationsGallery.closeLightBox();
    },
    sendToFriend: function(subject, body, path, tab) {
        var current = ApplicationsGallery.imagesDisplayAr[ApplicationsGallery.imagesArCurrentIndex];
        var url = "mailto:?subject=" + subject + "&body=" + body;
        if (current.id != "") {
            url = url + " " + escape(ApplicationsGallery.imagesDisplayAr[ApplicationsGallery.imagesArCurrentIndex].childElements()[0].src);
        }
        else {
            url = url + " " + path + "?tab=" + tab;
        }
        window.open(url);
        ApplicationsGallery.closeLightBox();
    },
    hideCategoryHolder: function(holderId) {
        $(holderId).hide();
    }
}