// JavaScript Document - Videos and Demos
var activeVideoId = "1001";
var activeCategoryName = "Why Qwest";
var t, f, l, c;
var defaultTemplateHt = "425";
var altTemplateHt = "570";
var isPlaying = false; //default condition for the Flash Player play state 
var total;
var first = 1;
var last;
var curr = 5;
var multi = 5;
var top = false;
var bottom = false;
var isPlaying = false;
var pageTitle = "Qwest Large Business | Videos and Demos | "
var catClick = false;
var flashTitle;
/*Send Path to Flash */
function sendPathToFlash(_pathToVideo) {
	log.info("sendPathToFlash", "JS: Calling sendPathToFlash", 5);
	log.info("sendPathToFlash", "JS: Path received in Flash: " + _pathToVideo, 3); //alert("_pathToVideo"+_pathToVideo);


    if(!DetectFlashVer(9,0,0))
	{
	log.info("sendPathToFlash", "JS: Flash not available", 5);
		
		$("div.bmg_videodemo_l:first").find("object").remove();
		$("div.bmg_videodemo_l:first").find("embed").remove();
		$("div.bmg_videodemo_l:first").prepend("<div class='noFlashCont' style='width: 446px; height: 357px'><span style='margin-top:170px; float: left; margin-left: 15px;'>To view the videos, please download the <a href='http://get.adobe.com/flashplayer/' target='_blank'>Flash Player.</a> </span></div>");
		
		return;
	}
	


	try {
		if (navigator.appName == "Netscape") {
			document.Videos.loadVideo(_pathToVideo);
		} else {
			document.getElementById("Videos").loadVideo(_pathToVideo);
		}
	} catch(e) {
		//alert(e.message);
		log.error("sendPathToFlash", "Error in sendPathToFlash: " + e.message);
	}
}
/* Set the video status */
function setVideoStatus(_isPlaying) {
	this.isPlaying = _isPlaying;
	document.title = pageTitle + flashTitle;
}
var videoStopped = false;
/* Stop the video  */
function stopVideo() {
	log.info("stopVideo", "JS: Calling stopVideo", 5);
	try {
		if (navigator.appName == "Netscape") {
			document.Videos.stopVideoFromJS();
		} else {
			document.getElementById("Videos").stopVideoFromJS();
		}
		videoStopped = true;
	} catch(e) {
		log.error("stopVideo", "Error in stopVideo: " + e.message);
	}
}
/*
The paging function
*/
function paging(total, first, multi, curr) {
	this.totalThumbItems = total;
	this.firstThumbItem = first;
	this.multiplierValue = multi;
	this.currThumbItem = curr;
}

