if ( !window.SCHL ) { window.SCHL = {}; } else if ( typeof window.SCHL != 'object' ) { throw new Error("SCHL not an object."); }
SCHL.clifBB = { final_stage : false };

var bbSTATES = {
	'WA' : { 'top' : '2', 'left' : '36', 'pos' : 47 },
	'OR' : { 'top' : '25', 'left' : '23', 'pos' : 37 },
	'ID' : { 'top' : '11', 'left' : '77', 'pos' : 12 },
	'NY' : { 'top' : '47', 'left' : '359', 'pos' : 32 },
	'DE' : { 'top' : '102', 'left' : '394', 'pos' : 8 },
	'VT' : { 'top' : '46', 'left' : '399', 'pos' : 45 },
	'NH' : { 'top' : '42', 'left' : '409', 'pos' : 29 },
	'ME' : { 'top' : '15', 'left' : '415', 'pos' : 19 },
	'MA' : { 'top' : '59', 'left' : '404', 'pos' : 21 },
	'RI' : { 'top' : '72', 'left' : '417', 'pos' : 39 },
	'CT' : { 'top' : '74', 'left' : '404', 'pos' : 7 },
	'PA' : { 'top' : '80', 'left' : '353', 'pos' : 38 },
	'NJ' : { 'top' : '85', 'left' : '393', 'pos' : 30 },
	'MD' : { 'top' : '102', 'left' : '365', 'pos' : 20 },
	'WV' : { 'top' : '103', 'left' : '344', 'pos' : 48 },
	'VA' : { 'top' : '110', 'left' : '339', 'pos' : 46 },
	'NC' : { 'top' : '137', 'left' : '333', 'pos' : 33 },
	'SC' : { 'top' : '157', 'left' : '342', 'pos' : 40 },
	'FL' : { 'top' : '202', 'left' : '309', 'pos' : 9 },
	'GA' : { 'top' : '163', 'left' : '323', 'pos' : 10 },
	'AL' : { 'top' : '165', 'left' : '302', 'pos' : 1 },
	'MS' : { 'top' : '167', 'left' : '275', 'pos' : 24 },
	'LA' : { 'top' : '188', 'left' : '252', 'pos' : 18 },
	'OK' : { 'top' : '148', 'left' : '179', 'pos' : 36 },
	'KS' : { 'top' : '116', 'left' : '185', 'pos' : 16 },
	'NE' : { 'top' : '84', 'left' : '175', 'pos' : 27 },
	'IA' : { 'top' : '81', 'left' : '231', 'pos' : 15 },
	'WI' : { 'top' : '43', 'left' : '259', 'pos' : 49 },
	'MN' : { 'top' : '24', 'left' : '228', 'pos' : 23 },
	'SD' : { 'top' : '55', 'left' : '176', 'pos' : 41 },
	'ND' : { 'top' : '25', 'left' : '179', 'pos' : 34 },
	'MT' : { 'top' : '13', 'left' : '98', 'pos' : 26 },
	'WY' : { 'top' : '58', 'left' : '120', 'pos' : 50 },
	'UT' : { 'top' : '85', 'left' : '90', 'pos' : 44 },
	'AZ' : { 'top' : '135', 'left' : '76', 'pos' : 3 },
	'HI' : { 'top' : '244', 'left' : '146', 'pos' : 11 },
	'NV' : { 'top' : '74', 'left' : '47', 'pos' : 28 },
	'CA' : { 'top' : '66', 'left' : '19', 'pos' : 5 },
	'AK' : { 'top' : '197', 'left' : '47', 'pos' : 2 },
	'MI' : { 'top' : '55', 'left' : '307', 'pos' : 22 },
	'OH' : { 'top' : '91', 'left' : '323', 'pos' : 35 },
	'MO' : { 'top' : '112', 'left' : '239', 'pos' : 25 },
	'IL' : { 'top' : '90', 'left' : '271', 'pos' : 13 },
	'IN' : { 'top' : '95', 'left' : '300', 'pos' : 14 },
	'KY' : { 'top' : '123', 'left' : '290', 'pos' : 17 },
	'TN' : { 'top' : '145', 'left' : '285', 'pos' : 42 },
	'AR' : { 'top' : '153', 'left' : '246', 'pos' : 4 },
	'TX' : { 'top' : '151', 'left' : '146', 'pos' : 43 },
	'NM' : { 'top' : '142', 'left' : '121', 'pos' : 31 },
	'CO' : { 'top' : '101', 'left' : '130', 'pos' : 6 }
};


