if( !window.DApi ){
	var DApi = {

		DOMAIN:( document.location.host && document.location.host.match( /\.dra\.lv/ ) ? document.location.host : 'www.draugiem.lv' ),
		anonimusN:0,
		anonimus:{},
		comp:{},

		JSON:{
			decode:function( s ){
				try{
					return !( /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( s.replace(/"(\\.|[^"\\])*"/g, '')) ) && eval( '(' + s + ')' );
				}
				catch( e ){
						return false;
				}
			}, // decode:function
			
			encode:function( v ){		
				return this[ typeof v ]( v );
			},	// encode:function
			
			// + types +
			'boolean': function (x) {return String(x);},
			'null': function (x) {return "null";},
			'undefined': function (x) {return "null";},
			number: function (x) {return isFinite(x) ? String(x) : 'null';},
			string: function (x) {
				if (/["\\\x00-\x1f]/.test(x)) {
					var $JSON = this;
					x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
						var c = $JSON.m[b];
						if (c) {
								return c;
						}
						c = b.charCodeAt();
						return '\\u00' +
								Math.floor(c / 16).toString(16) +
								(c % 16).toString(16);
					});
				}
				return '"' + x + '"';
			},
			array: function (x) {
				var a = ['['], b, i, l = x.length, v;
				for (i = 0; i < l; i += 1) {
					v = x[i];					
					if( this[typeof v] ){
						v = this[typeof v]( v );
						if (typeof v == 'string') {
								if (b) {
										a[a.length] = ',';
								}
								a[a.length] = v;
								b = true;
						}
					}
				}
				a[a.length] = ']';
				return a.join('');
			},
			object: function (x) {
				if (x) {
					if (x instanceof Array) {						
						return this.array(x);
					}
					var a = ['{'], b, i, v;
					for (i in x) {
						v = x[i];
						if( this[typeof v] ) {
							v = this[typeof v](v);
							if (typeof v == 'string') {
									if (b) {
											a[a.length] = ',';
									}
									a.push( this.string(i), ':', v );
									b = true;
							}
						}
					}
					a[ a.length ] = '}';
					return a.join('');
				}
				return 'null';
			},	
			// - types -
			
			m:{  // A character conversion map
				'\b': '\\b', '\t': '\\t',  '\n': '\\n', '\f': '\\f',
				'\r': '\\r', '"' : '\\"',  '\\': '\\\\'
			} // m
		},
		
		init:function(){
			if( this.init.ok ){
				return;
			}
			this.init.ok = true;
		},
		
		addHashEvent:function( w ){
			w = w || window;		
			if( 'onhashchange' in w ){
				if( w.addEventListener ){
					w.addEventListener( 'hashchange', DApi.hashOnchange, false );
				} else {
					w.attachEvent( 'onhashchange', DApi.hashOnchange );
				}
			} else {
				var hash = w.location.hash.replace( '#', '' );
				setInterval( function(){
					var h = w.location.hash.replace( '#', '' );
					if( hash == h ){
						return;
					}
					hash = h;
					DApi.hashOnchange.call( w );
				}, 50 );
			}
		},
		
		hashOnchange:function(){
			var h = this.location.hash.replace( '#', '' );
			var data = DApi.JSON.decode( decodeURIComponent( h ) );
			if( !data ){
				if( DApi.comp[ h.split('_')[0] ] ){
					try{
						DApi.comp[ h.split('_')[0] ].hashOnchange( h.split('_')[1] );
					} catch(e){
					}
				}
				return;
			}
			if( typeof data != 'object' || ! data.comp ){
				return;
			}
			/*
			if(	navigator.userAgent && (
					navigator.userAgent.indexOf( 'Firefox' ) != -1 ||
					//navigator.userAgent.indexOf( 'Chrome' ) != -1 ||
					//navigator.userAgent.indexOf( 'Safari' ) != -1 ||
					( navigator.vendor && navigator.vendor.indexOf( 'Safari' ) != -1 ) ||
					//navigator.userAgent.indexOf( 'Opera' ) != -1 ||
					navigator.userAgent.indexOf( 'MSIE 8.' ) != -1
			) ){
				this.history.go(-1);
			}	else {
				this.location.hash = '';
			}*/
			try{
				DApi.comp[ data.comp ].hashOnchange(data);
			} catch(e){
			}
		},

		sendParent:function( data, frname ){
			/*
			if(!frname){
				var get = new DApi.Get();
				frname = get.v('dapi');
			}
			var w = frames[frname];
			*/
			var w = document.getElementById('tmp').contentWindow;
			try{
				w.location.hash = encodeURIComponent( DApi.JSON.encode(data) );
			} catch(e){
				return false;
			}
			return true;
		}
		
	};


	// +++ Get
	DApi.Get = function( url ){
		this.__ = '';
		switch( typeof url ){
			case 'object':
				this._ = url;
				return;
			case 'undefined':
				url = document.location.href;
				break;
		}
		this._ = {};
		if( url ){
			var a = url.split( '?' );
			this.__ = a[0];
			if( a[ 1 ] ){
				a = a[ 1 ].split( '&' );
				var b;
				for( var i = 0; i < a.length; i ++ ){
					b = a[ i ].split( '=' );
					this._[ decodeURIComponent( b[0] ).replace( /\+/g, ' ' ) ] = ( b[1] ? decodeURIComponent( b[1] ).replace( /\+/g, ' ' ) : '' );
				}
			}
		}
	};
	DApi.Get.prototype.toStr = function(){
		return this.toStrItem( '', '', this._ );
	};
	DApi.Get.prototype.toStrItem = function( prefix, k, v ){
		prefix = ( prefix ? prefix + '[' + encodeURIComponent( k ) + ']' : encodeURIComponent( k ) );
		if( typeof v == 'object' ){
			var re = [];
			for( var k2 in v ) re.push( this.toStrItem( prefix, k2, v[ k2 ] ) );
			return re.join( '&' );
		} else {
			return prefix + ( v !== '' ? '=' + encodeURIComponent( v ) : '' );
		}
	};
	DApi.Get.prototype.str = function(){
		return this.toStr();
	};
	DApi.Get.prototype.toString = function(){
		return this.toStr();
	};
	DApi.Get.prototype.toUrl = function( k ){
		return this.__ + '?' + this.toStr();
	};
	DApi.Get.prototype.add = function( k, v ){
		this._[ k ] = ( v ? v : '' );
		return this;
	};
	DApi.Get.prototype.remove = function( k ){
		delete this._[ k ];
		return this;
	};
	DApi.Get.prototype.v = function( k ){
		return ( typeof this._[k] == 'undefined' ? null : this._[k] );
	};
	// --- Get

	// +++ biz fans +++
	/*
		par = {
			name:string, // profila exturl
			count:int // sekotāju skaits
		}
	*/
	DApi.BizFans = function( par ){
		par = par || {};
		par.showFans = typeof(par.showFans) != "undefined" && par.showFans;
		this.par = par;
		if( ! par.name ){
			return;
		}
		this.src = 'http://' + DApi.DOMAIN + '/business/ext/fans/?name=' + encodeURIComponent( par.name ) + '&inst=' + this.n + '&parent=' + encodeURIComponent( document.location.href.split('#')[0] );
		if(par.showFans) {
			DApi.BizFans.showFans = 1;
			this.src += '&showFans=1';
			if( par.count ){
				this.src += '&count=' + par.count;
			}
		} else {
			DApi.BizFans.showFans = 0;
		}
		if(par.showSay) {
			DApi.BizFans.showSay = 1;
			this.src += '&showSay=1';
			if( par.saycount ){
				this.src += '&saycount=' + par.saycount;
			}
		}
		else {
			DApi.BizFans.showSay = false;
		}
		this.node = document.createElement( 'div' );
		this.node.innerHTML = '<iframe src="' + this.src + '" frameborder="0" width="100%"></iframe>';
		this.iframe = this.node.firstChild;
	};

	DApi.BizFans.prototype = {
		
		append:function( node ){
			if( typeof node == 'string' ){
				node = document.getElementById( node );
			}
			if( ! node ){
				return;
			}
			node.appendChild( this.node );
			if( this.iframe.offsetWidth ){
				var rows = this.par.count / ( ( this.iframe.offsetWidth - 20 ) / 59 );
				rows = Math.ceil(rows);
				var height = 179;
				if(DApi.BizFans.showFans) {
					height = height + rows * 59;
				}
				if(DApi.BizFans.showSay) {
					height = height + 249;
				}
				this.iframe.style.height = height + 'px';

			}
		}
		
	};
	// --- biz fans ---


	// +++ events +++
	DApi.EvFans = function( par ){
		par = par || {};
		par.showFans = par.showFans || 1;
		this.par = par;
		if( ! par.name ){
			return;
		}

		this.src = 'http://' + DApi.DOMAIN + '/events/ext/?name=' + encodeURIComponent( par.name ) + '&inst=' + this.n + '&parent=' + encodeURIComponent( document.location.href.split('#')[0] );
		if( par.count ){
			this.src += '&count=' + par.count;
		}
		this.node = document.createElement( 'div' );
		this.node.innerHTML = '<iframe src="' + this.src + '" frameborder="0" width="100%"></iframe>';
		this.iframe = this.node.firstChild;
	};

	DApi.EvFans.prototype = {
		
		append:function( node ){
			if( typeof node == 'string' ){
				node = document.getElementById( node );
			}
			if( ! node ){
				return;
			}
			node.appendChild( this.node );
			if( this.iframe.offsetWidth ){
				var rows = this.par.count / ( ( this.iframe.offsetWidth - 20 ) / 59 );
				rows = Math.ceil(rows);
				var height = 182;
				height = height + rows * 59;
				this.iframe.style.height = height + 'px';
			}
		}
		
	};

	// --- events ---


	// +++ say recommend +++
	/*
		par = {
			link:[string], // defaultā tekošā lapa document.location.href
			title:[string], // defaultā document.title
			titlePrefix:[string] // defaultā domēns
			callback:[string] // redirekt url
		};
	*/
	DApi.Like = function( par ){
		par = par || {};
		this.link = par.link || document.location.href;
		this.title = par.title || document.title;
		if( par.titlePrefix ){
			this.titlePrefix = par.titlePrefix;
		} else {
			var ha = document.location.host.split( '.' );
			if( ha.length == 3 ){
				this.titlePrefix = ha.splice(1).join('.');
			} else {
				this.titlePrefix = document.location.host;
			}
		}
		if( par.callback ){
			this.callback = par.callback;
		}
		this.layout = '';
		this.width = '84px';
		this.height = '20px';
		if( par.layout == 'bubble' ){
			this.layout = par.layout;
			this.width = '55px';
			this.height = '62px';
		}
	};

	DApi.Like.prototype = {

		append:function( node ){
			this.src = 'http://' + DApi.DOMAIN + '/say/ext/like.php?url=' + encodeURIComponent( this.link ) +
				'&title=' + encodeURIComponent( this.title ) +
				'&titlePrefix=' + encodeURIComponent( this.titlePrefix ) +
				'&parent=' + encodeURIComponent( document.location.href.split('#')[0] );
			if( this.callback ){
				this.src += '&callback=' + encodeURIComponent( this.callback );
			}
			if( this.layout.length ){
				this.src += '&layout=' + this.layout;
			}
			this.node = document.createElement( 'iframe' );
			with( this.node ){
				src = this.src;
				frameBorder = '0';
				style.width = this.width;
				style.height = this.height;
			}
			if( typeof node == 'string' ){
				node = document.getElementById( node );
			}
			if( ! node ){
				return;
			}
			node.appendChild( this.node );
		},

		getCount:function( callback ){
			var n = ++ DApi.anonimusN;
			DApi.anonimus[n] = function(par){
				callback(par);
				delete DApi.anonimus[n];
			};
			var script = document.createElement('script');
			script.type = 'text/javascript';
			script.src = 'http://www.draugiem.lv/say/ext/like_count.php?url=' + encodeURIComponent( this.link ) + '&callback=' + encodeURIComponent( 'DApi.anonimus[' + String(n) + ']' ) + '&' + Math.random();
			document.getElementsByTagName('head')[0].appendChild(script);
			return true;
		}

	};
	// --- say recommend ---

	// +++ evnets +++
	DApi.Events = function( id ){
		this.id = id;
		this.n = ++ DApi.Events.instN;
		DApi.comp[ 'Events' + this.n ] = this;
		if( DApi.Events.instN == 1 ){
			DApi.addHashEvent();
		}
	};

	DApi.Events.instN = 0;

	DApi.Events.prototype = {

		append:function( node ){
			var get = new DApi.Get( {
				ev:this.id,
				n:this.n,
				reff:document.location.href.split('#')[0],
				title:document.title
			} );
			this.src = 'http://' + DApi.DOMAIN + '/events/ext/fancount.php?' + get.str();
			this.src2 = 'http://' + DApi.DOMAIN + '/events/ext/participiants.php?' + get.str();
			this.node = document.createElement( 'div' );
			with( this.node.style ){
				position = 'relative';
				width = '100px';
				height = '20px';
			}
			this.button = document.createElement( 'iframe' );
			this.node.appendChild( this.button );
			with( this.button ){
				src = this.src;
				frameBorder = '0';
				style.width = '100px';
				style.height = '20px';
			}
			this.userbox = document.createElement( 'iframe' );
			this.node.appendChild( this.userbox );
			with( this.userbox ){
				frameBorder = '0';
				style.width = '227px';
				style.height = '135px';
				style.position = 'absolute';
				style.left = '0px';
				style.backgroundColor = '#ffffff';
				style.top = '100%';
				style.display = 'none';
				style.zIndex = 1;
			}
			if( typeof node == 'string' ){
				node = document.getElementById( node );
			}
			if( !node ){
				return;
			}
			node.appendChild( this.node );
		},

		hashOnchange:function( data ){
			clearTimeout( this.timeout );
			if( data == 1 ){
				if( !this.userbox._dr ){
					this.userbox.src = this.src2;
					this.userbox._dr = true;
				}
				this.userbox.style.display = '';
				return;
			}
			$event = this;
			this.timeout = setTimeout( function(){
				$event.userbox.style.display = 'none';
			}, 500 );
		}
	};
	// --- evnets ---
}
