
$(document).ready(function(){
    var domain = $('.logo img').attr('src').replace(/^(.*?images\/[^\/]+\/public\/).*?$/, '$1');    
    var escort_name = $('#seen_username').val();       
    
    $("#dialog").dialog({
  	  autoOpen: false,
      modal: true,
      width: 620,
      position: [$('.content2.escort').offset().left,115],
      title: "Advertise " + escort_name + " Profile - Advanced Settings",
      open: function(event, ui){
	      var aParams = {
	    	id : $(this).dialog('option', 'id'),
	    	from_seen_on: 1,
	    	seen_on: $(this).dialog('option', 'seen_on')	    	
	      };

	      $('#js_touch_profile_content').load($(this).dialog('option', '_url'), aParams, function(){
	    	  $('#advertisment_overlay').hide();  
          });	      
       }
    });
   
    $("#dialog").dialog('option', {seen_on:'', _url:'', id:''});
   
    $('a.seen_on').click(function(){
    	var oClickedLink = $(this);
    	
    	$('#advertisment_overlay').show();
    	$("#dialog").dialog("option", "id", $(this).attr('rel'));
    	$("#dialog").dialog("option", "_url", $(this).attr('href') + 'advertise-profile');
    	$("#dialog").dialog("option", 'seen_on', $(this).attr('name'));
    	$("#dialog").dialog('open');
  
    	return false;
    });
    
    $('#save_btn').live('click', function() {
        $.ajax({
            url: '/advertise-profile',
            type: 'POST',
            dataType: 'json',
            data: {
                'do': 'save',
                'from_seen_on' : 1,
                'id':$('input[name="id"]').val(),
                'aOpening': $('form#advertisment_form').serialize()
            },

            /**
             * Before send request disable link and show loading picture
             */
            beforeSend: function(xhr) {
            	$('#advertisment_overlay').show(); 
            },

            /**
             * Change text of the link
             */
            success: function(data, status) {
            	var ok_div = $('#js_advertise_profile_ok');
            	var ok_div_content = $('#js_advertise_profile_ok>.bgr>.content');
            	var error_div = $('#js_advertise_profile_error');
            	var error_div_content = $('#js_advertise_profile_error>.bgr>.content');
            	
                if ('ok' == data.type) {
                	ok_div_content.append(data.message);
                	
                	ok_div.slideDown('slow');
                    setTimeout(function() {
                    	ok_div.slideUp('fast');
                    	ok_div_content.html('');}
                    ,5000);	                    
                
                } else {
                	for (i = 0; i < data.message.length; ++i) {
                		error_div_content.append(data.message[i] + '<br />');
                	}
                	
                	error_div.slideDown('slow');
                	setTimeout(function() {
                		error_div.slideUp('fast');
                		error_div_content.html('');}
                	,5000);
                }
            },

            /**
             * Hide loading picture
             */
            complete: function(xhr, status) {
            	$('#advertisment_overlay').hide(); 
            }
        });
        
    	return false;
    });
});
