function myRandNo () {
	var randNumber = Math.floor(Math.random()*1001);
	return (randNumber);
}

doSubMenu = function(obj) {
	if (window.timer) {
		clearTimeout(window.timer);
	}
	window.obj = obj.parentNode;
	subMenu = document.getElementById('subMenuHolder');
	subMenu.style.visibility = "hidden";
	hFactor = obj.offsetHeight;
	lFactor = obj.parentNode.offsetLeft;
	tFactor = obj.parentNode.offsetTop;
	tFactor = (hFactor + tFactor);
	subMenuContents = obj.parentNode.getElementsByTagName('div')[0].innerHTML;
	subMenu.innerHTML = subMenuContents;
	//alert (tFactor+","+lFactor+"["+hFactor+"]");
	subMenu.style.left = (lFactor) + "px";
	subMenu.style.top = (tFactor) + "px";
	subMenu.style.visibility = "visible";
}

showSub = function() {
	if (window.timer) {
		clearTimeout(window.timer);
	}
	//if (window.obj) {
		//window.obj.className = "current";
	//}
	subMenu = document.getElementById('subMenuHolder');
	subMenu.style.visibility = "visible";
	window.timer = setTimeout("hideSub()", 4500);
}

hideSub = function() {
	//if (window.obj) {
		//window.obj.className = null;
	//}
	subMenu = document.getElementById('subMenuHolder');
	subMenu.style.visibility = "hidden";
	clearTimeout(window.timer);
}


function getUpdate(typ, pr1, pr2, pid) {
	var started = false;
	if(typ == 'state') {
		if((pr1 == 2) && (started == false)) {
			// remember that player is started
			started = true;
		}
		// keep checking to see if player is completed
		if(pr1 == 3) {
			// reset started because player is completed
			started = false;
			// make him disappear
			document.getElementById('id').style.visibility = 'hidden';
		}
	}
};

function openTour(theURL){
	hideSub();
	tb_show('AuditMatic Tour', theURL, '');
	return (false);
}

function thisMovie(movieName) {
	if(navigator.appName.indexOf('Microsoft') != -1) {
		return window[movieName];
	} else {
	return document[movieName];
	}
};

function returnFilename(){
	var url = document.URL;
	var docName = (url.substring(url.lastIndexOf("\/")+1,url.lastIndexOf("\.")));
	return docName;
}

function navOptions(){
	var file = returnFilename();
	var theID = "#navOptions_"+file;
	return theID;
}

//Get date of next friday
function nextFriday(dateStr) {
	var d = new Date(dateStr);
	var n_date = d.getDate();
	var n_day = d.getDay();
	var n_month = (d.getMonth())+1;
	var n_year = d.getYear();
	var n_hour = (d.getHours())+1; // Host is MST
	var n_minute = d.getMinutes();
	function getMonthInfo() {
		function calcLeapYear() {
			if ((n_year == 2012) || (n_year == 2016)) {
				var days = 29;
			} else {
				var days = 28;
			}
			return days;
		}
	  if (n_month == 1) month = new Array("Jan.",31);
	  if (n_month == 2) month = new Array("Feb.",calcLeapYear());
	  if (n_month == 3) month = new Array("March",31);
	  if (n_month == 4) month = new Array("April",30);
	  if (n_month == 5) month = new Array("May",31);
	  if (n_month == 6) month = new Array("June",30);
	  if (n_month == 7) month = new Array("July",31);
	  if (n_month == 8) month = new Array("Aug.",31);
	  if (n_month == 9) month = new Array("Sept.",30);
	  if (n_month == 10) month = new Array("Oct.",31);
	  if (n_month == 11) month = new Array("Nov.",30);
	  if (n_month == 12) month = new Array("Dec.",31);
	  return (month);
	}
	var monthInfo = getMonthInfo();
	var offset = 5-n_day;
	var n_friday = n_date+offset;
	if (n_day > 5) {
		var n_friday = n_friday+7;	
	} else if (n_day == 5) {
		if (n_hour == 9) {
			if (n_minute >= 30) {
				var n_friday = n_friday+7;
			}
		}
		if (n_hour >= 10) {
			var n_friday = n_friday+7;
		}
	}
	if (n_friday > monthInfo[1]) {
		var n_friday = (n_friday-(eval(monthInfo[1])));
		var n_month = (n_month+1);
		var monthInfo = getMonthInfo();
	}

	// build date line
	var n_nextFriday = (", "+monthInfo[0]+" "+n_friday+" ");
	
	//insert next friday into page
	//alert(n_nextFriday);
	$(".nextFriday_date").html(n_nextFriday);
}


