﻿
var pauseSlideShow = false;
var allowClick = true;
var transitionSpeed = 1000;
var transitionDelay = 5000;
var loopId = 0;
var effect = 'fade'; //'fade', 'slidertl', 'slideltr'
var params = new Array();

jQuery(document).ready(function () {
    jQuery.ajax({
        url: '/DesktopModules/SteadyRain/M4/QueryStringService.asmx/GetUrlQueries',
        type: 'POST',
        data: '{ "Url":"' + window.location.href + '" }',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            params[0] = 'Main';
            if (msg.d.Region != null)
                if (msg.d.Region != '')
                    params.push('Region-' + msg.d.Region);
            if (msg.d.ListingType != null)
                if (msg.d.ListingType != '')
                    params.push('ListingType-' + msg.d.ListingType);
            if (msg.d.TagNames != null)
                if (msg.d.TagNames != '')
                    params.push('TagNames-' + msg.d.TagNames);
            LoadXML(params);
        }
    });
});

jQuery(document).ajaxError(function (e, xhr, settings, exception) {
    //alert('error in: ' + settings.url + ' \\n' + 'error:\\n' + exception);
    LoadXML(params);
});

function LoadXML(params) {
    var xmlfile = params.pop();
    if (xmlfile != 'undefined') {
        var xmlpath = '/portals/0/slideshow/' + xmlfile + '.xml';

        jQuery.get(xmlpath, function (data) {
            BuildSlideShow(data);
        });
    } else {
        LoadXML(params);
    }
}

function BuildSlideShow(data) {
    //next we must build the slide show
    var slideId = 1;
    jQuery(data).find('slide').each(function () {
        var imageurl = jQuery(this).find('file').attr("url");
        var imagealt = jQuery(this).find('file').text();
        var arrowurl = jQuery(this).find('arrow').attr("url");
        var arrowalt = jQuery(this).find('arrow').text();
        var color = jQuery(this).find('color').text();
        var description = jQuery(this).find('description').text();
        var href = jQuery(this).find('url').text();
        var currentSlide = jQuery(this).find('icon').text();
        var itemhtml = "";

        itemhtml += '<div class="slide" slidenumber="' + slideId + '">';

        itemhtml += '<a href="' + href + '"><img class="image" alt="' + imagealt + '" src="' + imageurl + '" border="0" /></a>';

        itemhtml += '<div class="footerBar">' + BuildIconBar(data, currentSlide) + '<img class="slide-arrow" src="' + arrowurl + '" alt="' + arrowalt + '" /><p>' + description + '&nbsp;-&nbsp;</p><a href="' + href + '" style="color: ' + color + ';">click here to learn more</a></div>';

        itemhtml += '</div>';

        jQuery('.slideshow').append(itemhtml);

        slideId += 1;
    });

    jQuery('.slide-icon').click(function () {
        if (allowClick) {
            allowClick = false;
            loopId += 1;
            var currentSlideId = jQuery(this).attr('currentslide');
            var targetSlideId = jQuery(this).attr('targetslide');

            //alert("currentslide: " + currentSlideId + " targetslide: " + targetSlideId);

            if (currentSlideId > targetSlideId) {
                //alert("Call left to right");
                jump_LTR(currentSlideId, targetSlideId);
            } else if (currentSlideId < targetSlideId) {
                //alert("Call right to left");
                jump_RTL(currentSlideId, targetSlideId);
            } else {
                allowClick = true;
            }
        };
    });

    jQuery('.slideshow').mouseover(function () {
        pauseSlideShow = true;
    });

    jQuery('.slideshow').mouseout(function () {
        pauseSlideShow = false;
    });

    jQuery('.slideshow .slide').first().show();

    setTimeout(function () {
        loopId += 1;
        loopSlideShow(jQuery('.slideshow .slide').first(), loopId);
    }, transitionDelay);

};

function jump_RTL(currentSlideId, targetSlideId) {
    var _currentSlide = jQuery('.slideshow .slide').get(currentSlideId - 1)
    var _targetSlide = jQuery('.slideshow .slide').get(targetSlideId - 1)
    switch (effect) {
        case 'fade':
            loopId += 1;
            var myLoopId = loopId;
            jQuery(_targetSlide).fadeIn(transitionSpeed);
            jQuery(_currentSlide).fadeOut(transitionSpeed, function () {
                setTimeout(function () {
                    loopSlideShow(_targetSlide, myLoopId);
                }, transitionDelay);
            });
            allowClick = true;
            break;
        case 'slidertl':
            RTL(_currentSlide, targetSlideId);
            break;
        case 'slideltr':
            RTL(_currentSlide, targetSlideId);
            break;
    }
}

function RTL(currentSlide, targetSlideId) {
    var nextSlide = jQuery(currentSlide).next();
    jQuery(nextSlide).css({ marginLeft: '980px' });
    jQuery(nextSlide).show();
    jQuery(nextSlide).animate({ marginLeft: '0px' }, (transitionSpeed / 2) - 10);
    jQuery(currentSlide).animate({ marginLeft: '-980px' }, transitionSpeed / 2, function () {
        jQuery(currentSlide).removeAttr('style');
        jQuery(currentSlide).hide();

        jQuery(currentSlide).removeAttr('active');
        jQuery(nextSlide).attr('active', true);

        if (jQuery(nextSlide).attr('slidenumber') == targetSlideId) {
            setTimeout(function () {
                allowClick = true;
                loopId += 1;
                loopSlideShow(nextSlide, loopId);
            }, transitionDelay);
        } else {
            RTL(nextSlide, targetSlideId)
        };
    });
}

