/**
 * common.js
 *
 * Libri common JavaScript functions
 *
 */

/**
 * Main url lang
 *
 */
var MAIN_URL_LANG = '/';
var CART_TYPE_ACTUAL = 3;
var CART_TYPE_FAVORITES = 1;
var CART_TYPE_SAVED = 2;

/**
 * onLoad listener arrays
 *
 */
window.onloadListeners = new Array();
window.onloadListenerArgs = new Array();

/**
 * Add onLoad listener
 *
 * @param listener function
 * @param args object
 * @return void
 */
window.addOnLoadListener = function (listener, args) {
	var listenerLength = window.onloadListeners.length;

	if (typeof(listener) == 'function') {
		window.onloadListeners[listenerLength] = listener;

		if (typeof(args) != 'undefined') {
			window.onloadListenerArgs[listenerLength] = args;
		} else {
			window.onloadListenerArgs[listenerLength] = null;
		}
	}
}

/**
 * Call onLoad functions
 *
 * @return void
 */
window.onload = function() {
	for (var i=0; i<window.onloadListeners.length; i++) {
		var listener = window.onloadListeners[i];
		var args = window.onloadListenerArgs[i];

		if (typeof listener == 'function') {
			listener.call(this, args);
		}
	}
}

function Get_Cookie(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
	var cookieString = name + "=" +escape(value) +
		( (expires) ? ";expires=" + expires.toGMTString() : "") +
		( (path) ? ";path=" + path : "") +
		( (domain) ? ";domain=" + domain : "") +
		( (secure) ? ";secure" : "");
	document.cookie = cookieString;
}

function Delete_Cookie(name,path,domain) {
	if (Get_Cookie(name)) document.cookie = name + "=" +
		( (path) ? ";path=" + path : "") +
		( (domain) ? ";domain=" + domain : "") +
		";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function book_file (id,type) {
	if (type==1) {
		open_popup(MAIN_URL_LANG+'book_file/?d_id='+id+'&type=1','belelapozok',1000,800,0,0);
	} else if(type==2) {
		open_popup(MAIN_URL_LANG+'book_file/?d_id='+id+'&type=2','diafilm',980,680,0,0);
	} else if(type==3) {
		open_popup(MAIN_URL_LANG+'book_file/?d_id='+id+'&type=3','belehallgatok',300,100,0,0);
	} else {
		window.location=MAIN_URL_LANG+'book_file/?f_id='+id+'&type=3';
	}
}

function open_popup(pURL,name,w,h,scrollbars,resizable)
{
	if (resizable==null) resizable=1;
	if (scrollbars==null) scrollbars=1;
	var top  = ((screen.availHeight-h)/2);
	var left = ((screen.availWidth-w)/2);
	var nyit = window.open(pURL,name,'toolbar=0,location=0,directories=0,status=0,\
		menubar=0,scrollbars='+scrollbars+',resizable='+resizable+',width='+w+',height='+h+',top='+top+',left='+left);
	nyit.focus();
	return nyit;
}


function pop_kep(kep,label,w,h)
{
	var ablak=open_popup('about:blank','nagykep',w,h,0,0);
	var d=ablak.document;
	var title=(label.length>0) ? label+' - ' : '';
	d.write('<html><head><title>'+addquotes(title)+'</title>',
		'<scr'+'ipt type="text/javasc'+'ript">',
		'var NS = (navigator.appName=="Netscape")?true:false;',
		'function FitPic() {',
		   'iWidth = (NS)?window.innerWidth:document.body.clientWidth;',
		   'iHeight = (NS)?window.innerHeight:document.body.clientHeight;',
		   'iWidth = document.images[0].width - iWidth; ',
		   'if (document.images[0].height<200) {iiHeight=200} else {iiHeight=document.images[0].height}',
		   'iHeight = iiHeight - iHeight;',
		   'window.resizeBy(iWidth, iHeight); ',
		   'self.focus(); ',
		'}; ',
		'</scr'+'ipt>',
		'</head><body marginheight="0" marginwidth="0" leftmargin="0" topmargin="0">',
		'<a name="nagykep" href="#" onclick="self.close();" onblur="self.close()">',
		'<img border="0" alt="',addquotes(label),'" src="',addquotes(kep),'" onload="FitPic();"></a></body></html>',
		'<scr'+'ipt type="text/javasc'+'ript">//FitPic();</scr'+'ipt>'
	);
	//d.anchors[0].focus();

}

