﻿var speed = 400;
var current = 0;
var properties = [];
var animating = false;
var slideshowTime = 11000;
var timer = 0;
var enableAutoTimer = true;
var cacheAhead = 4;
var cacheBehind = 2;
function setDefaults(target, propertList, timeEnabled, timerDelay, transitionTime, setCacheAhead, setCacheBehind) {
var slides = $('.slideframe1');
speed = transitionTime;
slideshowTime = timerDelay;
enableAutoTimer = timeEnabled;
properties = propertList;
current = target;
cacheAhead = setCacheAhead;
cacheBehind = setCacheBehind;
slides.each(function (index) {
var id = $(this).attr('rel');
var opacity = (id != 'slide_' + target ? 0 : 1);
var zIndex = (id != 'slide_' + target ? slides.length - index + 10 : slides.length + 10);
$(this).css({ opacity: opacity, 'z-index': zIndex });
properties[index].loaded = (index >= current - cacheBehind && index <= current + cacheAhead);
});
var left = properties.length < 3 || target < 2 ? 0 : (target < properties.length - 2 ? (target - 1) * 246 : (properties.length - 3) * 246);
$('.footer .frame .center .sleeve').css({ width: (properties.length * 246) + 'px', marginLeft: -left + 'px' });
showTime();
move(target);
}
function move(target) {
if (animating)
return;
setAdditonalBanners();
var slides = $('.slideframe1');
$('.footer .frame .center .button').each(function (index) {
$(this).removeClass('selected');
if (index == target)
$(this).addClass('selected');
});
if (target == 0)
$('#Previous').removeClass('left').addClass('left_disabled');
else
$('#Previous').removeClass('left').removeClass('left_disabled').addClass('left');
if (target == properties.length - 1)
$('#Next').removeClass('right').addClass('right_disabled');
else
$('#Next').removeClass('right').removeClass('right_disabled').addClass('right');
if (target == properties.length - 1)
$('#Next2').removeClass('right').addClass('right_disabled');
else
$('#Next2').removeClass('right').removeClass('right_disabled').addClass('right');
var left = properties.length < 3 || target < 2 ? 0 : (target < properties.length - 2 ? (target - 1) * 246 : (properties.length - 3) * 246);
$('.footer .frame .center .sleeve').animate({ marginLeft: -left + 'px' }, 500);
if (target == current)
return;
slides.each(function (index) {
var id = $(this).attr('rel');
var opacity = (id != 'slide_' + target && id != 'slide_' + current ? 0 : 1);
var zIndex = (id != 'slide_' + target && id != 'slide_' + current ? slides.length - index + 10 : id == 'slide_' + target ? slides.length + 10 : slides.length + 15);
$(this).css({ opacity: opacity, 'z-index': zIndex });
});
animating = true;
$(slides[current]).animate({ opacity: 0 }, speed, function () {
$(slides[target]).css({ 'z-index': slides.length + 50 });
current = target;
animating = false;
showTime();
});
}
function showTime() {
if (!enableAutoTimer)
return;
var target = (current + 1 > properties.length - 1 ? 0 : current + 1);
clearInterval(timer);
timer = setInterval(function () {
move(target);
}, slideshowTime);
}
function previous() {
if (current > 0)
move(current - 1);
}
function next() {
    if (current < properties.length - 1)
        move(current + 1);
}
function Trim(str) {
    try {
        if (!str || str == 'undefined' || str == null) {
            return '';
        } else {
            str = String(str);
            return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        }
    } catch (e) {
        return '';
    }
}
function setAdditonalBanners() {
var start = current - cacheBehind < 0 || current == properties.length - 1 ? 0 : current - cacheBehind;
var end = current + cacheAhead < properties.length ? current + cacheAhead : cacheAhead;
var slides = $('.slideframe1');
slides.each(function (index) {
var id = $(this).attr('rel');
var o = properties[index];
if (!o.loaded && index >= start && index <= end) {
o.loaded = true;
$(this).css({ 'background-image': 'url(' + o.BannerBackgroundImage + ')' });
$(this).find('.slideframe2').css({ 'background-image': 'url(' + o.BannerForegroundImage + ')' });
$(this).find('.slideframe2').find('.slidecontent').css({ 'background-image': 'url(' + o.AdCopyImage + ')' });
      }
  });
}
