/**
 * General Functions
 **/
this.image_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.image_tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='image_tooltip'><img src='"+ this.t +"' /></p>");
		$("#image_tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#image_tooltip").remove();
    });	
	$("a.image_tooltip").mousemove(function(e){
		$("#image_tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};



// starting the script on page load
$(document).ready(function(){
	image_tooltip();
	$(".tooltip").simpletooltip();
	
	$('li:first-child').addClass('first');
	$('li:last-child').addClass('last');
});


/**
 * Function to clear a form field on click if value is not initial value         
 **/
function clearField(field, initValue) {
  if ($(field).val() == initValue) {
    $(field).val('');
  }
}

/**
* Adds a function to the window onunload event
*/
function addUnloadEvent(func) {
  var oldOnunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  }
  else {
    window.onunload = function() {
      oldOnunload();
      func();
    }
  }
}

/**
 * additional Datepicker functions
 **/

function updateSecondDate(obj) {
  var yId = obj.id.replace('-mm','').replace('-dd','');
  var mId = yId + '-mm';
  var dId = yId + '-dd';

  var yVal = $('#'+ yId +' option:selected').val();
  var mVal = $('#'+ mId +' option:selected').val() - 1;
  var dVal = $('#'+ dId +' option:selected').val();

  var date = new Date();
  date.setFullYear(yVal,mVal,dVal);
  
  var data = {
    date: date,
    id:   yId,
    dd:   mVal,
    mm:   dVal,
    yyyy: yVal
  };
  
  setSecondDate(data);
}
    
function setSecondDate(argObj) {
  var className = argObj.id.replace('From','To');
  
  var firstDate  = argObj.date;
  var secondDate = new Date();
  secondDate.setTime(firstDate.getTime()+(24*60*60*1000));
  
  var data = { 
    d: new Number(secondDate.getDate() - 1),
    m: secondDate.getMonth(),
    y: secondDate.getFullYear()
  };
  
  $('#'+ className + '-dd option').attr('selected','');
  $('#'+ className + '-dd option')[ data.d ].selected = true;
  
  $('#'+ className + '-mm option').attr('selected','');
  $('#'+ className + '-mm option')[ data.m ].selected = true;
  
  $('#'+ className + ' option').attr('selected','');
  $('#'+ className + ' option[value='+ data.y +']').attr('selected','selected');
}