;	(function($){


$.volux = {

	window: function() {return $(window)}

,	html: function() {return $('html')}

,	body: function() {return $('body')}

,	waitor: function(){return $('#waitor')}

,	go:
		function () {
			$.volux.html().attr('id', 'js')
		;	if ($('#waitor').length = 0 ) {
				$.volux.body().append('<div id="waitor" class="ui-state-active"><span>?</span></div>')
			}
		;	$.volux.waitor().waitor()
		
		;	$.getScript($.volux.data.jsHost+'plugin/xLazyLoader.js', function(){

				$.xLazyLoader({
					js: [
						$.volux.data.jsHost+'volux/debug.js'
					]
				,	css: [
						$.volux.data.cssHost+'debug.css'
					]
				,	name: 'debug'
				,	load: function(){
						if ($.volux.data.sessionLogMessage !== '') {
							$.volux.alert($.volux.data.sessionLogMessage)
						}
					;	if ($.volux.timers) {
							$.each($.volux.timers, function(key, val){
								if (key !== 'last') {
									$.log(key+': '+val)
								}
							})
						;	$.volux.timers = {}
						}
					;	$.volux.logic.go()
					}
				})
			})
		}

,	infobox:
		function(head, message, h, w) {
			if (!$('#infobox').length) {
				$.volux.body().append('<div id="infobox" title="Title">Info</div>')
			}
		;	$.xLazyLoader('destroy','dialog');
		;	$.xLazyLoader({
				css: $.volux.data.cssHost + 'ui/'+$.volux.data.uiVersion+'/'+$.volux.data.uiTheme+'/jquery-ui.css'
			,	js:  $.volux.data.jsHost+'ui/'+$.volux.data.uiVersion+'.min.js'
			,	name: 'dialog'
			,	load: function(){
					$('#infobox')
						.attr({title:head}).html(message)
						.dialog({
							modal: false
						,	resizable: true
						,	height: (typeof(h) == 'undefined')? parseInt($.volux.calc.view_height()/2) : h
						,	width: (typeof(w) == 'undefined')? parseInt($.volux.calc.view_width()/2) : w
						,	close: function() {
								$.volux.dialog.close('#infobox')
							}
						})
				}
			})
		}

,	ask:
		function(message, callback, h, w) {
			if (!$('#ask').length) {
				$.volux.body().append('<div id="ask" title="Вопрос"><p></p></div>')
			}
		;	$.xLazyLoader('destroy','dialog');
		;	$.xLazyLoader({
				css: $.volux.data.cssHost + 'ui/'+$.volux.data.uiVersion+'/'+$.volux.data.uiTheme+'/jquery-ui.css'
			,	js:  $.volux.data.jsHost+'ui/'+$.volux.data.uiVersion+'.min.js'
			,	name: 'dialog'
			,	load: function(){
					$('#ask')
						.find('p')
							.html(message)
						.end()
						.dialog({
							modal: true
						,	resizable: false
						,	height: (typeof(h) == 'undefined')? parseInt($.volux.calc.view_height()/3) : h
						,	width: (typeof(w) == 'undefined')? parseInt($.volux.calc.view_width()/3) : w
						,	close: function() {
								$.volux.dialog.close('#ask')
							}
						,	buttons: {
								'Yes': function() {
									callback()
								;	$.volux.dialog.close('#ask')
								}
							,	'No': function() {
									$.volux.dialog.close('#ask')
								}
							}
						})
				}
			})
		}

,	input:
		function(message, callback, h, w) {
			if (!$('#value').length) {
				$.volux.body().append('<div id="value" title="'+message+'"><input class="input"/></div>')
			}
		;	$.xLazyLoader('destroy','dialog');
		;	$.xLazyLoader({
				css: $.volux.data.cssHost + 'ui/'+$.volux.data.uiVersion+'/'+$.volux.data.uiTheme+'/jquery-ui.css'
			,	js:  $.volux.data.jsHost+'ui/'+$.volux.data.uiVersion+'.min.js'
			,	name: 'dialog'
			,	load: function(){
					$('#value')
						.dialog({
							modal: true
						,	resizable: false
						,	height: (typeof(h) == 'undefined')? parseInt($.volux.calc.view_height()/3) : h
						,	width: (typeof(w) == 'undefined')? parseInt($.volux.calc.view_width()/3) : w
						,	close: function() {
								$.volux.dialog.close('#value')
							}
						,	buttons: {
								'Ok': function() {
									v = $('#value').find('input').val()
								;	callback(v)
								;	$.volux.dialog.close('#value')
								}
							,	'No': function() {
									callback(false)
								;	$.volux.dialog.close('#value')
								}
							}
						})
						.find('input')
							.keydown(function(event) {
								if (event.keyCode == '13') {
									v = $('#value').find('input').val()
								;	callback(v)
								;	$.volux.dialog.close('#value')
								}
							})
						.end()
				}
			})
		}

,	alert:
		function(message, h, w) {
			if (!$('#alert').length) {
				$.volux.body().append('<div id="alert" title="Внимание!"><p></p></div>')
			}
		;	$.xLazyLoader('destroy','dialog');
		;	$.xLazyLoader({
				css: $.volux.data.cssHost + 'ui/'+$.volux.data.uiVersion+'/'+$.volux.data.uiTheme+'/jquery-ui.css'
			,	js:  $.volux.data.jsHost+'ui/'+$.volux.data.uiVersion+'.min.js'
			,	name: 'dialog'
			,	load: function(){
					$('#alert')
						.find('p')
							.html(message)
						.end()
						.dialog({
							modal: true
						,	resizable: false
						,	close: function() {
								$.volux.dialog.close('#alert')
							}
						,	height: (typeof(h) == 'undefined')? parseInt($.volux.calc.view_height()/3) : h
						,	width: (typeof(w) == 'undefined')? parseInt($.volux.calc.view_width()/3) : w
						,	buttons: {
								'Ok': function() {
									$.volux.dialog.close('#alert')
								}
							}
						})
				}
			})
		}

,	blur:
		function() {
			$('a')
				.live(
					'focus'
				,	function() {
						this.blur()
					}
				)
		}

,	clearForId:
		function(str) {
			var s = new String(str)
		;	return s.replace('\/', '_').replace('-', '_').replace('\.', '_')
		}


,	encodeString:
		function(s) {
			return escape(s).replace(new RegExp('\\+','g'), '%2B')
		}
,	first:
		function(s) {
			return $.trim(s.split("\n")[0])	
		}

,	isEmpty:
		function(str) {
			return (!/\S/.test(str));
		}

}

;	$.volux.load = {
		images: // $.volux.load.images("image1.gif", "/path/to/image2.png", "some/image3.jpg")
			function() {
				for(var i = 0; i < arguments.length; i++) {
					$('<img />').attr('src', arguments[i])
				}
			}

	,	themeSwitcher:
			function() {
				$.getScript(
					'http://jqueryui.com/themeroller/themeswitchertool/'
				,	function(){
						$.volux.body()
							.append('<div id="themeswitcher"></div>')
							.find('#themeswitcher')
								.themeswitcher({
									initialText: 'Темы оформления'
								,	buttonPreText: 'Тема: '
								,	buttonHeight: 12
								})
					}
				)
			}
		
	,	token:
			function(id, url) {
				if (!$(id).is('form')) {
					$.volux.alert('Не применимо! Нужна форма.')
				} else {
					if (typeof(url) === 'undefined') {
						url = '/token'
					}
				;	$.get(
						url
					,	function(txt){
							$(id).append('<input type="hidden" name="token" value="'+txt+'" />')
						}
					)
				}
			;	return $(id)
			}

	,	login:
			function() {
				$.log('Load resources.')
			;	if ($.volux.login) {
					$.log('Exist, open form...')
				;	$.volux.login.load()
				;	return true
				}
			;	$.xLazyLoader('destroy','dialog')
			;	$.xLazyLoader({
					css: [
						$.volux.data.cssHost + 'ui/'+$.volux.data.uiVersion+'/'+$.volux.data.uiTheme+'/jquery-ui.css'
					,	$.volux.data.cssHost + 'login.css'
					]
				,	js: [
						$.volux.data.jsHost+'ui/'+$.volux.data.uiVersion+'.min.js'
					,	$.volux.data.jsHost+'volux/login.js'
					,	$.volux.data.jsHost+'md5.js'
					]
				,	name: 'dialog'
				,	load: function(){
						$.log('Resources loaded, open form...')
					;	$.volux.login.load()
					}
				})
			}
	}

;	$.volux.dialog = {

		close:
			function(id) {
				$(id)
					.dialog('destroy')
					.remove()
			;	$.log('div '+id+' remove from body')
			}

	}

;	$.volux.location = {
		out:
			function(){
				$.volux.ask(
					'Вы уверены?'
				,	function() {
						location.href = '/logout'
					}
				)
			}

	,	home:
			function() {
				location.href = '/'
			}
	}
	
;	$.volux.logic = {
		go:
			function() {
			}
	}

;	$.volux.calc = {
		view_height: function() {
			clientHeight = 0
		;	if (window.innerHeight) {
				clientHeight = window.innerHeight
			} else 
			if (document.body && document.body.offsetHeight) {
				clientHeight = document.body.offsetHeight
			}
		;	return clientHeight 
		}
	,	view_width: function() {
			clientWidth = 0
		;	if (window.innerWidth) {
				clientWidth = window.innerWidth
			} else 
			if (document.body && document.body.offsetWidth) {
				clientWidth = document.body.offsetWidth
			}
		;	return clientWidth 
		}
	}

;	$.volux.complete = {
		cache: {}
	}

// @example $('#nav').hoverClass('hover')
;	$.fn.hoverClass = function(cssClass) {

		return this.each(function(){
			$(this).hover(
				function(){ $(this).addClass(cssClass) }
			,	function(){ $(this).removeClass(cssClass) }
			)
		})
	}
	
;	$.fn.waitor = function(options) {
	
		var defaults = {
			wait: 'wait'
		,	walk: 'walk'
		}
	;	var options = $.extend(defaults, options)
	
	;	return this.each(function() {
	
			$(this)
				.bind(
					options.wait
				,	function() {
						$(this).show()
					;	$.volux.body().addClass(options.wait)
					}
				)
				.bind(
					options.walk
				,	function() {
						$(this).hide()
					;	$.volux.body().removeClass(options.wait)
					}
				)
				.ajaxStart(function() {$(this).trigger(options.wait)})
				.ajaxStop(function() {$(this).trigger(options.walk)})
				.trigger(options.wait)
		})
	}

})(jQuery);
