// vertical image scroller
// items visible when carousel is not moving
var vitemsshown=5;
// width of each item within the scroller (including margin between items)
var vmyitem=59;
// amount of items to be scrolled when moved
var vscrollby=1;
// next button image
var vnextImage= { FileName:'images/scroll_down.gif', Width:214, Height:26 };
// previous button image
var vprevImage= { FileName:'images/scroll_up.gif', Width:214, Height:26 };
// next button disabled image
var nextImageDis= { FileName:'images/spacer.gif', Width:214, Height:26 };
// previous button disabled image
var prevImageDis= { FileName:'../images/spacer.gif', Width:214, Height:26 };
// mask for thumbnail to make text readable, use a spacer gif if you don't need a mask
var vthumbmask='images/mask_small.png';
// mask for main image to make text readable, use a spacer gif if you don't need a mask
var vmask='images/mask.png';
// choose to switch carousel on (true) of off (false)
var vcarousel=true;
// choose pause time of carousel
var vcarspeed=5000;
// choose step speed of carousel
var vstep=5;

var ovid=0;
function makeImage(isrc) { var image=new Image();image.src=isrc;return image; }
function vshowContent(id) {
	$("#plyr").fadeOut("fast", function () {
        var mediaDiv=document.getElementById('plyr');
		var mediaTitle=document.getElementById('vtitle');
		var mediaSubtitle=document.getElementById('vsubtitle');
		var mediaLink=document.getElementById('vreadmore');
		var mediaGenre=document.getElementById('vgenre');
		var vidObj=myVideos[id];
		var vidloc=vidObj.Image.src;
		mediaDiv.style.backgroundImage='url('+vidloc+')';
		mediaSubtitle.innerHTML = "<h2 class='homesubtitle'>" + vidObj.Artist + "</h2>";
		mediaTitle.innerHTML = "<h2 class='hometitle'>" + vidObj.Title + "</h2>";
		mediaGenre.innerHTML="<h2 class='homegenre'>"+vidObj.Genre+"</h2>";
		var morelink='';var buylink='';
		if (vidObj.Readmore!='') { morelink='<a href="'+vidObj.Readmore+'" class="more">meer info</a>'; }
		if (vidObj.Buyticket!='') { buylink= vidObj.Buyticket; }
		mediaLink.innerHTML=morelink+buylink;
		$("#plyr").fadeIn("fast");
		ovid=id;
		document.getElementById('counter').innerHTML=ovid;     
	});	 
}
var vnxt;var vprv;
var vmyDivs=new Array();var vcontentnr=0;var vmyImgs=new Array();var vmyThumbs=new Array();var vmyBlocks=new Array();var vmyItems=new Array();
function getvContent() {
	if (myVideos.length > 0 && document.getElementById('vinner')) {
		vinnerDiv = document.getElementById('vinner');
		var newbigMask = document.createElement("IMG");
		newbigMask.src = vmask;	
		document.getElementById('plyr').appendChild(newbigMask);
		// set the next and previous buttons
		if (document.getElementById('vnxt')) {
			vnxt=document.getElementById('vnxt');
			var nxtImg = document.createElement("IMG");
			nxtImg.src = 'images/spacer.gif'; nxtImg.width = vnextImage.Width; nxtImg.height = vnextImage.Height;
			nxtImg.onclick = function() { moveDown(); }
			vnxt.appendChild(nxtImg);
		}
		if (document.getElementById('vprv')) {
			vprv=document.getElementById('vprv');
			var prvImg = document.createElement("IMG");
			prvImg.src = 'images/spacer.gif'; prvImg.width = vprevImage.Width; prvImg.height = vprevImage.Height;
			prvImg.onclick = function() { moveUp(); }
			vprv.appendChild(prvImg);
		}		
		// generate thumbnails
		for(var i=0; i < myVideos.length; i++) {
			var newDiv = document.createElement("DIV");
			newDiv.className += "thumb";			
			var newImg = document.createElement("IMG");
			newImg.src = myVideos[i].ThumbNail;	
			newDiv.style.backgroundImage='url('+newImg.src+')';
			var newMask = document.createElement("IMG");
			newMask.src = vthumbmask;	
			newDiv.appendChild(newMask);
			var newTitle = document.createElement("h2");
			newTitle.innerHTML = myVideos[i].Title;
			newDiv.appendChild(newTitle);
			var newArtist = document.createElement("h3");
			newArtist.innerHTML = myVideos[i].Artist;
			newDiv.appendChild(newArtist);
			vinnerDiv.appendChild(newDiv); newDiv.nr = i;
			//newDiv.onclick=function() { vshowContent(this.nr); }			
			vmyThumbs.push(newDiv);
		}		
		if (myVideos.length > vitemsshown) {
		    var n=0;
			for(var i=0; i<vitemsshown; i++) { 
				var clonedNode = vmyThumbs[i].cloneNode(true);
				vinnerDiv.appendChild(clonedNode); clonedNode.nr = n; vmyThumbs.push(clonedNode);				
				//clonedNode.onclick = function() { vshowContent(this.nr); }
				n=n+1;
			}
		}		
		if (vmyThumbs.length>0) { vshowContent(2); }
		vmoveinplace();
	}
}
var vscrollnr;var vmybox;var vmytop;var vthetop;var vtimer;var vtrgt;var vbusy=false;var waiting;var vstrt=true;
function vmoveinplace() {
	vscrollnr=(vscrollby*vmyitem);
	if (document.getElementById('vinner')) {
		document.getElementById('vinner').style.top='0px';
		var i=vmyThumbs;
		vmybox=-(((i.length-1)*vmyitem)-(vitemsshown*vmyitem));
		vmytop=document.getElementById('vinner').style.top;
		vthetop=parseInt(vmytop);
		vshowButtons(vthetop);
		// deals with automatic scrolling
		if (vcarousel==true && vstrt==false) { waiting=setTimeout("moveDown()",1); }
		if (vcarousel==true && vstrt==true) {  waiting=setTimeout("moveDown()",vcarspeed); }
		if (vcarousel==true) { 
			document.getElementById('vinner').onmouseover=function() { vcarousel=false;clearTimeout(waiting); }
			document.getElementById('vinner').onmouseout=function() { vcarousel=true;waiting=setTimeout("moveDown()",vcarspeed); } 
			
			document.getElementById('plyr').onmouseover=function() { vcarousel=false;clearTimeout(waiting); }
			document.getElementById('plyr').onmouseout=function() { vcarousel=true;waiting=setTimeout("moveDown()",vcarspeed); } 
			
			document.getElementById('vprv').onmouseover=function() { vcarousel=false;clearTimeout(waiting); }
			document.getElementById('vprv').onmouseout=function() { vcarousel=true;waiting=setTimeout("moveDown()",vcarspeed); } 
			
			document.getElementById('vnxt').onmouseover=function() { vcarousel=false;clearTimeout(waiting); }
			document.getElementById('vnxt').onmouseout=function() { vcarousel=true;waiting=setTimeout("moveDown()",vcarspeed); } 
		}
	}
}
function moveDown() {	
	vtrgt=vthetop-vscrollnr;
	if (vtrgt>=vmybox && vbusy==false) { vtimer=setInterval("moveBottom("+vtrgt+");",5);vbusy=true; }
	else if (vbusy==false) { 
		var lastitems=(vmybox+(-vthetop))/vmyitem;
		if (lastitems<=0) { 
			vscrollnr=-((lastitems*vmyitem)-vmyitem);
			vtrgt=vthetop-vscrollnr;
			vtimer=setInterval("moveBottom("+vtrgt+");",5);
			vbusy=true; 
		} else { 
			vmoveinplace();
			if (vcarousel==false) { moveDown(); }	
		}	 
	}
}
function moveBottom(vtrgt) {
	if (vthetop>vtrgt) { vthetop=vthetop-vstep; document.getElementById('vinner').style.top=(vthetop+'px');	} 
	else { 
		clearInterval(vtimer); 
		if (vthetop<vtrgt){ document.getElementById('vinner').style.top=(vtrgt+'px');vthetop=vtrgt; }
		vshowButtons(vthetop);
		vbusy=false;
		if (ovid<(myVideos.length-1)){ vshowContent(ovid+1); }
		else { vshowContent(0); }
		if (vcarousel==true) { waiting=setTimeout("moveDown()",vcarspeed); }
	}	
}
function moveUp() {		
	var startpoint=vmybox;	
	if (vthetop==0) { vthetop=(startpoint-vmyitem);document.getElementById('vinner').style.top=(startpoint-vmyitem+'px'); }
	vtrgt=vthetop+vscrollnr;
	if (vtrgt<0 && vbusy==false) { vtimer=setInterval("moveTop("+vtrgt+");",5);vbusy=true; }
	else if (vbusy==false) { 
		var lastitems=vthetop/vmyitem;
		if (lastitems<0) { 
			vscrollnr=-(lastitems*vmyitem);
			vtrgt=vthetop+vscrollnr;
			vtimer=setInterval("moveTop("+vtrgt+");",5);
			vbusy=true; 
		} else { vmoveinplace();moveUp(); }		 
	}
}
function moveTop(vtrgt) {
	if (vthetop<vtrgt) {	vthetop=vthetop+vstep; document.getElementById('vinner').style.top=(vthetop+'px');	} 
	else { 
		clearInterval(vtimer); 
		if (vthetop>vtrgt){ document.getElementById('vinner').style.top=(vtrgt+'px');vthetop=vtrgt; }
		vshowButtons(vthetop);
		vscrollnr=(vscrollby*vmyitem);
		vbusy=false; 
		if (ovid>0){ vshowContent(ovid-1); }
		else { vshowContent(myVideos.length-1); }
		if (vcarousel==true && vstrt==true) { vstrt=false;waiting=setTimeout("moveDown()",vcarspeed); }
	}	
}
function vshowButtons(vthetop) {
	if (vmybox<0) { 
		if (vnxt) { vnxt.style.backgroundImage='url('+vnextImage.FileName+')';vnxt.style.cursor='pointer'; }
		if (vprv) { vprv.style.backgroundImage='url('+vprevImage.FileName+')';vprv.style.cursor='pointer'; }
	}
}

