// JavaScript Document
$(document).ready(function() {

 // Handler for .ready() called. 
  $("#sort").change(function () {
    var val = $("#sort option:selected").attr('value');
    window.location.href = val;
  })
  
 $(".thumbnail").click(function() {
   var scr = $(this).attr('src');
   var title = $(this).attr('alt');
   $('img.largeImg').attr('alt',title);
   $('img.largeImg').attr('src', scr.replace("_thumb.","."));
   $('h2#gal-image-text').text(title);
 });


 $('.ag-photo-close').live('click', function() {
 $('.ag-photo-container'+$(this).attr('id')).html("");
  return false;
 });

 //getAllAgents(); // initialize call
   $("#viewAllAgent").click(function(event){
	 getAllAgents();
	 return false;
   });

   $("#addAgent").click(function(event){
	 addAgents();
	 return false;
   });

   $('#but-add-agent').live('click', function() {
	addAgentsSubmit();
    return false;
   });

   $('#but-update-agent').live('click', function() {
    updateAgentSubmit();
    return false;
   });

   $('.ag-edit').live('click', function() {
	editAgents($(this).attr('id'));
    return false;
   });

   $('.ag-delete').live('click', function() {
	deleteAgents($(this).attr('id'));
    return false;
   });

   $('.ag-photo-change').live('click', function() {
	changePhotoAgents($(this).attr('id'));
    return false;
   });

   $('.ag-photo-close').live('click', function() {
	 $('.ag-photo-container'+$(this).attr('id')).html("");
    return false;
   });


/* Special Offers */

var popupStatus = 0;

$('.special, .rate-card').click(function() {

var cont_id = $(this).attr('id');

if(cont_id == $("."+cont_id).attr('class'))
{
 
 if(popupStatus == 0)
 {
  $("#backgroundPopup").css({
   "opacity": "0.8"
  });

  $("."+cont_id).fadeIn("slow");  

   var windowWidth  = document.documentElement.clientWidth;
   var windowHeight = document.documentElement.clientHeight;
   var popupHeight  = $("."+cont_id).height();
   var popupWidth   = $("."+cont_id).width();

   // centering the popup
   $("."+cont_id).css({
    "top"  : windowHeight/2 - popupHeight/2,
    "left" : windowWidth/2 - popupWidth/2
   }); // return false;

   $("#backgroundPopup").fadeIn("slow");

   popupStatus = 1;
      
 }

}

});

/* Rate Card */

 var cont_id = $('.special').attr('id');
 $(".close-popup").click(function()
 {
  if(popupStatus == 1)
  {
   $("#backgroundPopup").fadeOut("slow");
   $("."+cont_id).fadeOut('slow');
   popupStatus = 0;
  }
 });

/* Special Offers */

 $(".close-popup-special").click(function()
 {
  var special_offer_id = $(this).attr('id');
  if(popupStatus == 1)
  {
   $("#backgroundPopup").fadeOut("slow");
   $("."+special_offer_id).fadeOut('slow');
   popupStatus = 0;
  }
});
 
/* Rate Card */
 //var rate_id = $('.rate-card').attr('id');
 $(".close-popup-rate").click(function()
 {
  var rate_id = $(this).attr('id');
  if(popupStatus == 1)
  {
   $("#backgroundPopup").fadeOut("slow");
   $("."+rate_id).fadeOut('slow');
   popupStatus = 0;
  }
 });

/*        DISABLED RIGHT CLICK, COPY AND SELECT ALL       */
    $(this).bind("contextmenu selectstart", function(e) {
       e.preventDefault();
    });

    $(this).keydown(function(e){
        if(e.ctrlKey) { 
         var sel ;
        if(document.selection && document.selection.empty)
            {document.selection.empty() ;}
        else if(window.getSelection) {
            sel=window.getSelection();
            if(sel && sel.removeAllRanges)
            sel.removeAllRanges() ;
        }
        e.preventDefault();
        }
    });
	
});


/* Jquery Functions */

function getAllAgents()
{
 $('.ag-content').html('<p></p><div style="font-size: 20px; text-align: center">Loading...</div>');
 $.post('ajax-request/get-all-agents.php', function(data) {
  $('.ag-content').html(data);
 });
}
function addAgents(){
	 $('.ag-content').html('<p></p><div style="font-size: 20px; text-align: center">Loading...</div>');
	 $.post('ajax-request/add-agents.php', function(data) {
	  $('.ag-content').html(data);
	});
}
function addAgentsSubmit(){
	//$('.ag-content').html('<div style="font-size: 15px; text-align: center">Saving...</div>');
	$('#but-add-agent').attr({disabled:true, value:"Saving..."});
	var d = $('#addAgentform').serialize();
	//alert(d);
	$.ajax({
		 	 url: 'ajax-request/submit-add-agent.php',
			type: "POST",
			cache: true,
			data: d,
			success: function(data) {
				if(data == 'ok'){
				  $('.ag-content').html('<div style="font-size: 15px; text-align: center">Agent has been added!</div>');
				  getAllAgents();
				}
				else{
				  alert(data);
				  $('#but-add-agent').attr({disabled:false, value:"Add"});
				}
		 	 }
	});
}

function editAgents(id){
	 $('.ag-content').html('<p></p><div style="font-size: 20px; text-align: center">Loading...</div>');
	 $.post('ajax-request/edit-agent.php',{agent_ID: ""+id}, function(data) {
	  $('.ag-content').html(data);
	});
}

function changePhotoAgents(id){
	 $('.ag-photo-container'+id).html('<p></p><div style="font-size: 11px; text-align: center">Loading...</div>');
	 $.post('ajax-request/photo-form.php',{agent_ID: ""+id}, function(data) {
	  $('.ag-photo-container'+id).html(data);
	});
}

function deleteAgents(id){
	 var del = confirm("Do you really want to delete this agent?");
	 if(del){
		 $.post('ajax-request/submit-delete-agent.php',{agent_ID: ""+id}, function(data) {
		   if(data == 'ok'){
		      $('#agent'+id).fadeOut('slow');
		   }
		   else
			 alert(data);
		});
	 }
}

function updateAgentSubmit(){
	//$('.ag-content').html('<div style="font-size: 15px; text-align: center">Saving...</div>');
	$('#but-update-agent').attr({disabled:true, value:"Updating..."});
	var d = $('#updateAgentform').serialize();
	//alert(d);
	$.ajax({
		 	 url: 'ajax-request/submit-update-agent.php',
			type: "POST",
			cache: true,
			data: d,
			success: function(data) {
				if(data == 'ok'){
				  $('.ag-content').html('<div style="font-size: 15px; text-align: center">Agent has been update!</div>');
				  $('.ag-content').delay(500);
				  getAllAgents();
				  //$('.ag-content').fadeIn("slow");
				}
				else{
				  alert(data);
				  $('#but-add-agent').attr({disabled:false, value:"Add"});
				}
		 	 }
	});
}

   function highlighttext()
   {
	var val = $('input[name="highlight-hotelname"]').val();
	if(val.length < 1) return false;
   $('.highlight').parent('a').css('background', '');
      $('.more-links, .more-links-wide, .private-beach-list li')
      .removeHighlight()
      .highlight(val);
   $('.highlight').parent('a').css('background', '#d0d0d0');
   $(document).scrollTop($('.highlight').position().top - 200);
   }
