//jQuery.noConflict();

jQuery(document).ready(function() {

	jQuery('#notes').dialog({
		autoOpen: false,
		close: function (event,ui) {
			jQuery('#notes').empty();
			jQuery('#notes').dialog('option','buttons',{});
		 },
		position: 'top'
	});

	jQuery('#falure').hover(function() {
		jQuery(this).html('Falure');
	}, function() {
		jQuery(this).html('Failure');
	});
});

function flash_title(msg) {
	window.focus();
    var oldTitle = document.title;
    var timeoutId = setInterval(function() {
        document.title = document.title == msg ? ' ' : msg;
    }, 1000);
    window.onmousemove = function() {
        clearInterval(timeoutId);
        document.title = oldTitle;
        window.onmousemove = null;
    };
}
function getHelp(id) {
	jQuery.get('/helpPopup.php', {helpId: id},
			function(data) {
				jQuery('#notes').html(data);
				jQuery('#notes').dialog('option','title','MafiaReturns Help');
				jQuery('#notes').dialog('open');
			}
		);
}

function do_petty() {
	flash_title("Petty time!!!");
	jQuery.get('/profit/pettycrime.php', {ajax:'1'},
		function(data) {
			jQuery('#notes').html(data);
			_gaq.push(['_trackPageview', '/profit/pettycrime.php']);
			jQuery('#notes').dialog('option','title','Petty Crime');
			jQuery('#notes').dialog('open');
			jQuery('textarea:first').focus();
			
			jQuery('#pettyform').submit(function() { 
				jQuery('#pettymsg').hide();

			selection = document.getElementById('pettyform').ptype;

			type = '';
			for (i=0; i<selection.length; i++)
				if (selection[i].checked == true)
						type = selection[i].value;

			jQuery.post('/profit/dopetty.php', 
				{
					ajax: true,
					ptype: type,
					submit: 'Do petty!'
				}, function (data) {
					jQuery('#pettymsg').html(data).fadeIn(3000);
					_gaq.push(['_trackPageview', '/profit/dopetty.php']);
					if (data.substr(0,7) == 'Success') {
						jQuery('#notes').dialog('close');
						jQuery.getScript('/js/timers.php');
					}
//					else {
//						alert(data);
//					}

				}, 'html'
			);
				return false; 
			});
		}
		,'html'
	);
}
function do_felony() {
    flash_title("Felony time!!!");
//document.title = 'Clicked';
    jQuery.get('/profit/felony.php',{ajax: true},
        function(data) {
//            alert(data);
			_gaq.push(['_trackPageview', '/profit/felony.php']);
			jQuery('#notes').html(data);
            jQuery('#notes').dialog('option','title','Felony');
            jQuery('#notes').dialog('open');
    	},'html'
    );
//document.title = 'End Of Function';
}
function show_airport() {
    jQuery.get('/manage/airport.php', {ajax:true},
        function(data) {
			_gaq.push(['_trackPageview', '/manage/airport.php']);
            jQuery('#notes').html(data);
            jQuery('#notes').dialog('option','title','Airport');
            jQuery('#notes').dialog('open');
            jQuery('#airportform').submit(function() {
				selection = document.getElementById('airportform').to;
				type = '';
				for (i=0; i<selection.length; i++)
					if (selection[i].checked == true)
						type = selection[i].value;
					jQuery.post('/manage/airport.php',
						{
							ajax: true,
							to: type,
							submit: 'Fly!'
						}, function (data) {
							_gaq.push(['_trackPageview', '/manage/airport.php']);
							if (data.substr(0,15) == 'You have landed') {
								loc = '<img src="/images/city_' + type;
								loc = loc + '.jpg" alt="'+data.substr(19,(data.length - 1))
+'" onClick="show_airport();" />';
								document.getElementById('loc').innerHTML=loc;
								jQuery('#notes').dialog('close');
								jQuery.getScript('/js/timers.php');
							}
						}, 'html'
					);
					return false;
            });
        }
        ,'html'
    );
}

function jail_time(){
	var timeoutid = setInterval(function(){
				jQuery('#msg').load('/js/jail.php',{},
				function(responseText, textStatus, Object){
					document.getElementById('msg').innerHTML = responseText;
					//jQuery('#msg').html = responseText;
					if(document.getElementById('msg').innerHTML == 'Your sentence is up. You are free to go.'){
    				    clearInterval(timeoutid);
   					}
				});
			}, 5000);
}
/**
 * AJAX POLLING
 */
var pollcnt = 0;
var pollmax = (30 * 60) / 15 - 1;

var do_poll = function(delay) {
	jQuery.get('/ajax.php' + (window.location.pathname == '/jail.php' ? '?jail=yes' : '') ,{},
		function(data) {	
			if ( data.s != 2 ) {
				try {
					if ( data.f >= 1 ) {
						jQuery('#psimg').attr('src','/images/flashy.gif');	
					} else {
						jQuery('#psimg').attr('src', '/images/title2.jpg');	
					}
					
					if ( data.m >= 1 ) {
						jQuery('#mmimg').attr('src','/images/mail.gif');	
					} else {
						jQuery('#mmimg').attr('src', '/images/nomail.jpg');	
					}
					
					jQuery('#cash').html('$' + data.c);
					
					jQuery('#clock').html(data.t);

				} catch (e) { }
			} 
			
			if ( data.n != null ) { 
				jQuery('#msg').html(data.n);
				jQuery('#costspan').html(data.cost);
			}

			if ( data.s == -1 && window.location.pathname != '/graveyard.php') {
				window.location = '/graveyard.php';
			}

			pollcnt++;
			if ( pollcnt <= pollmax ) {
				setTimeout(function() { do_poll(delay); } , delay*1000);
			}
		},'json'
	);
}

//do_poll.delay(15);

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