function worldmap(){
	$("#varArea").hide();
	var theFile = "world.swf?data_file=data-World.xml?"+myRandNo();
	var so = new SWFObject(theFile,'mpl','750','425','8','#f3f3f3');
	so.addParam('allowscriptaccess','sameDomain');
	so.addParam('wmode', 'transparent');
	so.write('buy-map');
	//$("#buy-map").removeClass("us-canada");
	$("#btn-showWorld").hide();
}

function uscanadamap(state){
	$("#varArea").hide();
	var theFile = "us_canada.swf?data_file=data-US-Canada.xml?"+myRandNo();
	var so = new SWFObject(theFile,'mpl','750','425','8','#f3f3f3');
	so.addParam('allowscriptaccess','sameDomain');
	so.addParam('wmode', 'transparent');
	so.write('buy-map');
	//$("#buy-map").addClass("us-canada");
	$("#btn-showWorld").show();
	getVAR(state);
}

function getVAR(state,item){
	if (!item) {
		if (state == "Canada") {
			stateLabel = "province";
		} else {
			stateLabel = "state";
		}
		document.getElementById('varArea').innerHTML = "<h3>"+state+"</h3><p>Please, select a "+stateLabel+" on the map.</p>";
	}
	else {
		$('#varArea').hide().html('');
		//$('#varArea').html("<h3>"+state+"</h3>");
		var dists = item.split("|");
		var distsL = dists.length;
		var i=0;
		for (i = 0; i < distsL; i++) {
			var thisDist = dists[i];
			if (state == "Pennsylvania" && thisDist == "dist_GMS-USA") {
				var stateP = state;
				var state = "Western Pennsylvania";
			}
			else if (state == "Kentucky" && thisDist == "dist_GMS-USA") {
				var stateP = state;
				var state = "Eastern Kentucky";
			}
			else if (stateP) {
				var state = stateP;
			}	
			$('#varArea').append("<h3>"+state+"</h3>");
			var varItem = $("#"+thisDist).html();
			$('#varArea').append(varItem);
		}
	}
	$('#varArea').slideDown('slow');
}


