$(document).ready(function(){
	$("#dialog_map").dialog({
                			autoOpen: false,
                			modal: true,
                			closeText: '',
                			width: 780,
                			height: 500,
                			position: 'center'
	});
	displayAdActions();
});

function displayAdActions(){
	$.ajax({
        type: "GET",
        url: APPURL+"ajax/user/ads/",
        data: "ads="+adsIds,
        dataType: 'json',
        processData: false,
        success: function(data) {
			$.each(data, function(adId) {
				$('#save_link_'+adId ).html('');
				
				if(data[adId] == 1){
					//ad is already saved - display remove
					$( "<a></a>" )
					.addClass( 'remove' ) 
					.append( tr_remove )
					.attr( 'href', 'javascript: void(0);' )
					.click(function() {
					     removeAd(adId);
					 })
					.appendTo( '#save_link_'+adId );
						
				}else{
					//display save
					$( "<a></a>" )
					.addClass( 'save' ) 
					.append( tr_save )
					.attr( 'href', 'javascript: void(0);' )
					.click(function() {
					     saveAd(adId);
					 })
					.appendTo( '#save_link_'+adId );
				}
				
				$('#sep_'+adId ).show();
			});
        }
    });
}


function toggle_properties(id) {
    if ($('#show_hidden_properties_'+id).val()==0) {
        $('#show_hidden_properties_'+id).val(1);
        show_properties(id);
    } else {
        $('#show_hidden_properties_'+id).val(0);
        hide_properties(id);
    }
}
function show_properties(id) {
    $('#properties_drop_'+id).removeClass('more');
    $('#properties_drop_'+id).addClass('less');
    $('#properties_drop_'+id).text(tr_less);
    $('#hidden_properties_'+id).show();
}
function hide_properties(id) {
	$('#properties_drop_'+id).removeClass('less');
    $('#properties_drop_'+id).addClass('more');
    $('#properties_drop_'+id).text(tr_more);
    $('#hidden_properties_'+id).hide();
}


function prev_pic(accID) {
    var pic_count=$('#pics_'+accID).val();
    var current_pic=$('#current_pic_'+accID).val();
    if (current_pic==0) {
        $('#current_pic_'+accID).val(pic_count-1);
    } else {
        current_pic--;
        $('#current_pic_'+accID).val(current_pic);
    }
    change_pic(accID);
}
function next_pic(accID, webID) {
    var pic_count=$('#pics_'+accID).val();
    var current_pic=$('#current_pic_'+accID).val();
    if (current_pic==pic_count-1) {
        $('#current_pic_'+accID).val(0);
    } else {
        current_pic++;
        $('#current_pic_'+accID).val(current_pic);
    }
    change_pic(accID);
}
function change_pic(accID) {
    $.ajax({
        type: "GET",
        url: APPURL+"ajax/ads/pic/",
        data: "ad="+accID+"&web="+$('#pid_'+accID).val()+"&pic="+$('#current_pic_'+accID).val(),
        processData: false,
        success: function(responseText) {
            $('#thumb_'+accID).attr('src',responseText);
            $('#displayed_pic_'+accID).html($('#current_pic_'+accID).val()-0+1);
        }
    });
}

function openSmallMapView(accID) {
	var title = $('#title_'+accID).html();
	var content = '';
	content += '<iframe name="smallMapIframe" id="smallMapIframe" frameborder="0" src="'+APPURL+'iframe-map/'+accID+'/"></iframe>';
	$("#dialog_map").dialog("open");
	$("#dialog_map").dialog("option", "height", "580");
	$("#dialog_map").dialog("option", "width", "780");
	$("#dialog_map").dialog("option", "title", title);
	$('#dialog_map').dialog('option', 'buttons', {
		"Close": function() {
			$("#dialog_map").html("");
			$("#dialog_map").dialog('close');
		}
	});
	$("#dialog_map").html(content);
}


function clearEmail() {
	var tr = $('#tr_email').html();
    if ($('#news_email').val() == tr) {
        $('#news_email').val('');
    }
}
function create_newsletter() {
	var search_results = $('#h_count').val();
	var search_id = $('#h_search').val();
	var phrase = $('#h_phrase').html();

    $.ajax({
        type: "POST",
        url: APPURL+"ajax/user/subscribe-search/",
        data: 'results='+search_results+'&search='+search_id+'&email='+$('#news_email').val(),
        processData: false,
        success: function(responseText){
            if (responseText=='ok') {
                $('#news_email').val('');
                showGeneralMessageDialog("", "<div>Thanks for your Submittion, your alert has been created. You will receive email updates for the latest ads with "+phrase+"</div>");
            } else {
                showGeneralMessageDialog("Error", responseText);
            }
        }
    });
}


function check_feedback() {
    if ($('#message').html() == tr_msg) {
        $('#message').html('');
    }
}
function send_feedback() {
    check_feedback();
    $('#feedback_button_id').hide();
    $('#feedback_wait_id').show();
    
    var url = $('#page_link').attr('href');
    $.ajax({
        type: "POST",
        url: APPURL+"ajax/search/send-feedback/",
        data: "text="+$('#message').val()+"&uri="+url,
        processData: false,//
        success: function(responseText) {
                    if (responseText=='ok') {
                        $('#feedback_container').hide();
                        $('#feedback_success_message').show();
                    } else {
                    	showGeneralMessageDialog("Error", responseText);
                        $('#feedback_button_id').show();
                        $('#feedback_wait_id').hide();
                    }
                }
    });

}





