var uid = 0001;
var videosArray = [];
var windowWidth = 0;
var windowHeight = 0;
var tilesWindowWidth = 0;
var tilesCounter = 0;


$(document).ready(function(){

	//updateContentTop();

	var randomnumber = createRandomNumber();
	loadXml('client/build_video_xml.asp?acctNumber='+uid+'&rnd='+randomnumber, processXml);
	//displayVideo();

});







function updateSorting(n,theTab){
	var randomnumber = createRandomNumber();
	sortXml('client/build_video_xml.asp?sortBy='+n+'&rnd='+randomnumber, processXml);
	
	$('.tabSortBy').removeClass('active');
	$('.'+theTab).addClass('active');
	
}

function sortXml(xmlLocation,callback) {

	try
	{
		$.ajax(
		{
			type: "GET",
			url: xmlLocation,
			dataType: "xml",
			success: function(xml)
			{
				if (callback)
				{
					callback(xml);
				}
			},
			error: function(x, s, e)
			{
				window.location = ''+windowsServer+'error.asp?errorMessage=FAVable has encountered an error.<br><br>Please try again.';
				//alert("xml loading error "+x+" / "+s+" / "+e+" / "+callback);
			}
		});
	}
	catch (e)
	{
		$.log('ERROR: (loadXml:catch) ' + e);
	}
}




function processXml(theXml) {
	
	videosArray.length = 0;
	
	totalFavorites = 0;
	
	if($("videoData", theXml).text() != 'none') {
	
	    $("videoData", theXml).each(function(i) {
			var videoItem = new Object();
	        videoItem.id = i;
			videoItem.siteId = $('siteId', this).text();
			videoItem.siteLocation = $('siteLocation', this).text();
			videoItem.siteDateAdded = $('siteDateAdded', this).text();
			videoItem.siteTitle = unescape($('siteTitle', this).text());
			videoItem.siteName = unescape($('siteName', this).text());
			videoItem.siteSize = unescape($('siteSize', this).text());
			videoItem.siteDescription = unescape($('siteDescription', this).text());
			videoItem.siteClicks = unescape($('siteClicks', this).text());
			videoItem.siteObjectCode = unescape($('siteObjectCode', this).text());
	        videosArray.push(videoItem);
			
	    });
		
		createTiles();

	}

}






function createTiles() {

	var theDiv = $('#videoTiles');
	
	$(theDiv).empty();
	
	tilesCounter = 0;
	
	$.each(videosArray, function(i){ 
	
		var theImageName = this.siteName;
		var theId = this.id;
		var theSiteId = this.siteId;
		var theSiteTitle = this.siteTitle;
		var theLocation = this.siteLocation;
		var theDateAdded = this.siteDateAdded;
		var theSize = this.siteSize;
		var theDescription = this.siteDescription;
		var theClicks = this.siteClicks;
		var theObjectCode = this.siteObjectCode;

		
		if(theDescription==''){
			theDescription='&nbsp;'
		}
		
		var thumbnail = '';
		thumbnail = theImageName.replace('_video.flv','_thumbnail.jpg');

		var theHtml = '';
		theHtml +='<div class="tileWrapper"><div class="tileBorder"><div class="tilePadding"><div class="tileContent">';
		theHtml +='<div class="tileImage'+theId+'"><img src=uploads/' + thumbnail + ' width=160 border=0></div>';
		theHtml += '<div class="tileDetailsWrapper">';

		theHtml += '<div class="tileTitle">'+theSiteTitle+'</div>';
		theHtml += '<div class="tileText">'+theDescription+'</div>';
		theHtml += '<div class="tileDate">Added:&nbsp;'+theDateAdded+'</div>';

		theHtml += '</div>';
		theHtml +='</div></div></div></div>';
	
		theDiv.append(theHtml);

		$('.tileImage'+theId).click(function(){

			displayVideo(i);

		});


	});


}



function updateTilesContentWidth(){
	tilesWindowWidth = tilesCounter * 190;
	tilesWindowWidth = tilesWindowWidth + 'px';
	$('#tiles').css('width',tilesWindowWidth);
}










function displayVideo(theVideoId){

	var theTitle = videosArray[theVideoId].siteTitle;

	var theObjectCode = videosArray[theVideoId].siteObjectCode;

	$('#video').empty();
	
	$('#videoModal').modal();

	 $('#video').html(theObjectCode);
	
	$('#videoTitle').html(theTitle);

}
