// JQUERY
$(document).ready(function() {
// do something here

	$(".formArea").corner("10px");
	
	if (location.search) {
		var tour = $.query.get('tour');
		if (tour) {
			var targURL = $('#prodTourTeaser .thickbox').attr("href");
			openTour(targURL);
		}
	}

    if (document.getElementById('timeAndDate')) {
    	var timeAndDate = $("#timeAndDate").text();
		var dateStr  = jQuery.trim(timeAndDate);
		var d = new Date(dateStr);
		nextFriday(d);
    }
    if (document.getElementById('leftSide')) {
    	//$("body").addClass("handheld");
    	var div__body = document.getElementById('div__body').getElementsByTagName('table')[0];
    	//var storetable = div__body.getElementsByTagName('table')[0];
    	$(div__body).addClass("storetable");
    	//$(#div__body).addClass("storetable");
    }
    if (document.getElementById('AuditMatic')) {
    	$("#AuditMatic").corner("14px");
    }
	//$('a.feed').gFeed({target:'#feeds',max:3});
	$('a.external').attr("target","_blank");
	$("#testimonialArea").click(function()
		{
			var targURL=this.getElementsByTagName('a')[1].href;
			window.location=targURL;
		}
	);
	$("#showPromoArea").click(function()
		{
			var targURL=this.getElementsByTagName('a')[0].href;
			window.location=targURL;
		}
	);
	$(".setOff").corner("12px");
	if (document.getElementById('navOptions')) {
    	$(navOptions()).addClass("current");
	}
    function laborSavingsGraph() {
	    //$("#data-collection .AuditMatic .bar").animate( { width:"60%" }, { preDelay: 2000, queue: "cost", duration: 300, scope: "01" } );
	    //$("#data-collection .Paper .bar").animate( { width:"90%" }, { preDelay: 2000, queue: "cost", duration: 400, scope: "01" } );
	    $("#data-entry .AuditMatic .bar").animate( { width:"5%" }, { queue: "cost", duration: 400, scope: "02" } );
	    //$("#data-entry .Paper .bar").animate( { width:"65%" }, { queue: "cost", duration: 200, scope: "02" } );
	    $("#reporting .AuditMatic .bar").animate( { width:"25%" }, { queue: "cost", duration: 500, scope: "06" } );
	    //$("#reporting .Paper .bar").animate( { width:"75%" }, { queue: "cost", duration: 300, scope: "06" } );
	    $("#scorecarding .AuditMatic .bar").animate( { width:"15%" }, { queue: "cost", duration: 600, scope: "04" } );
	    //$("#scorecarding .Paper .bar").animate( { width:"70%" }, { queue: "cost", duration: 400, scope: "04" } );
	    $("#notification .AuditMatic .bar").animate( { width:"10%" }, { queue: "cost", duration: 500, scope: "05" } );
	    //$("#notification .Paper .bar").animate( { width:"85%" }, { queue: "cost", duration: 300, scope: "05" } );
	    $("#duplication .AuditMatic .bar").animate( { width:"3%" }, { queue: "cost", duration: 400, scope: "03" } );
	    //$("#duplication .Paper .bar").animate( { width:"55%" }, { queue: "cost", duration: 200, scope: "03" } );
    }
    function procImpoveGraph() {
    	//$("#sect3 .l1").show({ duration: 500, queue: "proc"});
    	$("#pointer").show({ preDelay: 1000, duration: 125, queue: "proc", scope: "l1"});
    	$("#sect3 .l2").show({ duration: 700, queue: "proc", scope: "l2"});
    	$("#sect3 .guts").slideDown({ duration: 700, queue: "proc", scope: "l2"});
    	$("#sect2 .l3").show({ duration: 700, queue: "proc", scope: "l3"});
    	$("#sect2 .guts").slideDown({ duration: 700, queue: "proc", scope: "info", scope: "l3"});
    	$("#sect1 .l4").show({ duration: 500, queue: "proc", scope: "l4" });
    	$("#sect1 .guts").slideDown({ duration: 500, queue: "proc", scope: "l4"});
    }
    function compAssuranceGraph() {
    	$("#day1 .marker").show({ preDelay: 700, duration: 125, queue: "comp"});
    	$("#day2 .marker").show({ preDelay: 700, duration: 125, queue: "comp"});
    	$("#day3 .marker").show({ preDelay: 700, duration: 125, queue: "comp"});
    	$("#day4 .marker").show({ preDelay: 500, duration: 125, queue: "comp"});
    	$("#day5 .marker").show({ preDelay: 500, duration: 125, queue: "comp"});
    	//$("#graphNotes .one").slideDown({ preDelay: 500, duration: 500, queue: "comp"});
    	//$("#graphNotes .two").slideDown({ duration: 500, queue: "comp"});
    }
    function feasibilityEvaluation() {
    	$(".evalFindingsArea .e1").slideDown({ preDelay: 700, duration: 125, queue: "eval"});
    	$(".evalFindingsArea .e2").slideDown({ preDelay: 700, duration: 125, queue: "eval"});
    	$(".evalFindingsArea .e3").slideDown({ preDelay: 700, duration: 125, queue: "eval"});
    	$(".evalFindingsArea .e4").slideDown({ preDelay: 700, duration: 125, queue: "eval"});
    	$(".evalFindingsArea .e5").slideDown({ preDelay: 700, duration: 125, queue: "eval"});
    }
        
    if (document.getElementById('compAssureZone')) {
		$(".dayGroup .title").corner("top");
		$(".rowLabels .label").corner("tl bl");
    	compAssuranceGraph();
    }   
    if (document.getElementById('procImprZone')) {
    	procImpoveGraph();
    }
    if (document.getElementById('laborSavings')) {
    	$(".graphZone").corner("10px");
    	laborSavingsGraph();
    }
    if (document.getElementById('entFriend')) {
    	$(".listZone").corner("10px");
    }
    if (document.getElementById('benefitsZone')) {
    	$(".benefitArea").corner("10px");
    }
    if (document.getElementById('feasibility-evaluation')) {
    	$(".flowAround .actionItem").corner("10px");
    	$(".evalFindingsArea").corner("10px");
    	$(".evalFindingsArea li").corner("5px");
    	feasibilityEvaluation();
    }
    if (document.getElementById('varZone')) {
    	$(".varSect").corner("tl bl");
    }
    if (document.getElementById('buy-map')) {
		$("#btn-showWorld").click(function () {
		worldmap();
			return false;
		});
    	worldmap();
    }
});