/* 
	Title:		Gumtree Global Parameters & Functions
	Copyright:	2008-2009 Gumtree.com
	Date:		23 April 2009
	Authours:	Adam Perfect
	Email:		aperfect@gumtree.com
*/

if ($.browser.msie && $.browser.version < 8) {
	DD_roundies.addRule('div#global-nav #main-actions li.primary, div#global-nav #main-actions li.primary a', '0 0 10px 0');
	DD_roundies.addRule('div#list-postings-extra h3#sub-category-header', '10px 10px 0 0');
	DD_roundies.addRule('div#header form#search', '0 10px 10px 0');
	DD_roundies.addRule('#no-results h1', '10px 10px 0 0');
	DD_roundies.addRule('div#pagination', '0 0 10px 10px');
	DD_roundies.addRule('div#pagination ul.pagination li.page a, div#pagination ul.pagination li.page span.current', '4px');
	DD_roundies.addRule('ul.postings li.featured-ad h2 strong.featured', '3px');
	
	DD_roundies.addRule('div#list-postings-title ul li', '10px 10px 0 0');
	DD_roundies.addRule('div#list-posting-extra-filters', '0 10px 0 0');
}

$(document).ready(function(){
	if ($.browser.msie && $.browser.version < 8) {
		DD_roundies.addRule('div#list-postings-section', '0 10px 10px 10px');
	}
	
	listpostings_load();
	
	// Add saved ads count
	setSavedAdsCount();
	
	activateAdPlacements();
	
	// striping
	$('ul.postings').each(function(){
		if ($(this).children('li#featured-title').length > 0) {
			var evenName = 'even';
			var oddName = 'odd';
		} else {
			var evenName = 'odd';
			var oddName = 'even';
		}
		
		$(this).children('li:visible').removeClass('even odd');
		$(this).children('li:visible:even').addClass(evenName);
		$(this).children('li:visible:odd').addClass(oddName);
		$('input, select, textarea, button').each(function(i) {
			$(this).attr('tabindex',i + 1);
		});
	});
	
	var dateNow = new Date();
	var timeNow = Math.round((dateNow.getTime()) / 1000);
	var timeStartToday = Math.round((new Date(dateNow.getFullYear(), dateNow.getMonth(), dateNow.getDate(), 0, 0, 0).getTime()) / 1000);
	// alert(timeNow);
	$('ul.postings li.posting div.freshness').each(function(){
		var timestamp = parseInt($(this).attr('class').match(/ts-([0-9]+)/)[1]);
		// alert(timeNow + ' : ' + timestamp);
		var timeElapsed = timeNow - timestamp;
		var timeElapsedInMinutes = Math.round(timeElapsed / 60);
		
		switch(true) {
			case (timeElapsed < 60):
				var timeAgo = timeElapsed + ' seconds ago';
				break;
			case timeElapsedInMinutes == 1:
				var timeAgo = 'a minute ago';
				break;
			case timeElapsedInMinutes < 45:
				var timeAgo = timeElapsedInMinutes + ' minutes ago';
				break;
			case timeElapsedInMinutes < 90:
				var timeAgo = 'about an hour ago';
				break;
			case timeElapsedInMinutes < 300 || timestamp >= timeStartToday:
				var timeAgo = 'about ' + Math.round(timeElapsedInMinutes / 60) + ' hours ago';
				break;
			case timestamp >= (timeStartToday - 86400):
				var timeAgo = 'yesterday';
				break;
			case timeElapsedInMinutes < 43200:
				var timeAgo = Math.round(timeElapsedInMinutes / 1440) + ' days ago';
				break;
			case timeElapsedInMinutes < 86400:
				var timeAgo = 'about a month ago';
				break;
			case timeElapsedInMinutes < 525600:
				var timeAgo = Math.round(timeElapsedInMinutes / 43200) + ' months ago';
				break;
			case timeElapsedInMinutes < 1051200:
				var timeAgo = 'about a year ago';
				break;
			default:
				var timeAgo = Math.round(timeElapsedInMinutes / 525600) + ' years ago';
				break;
		}
		
		$(this).children('span.time-ago').text(timeAgo);
	});
	
	$('#remove-all-starred').click(function() {
		listpostings_starredpostings_removeAll(Starred);
		
		return false;
	});
	
	$('#photos-filter').click( function() {
		$('form#pictures-searchform').submit();
	});
	
	$('#couple-filter').click(function() {
		$('form#couple_filter-searchform').submit();
	});
	
	$('#urgent-filter').click(function() {
		$('form#urgent_filter-searchform').submit();
	});

	if ($('input#min_price').val() != '') {
		$('input#min_price').next('label.minmax').addClass('hidden');
	}
	if ($('input#max_price').val() != '') {
		$('input#max_price').next('label.minmax').addClass('hidden');
	}
	$('input#min_price, input#max_price').focus(function(){
		if ($(this).val() == '') {
			$(this).next('label.minmax').addClass('hidden');
		}
	});
	$('input#min_price, input#max_price').blur(function(){
		if ($(this).val() == '') {
			$(this).next('label.minmax').removeClass('hidden');
		}
	});
});

function setSavedAdsCount () {
	var posting_ids = getCookie('posting_ids');
	if  (posting_ids) {
		var savedAds = posting_ids.split(' ');
		$('#my-saved-ads-link span').text('('+savedAds.length+')');
	}
}

function updateSavedAdsCount (modifyBy) {
	if ($('#my-saved-ads-link span').text() != '') {
		var currCount = parseInt($('#my-saved-ads-link span').text().match(/\(([0-9]+)\)/)[1]);
	} else {
		var currCount = 0;
	}
	var newCount = currCount + modifyBy;
	if (newCount < 0) {
		newCount = 0;
	}
	$('#my-saved-ads-link span').text('('+newCount+')');
}
