﻿jQuery(document).ready(function() {

    var timer = "";

    $(".scrollprev").hover(function() {
        timer = setInterval(function() { previnterval(":last-child"); }, 500);
    }, function() {
        clearInterval(timer);
    });

    $(".scrollnext").hover(function() {
        timer = setInterval(function() { nextinterval(":first-child"); }, 500);
    }, function() {
        clearInterval(timer);
    });
});

function geteventpreview(tabid, evtid) {
    $(".preview").hide();
    $(tabid).show();
    $("#mycarousel li").each(function() {
        $(this).children().removeClass('selectedevent');
    });
    $(".evtsliderbackul li").each(function() {
        $(this).children().removeClass('selectedevent');
    });
    $(evtid).addClass('selectedevent');
    centerselected();
    return false;
}

function previnterval() {
    if ($("#mycarousel li:first-child").children().attr("id") == "1"){
        if ($(".evtsliderbackul li").length > 0) {
            $("#mycarousel").prepend($(".evtsliderbackul li:first-child").clone());
            $(".evtsliderbackul li:first-child").remove();
        } else {
            $("#mycarousel").prepend("<li></li>");
        }
    } else {
        $("#mycarousel").prepend($(".evtsliderbackul li:first-child").clone());
        $(".evtsliderbackul li:first-child").remove();
    }
}

function nextinterval(child) {
    var carousallen = $("#mycarousel li").length;
    if ($("#mycarousel li:last-child").prev().children().length == 0) {
        $("#mycarousel li:last-child").prev().remove();
        $("#mycarousel li:last-child").remove();
    }
    if ($("#mycarousel li:last-child").prev().children().attr("id") == $("#mycarousel li:first-child").children().attr("id")) {

    } else {
        $(".evtsliderbackul").prepend($("#mycarousel").children(child).clone());
        $("#mycarousel li" + child).remove();
    }
}

function centerselected() {
    var scrolleft = $(".scrollWrapper").offset().left;
    var firstwidth = $("#mycarousel li:first-child").width();
    if ($(".selectedevent").offset().left < scrolleft + firstwidth) {
        if ($(".evtsliderbackul li").length > 0) {
            $("#mycarousel").prepend($(".evtsliderbackul li:first-child").clone());
            $(".evtsliderbackul li:first-child").remove();
        } else {
            $("#mycarousel").prepend("<li></li>");
        }
    } else {
        if ($(".selectedevent").offset().left > scrolleft + firstwidth*2) {
            $(".evtsliderbackul").prepend($("#mycarousel li:first-child").clone());
            $("#mycarousel li:first-child").remove();
        }
    }
}

