﻿var retryCount = 0;
function LoadThumbs(s, p) {	
	$('#searchText').attr('disabled', 'disabled');
	$('#searchButton').attr('disabled', 'disabled');
	$.ajax({
		type: 'POST',
		url: "./Video/" + s,
		data: JSON.stringify((p !== undefined) ? p : []),
		contentType: 'application/json; charset=utf-8',
		dataType: 'json',
		cache: false,
		success: function (result) {
			retryCount = 0;
			if (result == undefined || result == null) {
				$('<br class="clear" /><h3 style="display:none; line-height:40px;"><img src="content/images/icons/warning.png" align="left" style="margin-left:10px;margin-right:10px;">No records were found matching your search.</h3>').insertBefore('#videosearchitemsstart').fadeIn(500);
				return;
			}

			var x = -1;
			var newitem = '';
			$.each(result, function () {
				x += 1;
				if (x == 0)
					lid = { lid: this.VideoID };
				var subtext = ((s == "MVT" | s == "LVT") ? "Votes: " + this.TotalVotes : ((s == "MVW" | s == "LVW") ? "Views: " + this.TotalViews : ((s == "BND") ? "Votes: " + this.TotalVotes : "City: " + this.City + ", " + this.State)));
				newitem += "<div class='videosearchresult' id='vid" + this.VideoID + "' relvid='" + this.YouTubeID + "'><h2>" + this.BandName + "</h2><img src='" + this.VideoThumbnailUrl + "' alt='" + this.BandName + " - " + this.BandDescription + "' title='" + this.BandName + " - " + this.BandDescription + "' /><h3>" + subtext + "</h3></div>";
			});
			$('#videothumbs').html(newitem);
			$('#searchText').removeAttr('disabled');
			$('#searchButton').removeAttr('disabled');
		},
		error: function (xhRequest, ErrorText, thrownError) {
			$('#searchText').removeAttr('disabled');
			$('#searchButton').removeAttr('disabled');
			retryCount += 1;
			if (retryCount < 3) {
				LoadThumbs(s, p);
			}
			else {
				retryCount = 0;
			}
		}
	});
}

$(document).ready(function () {
    startJingle("./");
    startEQ("./");
    $('#ddlStateList').selectbox();
    $('#searchType').selectbox({ inputClass: 'selectbox2', containerClass: 'selectbox2-wrapper', OnSelected: LoadThumbs });
    
    var lid = { lid: 0 };
    
    $('.videosearchresult').live("click",function () {
        location.href = "./Entry/" + $(this).attr('relvid');
    });

    $('.videosearchresult').live("mouseenter",function () { $(this).addClass('videosearchresultover') });
    $('.videosearchresult').live("mouseleave",function () { $(this).removeClass('videosearchresultover') });
    $('#searchButton').bind("click",function () {
        if (($('#searchText').attr('value') != "") && ($('#searchText').val().length > 3)) {
            var s = { keyword: $('#searchText').val() };
            LoadThumbs('KEY', s);
        }
    });

    $('input').keypress(function (e) {
        var code = null;
        code = (e.keyCode ? e.keyCode : e.which);
        return (code == 13) ? false : true;
    });

    $('#searchText').keydown(function (e) {
        if (e.keyCode == 13) {
            if (($('#searchText').attr('value') != "") && ($('#searchText').val().length > 3)) {
                var s = { keyword: $('#searchText').val() };
                LoadThumbs('KEY', s);
            }
        }
    });

    LoadThumbs('MVT');
});





