/**
 * @project seniorenbasel.ch
 * @author: Kilian Buenter
 * @date: 10.11.2010
 * @version: 1.0
 */

/* check if cookie seted */
if (document.cookie) {
	  a_cookiePart = document.cookie.split('; ');
	  
	  for (i=0; i < a_cookiePart.length; i++) {
	  	if (a_cookiePart[i].indexOf("fontSize") > -1) {
	  			a_fontSize = a_cookiePart[i].split("=");
	  			v_fontSize = a_fontSize[1];
	  			$('head').append('<style id="test" type="text/css">body {font-size: ' + v_fontSize + 'px;}</style>');
	  			i=a_cookiePart.length;
	  	}
	  }
}


$(function(){ // start when document ready

setButtonState();

/* selectors */
$bigger = $('#bigger');
$smaller = $('#smaller');
$searchTxt = $('.searchTxt');

/* event listener fontResizer */
$bigger.click(function(e){
	
	if ($bigger.hasClass('disabled') == false) {
		setFontSize('bigger');
	}
	
	if ($smaller.hasClass('disabled')) {
		$smaller.removeClass('disabled');
	}
	
	setCookie();
	e.preventDefault();
});

$smaller.click(function(e){
	if ($smaller.hasClass('disabled') == false) {
		setFontSize('smaller');
	}
	
	if ($bigger.hasClass('disabled')) {
		$bigger.removeClass('disabled');
	}
	
	setCookie();
	e.preventDefault();
});

/* event listener for search field */
vDefaultValue = 'Suchbegriff';
$searchTxt.focus(function(){
	if($(this).val() == vDefaultValue) {
		$(this).val('');
	}
}).blur(function(){
	if($(this).val() == '') {
		$(this).val(vDefaultValue);
	}
});



}); // end document ready



v_maxFontSize = 13;
v_minFontSize = 10;
	
/* Function setFontSize
 * @command string ('bigger' or 'smaller')
 */
setFontSize = function(command) {
	var v_currentFontSize = getCurrentFontSize();
	
	if (command != null) {
		if (command == 'bigger'){
    	$('body').css('font-size', v_currentFontSize + 1 + 'px');
    	setButtonState();
    }
    
    else {
    	$('body').css('font-size', v_currentFontSize - 1 + 'px');
    	setButtonState();
    }
  }
}

/* Function disableButton
 * @command string ('bigger' or 'smaller')
 */
setButtonState = function(){
	var v_isBiggest = isBiggest();
	var v_isSmallest = isSmallest();	

	if (v_isBiggest) {
		$('#bigger').addClass('disabled');
	}	
	
	else if (v_isSmallest) {
		$('#smaller').addClass('disabled');
	}
}

/* Function isBiggest
 * checks if font size on biggest size 
 * returns true or false
 */
isBiggest = function() {
	retVal = false;
	var v_currentFontSize = getCurrentFontSize();
	
	if (v_currentFontSize >= v_maxFontSize) {	
		retVal = true;
	}
	
	return retVal;
}

/* Function isSmallest
 * checks if font size on smallest size 
 * returns true or false
 */
isSmallest = function() {
	retVal = true;
	var v_currentFontSize = getCurrentFontSize();
	if (v_currentFontSize > v_minFontSize) {	
		retVal = false;
	}
	
	return retVal;
}

/* Function getCurrentFontSize
 * get the fontsize from body element
 * returns as integer
 */
getCurrentFontSize = function() {
	return parseInt($('body').css('font-size'));
}

/* Function setCookie
 * saves current fontsize in cookie
 */
setCookie = function() {
	endDate = new Date();
	var futureDate = endDate.getTime() + (30 * 24 * 60 * 60 * 1000); // in 30 days
	endDate.setTime(futureDate);
	document.cookie = 'fontSize=' + getCurrentFontSize() + '; expires=' + endDate.toGMTString() + '; path=/';	
}