soundManager.url = 'sm/';
soundManager.debugMode = false;
soundManager.defaultOptions.volume = 75;
soundManager.onload = function() {
	var bell = soundManager.createSound({
		id: 'bellSound',
		url: 'clifford_bell.mp3'
	});

	var bark = soundManager.createSound({
		id: 'barkSound',
		url: 'clifford_bark.mp3'
	});
	bell.load();
	bark.load();
};

function addCommas(nStr)
	{
	   nStr += '';
	   x = nStr.split('.');
	   x1 = x[0];
	   x2 = x.length > 1 ? '.' + x[1] : '';	   
	   var rgx = /(\d+)(\d{3})/;
	   while (rgx.test(x1)) {
	     x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function getDigit(d) {		
		var cn = (d == ',') ?  'Comma' :  d;
		
		return '<span class="d' + cn  + '">' + d + '</span>';
}

function setupDigits1(num) {
	var strNum = isNaN(parseInt(num, 10)) ? '0' : parseInt(num, 10) + '';	
	var c = '';

	if (strNum < 999) { c = 'stage0'; }
	else if (strNum < 1999) { c = 'stage1'; }
	else if (strNum < 2999) { c = 'stage2'; }
	else if (strNum < 3999) { c = 'stage3'; }
	else if (strNum < 4999) { c = 'stage4'; }
	else { c = 'stage5'; SCHL.clifBB.final_stage = true; }

	
	strNum = addCommas(strNum);
	var clock = '<div style="width:' + (strNum.length * 16) + 'px; margin: 0 auto;">';
	
	for ( var i = strNum.length - 1; i >= 0 ; i-- ) {
		//alert(strNum.charAt(i));
		//clock += getDigit(strNum[i]);
		clock += getDigit(strNum.charAt(i));
	}
	
	clock += '</div>';
	
	var bowl = document.getElementById('bowl');
	bowl.className = c;
	bowl.innerHTML = clock;
}

function setupDigits2(num) {
	var strNum = isNaN(parseInt(num, 10)) ? '0' : parseInt(num, 10) + '';	
	strNum = addCommas(strNum);
	var clock = '<div style="width:' + (strNum.length * 29) + 'px;">';
	
	for ( var i = strNum.length - 1; i >= 0 ; i-- ) {
		//clock += getDigit(strNum[i]);
		clock += getDigit(strNum.charAt(i));
	}
	clock += '</div>';


	document.getElementById('sNum').innerHTML = clock;
	
}

var bone_done = false, jazzit = false, callout = false;

function animateBones() {
	var rand_no = Math.ceil(Math.random()*10);
	var b;
	
	if ( !bone_done )
	{
		var div = document.createElement('div');
		div.id = 'bone';
		div.className = 'b' + rand_no;

		document.getElementById('boneDrop').appendChild(div);

		b = div;
		bone_done = true;
	}
	else {
		b = document.getElementById('bone');
		b.className = 'b' + rand_no;
	}
	
	var start = -55;


	if (!SCHL.clifBB.final_stage)
	{

		var bone_interval = setInterval(function(){
			start += 5;
			b.style.top = start + "px";
			if (start > 300)
			{
				clearInterval(bone_interval);
				animateBones();
			}
		}, 55);
	}
	
}




function tiker(msgs) {
	var tc = document.getElementById('tikerContent');
	tc.innerHTML = msgs;
	var w = tc.offsetWidth;
	var start = 600;

	tci = setInterval(function(){
		start -= 10;
		tc.style.left = start + 'px';

		if (start < (w*-1))
		{
			clearInterval(tci);
			tiker(msgs);
		}

	}, 200);

}


function mapOver(which) {
	var a, b;
	var txtHeight = 21;

	if ( !jazzit )
	{
		var div = document.createElement('div');
		div.id = 'jazz';
		document.getElementById('map').appendChild(div);

		b = div;
		
		var abbr = document.createElement('div');
		abbr.id = 'bbAbbr';
		document.getElementById('bbBox').appendChild(abbr);
		a = abbr;
		
		jazzit = true;
	}
	else {
		a = document.getElementById('bbAbbr');
		b = document.getElementById('jazz');
	}

	if (typeof which == 'undefined' || which == '')
	{
		a.style.display = 'none';
		b.style.display = 'none';
		return;
	}

	a.style.backgroundPosition = '0 -' + bbSTATES[which].pos * txtHeight + 'px';
	a.style.display = 'block';

	b.style.display = 'block';
	b.style.top = bbSTATES[which].top + 'px';
	b.style.left = bbSTATES[which].left + 'px';
	b.innerHTML = '<img src="images/map_over/' + which.toLowerCase() + '.gif" alt="" />';

}

function showCallout(which) {
	var b;	

	b = document.getElementById('bbCallout');

	document.getElementById('stxt').src = 'images/map_callout/' + which.toLowerCase() + '_txt.gif';
	document.getElementById('smap').src = 'images/map_callout/' + which.toLowerCase() + '.gif';
	
	b.style.display = 'block';

	dynamicContent(which);

	
	/*
	b.style.display = 'block';
	b.style.top = bbSTATES[which].top + 'px';
	b.style.left = bbSTATES[which].left + 'px';
	b.innerHTML = '<img src="images/map_over/' + which.toLowerCase() + '.gif" alt="" />';
	*/
}
function hideCallout() { 
	jQuery('#bbCallout, #bbAbbr').hide();
	//document.getElementById('bbCallout').style.display = 'none'; document.getElementById('bbAbbr').style.display = 'none'; 
}

function setUpMap() {
	var area = document.getElementsByTagName('area');
	for ( var i = 0; i < area.length; i ++ ) {
		var temp = area[i].getAttribute('alt');

		
		area[i].onmouseover = function(num) { return function() { mapOver(area[num].getAttribute('alt')); }; }(i);
		area[i].onmouseout = function() { mapOver(); };
		area[i].onclick = function(num) { return function() { showCallout(area[num].getAttribute('alt')); return false; }; }(i);
	}
}

setUpMap();

function makePromise() {
	hideCallout();	
	document.getElementById('bbMakePromiseFrm').style.display = 'block';
}

function wagTail() {
	var tail = document.getElementById('bbTail');
	tail.style.backgroundPosition = '-79px 0';	
	var counter = 0;
	var aa = setInterval(function(){		
		if (counter > 2)
		{
			clearInterval(aa); setOrigTail(tail);
		}
		if (counter % 2 == 0)
		{
			tail.style.backgroundPosition = '-158px 0';
		}
		else {
			tail.style.backgroundPosition = '-79px 0';
		}
		counter += 1;
	},100);
}

function animateTail() {
	wagTail();
	setTimeout(function(){
		if (soundManager.supported()) {
			soundManager.play('barkSound');
		}
		animateClifStage1();
	}, 1500);
}

function animateClifStage1() {
	var cliff = document.getElementById('bbClifford');
	var top = 163;
	var start = 370;
	//cliff.innerHTML = '<img src="images/c2.png" alt="" />';
	var cl = setInterval(function(){
		start -= 50;
		//if ( start < 230 ) { cliff.style.left = '279px'; }
		if ( (start - 50) < top ) { clearInterval(cl); cliff.style.top = top + 'px'; animateClifStage2(cliff);
		}
		else { cliff.style.top = start + 'px'; }
	}, 100);
}

function animateClifStage2(cliff) {
	var box = document.getElementById('bbBox');
	var top = 140;
	var start = 163;
	cliff.style.top = '163px';
	cliff.style.backgroundPosition = '-1269px 0';
	//cliff.innerHTML = '<img src="images/c3.png" alt="" />';
	var cl = setInterval(function(){
		start -= 30;
		if ( (start - 30) < top ) { clearInterval(cl); cliff.style.top = top +  'px'; cliff.style.zIndex = '40'; box.style.background = 'url(images/box_anim.gif)'; box.style.width = '657px'; box.style.height = '468px'; box.style.marginLeft = '42px'; setTimeout(function(){animateClifFinal(box, cliff);}, 200);  }
		else { cliff.style.top = start + 'px'; }
	}, 100);
}

function animateClifFinal(box, cliff) {
	jQuery(box).css({
		'background': 'url(images/box.gif)',
		'height': '454px',
		'margin-left': '45px',
		'width': '648px'
	});
	jQuery(cliff).html('<a href="#" onclick="makePromise(); return false;">Make the promise</a>').css({'background':'none','top':'133px'});
	setTimeout(function() {
		animateBones();
		//ScrollText();				
		dynamicContent('none');
		setTimeout(function(){
			animateTailR();
		}, 10000);		
	}, 100);	
}

function animateTailR() {
	if (soundManager.supported()) {
		soundManager.play('bellSound');
	}
	/*
	var tail = document.getElementById('bbTail');
	tail.style.backgroundPosition = '-79px 0';
	var timer = setTimeout(function(){
		tail.style.backgroundPosition = '-158px 0';
		setOrigTail(tail);
	}, 300);
	*/
	wagTail();
	setTimeout(function(){ animateTailR(); }, 10000);	
}

function setOrigTail(obj) {
	setTimeout(function() {obj.style.backgroundPosition = '0 0';}, 300);
}

window.onload = function() {
	if (soundManager.supported()) {
		soundManager.play('bellSound');
	}
	animateTail();
};


