function xor(a,b){
	return (!a && b) || (a && !b);
}

var $$,core;
core=$$=new function(){
	var config={
		fixPng:			false,
		fixPngString:	''
	}
	
	var current={
		host:			''
	}
	
	this.config={
		host:			'',
		speed:			200,
		hiddenFields:	new Object
	}

	this.onLoad=function(){
		defineHost(this);
		fixPng();
		handleAntispam();
		if(typeof($$$)!='undefined' && typeof($$$.onLoad)=='function'){
			$$$.onLoad();
		}
	}
	
	var defineHost=function($$){
		current.host=document.location.href.match(/^http:\/\/[^\/]+\//)[0];
		if($$.config.host){
			current.host=$$.config.host;
		}
		if(typeof($$$)!='undefined' && typeof($$$.config)!='undefined' && typeof($$$.config.host)!='undefined' && $$$.config.host){
			current.host=$$$.config.host;
		}
	}
	
	this.fixPng=function(fixPngString){
		if(typeof(defineFixPng)=='undefined'){
			alert('defineFixPng() is undefined');
			return;
		}
		defineFixPng();
		config.fixPng=true;
		config.fixPngString=fixPngString;
	}
	
	var fixPng=function(){
		if(config.fixPng && typeof(DD_belatedPNG)!='undefined'){
			DD_belatedPNG.fix(config.fixPngString);
		};
	}
	
	this.setConfig=function(newConfig){
		for(key in newConfig){
			if(typeof(config[key])==typeof(newConfig[key])){
				config[key]=newConfig[key];
			}
		}
	}
	
	this.getSpeed=function(){
		return config.speed;
	}
	
	var hexToString=function(hex){
		var s='';
		for(i=0;i<hex.length;i+=4){
			s+=unescape('%u'+hex.substr(i,4));
		}
		return s;
	}
	
	this.decryptForms=function(){
		$('form').each(function(){
			var key;
			if(!(key=$(this).find('input[name="DEFENCE_KEY"]').val()))
				return;
			key=key.split(':');
			if(key.length<2)
				return;
			switch(key[0]){
				case 'AES':
					$(this).find('input[name="FORM_TYPE"]').val(Aes.Ctr.decrypt(
						$(this).find('input[name="FORM_TYPE"]').val(),
						key[1],
						128
					));
					$(this).find('input[name="DEFENCE_KEY"]').val('');
					break;
				default:
					break;
			}
		});
	}
	
	//Обработка форм, сформированных ядром
	this.handleForms=function(success){
		if(typeof(formConstraintKey)!='undefined')formConstraintKey.onLoad();
		$$.decryptForms();
		if(typeof(success)!='function'){
			success=$$.handleForms;
		}
		$('form.ajax_form:not(.handled)').addClass('handled').each(function(){
			$(this).ajaxForm({
				target:			$(this).parent(),
				beforeSubmit:	function(){
					$(this).find('input[type="submit"]').attr('disabled','disabled');
				},
				success:		success
			});
			$(this).submit(function(){
				$('input[type="submit"]',this).val('Пожалуйста, подождите...').get(0).disabled=true;
			});
		});
	}
	
	//Склонение по падежам от кол-ва
	this.qtDecline=function(qt,nominative,genitive,dative){
		if(typeof(dative)=='undefined'){
			if(qt==1)
				return nominative;
			else
				return genitive;
		}
		else {
			qt=parseInt(qt);
			var unit=qt%10;
			var ten=(qt%100-unit)/10;
			if(ten!=1){
				if(unit==1)
					return nominative;
				else if(unit>1 && unit<5)
					return genitive;
			}
			return dative;
		}
	}
	
	//Перевод объекта в массив
	this.object2Array=function(o){
		var a=new Array();
		for(key in o){
			a[key]=o[key];
		}
		return a;
	}
	
	this.camel2JS=function(o){
		var newO=new Object;
		var newKey;
		for(key in o){
			newO[(key.charAt(0).toLowerCase()+key.substr(1)).replace(/ID$/,'Id')]=o[key];
		}
		return newO;
	}
	
	var handleAntispam=function(){
		if(!current.host)
			return;
		$('a.antispam').antispam(current.host);
	}
}

$.extend({
	//Скролл в топ
	scrollTop:	function(speed){
		$("html:not(:animated)"+(!$.browser.opera?",body:not(:animated)":"")).animate(
			{scrollTop:'0px'},
			speed,
			'swing'
		);
	}
});

$.fn.extend({
	antispam:	function(key){
		var mail;
		if(typeof(Aes)=='undefined'){
			mail='No Aes';
		}
		else {
			mail=Aes.Ctr.decrypt($(this).text(),key,128);
		}
		$(this).replaceWith( mail );
	},
	fadeInIE:	function(){
		
	}
});

$(document).ready(function(){
	if(typeof($$.onLoad)!='function')
		core.onLoad();
	else
		$$.onLoad();
});