function jump_LTR(currentSlideId, targetSlideId) {
    var _currentSlide = jQuery('.slideshow .slide').get(currentSlideId - 1)
    var _targetSlide = jQuery('.slideshow .slide').get(targetSlideId - 1)
    switch (effect) {
        case 'fade':
            loopId += 1;
            var myLoopId = loopId;
            jQuery(_targetSlide).fadeIn(transitionSpeed);
            jQuery(_currentSlide).fadeOut(transitionSpeed, function () {
                setTimeout(function () {
                    loopSlideShow(_targetSlide, myLoopId);
                }, transitionDelay);
            });
            allowClick = true;
            break;
        case 'slidertl':
            LTR(_currentSlide, targetSlideId);
            break;
        case 'slideltr':
            LTR(_currentSlide, targetSlideId);
            break;

    }
}

function LTR(currentSlide, targetSlideId) {
    var nextSlide = jQuery(currentSlide).prev();
    jQuery(nextSlide).css({ marginLeft: '-980px' });
    jQuery(nextSlide).show();
    jQuery(nextSlide).animate({ marginLeft: '0px' }, (transitionSpeed / 2) - 10);
    jQuery(currentSlide).animate({ marginLeft: '980px' }, transitionSpeed / 2, function () {
        jQuery(currentSlide).removeAttr('style');
        jQuery(currentSlide).hide();

        jQuery(currentSlide).removeAttr('active');
        jQuery(nextSlide).attr('active', true);

        if (jQuery(nextSlide).attr('slidenumber') == targetSlideId) {
            setTimeout(function () {
                allowClick = true;
                loopId += 1;
                loopSlideShow(nextSlide, loopId);
            }, transitionDelay);
        } else {
            LTR(nextSlide, targetSlideId)
        };
    });
}

function loopSlideShow(currentSlide, _loopId) {
    if (!pauseSlideShow && _loopId == loopId) {
        allowClick = false;
        var nextSlide = jQuery(currentSlide).next();
        var prevSlide = jQuery(currentSlide).prev();

        if (jQuery(nextSlide).length == 0) {
            nextSlide = jQuery('.slideshow .slide').first();
        };

        if (jQuery(prevSlide).length == 0) {
            prevSlide = jQuery('.slideshow .slide').last();
        };

        switch (effect) {
            case 'fade':
                jQuery(nextSlide).fadeIn(transitionSpeed);
                jQuery(currentSlide).fadeOut(transitionSpeed, function () {
                    allowClick = true;
                    setTimeout(function () {
                        loopSlideShow(nextSlide, _loopId);
                    }, transitionDelay);
                });
                break;
            case 'slidertl':
                jQuery(nextSlide).css({ marginLeft: '980px' });
                jQuery(nextSlide).show();
                jQuery(nextSlide).animate({ marginLeft: '0px' }, transitionSpeed - 10);
                jQuery(currentSlide).animate({ marginLeft: '-980px' }, transitionSpeed, function () {
                    jQuery(currentSlide).removeAttr('style');
                    jQuery(currentSlide).hide();

                    jQuery(currentSlide).removeAttr('active');
                    jQuery(nextSlide).attr('active', true);

                    allowClick = true;

                    setTimeout(function () {
                        loopSlideShow(nextSlide, _loopId);
                    }, transitionDelay);
                });
                break;
            case 'slideltr':
                jQuery(prevSlide).css({ marginLeft: '-980px' });
                jQuery(prevSlide).show();
                jQuery(prevSlide).animate({ marginLeft: '0px' }, transitionSpeed - 10);
                jQuery(currentSlide).animate({ marginLeft: '980px' }, transitionSpeed, function () {
                    jQuery(currentSlide).removeAttr('style');
                    jQuery(currentSlide).hide();

                    jQuery(currentSlide).removeAttr('active');
                    jQuery(prevSlide).attr('active', true);

                    allowClick = true;

                    setTimeout(function () {
                        loopSlideShow(prevSlide, _loopId);
                    }, transitionDelay);
                });
                break;
        }
    } else {
        setTimeout(function () {
            loopSlideShow(currentSlide, _loopId);
        }, 100);
    };
};

function BuildIconBar(data, currentSlide) {
    var IconBar = ""

    jQuery(data).find('slide').each(function () {
        var icon_on_url = jQuery(this).find('icon').attr("on_url");
        var icon_off_url = jQuery(this).find('icon').attr("off_url");
        var icon_alt = jQuery(this).find('icon').text();
        var icon_url;

        if (currentSlide == icon_alt) {
            icon_url = icon_on_url
        } else {
            icon_url = icon_off_url
        }

        IconBar += '<img class="slide-icon" src="' + icon_url + '" alt="' + icon_alt + '" currentslide="' + currentSlide + '" targetslide="' + icon_alt + '" />';
    });

    return IconBar;
};

function GetActiveSlide() {
    var activeSlide;

    jQuery('.slideshow .slide').each(function () {
        if (jQuery(this).attr('active') == "true") {
            activeSlide = this;
        }
    });

    return activeSlide;
}
