// myads.js

var my_ads = {
	
	do_once: false,

	loader:function(id,ad,ver) {
	
		$(".removeme").remove();
	
		$.ajax({

			type:"GET",

			url:"/apps/myads.php",

			data:"q="+id+"&d="+ad+"&ver="+ver,

			success:function(data) {

				if($.browser.opera) {

					var height = document.innerHeight;

				}

				else {

					var height = $(document).height();

				}

				$("#MyAds").html(data);
				
				$("#SlideDown").hide()
				
				.slideDown(800);

				$("#overlay").height(height)

					//.html(data)

					.show()

					.fadeTo("fast", 0.59);

				var x = ($(window).width()/2)-($("#MyAds").width()/2);

				var y = 100;

				$("#MyAds").show()

					.css("left", x)

					.css("top", y);

				$.scrollTo("#MyAds", 800);

			}

		});

	}, //launch

	end:function() {

		$("#game-window").hide();

		$("#MyAds").html("");

		$("#overlay").fadeTo("fast", 0, function() {

			$("#overlay").html("")

			.hide();

		});

	}, //End
	
	preview:function(ad,rate) {
		
		if ($("#previewad"+ad).html()==null && my_ads.do_once == false) {
		
		my_ads.do_once = true;
		
		$.ajax({

			type:"GET",

			url:"/apps/myads.php",

			data:"p="+ad+"&r="+rate,

			success:function(data) {
				
				$(".removeme").remove();
				
				$("#Ad"+ad).append(data);
						
				$("#previewad"+ad).slideDown("fast");
				
				my_ads.do_once = false;
		
			}

		});
		
		}

	}, //Preview
	
	unpreview:function() {

		$(".removeme").remove();
				
	}, //Unpreview
	
	markup:function(value) {
		
		if (value == true) {
			
			$(".findme").attr('checked', true);
			
		} else {
			
			$(".findme").attr('checked', false);
			
		}
		
	}, //Mark Up
	
	check:function() {
		
		if ($("input.findme:checked").length==0) {
			
			alert(lang.none_check);
			
		} else if ($("#MyAdsMessage").val()!="" && my_ads.do_once == false) {
			
			//$("#SendRecommendations").submit();
			
			my_ads.do_once = true;
			
			my_ads.loading(true);
			
			var Victims = "";
			
			$("input.findme:checked").each(function () {
				
				Victims = Victims+$(this).val()+",";
				
			});
			
			$("#SlideDown").slideUp(800, function () {
			
				$.ajax({
		
					type:"POST",
		
					url:"/apps/myads.php",
		
					data:"SenderID="+$(".UserID").val()+"&UserID="+Victims+"&AdID="+$(".AdID").val()+"&Message="+$("#MyAdsMessage").val(),
		
					success:function(data) {
						
						$(".remove_me").remove();
						
						$(".remove_bottom").remove();
						
						$(".insert_row").after(data)
						
						.remove();
						
						$("#SlideDown").slideDown(500, function () {
																				   
							my_ads.loading(false);
							
						});
						
						my_ads.do_once = false;
				
					}
		
				});
				
			});
						
		} else {
			
			alert(lang.fill_textarea);
			
		}
		
	},
	
	update:function(user,ad,val) {
		
		if (my_ads.do_once == false) {
		
		my_ads.do_once = true;
		
		my_ads.loading(true);
		
		$("#SlideDown").slideUp(800, function () {
						
			$.ajax({
	
				type:"GET",
	
				url:"/apps/myads.php",
	
				data:"q="+user+"&d="+ad+"&u="+val,
	
				success:function(data) {
					
					$(".removeme").remove();
					
					$(".remove_me").remove();
					
					$(".insert_row").after(data);
					
					$("#SlideDown").slideDown(800+(data.length*0.5), function () {
																			   
						my_ads.loading(false);
						
					});
					
					my_ads.do_once = false;
										
				}
	
			});
			
		});
		
		}
		
	},
	
	read:function(id,user) {
		
		if ($("#previewrec"+id).html()==null && my_ads.do_once == false) {
		
		my_ads.do_once = true;
		
		$.ajax({

			type:"GET",

			url:"/apps/myads.php",

			data:"r="+id+"&d="+user,

			success:function(data) {
				
				$(".removeme").remove();
				
				$("#Rectitle"+id).append(data);
						
				$("#previewrec"+id).slideDown("fast");
				
				if ($("#Rec"+id).attr('class') == 'unread') {
					
					$("#Rec"+id).removeClass('unread')
	
						.addClass('read');
						
					update.stats("MyRecsUnread");
				
				}

				$("#Rectitle"+id).css('font-weight', 'normal');
				
				my_ads.do_once = false;
		
			}

		});
		
		} else {
			
				$("#previewrec"+id).slideUp("fast", function () { 
					
					$("#previewrec"+id).remove();
					
				});
			
		}

	}, //Read
	
	deleter:function(id,user) {

		if ($("#previewrec"+id).html()==null && my_ads.do_once == false) {
		
		my_ads.do_once = true;
		
		$.ajax({

			type:"GET",

			url:"/apps/myads.php",

			data:"d="+id+"&ID="+user,

			success:function() {

			$("#Recrow"+id).fadeTo("slow", 0, function() {

				if ($("#Rec"+id).attr('class') == 'unread') {

					update.stats("MyRecsUnread");
				
				}

				$("#Recrow"+id).remove();
				
				update.stats("MyRecsAll");
				
				my_ads.do_once = false;
				
			});

			}

		});
		
		} else {
			
			$("#previewrec"+id).slideUp("fast", function () { 
				
				$("#previewrec"+id).remove();
				
			});
			
		}

	},
	
	rate:function(id,ad,ver) {
	
		if (my_ads.do_once == false) {
			
		my_ads.do_once = true;
	
		my_ads.loading(true);
	
		$("#SlideDown").slideUp(800, function () {
		
			$.ajax({
	
				type:"GET",
	
				url:"/apps/myads.php",
	
				data:"q="+id+"&a="+ad+"&ver="+ver,
	
				success:function(data) {
					
					$(".replace_me").remove();
	
					$(".find_me").after(data);
					
					$("#SlideDown").slideDown(800, function () { 
															 
						my_ads.loading(false); 
						
					});
	
					my_ads.do_once = false;
	
				}
	
			});
			
		});
		
		}

	}, //rate
	
	rating:function (opt,val) {
		
		var i = 1;
		
		var total;
		
		$("#hidden"+opt).val(val);
		
		total = Math.round( ( parseInt($("#hidden1").val()) + parseInt($("#hidden2").val()) + parseInt($("#hidden3").val()) ) / 3);
		
		$("#hidden4").val(total);
		
		while (i <= 5) {
							
			if (i <= val && val != 0) {
				
				$("."+opt+"_rate_"+i).removeClass('rate_stars_full')
				
				.removeClass('rate_stars_empty')
				
				.addClass('rate_stars_full');
				
			} else {
				
				$("."+opt+"_rate_"+i).removeClass('rate_stars_full')
				
				.removeClass('rate_stars_empty')
				
				.addClass('rate_stars_empty');
				
			}
			
			if (i <= total && total != 0) {
				
				$(".4_rate_"+i).removeClass('rate_stars_full')
				
				.removeClass('rate_stars_empty')
				
				.addClass('rate_stars_full');
				
			} else {
				
				$(".4_rate_"+i).removeClass('rate_stars_full')
				
				.removeClass('rate_stars_empty')
				
				.addClass('rate_stars_empty');
				
			}
			
			i++;
			
		}
		
	}, //rating
	
	rate_now:function (num,id,ad,ver) {
		
		if (my_ads.do_once == false) {
			
		my_ads.do_once = true;
		
		my_ads.loading(true);
		
		var val = parseInt($("#hidden"+num).val());
		
		$("#SlideDown").slideUp(800, function () {
			
			$.ajax({
	
				type:"GET",
	
				url:"/apps/myads.php",
	
				data:"val="+val+"&uID="+id+"&aID="+ad+"&ver="+ver,
	
				success:function(data) {
	
					$(".replace_me").remove();
	
					$(".find_me").after(data);
					
					$("#Ad"+ad).removeClass('rated_false')
					
					.addClass('rated_true')
	
					$("#SlideDown").slideDown(500, function () { 
															
						my_ads.loading(false); 
						
					});
					
					var image = data.match(/ [\d]+ /g);
					
					if (parseInt(image[0]) == 1) {
						
						activity.twice();
						
						if (parseInt(image[1]) == 4) {
							
							activity.check();
							
						}
												
					}
					
					my_ads.do_once = false;
	
				}
	
			});
			
		});
		
		}
		
	}, // rate now
 
	load_rec:function(id,ad,ver) {
		
		if (my_ads.do_once == false) {
			
		my_ads.do_once = true;
		
		my_ads.loading(true);
		
		$("#SlideDown").slideUp(800, function () {
			
			$.ajax({
	
				type:"GET",
	
				url:"/apps/myads.php",
	
				data:"q="+id+"&d="+ad+"&ver="+ver+"&load=isset",
	
				success:function(data) {
					
					$("#SlideDown").html(data)
	
					.slideDown(800, function () { 
											  
						my_ads.loading(false); 
						
					});
					
					my_ads.do_once = false;
	
				}
	
			});
			
		});
		
		}
		
	}, // load recommendations
	
	loading:function(bool) {
		
		if (bool == true) {
			
			$(".load").addClass('load_'+(Math.ceil(Math.random()*2)))
			
			.show()
			
			.fadeTo(1000,1);
			
		} else {
			
			$(".load").fadeTo(1000,0, function () {
				
				$(this).hide()
				
				.removeClass('load_1')
				
				.removeClass('load_2');
				
			});
						
		}
		
	}
	
};