$(document).ready(function() {
    $.ajax({
        url: "VZaarVideoList.aspx?username=nbcapital&datatype=jsonp",
        success: function(data) {
            var data = $(data);
            $("div#moviestrip div.movies").empty().html("<ul></ul>");
            var moviestrip = $("div#moviestrip div.movies ul");
            for (i = 0; i < data.length; i++) {
                var divouter = $(document.createElement("div"));
                divouter.addClass("vl_item");
                divouter.append("<div class='vl_textheadercontainer'><div class='vl_textheader'>" + data[i].title + "</div></div>");

                var img = document.createElement("img");
                img.src = data[i].thumbnail_url;
                img.alt = "";
                $(img).click(data[i], function(data) {
                    var id = data.data.id;
                    var link = "/VzaarVideoDetail.aspx?videoid=" + id;
                    $.ajax({
                        url: link
				, success: function(data) {
				    var text = $(data).find("html").text();
				    text = text.replace(/(height=\")[0-9]+\"/g, "$1226\"");
				    text = text.replace(/(width=\")[0-9]+\"/g, "$1406\"");
				    $("#movieplayer").slideDown(200, function() {
				        $("div.player").empty().html(text);
				    });

				    var title = $(data).find("title").text();
				    $("div.info p.headline").empty().text(title);

				    var description = $(data).find("description").text();
				    $("div.info p.body").empty().text(description);

				}
				, error: function(a, b) {
				}
				, dataType: "xml"
                    });


                });

                divouter.append(img);
                $("#vl_itemwrapper").append(divouter);
            }
        }, dataType: "jsonp"
	, error: function(a, b) {
	}
    });


    $('.arrowLeft').click(function() {
        Left();
    });
    $('.arrowRight').click(function() {
        Right();
    });

    $(".autoposition").each(function() {
        var width = $(window).width() - $(this).width();
        $(this).css('left', width / 2 + "px").slideDown();
    });


    $(window).resize(function() {
        $(".autoposition").each(function() {
            var width = $(window).width() - $(this).width();
            $(this).css('left', width / 2 + "px").slideDown();
        });
    });

    $(window).scroll(function() {
        $(".autoposition").each(function() {
            var width = $(window).width() - $(this).width();
            $(this).css('left', width / 2 + "px").slideDown();
        });
    });

    $("#moviestrip").attr('unselectable', 'on');

    if ($('#pastinvestmentstable').length == 1) {
        var elmIndex = parseInt(Math.random() * $('#pastinvestmentstable tbody tr.pu').length);
        var a = $('#pastinvestmentstable tbody tr.pu:nth('+elmIndex+')').detach();
        a.insertBefore($('#pastinvestmentstable tbody tr:nth(0)'));
        $('#pastinvestmentstable tbody tr:even').addClass("odd");
    }
})


var animating = false;
function Left() {
    if (true === animating) {
        return;
    }
    animating = true;
    var step = $(".vl_item:first").width();
    var width = step * $(".vl_item").length;
    var leftpos = $("#vl_itemwrapper").position().left;

    var scrolllength = width + leftpos - 1127;
    if (scrolllength < step) {
        step = scrolllength;
    }
    $("#vl_itemwrapper").animate({ left: '-=' + step }, 500, function() {
        animating = false;
    });
}

function Right() {
    if (true === animating) {
        return;
    }
    animating = true;
    var step = $(".vl_item:first").width();
    var width = step * $(".vl_item").length;
    var leftpos = $("#vl_itemwrapper").position().left;

    if (leftpos * -1 < step) {
        step = leftpos * -1;
    }
    $("#vl_itemwrapper").animate({ left: '+=' + step }, 500, function() {
        animating = false;
    });
}
