$(document).ready( function(){
	$('.yesscript').removeClass('yesscript');

	$(".defaultText").focus(function(srcc) {
		if ($(this).val() == $(this)[0].title) {
			$(this).removeClass("defaultTextActive");
			$(this).val("");
		}
	});
	
	$(".defaultText").blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("defaultTextActive");
			$(this).val($(this)[0].title);
		}
	});
	
	$(".defaultText").blur();        

	countdownWidget($('.number span'));

	$("#signin_a").fancybox({
		'hideOnOverlayClick' : false,
		'modal' : true,
		'scrolling'     : 'no',
		'transitionIn'  : 'elastic',
		'transitionOut' : 'elastic'
	});
	$("#fb_register_a").fancybox({
		'hideOnOverlayClick' : false,
		'modal' : true,
		'scrolling'     : 'no',
		'transitionIn'  : 'elastic',
		'transitionOut' : 'elastic'
	});
	$("#register_a").fancybox({
		'hideOnOverlayClick' : false,
		'modal' : true,
		'scrolling'     : 'no',
		'transitionIn'  : 'elastic',
		'transitionOut' : 'elastic'
	});
	$("#reset_a").fancybox({
		'hideOnOverlayClick' : false,
		'modal' : true,
		'scrolling'     : 'no',
		'transitionIn'  : 'elastic',
		'transitionOut' : 'elastic'
	});
	$("#inviter_box_a").fancybox({
		'hideOnOverlayClick' : false,
		'modal' : true,
		'scrolling'     : 'no',
		'transitionIn'  : 'elastic',
		'transitionOut' : 'elastic'
	});



	//------------------------------------------------------------------------------------------------------------------
	// stop form double submit

	$('form').submit(function(){
		$(':submit', this).attr( 'disabled', 'disabled' )
	});
	//------------------------------------------------------------------------------------------------------------------
	// tracking 

	// fires event the first time the user clicks submit on the registration form 
	// but not if they made errors and had to click again
	$('.create_account_form form').submit(function(){
		//alert ('will fire event');
		//alert ('did fire event');
		
		// if the user had the news button selected, do something...
	
		if ($(this).find('#UserNews')[0]){
			if ($(this).find('#UserNews')[0].checked){
				//alert ('news is selected');
			}
		}
	});

	// this event fires the first time the user clicks the "register with facebook" link
	// we have to attach this function to the facebook connect elements, but they may not
	// be initialized in a timely manner.
	function CheckFBElementsReady(){
		var elements = $('.FB_login_button');
		for (var i=0; i<elements.length; i++){
			if (!elements[i].FB_bmr_init){
				$(elements[i]).bind('click', function(){
					if (!document.fbLinkClicked){
						//alert ('you clicked the facebook');
						document.fbLinkClicked = 1;
					}
				});
            }
        }
		setTimeout(CheckFBElementsReady, 500);
		// keeps looking for new elements, forever
	}
	CheckFBElementsReady();

	//------------------------------------------------------------------------------------------------------------------

	//onCoremetricsLoad(function(){ alert ('cm is loaded'); });
	//
	//

});


function onCoremetricsLoad(task){

	if (window.cm_bit_rol){
		task();
		alert ('count was ' + window.cm_count);
	}
	else{
		if (!window.cm_count){
			window.cm_count = 0;
		}
		window.cm_count += 100;
		setTimeout(function (){ onCoremetricsLoad(task) },100);
	}
}
function calcage(secs, num1, num2) {
	s = ((Math.floor(secs/num1))%num2).toString();
	if (s.length < 2){
		s = "0" + s;
	}
	return s;
}
function CountBack(secs, fields) {
	if (secs < 0) {
		return;
	}

	var days =  calcage(secs,86400,100000);
	var hours = calcage(secs,3600,24);
	var minutes = calcage(secs,60,60);
	var seconds = calcage(secs,1,60);

	if (days.length < 3){
		days = "0" + days;
	}

	fields[0].innerHTML = days.substring(0,1);
	fields[1].innerHTML = days.substring(1,2);
	fields[2].innerHTML = days.substring(2,3);

	fields[3].innerHTML = hours.substring(0,1);
	fields[4].innerHTML = hours.substring(1,2);

	fields[5].innerHTML = minutes.substring(0,1);
	fields[6].innerHTML = minutes.substring(1,2);

	fields[7].innerHTML = seconds.substring(0,1);
	fields[8].innerHTML = seconds.substring(1,2);

	setTimeout(function(){ CountBack(secs-1, fields)}, 1000);
}
function countdownWidget(fields){
	if (fields.length > 0){
		var TargetDate = "07/06/2010 12:00 AM";

		var dthen = new Date(TargetDate);
		var dnow = new Date();
		var ddiff = new Date(dthen-dnow);

		gsecs = Math.floor(ddiff.valueOf()/1000);

		CountBack(gsecs, fields);
	}
}
function removeHash(){
    if (top.document.location.href.indexOf('#') > 0 ){
		top.document.location.hash='';
		top.document.location.href = top.document.location.href.slice(0, -1);
	}
}
function setSearchHash(url){
	url = url.replace('/essays/search/', '');
	url = url.replace('essays/search/', ''); // FOR IE

	var sort = 'rank';
	var page = url.replace('page:', '');
	if (page.indexOf('/') > 0){
		page = page.substring(0, page.indexOf('/'));
		if (url.indexOf('sort:created') > 0){
			sort = 'created';
		}
	}

	//alert ('page: ' + page);

	location.hash = sort + ':' + page;
}
		

// aliases to shorten the onclick attributes
// these should be fired when a user clicks on a outgoing link
//
// bmr - bourdain medium raw
// kc - kitchen confidental
// act - a cooks tour
// dfa - decoding adrian ferran

function cm_bmr_am(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Amazon', '9780061718946-_-Medium_Raw']);
}
function cm_bmr_bn(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Barnes_and_Noble', '9780061718946-_-Medium_Raw']);
}
function cm_bmr_bo(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Borders', '9780061718946-_-Medium_Raw']);
}
function cm_bmr_ind(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Indiebound', '9780061718946-_-Medium_Raw']);
}


function cm_kc_am(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Amazon', '9780060899226-_-Kitchen Confidential']);
}
function cm_kc_bn(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Barnes_and_Noble', '9780060899226-_-Kitchen Confidential']);
}
function cm_kc_bo(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Borders', '9780060899226-_-Kitchen Confidential']);
}
function cm_kc_ind(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Indiebound', '9780060899226-_-Kitchen Confidential']);
}


function cm_act_am(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Amazon', '9780060012786-_-A_Cooks_Tour']);
}
function cm_act_bn(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Barnes_and_Noble', '9780060012786-_-A_Cooks_Tour']);
}
function cm_act_bo(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Borders', '9780060012786-_-A_Cooks_Tour']);
}
function cm_act_ind(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Indiebound', '9780060012786-_-A_Cooks_Tour']);
}


function cm_dfa_am(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Amazon', '9780061157073-_-Decoding_Ferran_Adria']);
}
function cm_dfa_bn(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Barnes_and_Noble', '9780061157073-_-Decoding_Ferran_Adria']);
}
function cm_dfa_bo(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Borders', '9780061157073-_-Decoding_Ferran_Adria']);
}
function cm_dfa_ind(){
	_gaq.push(['_trackEvent', 'OFFSITE_BUY_LINK', 'Indiebound', '9780061157073-_-Decoding_Ferran_Adria']);
}



