/**
* @author Alexander Farkas
* v. 1.22
*/
(function ($) {
    if (!document.defaultView || !document.defaultView.getComputedStyle) { // IE6-IE8
        var oldCurCSS = $.curCSS;
        $.curCSS = function (elem, name, force) {
            if (name === 'background-position') {
                name = 'backgroundPosition';
            }
            if (name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[name]) {
                return oldCurCSS.apply(this, arguments);
            }
            var style = elem.style;
            if (!force && style && style[name]) {
                return style[name];
            }
            return oldCurCSS(elem, 'backgroundPositionX', force) + ' ' + oldCurCSS(elem, 'backgroundPositionY', force);
        };
    }

    var oldAnim = $.fn.animate;
    $.fn.animate = function (prop) {
        if ('background-position' in prop) {
            prop.backgroundPosition = prop['background-position'];
            delete prop['background-position'];
        }
        if ('backgroundPosition' in prop) {
            prop.backgroundPosition = '(' + prop.backgroundPosition;
        }
        return oldAnim.apply(this, arguments);
    };

    function toArray(strg) {
        strg = strg.replace(/left|top/g, '0px');
        strg = strg.replace(/right|bottom/g, '100%');
        strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g, "$1px$2");
        var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
        return [parseFloat(res[1], 10), res[2], parseFloat(res[3], 10), res[4]];
    }

    $.fx.step.backgroundPosition = function (fx) {
        if (!fx.bgPosReady) {
            var start = $.curCSS(fx.elem, 'backgroundPosition');
            if (!start) {//FF2 no inline-style fallback
                start = '0px 0px';
            }

            start = toArray(start);
            fx.start = [start[0], start[2]];
            var end = toArray(fx.end);
            fx.end = [end[0], end[2]];

            fx.unit = [end[1], end[3]];
            fx.bgPosReady = true;
        }
        //return;
        var nowPosX = [];
        nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
        nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
        fx.elem.style.backgroundPosition = nowPosX[0] + ' ' + nowPosX[1];

    };
})(jQuery);


//settings
var timeout = 6; //timeout between animating  slides (in seconds)
var timebetweenslides = 500; //timeout between removing a slide and showing next one (in miliseconds)
var isAnimating = false;

var currentSlide = 1;
var currentBGpos = 50;
var lteie8 = false;
if ($.browser.msie) {
    var vers = Number(parseInt($.browser.version, 10));
    if (vers < 9) var lteie8 = true;
}

function nextSlide() {
    if (isAnimating == false) {
        if (currentSlide == 6) {
            currentSlide = 1;
            removeSlide(6);
        } else {
            removeSlide(currentSlide);
            currentSlide++;
        }
        isAnimating = true;
    }
}
function prevSlide() {
    if (isAnimating == false) {
        if (currentSlide == 1) {
            currentSlide = 6;
            removeSlideBackwards(1);
        } else {
            removeSlideBackwards(currentSlide);
            currentSlide--;
        }
        isAnimating = true;
    }
}

//function setSelectedCandy(index) {
//    $("#slider-nav li").removeClass("selected");
//    $("#slider-nav li:nth-child(" + index + ")").addClass("selected");
//    currentSlide = index;
//    isAnimating = false;
//}

function setSelectedCandy(index) {
    $("#slider-nav li a").removeClass("selected");
    $("#slider-nav li:nth-child(" + index + ") a").addClass("selected");
    currentSlide = index;
    isAnimating = false;
}

function showSlide(index) {

    if (isAnimating == false) {
        isAnimating = true;
        if (index < currentSlide)
            removeSlideBackwards(currentSlide, index);
        else {
            removeSlide(currentSlide, index);
        }
    }
}