// item viewer
function itemScroller(id, viewable) {
    /*alert('itemcount: ' + itemcount);*/
    var itemcount = $("#" + id + " .itemholder div.item").length;
    //alert('itemcount: ' + itemcount);	
    
    if (itemcount > viewable) {
        
		for (var i=1; i <= viewable; i++) {	$("#"+id+" .itemholder div.item:nth-child("+i+")").clone().appendTo("#"+id+" .itemholder"); }
		itemcount=itemcount+viewable;
		var itemwidth = $("#"+id+" .itemholder div.item:first").width();
		var framewidth = itemwidth*viewable;
		var endpoint = ((itemwidth*-itemcount)+framewidth)+"px";
		$("#"+id+" .next").addClass("nexton").click(function(){
	      	if ($("#"+id+" .itemholder").css("left") == endpoint) { $("#"+id+" .itemholder").css("left","0px");	}
			$("#"+id+" .itemholder").animate({"left": "-="+itemwidth+"px"}, "slow");
	    }); 
		$("#"+id+" .prev").addClass("prevon").click(function(){
	      	if ($("#"+id+" .itemholder").css("left") == "0px") { $("#"+id+" .itemholder").css("left",endpoint);	}
			$("#"+id+" .itemholder").animate({"left": "+="+itemwidth+"px"}, "slow");
	    });
	}
}

$(document).ready(function () {

    $(".page").css("paddingBottom", "15px");
    GetAllShows(11);

});

var calls = 0;

var GetAllShows = function (n, date) {
    $.getJSON(

        "/handler/allshows.ashx?n=" + n + "&d=" + date,
        function (data) {
            //alert("Data Saved: " + data.shows[0].Title);
            var count = 0;
            if (data != null) {
                $.each(data.shows, function (i, item) {
                    /*alert(item.Url);*/
                    //alert(i);
                    $('#itemholder').append('<div class="item">\
							<div onclick="window.location=\'' + escape(item.Url) + '\';" class="block clr">\
								<h2 class="homedate">' + item.NiceDate + '</h2>\
								<img src="' + item.Image + '" width="90" height="66" alt="" border="0" />\
								<h2 class="hometitle">' + item.Subtitle + '</h2>\
                                <h2 class="homeartist">' + item.Title + '</h2>\
							</div>\
						</div>');
                    count = count + 1;

                });
            }

            /*
            if (count == n && calls <= 50)
                GetAllShows(20, data.shows[count - 1].Date);
            else
            */
                itemScroller("itemscroller", 4);

            calls = calls + 1;

        });
}


