

window.onload = initSkillIndex;
var providerTabView;
var jobTabView;

function initSkillIndex() {
	if( $('skillPage') &&
	    $('skillPage').value == 'Y' ) {
		providerTabView = new YAHOO.widget.TabView('provider_tabs');
		removeClassName($('provider_tabs'), 'displayNone');
		
		jobTabView = new YAHOO.widget.TabView('job_tabs');
		removeClassName($('job_tabs'), 'displayNone');
	}

	if( $('totalEarnings') ){
		rollingCounter.init( 
			$('totalEarnings').value, $('totalEarningsStep').value, $('totalEarningsTime').value 
		);
	}

	//create our Accordion instance
	var myAccordion = new Accordion($('accordion'), 'div.boxHeader', 'div.boxContent', {
		opacity: false,
		start:'first-open',
		alwaysHide:true,
		onActive: function(toggler, element){
			var parent = toggler.getParent('div');
			parent.removeClass('closedBox');
		},
		onBackground: function(toggler, element){
			var parent = toggler.getParent('div');
			parent.addClass('closedBox');
		}
	});
	var toggler = new Element('div.boxHeader', {
		
	});
	
		
}

function switchLeftNavBox(box) {
	if( $(box + 'Box').className.search('closedBox') == -1 ){
		$(box + 'Box').addClass('closedBox');
	} else {
		$(box + 'Box').removeClass('closedBox');
	}
} 

function switchCat(catid) {
	var catEntry = document.getElementsByTagName('div');
	for(var i = 0; i < catEntry.length; i++) {
		if( catEntry[i].id.substr(0,5) != 'catid' ){ continue; }
		addClassName(catEntry[i], 'displayNone');
	}
	$('catid'+catid).removeClass('displayNone');

	var catBullets = document.getElementsByTagName('li');
	for(var i = 0; i < catBullets.length; i++) {
		if( catBullets[i].id.substr(0,9) != 'catBullet' ){ continue; }
		removeClassName(catBullets[i], 'on'); // Skills Index
		removeClassName(catBullets[i], 'selected'); // Homepage
	}
	$('catBullet'+catid).addClass('on'); // Skills Index
	$('catBullet'+catid).addClass('selected'); // Homepage
}


/*** ROLLING COUNTER ***/
var rollingCounter = {
	num : '1',
	dH : 35, // Height of one digit
	tickStep : 1,    // how much to tick each interval
	tickTime : 1000, // length of tick interval
	flipStep : 5,  // How much to move each flip interval [must be divisor of height!]
	flipTime : 30, // length of flip interval
	intervals    : new Array(20),
	flipProgress : new Array(20),

	// Initialize counter	
	init : function( newNum, step, time ){	
		newNum = parseInt( newNum );
		if( newNum == 'NaN' ){ return; }

		// Read the dH from rolling counter's clientHeight
		rollingCounter.dH = $('rollingCounterNoScript').clientHeight;

		// Put the number on the page
		$('rollingCounterNoScript').style.display = 'none';
		rollingCounter.num = '' + newNum;
		for( i = 1; i <= rollingCounter.num.length; i++ ){
			rollingCounter.addDigit(i);
		}	
		rollingCounter.putNum();

		// check inc amt and inc time variables
		step = parseInt( step );
		if( step == 'NaN' ){ return; }
		rollingCounter.tickStep = step;

		time = parseInt( time );
		if( time == 'NaN' ){ return; }
		rollingCounter.tickTime = time * 1000;

		// If we are good and positive, start the ticking		
		if( rollingCounter.tickStep > 0 ){		
			setInterval( rollingCounter.tick, rollingCounter.tickTime );
		}
	},

	// Add one digit window
	addDigit : function( i ){
		// Every 3rd digit from 1 (4, 7, 10,...) needs a comma
		if( i > 3 && i % 3 == 1 ){
			var comma       = document.createElement('div');
			comma.className = 'digitComma';
			comma.innerHTML = ',';
			$('counter').insertBefore(comma, $('counterFirst').nextSibling );
  	}

		// Add the digit window
		var elem = $('init_digitWindow').clone();
		elem.id = 'digitWindow' + i;
		elem.style.display = '';
		elem.firstChild.id = 'digitSlider' + i;
	
		$('counter').insertBefore(elem, $('counterFirst').nextSibling );
	},


	/** Puts the number into the space **/
	putNum: function(){
		for( i = 1; i <= rollingCounter.num.length; i++ ){
			var thisDigit = rollingCounter.num.substr( rollingCounter.num.length - i, 1);
			rollingCounter.putDigit(i, thisDigit);
		}
	},

	// Puts one digit into place
	putDigit : function( i, val ){
		// Complex! 
		// dH * 10 is for the extra digits [think of them as 10->19, ex:for flip from 9 -> 14]
		// 9 - val because they are arranged from bottom to top.
		$('digitSlider' + i).style.bottom = 
			((rollingCounter.dH * 10) + ((9 - val) * rollingCounter.dH)) + 'px';
	},

	/** For fliping a single digit **/
	flipDigit : function(i, thisDigit, delta ){
		clearInterval( rollingCounter.intervals[i] ); // sanity
		rollingCounter.flipProgress[i] = 0;           // sanity
		rollingCounter.intervals[i] = setInterval( 
			'rollingCounter.flipper('+i+','+ thisDigit + ',' + delta +');', 
			rollingCounter.flipTime
		);
	},

	// This function is called on interval to slowly move the new digit into place
	flipper : function( i, thisDigit, delta ){
		// Exit condition: when we moved delta digits
		if( rollingCounter.flipProgress[i] >= delta * rollingCounter.dH ){
			clearInterval( rollingCounter.intervals[i] );
			return;
		}
		rollingCounter.flipProgress[i] += rollingCounter.flipStep * delta; // Rate of flipping
		// Move the digit slider down some
		$('digitSlider' + i).style.bottom = 
			((rollingCounter.dH * 10) + ((9 - thisDigit) * rollingCounter.dH) - rollingCounter.flipProgress[i]) + 'px';
	},


	/** increments the number and flips digits **/
	tick : function(){
		rollingCounter.putNum(rollingCounter.num);
		newNum = (parseInt(rollingCounter.num) + rollingCounter.tickStep) + '';

		// If the new number is longer than the old one, we need to add digit(s)
		if( newNum.length > rollingCounter.num.length ) {
	  	for( i = rollingCounter.num.length + 1; i <= newNum.length; i++ ){
				rollingCounter.addDigit(i);
				// pad the number so the digits line up correctly
				rollingCounter.num = '0' + rollingCounter.num; 
			}
		}

		// Foreach digit, calculate the delta, then flip
		for( i = 1; i <= rollingCounter.num.length; i++ ){
			var thisDigit1 = rollingCounter.num.substr( rollingCounter.num.length - i, 1);
			var thisDigit2 = newNum.substr( newNum.length - i, 1);
			if( thisDigit1 != thisDigit2 ){
				var delta = thisDigit2 - thisDigit1;
				if( delta < 0 ){ delta += 10; }       // No negative delta please!
				rollingCounter.flipDigit(i, thisDigit1, delta );
			}
		}

		rollingCounter.num = newNum; // set the new number
	}
} // End rolling counter




