$(function(){
	var search_wm = 'Search by keyword';
	
	var s = $('div.simple_search_tab input[name=s]'); //search input
	if(s.length)
		s.Watermark(search_wm, "#BCBCBC");
	
	// spell check
	var sc = $('#spell_check_container');
	if(sc.length)
		sc.find('span').click(function(){
			var $this = $(this);
			
			s.val( $this.text() );
			
			s.parent().submit(); // find the form then submit it
		});
	
	//prevent empty search
	$('#searchbar .button').bind('click', function(){
		var s = $('#searchbar input[name=s]');
		
		if(s.val() == search_wm){
			//alert('Please define your search input');
			$('#searchbar input[name=s]').fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
			s.focus();
			return false;
		}
	});
	
	//recent history
	$('.recent_history .recent_history_kw').click(function(){
		$('input.field').val($(this).text()).focus();
		return false;
	});
	
	//back to top
	$('.backtop').click(function(){
		$.scrollTo(0, 800);
		return false;
	});
	
	$('ul.cv li').hover(function(){
		$(this).find('.suspend').show();
	},
	function(){
		$(this).find('.suspend').hide();
	});
	
	//feedback button when hover
	$('#feedback').hover(function(){
		$(this).css({left:'-3px'});
	},function(){
		$(this).css({left:'-6px'});
	});
});

function get_date_picker(el){
	$(el).datepicker({
		dateFormat: "yy-mm-dd",
		yearRange: '-80:0',
		showOn: "button", 
		buttonImage: site_url+"/images/calendar.gif", 
		buttonImageOnly: true
	});
}

function hide_notification(){
	$('.notification, #query_notification').fadeOut(700);
}

function personalized_url(val){
	if(val=='') val='your_personalized_url';
	
	$('.personalized_url em').text(val);
}

function check_taken(val, check, el){
	$.ajax({
		type: 'post',
		url: site_url+'process.php',
		data: 'a=check_taken&val='+val+'&check='+check,
		beforeSend: function(){
			$(el).html('Checking...');
		},
		success: function(data){alert(data);
			$(el).html(data);
		}
	});
}

function addWatch(ad_id){
	$.ajax({
		type: 'post',
		url: 'process.php',
		data: 'a=add_watch&ad_id='+ad_id,
		beforeSend: function(){
			$("#loading").text('Requesting to add ... Please wait').show();
		},
		success: function(data){
			if(data==1){
				$("#loading").text('Successfully added to your watchlist');
			}
			else{
				$("#loading").text('Request failed!');
			}
			
			setTimeout('$("#loading").fadeOut(500)', 1500);
		}
	});
}

function addWatch2(ad_id){
	$.ajax({
		type: 'post',
		url: 'process.php',
		data: 'a=add_watch&ad_id='+ad_id,
		success: function(data){
			if(data==1){
				alert('Successfully added to your watchlist');
			}
			else{
				alert('Request failed!');
			}
		}
	});
}

function reportAd(ad_id, action, referer){
	$.ajax({
		type: 'post',
		url: 'process.php',
		data: 'a=report_ad&rpt_action='+action+'&ad_id='+ad_id,
		beforeSend: function(){
			$(".report_overlay").html('<h2 align="center">Requesting to add ... Please wait</h2>').css({top:0});
		},
		success: function(data){
			//$('.report_msg').html(data);
			if(data!='0'){
				//location.replace(referer);
				$('.report_overlay').html('<h2 align="center">This ad has been reported. Thank you.</h2>');
				setTimeout("$('.report_overlay').css({top:'-38px'})", 2500);
			}
			else{
				$('.report_overlay').html('<h2 align="center">Request failed or it has already been reported</h2>');
				setTimeout("$('.report_overlay').css({top:'-38px'})", 2500);
			}
		}
	});
}

function suspendAd(ad_id, action, referer){
	$.ajax({
		type: 'post',
		url: 'process.php',
		data: 'a=suspend_ad&rpt_action='+action+'&ad_id='+ad_id,
		beforeSend: function(){
			$(".report_overlay").html('<h2 align="center">Requesting to add ... Please wait</h2>').css({top:0});
		},
		success: function(data){
			//$('.report_msg').html(data);
			if(data!='0'){
				//location.replace(referer);
				$('.report_overlay').html('<h2 align="center">This ad has been suspended from viewing</h2>');
				setTimeout("$('.report_overlay').css({top:'-38px'})", 2500);
			}
			else{
				$('.report_overlay').html('<h2 align="center">Request failed or it has already been reported</h2>');
				setTimeout("$('.report_overlay').css({top:'-38px'})", 2500);
			}
		}
	});
}

function approveAd(ad_id, action, referer){
	$.ajax({
		type: 'post',
		url: 'process.php',
		data: 'a=approve_ad&rpt_action='+action+'&ad_id='+ad_id,
		beforeSend: function(){
			$(".report_overlay").html('<h2 align="center">Requesting to approve ... Please wait</h2>').css({top:0});
		},
		success: function(data){
			//$('.report_msg').html(data);
			if(data!='0'){
				//location.replace(referer);
				$('.report_overlay').html('<h2 align="center">This ad has been reported. Thank you.</h2>');
				setTimeout("$('.report_overlay').css({top:'-38px'})", 2500);
			}
			else{
				$('.report_overlay').html('<h2 align="center">Request failed or it has already been reported</h2>');
				setTimeout("$('.report_overlay').css({top:'-38px'})", 2500);
			}
			
			$('.check_approval').remove();
		}
	});
}

function toggle_searchbar(){
	if($('.adv_search_tab').is(':hidden')){
		$('.simple_search_tab').hide();
		$('.adv_search_tab').show();
	}
	else if($('.simple_search_tab').is(':hidden')){
		$('.simple_search_tab').show();
		$('.adv_search_tab').hide();
	}
}



function delete_ad_image(img_id, img_name){
	if(confirm('Are you sure? (Cannot undo later)'))
	$.ajax({
		type: 'post',
		url: 'process.php',
		data: 'a=delete_ad_image&img_id='+img_id+'&img_name='+img_name,
		beforeSend: function(){
			alert('Deleting...');
		},
		success: function(data){
			if(data==1){
				$('#img_id_'+img_id).fadeOut(800, function(){ alert('Successfully deleted image: img_id_'+img_id) });
			}
			else{
				alert('Request failed!');
			}
		}
	});
}

function confirm_post() {
	var txt = 'Are you sure want to post?';
	
	if(confirm(txt))
		return true;
	else
		return false;
}