/**
 * @author rock
 */
/** 
 * This file uses a Stopwatch instance to handle DOM events.
 * It assumes that the document has elements with the following IDs:
 * time: placeholder to display time
 * start: source of start/stop event
 * reset: source of reset/lap event
 */

// A Stopwatch instance that displays its time nicely formatted.
var s = new Stopwatch(function(runtime) {
  // format time as m:ss.d
  var minutes = Math.floor(runtime / 60000);
  var seconds = Math.floor(runtime % 60000 / 1000);
  var decimals = Math.floor(runtime % 1000 / 100);
  var displayText = minutes + ":" + (seconds < 10 ? "0" : "") + seconds ; // + "." + decimals;
  $("#time1").html(displayText);
});



// Code to create instances and wire everything together.
$(document).ready(function(){
  // DOM Level 2 event model allows us to attach more than one event listener to a source!
  // jQuery's bind method hides browser-specific differences
  // first stopwatch
  $("#start1").bind("click", function(){ s.startStop(); });
  $("#reset1").bind("click", function(){ s.resetLap(); });
  s.doDisplay();

});


/**
 * This file defines the Stopwatch class.
 * Note that it knows nothing about instances and how those instances are used.
 */
var Stopwatch;
if (!Stopwatch) 
    Stopwatch = {};

/**
 * Constructs a new Stopwatch instance.
 * @param {Object} displayTime the strategy for displaying the time
 */
function Stopwatch(displayTime){
    this.runtime = 0; // milliseconds
    this.timer = null; // nonnull iff runnig
    this.displayTime = displayTime; // not showing runtime anywhere
}

/**
 * The increment in milliseconds.
 * (This is a class variable shared by all Stopwatch instances.)
 */
Stopwatch.INCREMENT = 200

/**
 * Displays the time using the appropriate display strategy.
 */
Stopwatch.prototype.doDisplay = function(){
    if (!this.laptime) 
        this.displayTime(this.runtime);
    else 
        this.displayTime(this.runtime);
}

/**
 * Handles an incoming start/stop event.
 */
Stopwatch.prototype.startStop = function(){
    if (!this.timer) {
        var instance = this;
        this.timer = window.setInterval(function(){
            instance.runtime += Stopwatch.INCREMENT;
            instance.doDisplay();
			
        }, Stopwatch.INCREMENT);
    }
    else {
 //       window.clearInterval(this.timer);
   //     this.timer = null;
	//	alert('a');
    //    this.doDisplay();
    }
}

Stopwatch.prototype.stop = function(){
	window.clearInterval(this.timer);
        this.timer = null;
	//	alert('a');
        this.doDisplay();
}
Stopwatch.prototype.start = function(){
  var instance = this;
        this.timer = window.setInterval(function(){
            instance.runtime += Stopwatch.INCREMENT;
            instance.doDisplay();
			
        }, Stopwatch.INCREMENT);
}



/**
 * Handles an incoming reset/lap event.
 */
Stopwatch.prototype.resetLap = function(){
	
    if (!this.laptime) {
        if (this.timer) {
            this.laptime = this.runtime;
        }
        else {
            this.runtime = 0;
        }
    }
    else {
        delete this.laptime;
    }
	this.runtime = 0;
    this.doDisplay();
}

Stopwatch.prototype.addtime = function(toadd){
	if (toadd > 0) {
		toadd = toadd * 1000;
			this.runtime = this.runtime + toadd;
		
	}
		else {
		
			this.runtime = this.runtime + 20000;
		}
	}
	
Stopwatch.prototype.gettime = function(){
	
	return this.runtime ;
	}	
	
	

$(document).ready(function(){

	//Hide (Collapse) the toggle containers on load
	$(".toggle_container").hide(); 

	//Switch the "Open" and "Close" state per click
	$("h2.trigger").toggle(function(){
		$(this).addClass("active");
		}, function () {
		$(this).removeClass("active");
	});

	//Slide up and down on click
	$("h2.trigger").click(function(){
		$(this).next(".toggle_container").slideToggle("fast");
	});

});	




	// facebook

function gfc_save(name, gfc_id, image)
{
		var url = '/authentication/getgfcajaxlogin/'; // ajax url which is here display.php
		$.ajax({
	type: "POST",
	url:url,
	data: "save=yes&name="+name+"&gfc_id="+gfc_id+"&image="+image,
	success: function(msg){
		eval('var z='+msg);
		 
		var user_box = document.getElementById("user");
		user_box.innerHTML = "Welcome, " + name  +"  you are loged in "; 
		$(document).ready(function(){
			 jQuery("#logedin-popup").html("Welcome, " + name  +"  you are loged in "); 
			 $('#notlogedin-popup').toggle('fast');
			 
		});
		
	}
});
}



$(document).ready(function(){
  $('#pause-button').click(function() {
    $('#sudoku-table').toggle('fast');
	$('#sudoku-table-hidden').toggle('fast');
	$('#unpause-button').toggle('fast');
	$('#pause-button').toggle('fast');	
	s.stop();
    return false;
  });
   $('#sudoku-table-hidden, #unpause-button').click(function() {
    $('#sudoku-table').toggle('fast');
	$('#sudoku-table-hidden').toggle('fast');
	$('#pause-button').toggle('fast');	
	$('#unpause-button').toggle('fast');
	s.start();
    return false;
  });
});  



function send_popup(){
	//alert('aaa');
	var time1 = s.gettime();
	//alert('aa'+time1 );
	  var minutes = Math.floor(time1 / 60000);
  var seconds = Math.floor(time1 % 60000 / 1000);
  var displayText = minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
//	var timer =  $("#time1").val();
	$("input#yourtimepopup").val( displayText );
	$("input#ptime").val( time1 );
	$(document).ready(function(){
		$.fn.colorbox({width:"50%", inline:true, href:"#completed-div", open:true});
	});
	
}

this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