function pop_inner(inner,label,w,h)
{
	var ablak=open_popup('about:blank','inner_popup',w,h,1,1);
	var d=ablak.document;
	var title=(label.length>0) ? label+' - ' : '';
	d.write('<html><head><title>',title,
		'</title>\n',
		'<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">\n',
		'<link rel=stylesheet type="text/css" href="/style.css">\n</head>\n',
		'<body marginheight="0" marginwidth="0" leftmargin="0" topmargin="0">\n',
		inner,
		'\n</body>\n</html>\n');
	d.close();
}


/**
 * libriBasket addToBasket wrapper function
 *
 * @param id integer
 * @return void
 */
function addToBasket(id) {
	if (typeof(myLibriBasket) == 'object' && id) {
		myLibriBasket.addToBasket(id);
	}
}

function changePiece(formObj) {
	if (typeof(myLibriBasket) == 'object' && formObj) {
		myLibriBasket.changePiece(formObj);
	}
}

function addOne(formObj) {
	if (typeof(myLibriBasket) == 'object' && formObj) {
		myLibriBasket.addOne(formObj);
	}
}

function removeOne(formObj) {
	if (typeof(myLibriBasket) == 'object' && formObj) {
		myLibriBasket.removeOne(formObj);
	}
}

function showRegistration() {
	if (typeof(myLibriMember) == 'object') {
		myLibriMember.showRegistration();
	}
}

function bookVote(book_id, val) {
	if (typeof(myLibriMember) == 'object') {
		myLibriMember.bookVote(book_id, val);
	}
}

function showBookVote(book_id, val, type) {
	if (typeof(myLibriMember) == 'object') {
		myLibriMember.showBookVote(book_id, val, type);
	}
}

function hideBookVote(book_id, val, type) {
	if (typeof(myLibriMember) == 'object') {
		myLibriMember.hideBookVote(book_id, val, type);
	}
}

function showLoginBox() {
	if (typeof(myLibriMember) == 'object') {
		myLibriMember.showLoginBox();
	}
}

function postLogin() {
	if (typeof(myLibriMember) == 'object') {
		myLibriMember.postLogin();
	}
}

function postLogout() {
	if (typeof(myLibriMember) == 'object') {
		myLibriMember.postLogout();
	}
}

function showReminderBox() {
	if (typeof(myLibriMember) == 'object') {
		myLibriMember.showReminderBox();
	}
}

function postReminder() {
	if (typeof(myLibriMember) == 'object') {
		myLibriMember.postReminder();
	}
}

function checkNumeric(e, formObj) {
	e = (e) ? e : ((event) ? event : null);

	var keyCode = (e.which) ? e.which : e.keyCode;
	
	if (keyCode == 13 && formObj) {
		changePiece(formObj);
		return false;
	}

	if (
		(keyCode == 9 || keyCode == 8 || keyCode == 46) ||
		(keyCode >= 48 && keyCode <= 57)
	) {
		return true;
	}
	return false;
}

function fbs_click() {
    u = location.href;
    t = document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

function antikvariumSearch(args) {
	if (typeof(myLibriSearch) == 'object' && typeof(args) == 'object' && args.query) {
		myLibriSearch.antikvariumSearch(args.query);
	}
}

/**
 * Libri JavaScript library class
 * 
 */
var Libri = {
	/**
	 * Require library
	 *
	 * @param libraryName string
	 * @return void
	 */
	requireLibrary: function(libraryName) {
		document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
	},

	/**
	 * Load library
	 *
	 * @return void
	 */
	loadLibrary: function() {
		var js = /common\.js(\?.*)?$/;

		$$('head script[src]').findAll(function(s) {
			return s.src.match(js);
		}).each(function(s) {
			var path = s.src.replace(js, '')+'lib/';
			var includes = s.src.match(/\?.*load=([a-z,]*)/);

			Libri.requireLibrary(path+'browser.js');
			Libri.requireLibrary(path+'common.js');
			Libri.requireLibrary(path+'overlay.js');
			Libri.requireLibrary(path+'page.js');
			Libri.requireLibrary(path+'basket.js');
			Libri.requireLibrary(path+'member.js');

			includes = (includes) ? includes[1] : null;

			if (includes) {
				includes.split(',').each(
					function(include) {
						if (include) {
							Libri.requireLibrary(path+include+'.js');
						}
					});
				}
			});
	}
}

/**
 * Load libraries
 */
Libri.loadLibrary();