function insertSlide(index) {
    var thisSlide = "";
    if (index == 1) { thisSlide = "one"; }
    if (index == 2) { thisSlide = "two"; }
    if (index == 3) { thisSlide = "three"; }
    if (index == 4) { thisSlide = "four"; }
    if (index == 5) { thisSlide = "five"; }
    if (index == 6) { thisSlide = "six"; }
    $("#slider section." + thisSlide).fadeIn(200);
    var kimg = $("#slider section." + thisSlide + " img");
    var kh2 = $("#slider section." + thisSlide + " h2");
    var kcopy = $("#slider section." + thisSlide + " .copy");

    kh2.animate({ left: '50%', opacity: 1, marginLeft: '-395px' }, 1000, 'easeInOutExpo');
    kcopy.delay(200).animate({ left: '50%', opacity: 1, marginLeft: '-395px' }, 800, 'easeInOutExpo');

    if (!lteie8) {
        kimg.delay(200).animate({ left: '50%', opacity: 1, marginLeft: '120px' }, 900, 'easeInOutExpo', function () { setSelectedCandy(index); });
    } else {
        kimg.css("display", "block").delay(200).animate({ left: '50%', marginLeft: '120px' }, 900, 'easeInOutExpo', function () { setSelectedCandy(index); });
    }
}
function removeSlide(index, newIndex) {
    if (newIndex == null)
        newIndex = index + 1;
    var thisSlide = "";
    if (index == 1) { thisSlide = "one"; }
    if (index == 2) { thisSlide = "two"; }
    if (index == 3) { thisSlide = "three"; }
    if (index == 4) { thisSlide = "four"; }
    if (index == 5) { thisSlide = "five"; }
    if (index == 6) { thisSlide = "six"; }
    var kimg = $("#slider section." + thisSlide + " img");
    var kh2 = $("#slider section." + thisSlide + " h2");
    var kcopy = $("#slider section." + thisSlide + " .copy");
    if (timeout) clearTimeout(timeout);
    var timeout = setTimeout("resetSlide(" + Number(newIndex) + ")", timebetweenslides);
    kh2.stop(true, true).animate({ left: '-1%', opacity: 0 }, 1200, 'easeInOutBack');
    kcopy.stop(true, true).delay(300).animate({ left: '-1%', opacity: 0 }, 1200, 'easeInOutBack');
    //    kbutton.stop(true, true).fadeOut();
    if (!lteie8) {
        kimg.stop(true, true).delay(400).animate({ left: '-1%', opacity: 0 }, 1200, 'easeInOutExpo');
    } else {
        kimg.stop(true, true).delay(400).animate({ left: '-100%' }, 1200, 'easeInOutExpo', function () { kimg.hide(); });
    }
}
function removeSlideBackwards(index, newIndex) {
    if (newIndex == null)
        newIndex = index - 1;
    var thisSlide = "";
    if (index == 1) { thisSlide = "one"; }
    if (index == 2) { thisSlide = "two"; }
    if (index == 3) { thisSlide = "three"; }
    if (index == 4) { thisSlide = "four"; }
    if (index == 5) { thisSlide = "five"; }
    if (index == 6) { thisSlide = "six"; }

    var kimg = $("#slider section." + thisSlide + " img");
    var kh2 = $("#slider section." + thisSlide + " h2");
    var kcopy = $("#slider section." + thisSlide + " .copy");

    if (timeout) clearTimeout(timeout);
    var timeout = setTimeout("resetSlideBackwards(" + Number(newIndex) + ")", timebetweenslides);

    kh2.stop(true, true).delay(400).animate({ left: '101%', opacity: 0 }, 800, 'easeInOutExpo');
    kcopy.stop(true, true).delay(200).animate({ left: '101%', opacity: 0 }, 700, 'easeInOutExpo');

    if (!lteie8) {
        kimg.stop(true, true).animate({ left: '101%', opacity: 0 }, 1200, 'easeInOutBack');
    } else {
        kimg.stop(true, true).animate({ left: '101%' }, 1200, 'easeInOutBack', function () { kimg.hide(); });
    }
}
function resetSlide(index) {
    if (index > 6) index = 1;
    var thisSlide = "";
    if (index == 1) { thisSlide = "one"; }
    if (index == 2) { thisSlide = "two"; }
    if (index == 3) { thisSlide = "three"; }
    if (index == 4) { thisSlide = "four"; }
    if (index == 5) { thisSlide = "five"; }
    if (index == 6) { thisSlide = "six"; }

    var kimg = $("#slider section." + thisSlide + " img");
    var kh2 = $("#slider section." + thisSlide + " h2");
    var kcopy = $("#slider section." + thisSlide + " .copy");

    kh2.css({ "left": '100%', "margin-left": '0' });
    kcopy.css({ "left": '100%', "margin-left": '0' });
    kimg.css({ "left": '100%', "margin-left": '0' });

    insertSlide(index);
}
function resetSlideBackwards(index) {
    if (index < 1) index = 6;
    var thisSlide = "";
    if (index == 1) { thisSlide = "one"; }
    if (index == 2) { thisSlide = "two"; }
    if (index == 3) { thisSlide = "three"; }
    if (index == 4) { thisSlide = "four"; }
    if (index == 5) { thisSlide = "five"; }
    if (index == 6) { thisSlide = "six"; }

    var kimg = $("#slider section." + thisSlide + " img");
    var kh2 = $("#slider section." + thisSlide + " h2");
    var kcopy = $("#slider section." + thisSlide + " .copy");

    kh2.css({ "left": '-1%', "margin-left": -kh2.width() });
    kcopy.css({ "left": '-1%', "margin-left": -kcopy.width() });
    kimg.css({ "left": '-80%', "margin-left": -kimg.width() });

    insertSlide(index);
}

$(document).ready(function () {

    //animate slider
    var autoAnimation = window.setInterval("nextSlide()", timeout * 1000);
    $("#teaser").hover(function () {
        window.clearInterval(autoAnimation);

        if (!lteie8) {
            $("#teaser .slider-prev").fadeIn();
            $("#teaser .slider-next").fadeIn();
        } else {
            $("#teaser .slider-prev").show();
            $("#teaser .slider-next").show();
        }
    }, function () {
        autoAnimation = window.setInterval("nextSlide()", timeout * 1000);
        if (!lteie8) {
            $("#teaser .slider-prev").stop(true, true).fadeOut();
            $("#teaser .slider-next").stop(true, true).fadeOut();
        } else {
            $("#teaser .slider-prev").hide();
            $("#teaser .slider-next").hide();
        }
    });





})
