var default_timer =  1;
var first = true;
var capture = '';

$(document).ready(function() {
	if($('.monster_wrapper').length){
		init_monster();
	
		$('.free_sim_link').bind('mouseenter', function(){
			
			if(capture=='wave'){
				$('#wave').destroy();
			}else if(capture=='itchy'){
				$('#itchy').destroy();
			}else if(capture=='leg'){
				$('#leg').destroy();
			}else if(capture=='freesim'){
				$('#freesim').destroy();
			}else if(capture=='topup'){
				$('#topup').destroy();
			}
			
			clearInterval(myTimer);
			capture = 'freesim';
			$('#wave,#itchy,#leg,#freesim,#topup').css({"visibility":"hidden"});
			freesim();
			
			$('.topup_link').hide();
		});
		
		$('.free_sim_link').bind('mouseleave', function(){
			//$('#monster').css('background-position','0px 0px');
			
			if(capture=='wave'){
				$('#wave').destroy();
			}else if(capture=='itchy'){
				$('#itchy').destroy();
			}else if(capture=='leg'){
				$('#leg').destroy();
			}else if(capture=='freesim'){
				$('#freesim').destroy();
			}else if(capture=='topup'){
				$('#topup').destroy();
			}
					
			clearInterval(myTimer);
			freesim_open();
			init_monster();
			
			$('.topup_link').show();
		});
		
		$('.topup_link').bind('mouseenter', function(){
			
			if(capture=='wave'){
				$('#wave').destroy();
			}else if(capture=='itchy'){
				$('#itchy').destroy();
			}else if(capture=='leg'){
				$('#leg').destroy();
			}else if(capture=='freesim'){
				$('#freesim').destroy();
			}else if(capture=='topup'){
				$('#topup').destroy();
			}
			
			clearInterval(myTimer);
			capture = 'topup';
			$('#wave,#itchy,#leg,#freesim,#topup').css({"visibility":"hidden"});
			topup();
			
			$('.free_sim_link').hide();
		});
		
		$('.topup_link').bind('mouseleave', function(){
			
			if(capture=='wave'){
				$('#wave').destroy();
			}else if(capture=='itchy'){
				$('#itchy').destroy();
			}else if(capture=='leg'){
				$('#leg').destroy();
			}else if(capture=='freesim'){
				$('#freesim').destroy();
			}else if(capture=='topup'){
				$('#topup').destroy();
			}
					
			clearInterval(myTimer);
			topup_open();
			init_monster();
			
			$('.free_sim_link').show();
		});
	}
});

function init_monster(){
	var counter = default_timer;
	
	myTimer = window.setInterval(function() {
		if(counter ==0){
			clearInterval(myTimer);
			counter = default_timer;
			
			var rand = Math.floor(Math.random()*3);
			
			if(first){
				first =  false;
			}else{
				if(capture=='wave'){
			$('#wave').destroy();
		}else if(capture=='itchy'){
			$('#itchy').destroy();
		}else if(capture=='leg'){
			$('#leg').destroy();
		}else if(capture=='freesim'){
			$('#freesim').destroy();
		}else if(capture=='topup'){
			$('#topup').destroy();
		}
			}
			
			$('#wave,#itchy,#leg,#freesim,#topup').css({"visibility":"hidden"});

			if(rand == 0){
				wave();
				capture = 'wave';
			}else if(rand == 1){
				itchy();
				capture = 'itchy';
			}else{
				leg();
				capture = 'leg';
			}
			
			$('.free_sim_link').show();
			$('.topup_link').show();
		
		}
		
		counter--;
	}, 800);
}

function wave(){
	$('#chac').html('wave');
	$('#wave').css({"visibility":"visible"});
	$('#wave').sprite({fps: 9, no_of_frames: 29,
	 start_at_frame: 1,
	 on_last_frame: function(obj) {
		obj.spStop();
		init_monster();
	}});
}

function itchy(){
	$('#chac').html('itchy');
	$('#itchy').css({"visibility":"visible"});
	$('#itchy').sprite({fps: 9, no_of_frames: 36,
	 start_at_frame: 1,
	 on_last_frame: function(obj) {
		obj.spStop();
		init_monster();
	}});
}

function leg(){
	$('#chac').html('leg');
	$('#leg').css({"visibility":"visible"});
	$('#leg').sprite({fps: 9, no_of_frames: 37,
	 start_at_frame: 1,
	 on_last_frame: function(obj) {
		obj.spStop();
		init_monster();
	}});
}

function freesim(){
	$('#chac').html('freesim');
	$('#freesim').css({"visibility":"visible"});
	$('#freesim').sprite({fps: 5, no_of_frames: 4,
	 start_at_frame: 1,
	  on_frame: {3: function(obj){
		obj.destroy();
	 }}});
}

function freesim_open(){
	$('#chac').html('freesim');
	$('#freesim').css({"visibility":"visible"});
	$('#freesim').sprite({fps: 5, no_of_frames: 7,
	 start_at_frame: 5,
	  on_frame: {6: function(obj){
		obj.destroy();
	 }}});
}

function topup(){
	$('#chac').html('topup');
	$('#topup').css({"visibility":"visible"});
	$('#topup').sprite({fps: 5, no_of_frames: 4,
	 start_at_frame: 1,
	 on_frame: {3: function(obj){
		obj.destroy();
	 }}});
}

function topup_open(){
	$('#chac').html('topup');
	$('#topup').css({"visibility":"visible"});
	$('#topup').sprite({fps: 5, no_of_frames: 7,
	 start_at_frame: 5,
	 on_frame: {6: function(obj){
		obj.destroy();
	 }}});
}
