$(function() // Shorthand for $(document).ready(function() 
{ 
   //$('div#view').hide();
   $('.details_box').hide();
   $('.details_box_trigger').click(function() 
   {
    $(this).siblings('.details_box').stop(true, true); // Finish any current animation
    var visible = $(this).siblings('.details_box').is(':visible'); // Is it now visible
    $(this).siblings('.details_box')[visible ? 'fadeOut' : 'fadeIn']('fast'); // Fade in/out based on visibility
    return false;
    });
    $('#exp').click(function() 
    {
     $('.details_box').show();
    });
    $('#clp').click(function() 
    {
     $('.details_box').hide();
    });
		
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	//here goes the script for hiding the investigation results div block upon load/reload of the page.
	$('div.info_block_results').hide();
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------
	//here goes the script to add removing control of the investigation results div block. Click X image in the 
	//rightmost top corner to remove the div.
	$('div.close').click(function()
	{
		$('div.info_block_results').hide();
	});
  $('#button').removeAttr('disabled');
	//----------------------------------------------------------------------------------------------------------------
	//----------------------------------------------------------------------------------------------------------------	
  $('#details').hide();
  //$('#rescan').hide();
  $('#input_url_for_check').submit(function(event)
  {
  	$('#button').attr('disabled', true);
   //$('#button').attr('src', '/images/submit_gray.png');
  $('#button').css('cursor','default');
  	event.preventDefault();
   var submit_url = $('#url_input_field').val();
   if (submit_url)
   {
    $('div.info_block_results').show();
    $('#url_sub').empty();  
	 $('#url_stat').empty();
	 $('#time').empty();
    $('#details').hide();
    //$('#rescan').hide();
    $('div.info_block_results').block({ 
                message: '<h1>Investigation in progress. Please wait...</h1> <img src="/images/ajax-loader.gif"> ',
                overlayCSS: { backgroundColor:'#333333', 
                              opacity: .8, 
                              color: '#00f'
                            },
                css: {padding: '10px', width:'200px'}
    });
	 $('div.info_block_results').smartupdater({
		url : '/scan',
		type:'POST',
		data: {url: submit_url},
 		dataType:'json', 
 		minTimeout: 5000 // 5 seconds
 		}, function (data) {
 			var state = data.state.toLowerCase();
 			if (state =='new' || state =='download' || state =='scan' || state =='downloaded' || state =='scanned')
 			{
				var urlDate = new Date(data.age * 1000);
				var currentdate = urlDate.toLocaleString();
				$('#url_stat').html(state).css({'color':'black','font-weight':'bolder'});
				$('#url_sub').html(data.url);  
				$('#url_stat').html(state);
				$('#time').html(currentdate); 
				
		  }
		  else
		  {
		  		var urlDate = new Date(data.age * 1000);
				var currentdate = urlDate.toLocaleString();
				$('#url_sub').html(data.url);  
				$('#url_stat').html(state).css({'color':'green','font-weight':'bolder'});
				$('#time').html(currentdate);
		  	   $('div.info_block_results').smartupdaterStop();
		  	   $('div.info_block_results').unblock();
		  	   //$('#button').attr('src', '/images/submit_blue.png');
		  	   $('#button').removeAttr('disabled');
		  	   $('#button').css('cursor','pointer');
		  	   //$('#rescan').show();
				$(document).ajaxStop();
				if (state=='Address is unreachable')
            {
               $('#url_stat').css({'color':'black','font-weight':'bolder'});
            }
				if (state=='potentially suspicious' || state=='potentially unsafe')
				{
					$('#url_stat').css({'color':'#ff9900','font-weight':'bolder'});
					$('#details').show(); 
					$('#report').attr('href','/detailed_report/'+data.url);
				   //$('#button').attr('src', '/images/submit_blue.png');
					$('#button').removeAttr('disabled');
					$('#button').css('cursor','pointer');
				}
				if (state=='suspicious' || state=='unsafe')
				{
					$('#url_stat').css({'color':'#ff6600','font-weight':'bolder'});
					$('#details').show(); 
					$('#report').attr('href','/detailed_report/'+data.url);
				   //$('#button').attr('src', '/images/submit_blue.png');
					$('#button').removeAttr('disabled');
					$('#button').css('cursor','pointer');
				}
				if (state=='malicious')
				{
					$('#url_stat').css({'color':'red','font-weight':'bolder'});
					$('#details').show(); 
					$('#report').attr('href','/detailed_report/'+data.url);
				   //$('#button').attr('src', '/images/submit_blue.png');
					$('#button').removeAttr('disabled');
					$('#button').css('cursor','pointer');
				}
		  }
   	});
   }
   else
   {  
      //$('#button').attr('src', '/images/submit_blue.png');
	  $('#button').removeAttr('disabled');
	  $('#button').css('cursor','pointer');
     $('div.info_block_results').hide();
     alert("Please enter URL");
	 }
  });
});