function bigimage(isPlaying, catImagePath, vidPath, vidId, thumbClick, vidIsModal, vidWidth, vidHeight, vidDuration, vidContentPath, vidTitle, vidCat, vidDate) {
	log.info("bigimage", "JS: New window opened, and video object path: " + vidPath, 5);
	log.info("bigimage", "JS: New window opened, and video width: " + vidWidth, 5);
	log.info("bigimage", "JS: New window opened, and video height: " + vidHeight, 5);
	str1 = "<object id='abcd' width=\"" + vidWidth + "\"";
	str1 += "\" height=\"" + vidHeight;
	str1 += "\"codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\"classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"><param value=\"";
	str1 += vidPath + "\" name=\"movie\"/><param value=\"high\" name=\"quality\"/><embed width=\"";
	str1 += vidWidth + "\" height=\"";
	str1 += vidHeight + "\"pluginspace=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" quality=\"high\" src=\"";
	str1 += vidPath + "\"/></object>";
	vidWidth1 = parseInt(vidWidth) + (70);
	vidHeight1 = parseInt(vidHeight) + (190);
	sw = (screen.width - vidWidth) / 2;
	sh = (screen.height - vidHeight) / 2;
	newwin = window.open('', 'newwin', 'width=' + vidWidth1 + ',height=' + vidHeight1 + ',scrollbars=0,menubars=0,toolbars=0,location=0,directories=0,status=0,top=0,left=0,resizable=1');
	newwin.document.open();
	newwin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n');
	newwin.document.write('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	newwin.document.write('\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />\n');
	newwin.document.write('<meta http-equiv="Imagetoolbar" content="no" />\n');
	newwin.document.write('<title>' + vidTitle + '</title>\n');
	newwin.document.write('<link href="/qptcms/qCmsRepository/Global/largeBusiness/css/base.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/desktop.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/themes.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/layouts.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/bmg.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/jquery.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/bmg_menu.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/bmg_channelMenu.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/bmg_features.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/qPortal/css/bmg_modal_window.css" rel="stylesheet" type="text/css" /><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/thickbox.css" rel="stylesheet" type="text/css"><link href="/qptcms/qCmsRepository/Global/largeBusiness/css/bmg_video_news.css" rel="stylesheet" type="text/css">');
	newwin.document.write('</head>\n');
	newwin.document.write('<body style="margin:0;padding:0" scroll="auto">\n');
	newwin.document.write('<div class="bmg_layout_modal_675" id="interactiveVideo"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="bmg_lgr_modal"><tr class="top64"><td valign="top"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td class="modal_tl">&nbsp;</td><td class="modal_tc"><div class="lgr_modal_title" style="height:45px;"><div class="bmg_video_info"><h4><span class="catg">' + vidCat + '</span><br /><span>' + vidTitle + '</span><br /><span class="date">' + vidDate + '</span><span class="duration">' + vidDuration + '</span></h4><div class="bmg_clear"></div></div></div></td><td class="modal_tr">&nbsp;</td></tr></table></td></tr><tr class="middle"><td><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td class="modal_ml">&nbsp;</td><td class="modal_mc_hauto" colspan="" valign="top"><div class="modal_mc_container"><div class="bmg_video_content"><p>' + vidContentPath + '</p><div>' + str1 + ' </div><div class="modalBtn"><a class="bmg_button_gen_wt" href="#"><span onclick="self.close()">Exit</span></a></div></div></div></td><td class="modal_mr">&nbsp;</td></tr></table></tr><tr class="bottom"><td><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td class="modal_bl">&nbsp;</td><td class="modal_bc">&nbsp;</td><td class="modal_br">&nbsp;</td></tr></table></td></tr></table></div>');
	newwin.document.write('<script>  	</script>');
	newwin.document.write('\n</body></html>');
	newwin.document.close();
	newwin.focus();
}
/*  If the video is Modal */
function isModal(thumbClick, isPlaying) {
	log.info("isModal", "JS: Calling isModal", 5);
	$.each(JSONobj.videos.categories.entry, function (i, n) { // alert("=="+$.trim(this.categories.name)+"=="+$.trim(activeCategoryName)+"==")
		if ($.trim(this.categories.name).toLowerCase() == $.trim(activeCategoryName).toLowerCase()) {
			$(this.categories.videos.video).each(function (i, n) {
				var vidIsModal = this.isModal;
				var vidId = this.id; //set the variable for the id
				var vidPath = this.videoPath; //set the variable for the video path
				var vidThumb = this.thumbnailPath; //set the variable for the thumbnail path
				var vidCat = this.category; //set the variable for the category name
				var vidContentPath = this.contentPath;
				var vidDate = this.date;
				var vidTitle = this.title;
				var vidDuration = this.duration;
				var vidWidth = this.width;
				var vidHeight = this.height;
				var catImagePath = this.placeholderPath;
				if ($.trim(activeVideoId).toLowerCase() == $.trim(vidId).toLowerCase()) {
					var videoURL = '/business/getVideosAndDemos.ajax?videoId=?height=' + (vidHeight) + '&width=' + (vidWidth + 70) + '&modal=true&$id=' + vidId;
					var vidPathExt = vidPath.lastIndexOf(".");
					vidPath1 = vidPath.slice(vidPathExt + 1);
					if (vidPath1 == "exe" || vidPath1 == "mpg") {
						window.open(vidPath);
					} else {
						bigimage(isPlaying, catImagePath, vidPath, vidId, thumbClick, vidIsModal, vidWidth, vidHeight, vidDuration, vidContentPath, vidTitle, vidCat, vidDate);
					}
				}
			})
		}
	});
	return false;
}
var thumbClick = false;
/* Load appropriate category/video image */
function getVideoById(id, activeCategoryName, thumbClick) {
	try {
		$("div.scrollable_area:first ul li").find("a[href$=#" + activeVideoId + "]").parents("li").addClass("selected active").siblings("li").removeClass("selected active");
		$.each(JSONobj.videos.categories.entry, function (i, n) {
			$(this.categories.videos.video).each(function (i, n) {
				var vidIsModal = this.isModal;
				var vidId = this.id; //set the variable for the id
				var vidPath = this.videoPath; //set the variable for the video path
				var vidThumb = this.thumbnailPath; //set the variable for the thumbnail path
				var vidCat = this.category; //set the variable for the category name
				var vidContentPath = this.contentPath;
				var vidDate = this.date;
				var vidTitle = this.title;
				var vidDuration = this.duration;
				var vidWidth = this.width;
				var vidHeight = this.height;
				var catImagePath = this.placeholderPath;
				if ($.trim(id).toLowerCase() == $.trim(vidId).toLowerCase()) {
					flashTitle = vidTitle;
					if (!catClick && thumbClick || catClick && thumbClick) {
						activeVideoId = vidId;
						activeCategoryName = vidCat;
						$("div.bmg_videodemo_detail_top:first").find("img").hide();
						$("div.bmg_videodemo_l:first").find("object,embed").css("height", "357px");
						loadCategoryImage(isPlaying, catImagePath, vidPath, vidId, thumbClick, vidIsModal, vidWidth, vidHeight, vidDuration, vidContentPath, vidTitle, vidCat, vidDate);
					} else {
						loadCategoryImage(isPlaying, catImagePath, vidPath, vidId, thumbClick, vidIsModal, vidWidth, vidHeight, vidDuration, vidContentPath, vidTitle, vidCat, vidDate);
						activeVideoId = activeVideoId;
						activeCategoryName = vidCat;
					}
					return false;
				} else {}
			});
		});
	} catch(e) {}
}
/*Update the content for the video */
function updateVideoContent() {
	log.info("updateVideoContent", "JS: Calling updateVideoContent", 5);
	try {
		$.each(JSONobj.videos.categories.entry, function (i, n) {
			if ($.trim(activeCategoryName).toLowerCase() == this.string.toLowerCase()) {
				$(this.categories.videos.video).each(function (i, n) {
					var vidIsModal = this.isModal;
					var vidId = this.id; //set the variable for the id
					var vidPath = this.videoPath; //set the variable for the video path
					var vidThumb = this.thumbnailPath; //set the variable for the thumbnail path
					var vidCat = this.category; //set the variable for the category name
					var vidContentPath = this.contentPath;
					var vidDate = this.date;
					var vidTitle = this.title;
					var vidDuration = this.duration;
					var vidWidth = this.width;
					var vidHeight = this.height;
					var catImagePath = this.placeholderPath;
					if ($.trim(this.id).toLowerCase() == $.trim(activeVideoId).toLowerCase()) {
						if (!isPlaying) {
							$("div.bmg_videodemo_detail_btm:first").find("h3.catg:first").text(vidCat).end().find("p:first").text(vidContentPath).end().find("h3.date:first").text(vidDate).end().find("h3.title:first").text(vidTitle).append(" <br />" + vidDuration);
							return false;
						}
						return false;
					}
				})
			}
		});
	} catch(e) {}
}
/* Load the Category/Video Image for the video with the path written in to the anchor*/
function loadCategoryImage(isPlaying, catImagePath, vidPath, vidId, thumbClick, vidIsModal, vidWidth, vidHeight, vidDuration, vidContentPath, vidTitle, vidCat, vidDate) {
	log.info("loadCategoryImage", "JS: Calling loadCategoryImage", 5);
	modifyTitle(vidTitle);
	$("div.noFlashCont").remove();
	if (!isPlaying) {
		activeCategoryName = vidCat;
		$("div.bmg_videodemo_detail_top:first").hide().empty().html('<a href=' + vidPath + '#' + vidId + ' class="playMovie"> <img src="' + catImagePath + '" alt="Click to Play"/> </a>').fadeIn('fast');
		$("div.bmg_videodemo_l:first").find("object, embed").css("height", "0px");
		$("p,h3.catg,h3.title,h3.date", $("div.bmg_videodemo_detail_btm:first")).empty(); //return false;
	} else {}
	$("a.playMovie").click(function (e) {
		e.preventDefault();
		setActiveVideo(vidId);
		if (!allVideos) {
			var thisURL = $(this).attr('href');
			marker = thisURL.lastIndexOf("#");
			thisURL = thisURL.slice(marker + 1);
		} else {
			thisURL = $(this).attr("href").split("#")[1];
		}
		
		
		if (!isPlaying) {
			$("div.scrollable_area:first ul li").find("a[href$='#"+thisURL+"']").parents("li:first").addClass("selected active").siblings("li").removeClass("selected active");
		}
		log.info("loadCategoryImage", "Is the Video an Overlay: " + vidIsModal, 5);
		updateVideoContent();
		window.location.hash = "#" + thisURL;
		if (isVideoModal() === 'false') {
			sendPathToFlash($(this).attr('href')); //Send the path to the player to Play
			$("div.bmg_videodemo_detail_top:first").find("img").hide();
			$("div.bmg_videodemo_l:first").find("object, embed").css('height', '357px');
		} else {
			isModal(thumbClick, isPlaying);
		}
	});
}
/* Based on the ActiveCategoryName variable, run coresponding functions to generate the interface*/
function setActiveCategory(activeCategoryName) {
	log.info("setActiveCategory", "JS: Calling setActiveCategory", 5);
	loadThumbnails(activeCategoryName);
	setActiveVideo(activeVideoId);
	getVideoById(activeVideoId, activeCategoryName, thumbClick);
	updateVideoContent();
	setURL();
	countItems(activeCategoryName);
	truncateTitle(minLength);
	updatePaging(total, first, multi, curr);
	scrollItems(top, bottom);
}
var allVideos = false;

function loadThumbnails(activeCategoryName) {
	var vidId; //get the id from the JSON object
	var vidTitle;
	var vidThumb;
	var vidContentPath;
	var vidDate;
	var vidDuration;
	var vidCat;
	var catImagePath;
	var vidPath;
	var vidIsModal;
	var vidWidth;
	var vidHeight;
	$("div.scrollable_area:first ul").empty();
	try {
		$.each(JSONobj.videos.categories.entry, function (i, n) {
			if ($.trim(activeCategoryName) == "All Videos") {
				$(this.categories.videos.video).each(function (i, n) {
					vidId = this.id; //get the id from the JSON object
					vidTitle = this.title;
					vidThumb = this.thumbnailPath;
					vidContentPath = this.contentPath;
					vidDate = this.date;
					vidDuration = this.duration;
					vidCat = this.category;
					catImagePath = this.placeholderPath;
					vidPath = this.videoPath;
					vidIsModal = this.isModal;
					vidWidth = this.width;
					vidHeight = this.height;
					$("div.scrollable_area:first ul").hide().append('<li class="carThumb"><div class="bmg_videoTitle"><span>' + vidTitle + '</span></div><div class="bmg_videoIcon"> <a class="link" href=' + vidPath + '#' + vidId + '><img src="' + vidThumb + '" /> </a></div></li>').fadeIn('fast');
					allVideos = true;
				});
			} else if ($.trim(this.categories.name).toLowerCase() == $.trim(activeCategoryName).toLowerCase()) {
				$(this.categories.videos.video).each(function (i, n) {
					vidId = this.id; //get the id from the JSON object
					vidTitle = this.title;
					vidThumb = this.thumbnailPath;
					vidContentPath = this.contentPath;
					vidDate = this.date;
					vidDuration = this.duration;
					vidCat = this.category;
					catImagePath = this.placeholderPath;
					vidPath = this.videoPath;
					vidIsModal = this.isModal;
					vidWidth = this.width;
					vidHeight = this.height;
					$("div.scrollable_area:first ul").hide().append('<li class="carThumb"><div class="bmg_videoTitle"><span>' + vidTitle + '</span></div><div class="bmg_videoIcon"> <a class="link" href=' + vidPath + '#' + vidId + '><img src="' + vidThumb + '" /> </a></div></li>').fadeIn('fast');
					allVideos = false;
				});
				return false;
			}
		});

		$("a.link").click(function (e) {
			e.preventDefault();
			thumbClick = true;
			
			if(!DetectFlashVer(9,0,0)) {
				sendPathToFlash("demoPath");
				$("div.bmg_videodemo_detail_top:first").find("img").hide();
				return;	
			}
			
			var elemTop = $("object").offset().top;
			var winScrollTop = $(window).scrollTop();
			if (winScrollTop > elemTop) {
				window.scrollTo(0, elemTop);
			}
			if (!allVideos) {
				var thisURL = $(this).attr('href');
				marker = thisURL.lastIndexOf("#");
				thisURL = thisURL.slice(marker + 1);
			} else {
				thisURL = $(this).attr("href").split("#")[1];
			}
			$(this).parents("li").addClass("selected active").siblings("li").removeClass("selected active");
			modifyTitle(vidTitle);
			stopVideo(); //stop the video if playing
			getVideoById(thisURL, activeCategoryName, thumbClick);
			$(this).parents("li").addClass("selected active").siblings("li").removeClass("selected active");
			window.location.hash = "#" + thisURL;
			if (isVideoModal() === 'false') {
				sendPathToFlash($(this).attr('href')); //Send the path to the player to Play
				$("div.bmg_videodemo_detail_top:first").find("img").hide();
				$("div.bmg_videodemo_l:first").find("object, embed").css('height', '357px');
			} else {
				isModal(thumbClick, isPlaying);
			}
			updateVideoContent();
		});
	} catch(e) {}
}

function isVideoModal() {
	var vidIsModal = false;
	$.each(JSONobj.videos.categories.entry, function (i, n) {
		$(this.categories.videos.video).each(function (i, n) {
			var vidId = this.id;
			if ($.trim(activeVideoId).toLowerCase() == $.trim(vidId).toLowerCase()) {
				vidIsModal = this.isModal;
				vidPath = this.videoPath;
				return false;
			}
		})
	});
	return vidIsModal;
}

function setActiveVideo(id) {
	log.info("setActiveVideo", "JS: Calling setActiveVideo", 5);
	try {
		if (!allVideos) {
			$.each(JSONobj.videos.categories.entry, function (i, n) {
				$(this.categories.videos.video).each(function (i, n) {
					if ($.trim(this.id).toLowerCase() == $.trim(activeVideoId).toLowerCase()) {
						var vidId = this.id; //get the id from the JSON object
						var vidTitle = this.title;
						var vidThumb = this.thumbnailPath;
						var vidContentPath = this.contentPath;
						var vidDate = this.date;
						var vidDuration = this.duration;
						var vidCat = this.category;
						var catImagePath = this.placeholderPath;
						var vidPath = this.videoPath;
						var vidIsModal = this.isModal;
						var vidWidth = this.width;
						var vidHeight = this.height;
						loadCategoryImage(isPlaying, catImagePath, vidPath, vidId, thumbClick, vidIsModal, vidWidth, vidHeight, vidDuration, vidContentPath, vidTitle, vidCat, vidDate);
					}
					return false;
				});
			});
		} else {
			$.each(JSONobj.videos.categories.entry, function (i, n) {
				$(this.categories.videos.video).each(function (i, n) {
					var vidId = this.id; //get the id from the JSON object
					var vidTitle = this.title;
					var vidThumb = this.thumbnailPath;
					var vidContentPath = this.contentPath;
					var vidDate = this.date;
					var vidDuration = this.duration;
					var vidCat = "All Videos";
					var catImagePath = this.placeholderPath;
					var vidPath = this.videoPath;
					var vidIsModal = this.isModal;
					var vidWidth = this.width;
					var vidHeight = this.height;
				});
				return false;
			});
		}
		if (!isPlaying && catClick) {
			var thisURL = $("div.scrollable_area:first ul li:first").find("a").attr('href');
			activeVideoId = $("div.scrollable_area:first ul").children("li:first").find("a").attr("href").split("#")[1];
		} else {
			activeVideoId = activeVideoId; //set the activeVideoId	
		}
		if (!isPlaying) {
			$("div.scrollable_area:first ul").children("li:first").addClass("selected active");
		}
		return;
	} catch(e) {}
}

function modifyTitle(vidTitle) {
	$.each(JSONobj.videos.categories.entry, function (i, n) {
		$(this.categories.videos.video).each(function (i, n) {
			if ($.trim(this.id).toLowerCase() == $.trim(activeVideoId).toLowerCase()) {
				vidTitle = this.title;
				return false;
			}
		});
		return false;
	});
	var arrVidTitle = [];
	$.each(vidTitle.split(" "), function (i) {
		arrVidTitle.push(this);
		if (i == 6) {
			return false;
		}
	});
	var titleLength = arrVidTitle.length;
	var truncateTitle = arrVidTitle.join(' ');
	if (titleLength >= 7) {
		document.title = pageTitle + truncateTitle + "...";
	} else {
		document.title = pageTitle + truncateTitle;
	}
}
var setToLast = false;

function buildTabs() {
	log.info("buildTabs", "JS: Calling buildTabs", 5);
	try {
		$.each(JSONobj.videos.categories.entry, function (i, n) {
			$("div.bmg_videodemo_tabCont ul").append('<li> <a class="abcd" href="#' + this.categories.name.replace(/ /g, '-').toLowerCase() + '"> <span>' + this.categories.name + ' </span> </a> </li>');
			if (activeCategoryName == this.categories.name) {
				$("div.bmg_videodemo_tabCont ul li").addClass("bmg_activeLink");
			}
		});
		$("div.bmg_videodemo_tabCont ul").append('<li> <a class="abcd" href="#all-videos"> <span>All Videos</span> </a> </li>');
		setActiveTab();
		$(".bmg_videodemo_tabConatiner").find("ul li a").click(function () {
			first = 1;
			curr = 5;
			catClick = true;
			activeCategoryName = $(this).children('span').text();
			setActiveTab();
			setActiveCategory(activeCategoryName);
			showArrowScrollers();
			updateVideoContent();
		});
	} catch(e) {}
}

function setActiveTab() {
	log.info("setActiveTab", "JS: Calling setActiveTab", 5);
	$(".bmg_videodemo_tabCont ul li").each(function (i) {
		var catText = $(this).find("a span").text();
		if (!setToLast) {
			if ($.trim(catText) == "All Videos") {
				$(this).appendTo($(this).parent("ul"));
				setToLast = true;
			}
		}
		if ($.trim(catText).toLowerCase() == $.trim(activeCategoryName).toLowerCase()) {
			$(this).addClass("bmg_activeLink").siblings().removeClass("bmg_activeLink");
			return false;
		}
	});
}

function setURL() {
	log.info("setURL", "JS: Calling setURL", 5);
	$("a.link").each(function () {
		var thisURL = $(this).attr('href');
		marker = thisURL.lastIndexOf("#");
		thisURL = thisURL.slice(marker);
		$(this).hover(function () {
			$toolLink = $(this);
			$.each(JSONobj.videos.categories.entry, function (i, n) {
				$(this.categories.videos.video).each(function (i, n) {
					vidId = n.id;
					vidTip = n.tip;
					if ("#" + vidId == thisURL) {
						genTooltip(this, vidId);
						return false;
					}
				});
			});
		},
		function () {
			desTooltip(this);
		});
	});
}

function countItems(activeCategoryName) {
	log.info("countItems", "JS: Calling countItems", 5);
	videoLen = $("div.scrollable_area:first ul").find("li").length;
	alt = false;
	def = false;
	if ($.trim(activeCategoryName) == "All Videos") {
		alt = true;
	} else {} if (alt) {
		minLettersTitle = 27; // Number of letters before adding "..." to the video title
		minLength = 32;
	} else {
		minLettersTitle = 54;
		minLength = 60;
	}
	loadTemplate(alt, videoLen);
	return alt;
}
/*
	changing classnames to accomodate 2 columns.

*/
function loadTemplate(alt, videoLen) {
	log.info("loadTemplate", "JS: Calling loadTemplate", 5);
	if (alt && videoLen >= 5) {
		$("div.scrollable_area:first").css("height", altTemplateHt+"px").find("ul").removeClass("one").addClass("two");
	} else {
		$("div.scrollable_area:first").css("height", defaultTemplateHt+"px").find("ul").removeClass("two").addClass("one");
	}
}
/*Update Paging */
function updatePaging(total, first, multi, curr) {
	log.info("updatePaging", "JS: Calling updatePaging", 5);
	total = $("ul.carCol li").length;
	if (total < curr) {
		curr = total;
	} else {
		curr = curr;
	}
	if (!$("ul.carCol").hasClass("two")) { //	alert('one')
		$("div.bmg_video_paging").text(first + " - " + curr + " of " + total);
	} else {
		var abc = (parseInt(curr + 5) <= total) ? parseInt(curr + 5) : total;
		$("div.bmg_video_paging").text(first + " - " + abc + " of " + total);
	}
}

function showArrowScrollers() {
	if ((alt && videoLen <= 10) || (!alt && videoLen <= 5)) {
		$("div.scroll_btn").css("visibility", "hidden");
	} else {
		$("div.scroll_btn").css("visibility", "visible");
	}
}

function truncateTitle(minLength) {
	$("div.bmg_videoTitle").each(function (i) {
		var data = $(this).text();
		if (data.length <= minLength) {
			$(this).text(data);
		} else {
			var truncStr = stringTruncate(minLength, data);
			$(this).text(truncStr);
		}
	});
	data = null;
}

function stringTruncate(lineLen, str) {
	var j = 0;
	var x = 0;
	var xyz = '';
	var abc = null; //   alert("str: "+str);    
	var abc = str.split(" ");
	for (j = 0; j < abc.length; j++) {
		if (x < lineLen) {
			x += abc[j].length + 1;
		}
		if (x < lineLen) xyz += abc[j] + ' ';
	}
	xyz += '...';
	str = null;
	return xyz;
}

function formatDate() {
	$.each(JSONobj.videos.categories.entry, function (i, n) {
		$(this.categories.videos.video).each(function (i, n) {													
			log.info("formatDate","this.date: "+this.date,5);
			var dateFormat = this.date.split(' ');
			log.info("formatDate","this.date.split(' ') or dateFormat "+dateFormat,5);
			var fullMonth = ["January","February","March","April","May","June","July","August","September","October","November","December"];
			var shortMonth = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
			$.each(shortMonth,function(i) {
				if(dateFormat[1] == this){
					log.info("formatDate","dateFormat[1] == this -> this:  "+this,5);
					log.info("formatDate","fullMonth[i]:  "+fullMonth[i],5)					
					dateFormat[1] = fullMonth[i];
					return false;
				}
			});
			dateFormtat = dateFormat[1] + " "+ dateFormat[0] +", "+dateFormat[2];
			log.info("formatDate","dateFormat after concatenation:  "+dateFormtat,5);
			this.date = dateFormtat;
			
		})
	});
}


$(document).ready(function () {
	try {
		if (JSONobj);
	} catch(e) {
		var messagePopup = '<div class="bmg_layout_modal_410 popup" id="bmg_repair_confirmation" style="display:block;"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="bmg_gr_modal"><tr class="top"><td class="modal_tl">&nbsp;</td><td class="modal_tc"><div class="modal_title"> <span class="modal_title_active"><h2>Message</h2></span> <span class="modal_title_inactive"><h2><!-- --></h2></span> <span class="modal_win_close"><a class="closePopup" href="javascript:;"><span>Close</span></a></span> </div></td><td class="modal_tr">&nbsp;</td></tr><tr class="middle"><td class="modal_ml">&nbsp;</td><td class="modal_mc_h200"><div class="bmg_gr_modal_container" style="display:block;"><div class="bmg_alert_content" id="bmg_alert_content"><h1 style="color: #fff;">Please try again after sometime</h1></div></div><div class="bmg_gr_modal_footer"><hr /><div class="bmg_button_container"><a href="#" class="bmg_button_gen_gr bmg_right closePopup" style="margin-right:10px;"><span>Ok</span></a></div></div></td><td class="modal_mr">&nbsp;</td></tr><tr class="bottom"><td class="modal_bl">&nbsp;</td><td class="modal_bc">&nbsp;</td><td class="modal_br">&nbsp;</td></tr> </table></div>';
		$("body").append(messagePopup);
		tb_show('Some Text', '#TB_inline?height=287&width=400&inlineId=bmg_repair_confirmation&modal=true', '');
		$("a.closePopup").click(function () {
			tb_remove();
			$("#bmg_repair_confirmation").remove();
			document.location.href = "/business/resource-center/overview/list.html";
		});
	}
	page = new paging(total, first, multi, curr);
	
	formatDate();
	
	$("div.carArrowTop").click(function () {
		top = true;
		bottom = false; //alert(top)
		scrollItems(top, bottom);
		if ((canScrollUp && !canScrollDown) && (!$("ul.carCol").hasClass("two"))) {
			first -= 5;
			curr -= 5;
		} else if ((canScrollUp && canScrollDown) && (!$("ul.carCol").hasClass("two"))) {
			first -= 5;
			curr -= 5;
		} else if ((canScrollUp && !canScrollDown) && ($("ul.carCol").hasClass("two"))) {
			first -= 10;
			curr -= 10;
		} else if ((canScrollUp && canScrollDown) && ($("ul.carCol").hasClass("two"))) {
			first -= 10;
			curr -= 10;
		}
	});
	$("div.carArrowBott").click(function () {
		top = false;
		bottom = true; //alert(bottom);
		scrollItems(top, bottom);
		if ((!canScrollUp && canScrollDown) && (!$("ul.carCol").hasClass("two"))) {
			first += 5;
			curr += 5;
		} else if ((canScrollUp && canScrollDown) && (!$("ul.carCol").hasClass("two"))) {
			first += 5;
			curr += 5;
		} else if ((!canScrollUp && canScrollDown) && ($("ul.carCol").hasClass("two"))) {
			first += 10;
			curr += 10;
		} else if ((canScrollUp && canScrollDown) && ($("ul.carCol").hasClass("two"))) {
			first += 10;
			curr += 10;
		}
	});
	if (window.location.hash != "") {
		var hashMark = window.location.hash.split("#")[1];
		$.each(JSONobj.videos.categories.entry, function (i, n) {
			if (isNaN(hashMark)) {
				vidCat = this.categories.name;
				if (this.categories.name.toLowerCase() == hashMark.replace(/-/g, ' ')) {
					activeCategoryName = vidCat; //activeVideoId = vidId;
					$(this.categories.videos.video).each(function (i, n) {
						activeVideoId = this.id;
						return false;
					});
				};
			} else {
				activeVideoId = hashMark;
				$(this.categories.videos.video).each(function (i, n) {
					vidId = this.id;
					vidCat = this.category;
					if ($.trim(vidId).toLowerCase() == $.trim(activeVideoId).toLowerCase()) {
						activeCategoryName = vidCat;
					};
				});
			}
		});
	} else {
		var locationURL = window.location.href;
		var locationId = locationURL.lastIndexOf("/");
		locationURL = locationURL.slice(locationId + 1);
		locationURL = locationURL.split(".")[0].toLowerCase();
		try {
			$.each(JSONobj.videos.categories.entry, function (i, n) {
				$(this.categories.videos.video).each(function (i, n) {
					vidId = this.id;
					vidCat = this.category;
					vidFileName = this.fileName;
					if ($.trim(vidFileName).toLowerCase() == $.trim(locationURL).toLowerCase()) {
						activeCategoryName = vidCat;
						activeVideoId = vidId;
						return false;
					}
				})
			});
		} catch(e) {}
	}
	buildTabs(); //build the categories tabset
	setActiveCategory(activeCategoryName);
	showArrowScrollers();
	$("a.link").click(function () {
		$(this).parents("li").addClass("selected active").siblings("li").removeClass("selected active");
	});
});