// <![CDATA[

// on DOM ready, let the magic begin
$(document).ready(function(){

	// thumbnail hover
	var offsetX = 50;
	var offsetY = -200;
	
	$('.firstAd IMG:first-child').animate({opacity: 1.0}, 1000).animate({'width':'125px','height':'125px'},'slow').animate({opacity: 1.0}, 3000).animate({'width':'90px','height':'90px'},'slow');
	
	$('.ad125x125 IMG').hover(function() {
		$(this).stop().animate({'width':'125px','height':'125px'},'fast');
	},function() {
		$(this).stop().animate({'width':'90px','height':'90px'},'fast');
	});


	// functin which makes sure that not too many
	// app windows are open (like settings, file lists, etc.)
	// keeps them at one at a time
	function closeOtherOptions(id,leaveThis){
		if(id == '' || leaveThis == '') return;
		
		if($("#applyDiv-"+id).is(":visible") && leaveThis != 'applyDiv'){
			$("#applyDiv-"+id).hide();
		}
		
		if($("#filesDiv-"+id).is(":visible") && leaveThis != 'filesDiv'){
			$("#filesDiv-"+id).hide();
		}	
	}
	
	// target search key up
	$("#app_search").keyup(function(){
		if(this.value.length == 0){
			$("#searchStatus").removeClass("checkNotOK");
			$("#searchStatus").html('');
			$("#allAppsDiv").html('<h2>No apps have been found matching your search criteria...</h2>');
			$("#allAppsDiv").hide();
			$("#allAppsDivOriginal").show();
			return;
		}
		if(this.value.length < 4){
			$("#allAppsDivOriginal").hide();
			$("#allAppsDiv").show();
			$("#searchStatus").addClass("checkNotOK");
			$("#searchStatus").html('Minimum 4 characters...');
			$("#allAppsDiv").html('<h2>No apps have been found matching your search criteria...</h2>');
			return;
		}else{
			$("#allAppsDivOriginal").hide();
			$("#allAppsDiv").show();
			$("#searchStatus").removeClass("checkNotOK");
			$("#searchStatus").html('');

			$("#searchStatus").addClass("simpleAjaxLoader");
			$.ajax({
				type: "POST",
				url: "ajax_search_app.php",
				data: "q=" + this.value,
				success: function(ajaxResponse){
					switch(ajaxResponse){
						case "FAILED":
							alert('ERROR: Can\'t do it ... :(');
							$("#searchStatus").removeClass("simpleAjaxLoader");
						break;
						case "NOT FOUND":
							$("#searchStatus").removeClass("simpleAjaxLoader");
							$("#allAppsDiv").html('<h2>No apps have been found matching your search criteria...</h2>');
						break;
						default: // everything is most likely OK
							$("#searchStatus").removeClass("simpleAjaxLoader");
							$("#allAppsDiv").html(ajaxResponse);
						break;
					}
				}
			});
		}
	});
	// target search options click
	$("#moreSearchOptions").click(function(){
		if($("#moreSearchOptions").text() == "more options")
			$("#moreSearchOptions").text("less options");
		else
			$("#moreSearchOptions").text("more options");
		
		$("#searchOptionsDiv").toggle('fast');
		return false;
	});

	// target all testers clicks
	$("[id*=showIncentive-]").click(function(){
		var splitId = this.id.split('-');
		if($("#showIncentive-"+splitId[1]).text() == "more info")
			$("#showIncentive-"+splitId[1]).text("less info");
		else
			$("#showIncentive-"+splitId[1]).text("more info");
		$("#incentiveInfo-"+splitId[1]).toggle('fast');
		return false;
	});
	
	var targetOffset = 0;
	// target all testers clicks
	$("[id*=apply-]").click(function(){
		var splitId = this.id.split('-');
		closeOtherOptions(splitId[1],'applyDiv');
		$("#applyDiv-"+splitId[1]).toggle();
		
		// todo - scroll to app-id
		//targetOffset = $("#app-"+splitId[1]).offset().top;
		//$('html,body').animate({scrollTop: targetOffset}, 500);
		
		$("#applyDiv-"+splitId[1]).addClass("simpleAjaxLoader");
		$.ajax({
			type: "POST",
			url: "ajax_get_apply_form.php",
			data: "q=" + splitId[1],
			success: function(ajaxResponse){
				switch(ajaxResponse){
					case "FAILED":
						alert('ERROR: Can\'t do it ... :(');
					break;
					default: // everything is most likely OK 
						$("#applyDiv-"+splitId[1]).removeClass("simpleAjaxLoader");
						$("#applyDiv-"+splitId[1]).html(ajaxResponse);
					break;
					
				}
			}
		});
		return false;
		
	});
	
	// target all activate clicks
	$("[id*=files-]").click(function(){
		var splitId = this.id.split('-');
		
		closeOtherOptions(splitId[1],'filesDiv');
		
		if($("#filesDiv-"+splitId[1]).is(":visible")){
			$("#filesDiv-"+splitId[1]).html('');
			$("#filesDiv-"+splitId[1]).hide();
			$("#filesText-"+splitId[1]).text("view screenshots");
			return false;
		}else{
			$("#filesText-"+splitId[1]).text("hide screenshots");
		}
		
		$("#filesDiv-"+splitId[1]).show();
		$("#filesDiv-"+splitId[1]).addClass("simpleAjaxLoader");
		$.ajax({
			type: "POST",
			url: "ajax_get_files.php",
			data: "q=" + splitId[1],
			success: function(ajaxResponse){
				$("#filesDiv-"+splitId[1]).removeClass("simpleAjaxLoader");
				switch(ajaxResponse){
					case "FAILED":
						alert('ERROR: Can\'t do it ... :(');
					break;
					default: // everything is most likely OK
						$("#filesDiv-"+splitId[1]).html(ajaxResponse);
						
						// reload hover for thumbnails
						$("#screenshotsList-"+splitId[1]+" a").hover(function(e) {
							var href = $(this).attr('href');
							$("#zoomer").html('<img id="largeImage" src="' + href + '" alt="big image" height=\"480px\" />')
							.css('top', e.pageY + offsetY)
							.css('left', e.pageX + offsetX);
							$("#zoomer").show("fast");
						}, function() {
							$('#zoomer').hide();
							$('#zoomer').html("");
						}).click(function(e){
							return false;
						});
						
						$("#screenshotsList a").mousemove(function(e) {
							$("#zoomer").css('top', e.pageY + offsetY).css('left', e.pageX + offsetX);
						});
						
					break;
					
				}
			}
		});
		return false;
	});

	// submit
	$("#newDeviceForm").submit(function(e){
		var validates = true;
		$("#errorsList").html("");
		$("#errors").hide("fast");
		
		if($("#udid").val() == '' || $("#udid").val().length != 40){ // device UDID
			$("#errorsList").append("<li>Please enter a valid UDID (40 characters exactly)</li>");
			validates = false;
		}
		
		if($("#device_name").val() == '' || $("#device_name").val().length < 4){ // device name
			$("#errorsList").append("<li>Please name your device (4 characters minimum)</li>");
			validates = false;
		}
		
		if(validates == false){ // show error and stop submit
			$("#errorTitle").show();
			$("#errors").show("fast");
			$('html, body').animate({scrollTop:0}, 'slow'); 
			return false;
		}
		
		// if all OK - then disable submit button while processing
		$("#submitButton").attr("disabled", "false");
		$("#submitStatus").addClass("ajaxLoader");
	});
	
});

// ]]>