//
// OnMouseOver for landing page
//
function slotMouseOver(slot) {
  for( i = 1; i < 10; i++ ) {
    if( !$('slot'+i) ){ continue; }
    document.getElementById('slot'+i).style.display = 'none';
    document.getElementById('bubbleLP-SH'+i).className = 'bubbleLP-SH';
    document.getElementById('bubbleLP-SH'+i+'L').className = 'bubbleLP-SHL';
    document.getElementById('bubbleLP-SH'+i+'R').className = 'bubbleLP-SHR';
  }

  document.getElementById('slot'+slot).style.display = 'block';
  document.getElementById('bubbleLP-SH'+slot).className = 'bubbleLP-SHA';
  document.getElementById('bubbleLP-SH'+slot+'L').className = 'bubbleLP-SHLA';
  document.getElementById('bubbleLP-SH'+slot+'R').className = 'bubbleLP-SHRA';
}


//
// Load the customer quotes animated GIF 
//
// type is either 'Buyer' or 'Provider'
function loadQuote( type ) {
  var randGif = Math.random();
	var animated = '<a href="/p/testimonials"><img src="/media/images/4.0/landing/customer_quotes_emp_v1.gif" border="0"/></a>';

	if( type == 'Provider' ) {
	  randGif = Math.floor(randGif * 3) + 1;
	  animated = '<a href="/p/provider_testimonials.html"><img src="/media/images/4.0/landing/customer_quotes_prov_v' + randGif + '.gif" border="0"/></a>';
	}
  else {
		randGif = Math.floor(randGif * 4) + 1;
		animated = '<a href="/p/testimonials"><img src="/media/images/4.0/landing/customer_quotes_emp_v' + randGif + '.gif" border="0"/></a>';
	}
  $("landingcustomersays").innerHTML = animated;
}


//
// Load the video
//
function loadVideo( swfUrl ) {
	if( $('mainvideo') ) {
	  var vid = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="673" height="308"><param name="movie" value="' + swfUrl + '" /><param name="quality" value="high" /><param name="wmode" value="opaque" /><param name="base" value="' + swfUrl + '"><embed src="' + swfUrl + '" base="' + swfUrl + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="673" height="308" wmode="opaque"></embed></object>';
	  $('mainvideo').innerHTML = vid;
	}
}

//
// Check Flash plugin installed or not
//
function isFlashPluginInstalled(){
	try{
		if(typeof navigator.plugins != "undefined"){
			var a = null;
			try{
				a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			}catch(e1){
			}
			if(typeof navigator.plugins["Shockwave Flash"] == "object" || typeof navigator.mimeTypes["application/x-shockwave-flash"] != "undefined"
				|| typeof navigator.plugins["Shockwave Flash 2.0"] != "undefined" || a){
				return true;
			}else{
				return false;
			}
		}	
	}catch(e){
		
	}
	return false;
}
