// jquery case insensitive contains from http://fatfolderdesign.com/259/code/couple-of-jquery-selectors
jQuery.expr[':'].Contains = function(a,i,m){
	return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

$(document).ready(function(){
	filter_init();
	preview_link_init();
	prepost_location_dropdowns();
	prepost_template_dropdown();
	preport_banner_preview();
	broken_image_fixer();
	quickpost();
})
function broken_image_fixer(){
	$("img").error(function () {
		$(this).unbind("error").attr("src", "_blank.gif");
	});
}
function filter_init(){
	$('input.filter_box').keydown(function(){filter(this)});
	$('input.filter_box').keyup(function(){filter(this)});
}
function filter(that){
	input = $(that).attr('name');
	input = input.substr(0, input.length-7);
	filter_val = $(that).val();
	$('#'+input+' li:Contains("'+filter_val+'")').each(function(){
		$(this).show();
	});
	$('#'+input+' li:not(:Contains("'+filter_val+'"))').each(function(){
		$(this).hide();
	});
}
function preview_link_init(){
	$('#preview_show_html_link').click(function(){
		$('#preview_show_html_link').hide()
		$('#preview_show_preview_link').show()
	});
	$('#preview_show_preview_link').click(function(){
		$('#preview_show_preview_link').hide()
		$('#preview_show_html_link').show()
	});
}
function prepost_location_dropdowns(){
	$('select[name="post_coarselocation"]').change(function(){
		selected = $(this).val()
		$('select.finelocations').hide();
		$('select#fine_location_'+selected).show();
	});
	$('select[name="post_coarselocation"]').change();
}
function prepost_template_dropdown(){
	$('select[name="post_template"]').change(function(){
		$('#prepost_template_preview').attr('src','templates/'+$(this).val()+'.jpg')
	});
	$('select[name="post_template"]').change();
}
function preport_banner_preview(){
	$('select.bannerselect').each(function(){
		$(this).change(function(){
			$('#'+$(this).attr('name')+'_img').attr('src',$(this).children('option[value="'+$(this).val()+'"]').attr('rel-img'));
		})
	})
	$('select.bannerselect').change();
}
function goto_prepost(carid){
	window.open('?pg=prepost&car='+carid);
	show_lastclick(carid)
}
function quickpost(){
	$('input.quickpost').click(function(e){
		carid = $(this).attr('id').substr(9);
		window.open('quickpost.php?car='+carid);
		show_lastclick(carid)
		e.stopPropagation();
	});
}
function show_lastclick(carid){
	var nD = new Date();
	hour = nD.getHours();
	minute = nD.getMinutes();
	if(hour>12){
		hour = hour-12;
		if(hour==12){
			meridian = 'am';
		}else{
			meridian = 'pm';
		}
	}else{
		if(hour==12){
			meridian = 'pm';
		}else{
			meridian = 'am';
		}
	}
	time = hour+':'+minute+meridian;
	$('#car'+carid+' > div.cardetails_price > span.lastposted').html('<b>Last Clicked '+time+'</b>');
}
