// Big Picture namespace
var BP = {};


/*----------------------------
	=Masthead Dropout Counter
----------------------------*/
BP.DropoutCounter = function() {
	this.elementID = "#dropout-count span.count";	// id of the html element that displays the counter
	this.interval = 5000;	// time between updates in milliseconds
	this.startDate = new Date(2009, 8, 1);	// date which the counter begins at
	this.secondsPerDropout = 12;	// number of seconds per dropout
	
	// update the counter
	this.update = function() {
		var counterDiv = jQuery(this.elementID);
		if (counterDiv) {
			counterDiv.attr('innerHTML', this.count());
			setTimeout("BP.dropoutCounter.update()", this.interval);
		}
	};

	// calculate the new count
	this.count = function() {
		var now = new Date();
		var dropouts = 0;

		// figure out how many seconds have elapsed since start Date
		var startMS = parseInt(this.startDate.getTime());
		var nowMS = parseInt(now.getTime());
		var seconds = (nowMS - startMS) / 1000;
		dropouts = Math.floor(seconds / this.secondsPerDropout).toString();
		
		// convert dropouts number to a string with thousands commas inserted
		var str = [];
		var count = 0;
		for (var i=dropouts.length - 1; i >= 0; i--) {
			if (count == 3) {
				count = 1;
				str.push(',');
			} else {
				count++;
			}
			str.push(dropouts.substr(i, 1));
		}
		
		str.reverse();
		return str.join('');
	};
};


BP.dropoutCounter = new BP.DropoutCounter();
BP.dropoutCounter.update();


BP.setSearchFilter = function(filter) {
	switch(filter) {
		// news and press only
		case 1:
//			jQuery("#search_news").removeClass("off");
			jQuery("#filter_set_1").hide();
			jQuery("#filter_set_2").show();
//			jQuery("#search_entire").addClass("off");
			jQuery("#cat").attr("value", "news");
			jQuery("#searchform").css("backgroundLeft", 105);
			jQuery("#searchform").css("background-position", "105px 22px");
			break;
		// entire site, by default
		default:
			jQuery("#filter_set_2").hide();
			jQuery("#filter_set_1").show();
//			jQuery("#search_news").addClass("off");
//			jQuery("#search_entire").removeClass("off");
			jQuery("#cat").attr("value", "");
			jQuery("#searchform").css("background-position", "21px 22px");
			break;
	}
};


/*----------------------------
	=Document Ready Actions
----------------------------*/
jQuery(document).ready(function() {
	if (BP.dropoutCounter)
		BP.dropoutCounter.update();
});

jQuery(document).ready(function() {
	if(document.getElementById('shoppingcartcontents') != null){
		ajax.post("index.php?refreshcart=true",getresults,"");
	}
});

