﻿// AJAX url
var $url = "http://www.passarihop.se/lovematch/ajax-match.php";



// Use different shortcut for jQuery ($ conflicts with prototype)
var $j = jQuery.noConflict();

// Timeout 
var timeout = null;

// Init document
$j(document).ready(function() {
	updateMatchings();
	
	$j("#n1").click(function() {
		$j("#n1").val('');
	});
	$j("#n2").click(function() {
		$j("#n2").val('');
	});
});



function updateMatchings() {
	clearTimeout( timeout );
	
	var ts = $j("#ts").text();
	
	for (var i = 1; i < 5; i++) {
		var id = $j("#id"+i).text();
		var type = $j("#type"+i).text();
		var datai = i;
		
			// Load new result
			$j.ajax({
				type: "GET",
				url: $url,
				cache: false,
				dataType: "json",
				data: "ts=" + ts + "&id=" + id + "&type=" + type + "&datai=" + datai,
				success: function( data ) {
					// Hide result
				$j("#first-name"+data["datai"]).hide();
				$j("#resultamp"+data["datai"]).hide();
				$j("#second-name"+data["datai"]).hide();
				$j("#result-heart"+data["datai"]).hide();
				$j("#result-value"+data["datai"]).hide();
				$j("#result-comment"+data["datai"]).hide();
				
					// Update values					
					$j("#first-name"+data["datai"]).text(data["name1"]);
					$j("#resultamp"+data["datai"]).text(" & ");
					$j("#second-name"+data["datai"]).text(data["name2"]);
					$j("#result-value"+data["datai"]).text("0%");
					$j("#result-comment"+data["datai"]).text(data["comment"]);
					$j("#ts").text(data["ts"]);
					$j(id+data["datai"]).text(data["id"]);
					
					// Show result
					$j("#first-name"+data["datai"]).fadeTo(800, 1, function(){
						if ( this.style.removeAttribute ) 
							this.style.removeAttribute("filter");
					});
					$j("#resultamp"+data["datai"]).fadeTo(800, 1, function(){
						if ( this.style.removeAttribute ) 
							this.style.removeAttribute("filter");
					});
					$j("#second-name"+data["datai"]).fadeTo(800, 1, function(){
						if ( this.style.removeAttribute ) 
							this.style.removeAttribute("filter");
					});
					$j("#result-heart"+data["datai"]).fadeTo(800, 1, function(){
						if ( this.style.removeAttribute ) 
							this.style.removeAttribute("filter");
					});
					$j("#result-value"+data["datai"]).fadeTo(1200, 1, function(){
						if ( this.style.removeAttribute ) 
							this.style.removeAttribute("filter");
					});
					
					setTimeout(function(){updateCounter( data["datai"], 0, data["prob"]);}, 1200);			
				}
			});		
	}
	timeout = setTimeout( updateMatchings, 15000 );	
}

function updateCounter( counterid, value, max ) {
	if ( value <= max ) {
		$j("#result-value" + counterid).text(value + "%");
		setTimeout(function(){updateCounter(counterid, ++value, max);}, 25);
	}
	else {
		$j("#result-comment" + counterid).fadeTo(800, 1, function(){
			if ( this.style.removeAttribute ) 
				this.style.removeAttribute("filter");
		});
	}
}

function showNext() {
	if ( $j("#id").text() > 0 ) {
		$j("#id").text( parseInt( $j("#id").text() ) + 1 ); 
	} 
	
	updateMatchings();
}

function showPrev() {
	if ( $j("#id").text() > 0 ) {
		$j("#id").text( parseInt( $j("#id").text() ) - 1 ); 
	} 	

	updateMatchings();
	
}

/* Webbmatching */
//AJAX url
var $weburl = "http://www.passarihop.se/lovematch/web-match.php";

function getWebMatching() {
	
	// webbtest
	var n1 = $j("#n1").val();
	var n2 = $j("#n2").val();
	var ip = $j("#ip").val();
	
	// validate
	if (n1 == "" || n2 == "") {
		$j.facebox('<div style="padding: 30px;"><h1>Fel i formuläret</h1><p>För att kunna matcha era namn så måste namnen fyllas i, försök igen och lycka till med kärleken.</p><h3 style="text-align:right;">// passarihop.se</h3></div>');
		return;
	}
	
	// Load result
	$j.ajax({
		type: "GET",
		url: $weburl,
		cache: false,
		dataType: "json",
		data: "n1=" + n1 + "&n2=" + n2 + "&ip=" + ip,
		success: function( data ) {
			// Update values	
			var dataid = data["id"]*1979; // not specific id
			$j("#n1-res").text(data["n1"]);
			$j("#n2-res").text(data["n2"]);
			$j("#value-res").text(data["value"]+'%');
			$j("#comment-res").text(data["message"]);
			$j("#mymatch").text("<div id=\"my-match"+dataid+"\" style=\"width:200px;\" class=\"lm-color lm-rounded\"></div><script src=\"http://www.passarihop.se/passarihop-mymatch.php?cs="+dataid+"\" type=\"text/javascript\"></script>");
		    $j.facebox({div: '#resultbox'});
		}
	});
}

