/**
 * Cookie object (data storage for font resizer)
 */
var cookie = {
	set: function(name, value, days) {
		var expires = '';
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			expires = '; expires=' + date.toGMTString();
		}
		document.cookie = name + '=' + value + expires + '; path=/';
	},
	get: function(name) {
		var nameEQ = name + '=';
		var ca = document.cookie.split(';');
		for (var i=0; i < ca.length; i++) {
			var c = ca[i];
			
			if (c.charAt(0) == ' ') 
			{
				//c = c.substr(l);
				c = c.replace(/^ /, "");				
			}
			if (c.indexOf(nameEQ) > -1) 
				return c.substr(nameEQ.length);
		}
		return null;
	}
};

/**
 * Font Resizer object
 */
var fontResizer = {
	fontSize: '62.5%',
	fontSizes: ['62.5%', '75%', '80%'],
	init: function() {
		this.readFromCookie();
	},
	readFromCookie: function() {
		var cookieFontSize = cookie.get('fontSize');
		if (cookieFontSize != null && cookieFontSize != '' && $.inArray(cookieFontSize, this.fontSizes)) {
			this.fontSize = cookieFontSize;
			$('body').css('fontSize', this.fontSize);
		}
	},
	enlarge: function() {
		this.setSize(1);
	},
	shrink: function() {
		this.setSize(-1);
	},
	setSize: function(inc) {
		var currentIndex = $.inArray(this.fontSize, this.fontSizes);
		if (this.fontSizes[currentIndex + inc] != null) {
			var newFontSize = this.fontSizes[currentIndex + inc];
			this.fontSize = newFontSize;
			$('body').css('fontSize', newFontSize);
		}
		cookie.set('fontSize', this.fontSize, 30);
	}
};

$(document).ready(function() {
	$.browser.msie6 = $.browser.msie && parseInt($.browser.version) == 6 && !window["XMLHttpRequest"];
	// Add accordion to the service menu
	$('ul.service > li > a').bind('click', function(evt) {
		if ($(evt.target).attr('href') != '#') {
			evt.stopPropagation();
		}
	});
//	$('ul.service').accordion({'animated':false, 'header':'li > a'});
	$('ul.service').accordion({'animated':false, 'autoheight': false, 'header':'li > a'});
	
	// Pagination
	$('.itemsPage ul').bind('mouseenter', function(evt) {
		evt.preventDefault();
		$('li', $(this)).show();
	});
	$('.itemsPage ul').bind('mouseleave', function(evt) {
		evt.preventDefault();
		$('li', $(this)).not('.selected').hide();
	});	
	
	// Content background image scaling
	if($('img.contentBack').length > 0) {
		var height = $('#middle').height();
		//$('img.contentBack').attr('height', height);
		$('img.contentBack').css('height', height);
	}
	
	// Search ISBN tooltip
	$('.btnExclamation a').bind('mouseover', function(evt) {
		$('.tooltipInfo').css({'left':evt.clientX + 5, 'top':evt.clientY + 5}).show();
	}).bind('mouseout', function(evt) {
		$('.tooltipInfo').hide();
	});
	
	// Bind Font Resizer
	fontResizer.init();
	$('.text_smaller').bind('click', function(evt) {
		evt.preventDefault();
		fontResizer.shrink();
	});
	$('.text_larger').bind('click', function(evt) {
		evt.preventDefault();
		fontResizer.enlarge();
	});
});
