﻿var setupCarouselHasRun = false;

jQuery(document).ready(function ($) {

//PVH TIME FORMATTING
function formatTime(secs){
	var hours = Math.floor(secs / (60 * 60));
	var divisor_for_minutes = secs % (60 * 60);
	var minutes = Math.floor(divisor_for_minutes / 60);
	var divisor_for_seconds = divisor_for_minutes % 60;
	var seconds = Math.ceil(divisor_for_seconds);
	var obj = minutes + ':' + seconds;
	return obj;
}

//PVH FORMAT TITLE TO 75 CHARACTERS
function formatTitle(title){
	if (title.length > 75) {
		var shortTitle = title.substr(0,75) + ' ...';
		return shortTitle;
	} else {
		return title;
	}
} 

//PVH FORMAT DATE
function formatDate(unixTime) {
	var vt = new Date();
	vt.setTime(unixTime * 1000);
	var s = "";
	var today = new Date();

	if (today.getFullYear() == vt.getFullYear()) {
		if (today.getMonth() == vt.getMonth()) {
			if (today.getDate() == vt.getDate()) {
				return (formatTime(vt));
			}
		}
	}
	switch (vt.getMonth()) {
		case 0: s = "Jan "; break;
		case 1: s = "Feb "; break;
		case 2: s = "Mar "; break;
		case 3: s = "Apr "; break;
		case 4: s = "May "; break;
		case 5: s = "Jun "; break;
		case 6: s = "Jul "; break;
		case 7: s = "Aug "; break;
		case 8: s = "Sep "; break;
		case 9: s = "Oct "; break;
		case 10: s = "Nov "; break;
		case 11: s = "Dec "; break;
        }
	return (s + " " + vt.getDate());
}

// SCROLL ANIMATION
function goToByScroll(id){
	$("html,body").animate({ scrollTop: $('body').css('height') }, 0);
}

// PVH JCAROUSEL CONTROLS
function setupCarouselControls() {
	$('<a id="pvhListNavPrev" class="disabled">prev</a><a id="pvhListNavNext">next</a>').insertAfter('#pvhList .content');
}

// PVH JCAROUSEL
function setupCarousel() {	
	if ( setupCarouselHasRun == false ) {
		$('#pvhList .content').jCarouselLite({
			btnNext: '#pvhListNavNext',
			btnPrev: '#pvhListNavPrev',
			visible: 5,
			scroll: 1,
			circular: false
		});
		setupCarouselHasRun = true;
	}		
}

// RUN PERSONAL VIEWING HISTORY
function runPVH() {
        $.getJSON('http://www.brainshark.com/brainshark/webservices_mobile/history.ashx?callback=?', function(data) {
            	if (data.PresentationHistory.length > 0) {
			setupCarouselControls();
			var pvhCookie=getCookie("pvhCookie");
                        var thisPage = location.href;
                        if (thisPage.substr(-1) == '/') {
                                currPage=thisPage.substr(0, thisPage.length - 1);
                            } else {
                                currPage = thisPage;
                        };

			if (pvhCookie != "false") {
				if ((currPage == "http://staging.brainshark.com")  || (currPage == "http://staging.brainshark.com/") || (currPage == "http://training.brainshark.com") || (currPage == "http://staging.brainshark.com/help-and-training") || (currPage == "http://www.brainshark.com/") || (currPage == "http://www.brainshark.com")  || (currPage == "http://www.brainshark.com/help-and-training") || (currPage == "http://training.brainshark.com/help-and-training")) {
					$('#pvhButton').slideDown();
                         		$('#footer-primary .footer-tab-control #pvhTab a').click(function() {
					        $('#pvhButton').slideUp();
					        $('#pvhList').css('position','relative').css('z-index','100');
					        $('#pvhList').insertAfter('#footer-primary .footer-tab-control');
					        setupCarousel();
					        goToByScroll("pvhList");
					        return false;
				        }); 
				} else {
				        $('#footer-primary .footer-tab-control #pvhTab a').click(function() {
				        	$('#pvhButton').slideUp();
				        	$('#pvhList').css('position','relative').css('z-index','100');
				        	$('#pvhList').insertAfter('#footer-primary .footer-tab-control');
				        	setupCarousel();
				        	goToByScroll("pvhList");
				        	return false;
				        }); 
				}
				$("#pvhCheckbox").attr("checked", true);
			} else {
				$("#pvhCheckbox").attr("checked", false);
				$('#footer-primary .footer-tab-control #pvhTab a').click(function() {
					$('#pvhButton').slideUp();
					$('#pvhList').css('position','relative').css('z-index','100');
					$('#pvhList').insertAfter('#footer-primary .footer-tab-control');
					setupCarousel();
					goToByScroll("pvhList");
					return false;
				}); 
			}

			$.each(data.PresentationHistory, function(i, val) {
				var isActive = data.PresentationHistory[i].IsActive;
				var isDeleted = data.PresentationHistory[i].IsDeleted;
				var isExpired = data.PresentationHistory[i].IsExpired;
				var MayView = data.PresentationHistory[i].MayView;
				//alert("isActive = "+isActive);
				//alert("isDeleted = "+isDeleted);
				//alert("isExpired = "+isExpired);
				//alert("MayView = "+MayView);
				var insertBSK = false;
				if ((isActive == true) && (isDeleted == false) && (isExpired == false) && (MayView == true)) insertBSK = true;
				//alert("insertBSK = "+insertBSK);
				if (insertBSK == true) { 
					var title=formatTitle(data.PresentationHistory[i].Title);
					var time=data.PresentationHistory[i].Duration;
					var duration=formatTime(time);
					var slides=data.PresentationHistory[i].NumberOfSlides;
					if (slides > 1) {
						slides = slides + ' slides';
						} else {
							slides = '1 slide';
						}
					var author=data.PresentationHistory[i].AuthorName;
					if (author == "") {
						author = "Brainshark";
					}
					$('#pvhList .wrap .content ul').append('<li><a href="' + data.PresentationHistory[i].Url + '" target="_blank"><span class="title"><strong>' + title + '</strong></span><img src="'  + data.PresentationHistory[i].ThumbnailUrl  + '" alt="' + title + '" width="150" height="120" border="0" ><span class="extra">' + author + '<br />(' + duration + ') ' + slides + '<br />Last viewed: ' + formatDate(data.PresentationHistory[i].LastViewTime) + '</span></a></li>');
					}	    
			});
                              

			$('#pvhButton .close').click(function() {
				$("#pvhCheckbox").attr("checked", true);
                    		$('#pvhButton').slideUp();
			});

			$('#pvhButton .open').click(function() {
					$('#footer-primary .footer-tab-control #pvhTab a').click();
					//clicking text link works here
			});

		} else {
			removePVHtab();
		}
        });
}

// CHECK IF NOT IE6 AND RUN PVH
var IE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
if(!IE6) {
	runPVH();
} else {
	removePVHtab();
}

function removePVHtab() {
	$('#pvhTab').remove();
	$('#footer-primary .footer-tab-control #ft-ps').addClass('last');
}

$('#footer-primary .footer-tab-control li a').click(function() {
        $('#pvhList').css('position','relative').css('z-index','100');
        $('#pvhList').insertAfter('#footer-primary .footer-tab-control');
	setupCarousel();
	goToByScroll("pvhList");
	return false;
}); 


// JS COOKIE CODE
function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name) {
			return unescape(y);
		}
	}
}

function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function checkCookie() {
	var pvhCookie=getCookie("pvhCookie");
	if (pvhCookie == "true") {
		$('#pvhCheckbox').checked(false);
	} else {
		$('#pvhCheckbox').checked(true);
	}
}

$('#pvhCheckbox').click(function() {
	var checked = $('#pvhCheckbox:checked').val() != undefined;
	setCookie("pvhCookie",checked,365);
});

// END pvh.js
});

