$(document).ready(function(){
	$('#myForm2').ajaxForm({ dataType: 'json', success: processJson2 });

  	$('a[rel*=facebox]').facebox();
	
	$('.tc-items').draggable({ revert:true,scrollSensitivity:50,scrollSpeed:30});
	$('.tc-items').draggable({scroll:true,helper:function(){
			$(ui.helper).addClass('tc-object-glow');
		}
	});

	$('#tc').droppable({
		accept: '.tc-items',
		hoverClass: 'tc-glow',
		tolerance: 'touch',
		drop: function(ev, ui){
			var myid = (ui.draggable).attr('id').replace('tc-item-','');
			$(ui.draggable).remove();
			
			$.getJSON('treasureapi.cfm?name='+myid,function(json){
				if(json.result=='ok'){
					$('#counter-change').text(json.counter);
					$('#txt-play-our-hidden-objects-game').hide();
					$('#position2').hide();
					$('#position2').text('');
					$('#position2').text(json.message);
					$('#position2').show();
					if(json.counter=='10'){
						if ($.browser.msie == false){
							 $('.media').flash({src:'audio/clip2.swf',width:0,height:0});
						}
						$('#tc-image').addClass('tc-glow');
						$('#tc-image').addClass('tc-glow-open');
						$('#tc-image').attr("href","homepopup.php?image=congratulations");
					}else{
						if ($.browser.msie == false){
							 $('.media').flash({src:'audio/clip1.swf',width:0,height:0});
						}
						$('#position2').fadeTo(2500,1).fadeOut(700);
					}
				}
				if(json.result=='error'){
					$('#counter-change').text('err');
				}
			});
		}
	});

});

function processJson2(data){
	if(data.result=='ok'){
		window.location.href='members/games/index.php';
	}
	if(data.result=='error'){
		window.location.href='login/?m=true';
	}
}
