//
// PYM Behaviours

$(function() {

	$('#special-offers .special-offer a.more').click(function() {
		if(!$(this).hasClass('open')) {
		  $('#special-offers .special-offer .content').slideUp();
		  $('#special-offers .more').removeClass('open');
		  var p = $(this).parents('.special-offer');
		  $('.content', p).slideDown();
		  $(this).addClass('open');
		}
		return false;
	});

	$('input.memo').each(function() {
	   var val = $(this).val();
	   $(this).focus(function() { if ($(this).val() == val) $(this).val(''); }); 
	   $(this).blur(function() { if ($(this).val() == '') $(this).val(val); }); 
	});
	
	$('.rating-bar').starBar();
	
	$('.ajax-link').click(function() {
    $.ajax({
      dataType: 'script',
      url: this.href,
      beforeSend: function(xhr) { xhr.setRequestHeader("Accept", "text/javascript"); }
    });
    return false;
  });

});

//
// Legacy

function boot() {
	countdown_init();
	if (typeof(page_init) == 'function') {
		page_init();
	}
}

function countdown_init() {
	window.setTimeout("countdown_do()", 1000);
	var now = parseInt((new Date()).getTime() / 1000);
	for (id in countdowns) {
		countdowns[id] = countdowns[id] + now;
	}
}

function countdown_do() {
	var now = parseInt((new Date()).getTime() / 1000);
	for (id in countdowns) {
		var time = countdowns[id] - now;
		var str = '';
		if (time <= 0) {
			str = "<span class='expired'>AUCTION ENDED</span>";
		} else {
			
			var tmp = Math.floor(time / 60);
			var seconds = time % 60;
			var hours = Math.floor(tmp / 60);
			var days = Math.floor(hours / 24);
			hours = hours % 24;
			var mins = tmp % 60;
			
			var found = false;
			
			if (days > 0) {
				str += "" + days + "d ";
				found = true;
			}
			if (found || hours > 0) {
				str += "" + hours + "h ";
				found = true;
			}
			if (found || mins > 0) {
				str += "" + mins + "m ";
				found = true;
			}
			if (found || seconds > 0) {
				str += "" + seconds + "s";
			}
			
			if (days < 1 && hours < 1) {
				str = "<span class='expired'>" + str + "</span>";
			}
			
		}
		document.getElementById(id).innerHTML = str;
	}
	window.setTimeout("countdown_do()", 1000);
}

function google_map(bid) {
	window.open(
		'/business/map.php?business_id=' + bid,
		'_blank',
		'width=720,height=504,status=no,toolbar=no,menubar=0,resizable=0,scrollbars=0'
	);
}

function open_help(id) {
	window.open(
		'/help/view.php?id=' + id,
		'_blank',
		'width=580,height=480,status=no,toolbar=no,menubar=0,resizable=1,scrollbars=1'
	);
}

function open_popup(name) {
	window.open('/popup/' + name + '.php', '_blank', 'width=580,height=355,status=no,toolbar=no,menubar=0,resizable=1,scrollbars=0')
}

