var Say = {
	rpc:new RPC( '/say/rq/app.php' ),
	
	MAX_POST_LENGTH:140,

	formRemainingChars:function( el, formId, event ){
		if( event && event.ctrlKey && ( event.keyCode == 13 || event.keyCode == 10 ) ){
			var node = document.getElementById( 'sayForm' + formId );
			if( node.onsubmit() ){
				node.submit();
			}
		}
		var node = document.getElementById( 'sayRemainingChars' + formId );
		var c = Say.MAX_POST_LENGTH - el.value.length
		node.style.fontWeight = ( c < 20 ? 'bold' : '' );
		node.style.color = ( c < 0 ? '#f00' : '' );
		node.style.fontSize = ( c < 0 ? '17px' : '' );
		node.innerHTML = c;
	},
	
	checkAddPostForm:function( formId ){
		var node = document.getElementById( 'sayRemaining' + formId );
		var textArea = document.getElementById( 'sayAddPostText' + formId );
		if( textArea.value.length == 0 || textArea.value.length > Say.MAX_POST_LENGTH || textArea.className.indexOf( 'beforeOnclick' ) != -1 ){
			blink( {
				node:node
			} );
			return false;
		}
		return true;
	},
	
	ignore:function( par ){
		InfoBox.open( '/say/rq/ignore.php?uid=' + par.uid + '&uType=' + par.uType, { width:300, height:150 } );
		return false;
	},
	
	doIgnore:function( par ){
		var get = new Get( '' );
		get.add( 'addIgnoreUid', par.uid ).add( 'addIgnoreUType', par.uType );
		get.add( 'redir', document.location.href.split( '#' )[0] );
		document.location.href = '/say/?' + get.str();
	},
	
	addComment:function( par ){
		var id1 = 'sayAddCommentUnderPost' + String( par.pid2 );
		var id2 = 'sayAddCommentUnderPostRq' + String( par.pid2 );		
		var node = document.getElementById( id1 );
		if( node.style.display == '' ){
			node.style.display = 'none';
			return false;
		}
		node.style.display = '';
		rq( '/say/rq/add_comment.php?pid=' + par.pid + '&pid2=' + par.pid2, id2 );
		return false;
	},
	
	linkCheckUser:function( el ){
		if( empty( id ) ){
			Say.onLogin = function(){
				document.location.href = el.href;
			};
			return Say.showLogin();
		}
	},
	
	watch:function( par ){
		var get = new Get( '' ); // document.location.href
		get.add( 'addWatchUid', par.uid ).add( 'addWatchUType', par.uType ).add( 'DS', sessid );
		get.add( 'uid', par.uid ).add( 'uType', par.uType ).add( 'selectedTab', 4 );
		document.location.href = '/say/?' + get.str();
		return false;
	},
	
	addPost2onSubmit:function( el, formId, ajaxUrl, ajaxId ){		
		/*
		el.target = 'sayIframe_' + formId;
		var actionArr = el.action.split( '?' );		
		el.action = actionArr[ 0 ] + '?' + ( actionArr[ 1 ] ? actionArr[ 1 ] : '' ) + '&die';*/
		var node = document.getElementById( 'sayRemaining' + formId );
		var textArea = document.getElementById( 'sayAddPostText' + formId );
		if( textArea.value.length == 0 || textArea.value.length > Say.MAX_POST_LENGTH || textArea.className.indexOf( 'beforeOnclick' ) != -1 ){
			blink( {
				node:node
			} );
			return false;
		}
		D.ajaxPost( el, ajaxUrl, ajaxId );
		return false;
	},
	
	addPost2iframeOnload:function( el, par ){
		if( el.contentWindow && el.contentWindow.document ){
			if( el.contentWindow.document.location.href.indexOf( '/say/' ) != -1 ){
				rq( par.onDoneLoad, el.parentNode );
			}
		}
	},
	
	profileTab:function( tab ){
		rq( '/say/profile.php?sayTab=' + String( tab ), 'say' );
		return false;
	},
	
	beforeOnclick:function( el ){
		el.onfocus = function(){};
		removeClassName( el, 'beforeOnclick' ); 
		el.value = '';
	},
	
	showSettings:function(){
		InfoBox.open( '/say/rq/settings.php', { width:300, height:200 } );
		return false; 
	},
	
	saveSettings:function( el ){
		el.disabled = true;
		Say.rpc.send(
			'saveSettings',
			{
				settings:{
					with_comments:Say.elByIdIsChecked( 'say_set_without_comments' ),
					with_recommends:Say.elByIdIsChecked( 'say_set_without_rec' ),
					hide_event_new_watcher:Say.elByIdIsChecked( 'say_set_hide_event_new_watcher' ),
					hide_event_new_comment:Say.elByIdIsChecked( 'say_set_hide_event_new_comment' )
				}
			},
			function( re, par ){
				D.reload();
			}
		);		
	},
	
	elByIdIsChecked:function( id ){
		var node = document.getElementById( id );
		if( ! node ) return false;
		return node.checked;
	},
	
	checkExternalUrl:function( el ){
		var node = document.getElementById( 'sayAddExternal_path' );
		var url = el.value.replace( /[^A-Za-z0-9._-]/g, '' ).toLowerCase();
		node.innerHTML = encodeURIComponent( url );
		node.style.color = '';
		if( Say.checkExternalUrl.timeout ) clearTimeout( Say.checkExternalUrl.timeout );		
		if( empty( url ) ) return false;
		Say.checkExternalUrl.timeout = setTimeout( function(){
			Say.rpc.send(
				'checkExternalUrl',
				{
					url:url
				},
				function( re ){
					node.style.color = ( re.exists ? '#C00' : '#0A0' );
				}
			)
		}, 1000 );
	},
	
	disableAddExternal:function( el ){
		document.getElementById( 'sayAddExternal_name' ).disabled = el.checked;
		document.getElementById( 'sayAddExternal_about' ).disabled = el.checked;
		document.getElementById( 'sayAddExternal_img' ).style.display = ( el.checked ? 'none' : '' );
		document.getElementById( 'sayAddExternal_users' ).style.display = ( el.checked ? 'none' : '' );
		document.getElementById( 'sayAddExternal_usersCapt' ).style.display = ( el.checked ? 'none' : '' );
	},
	
	imgOnchange:function( el ){
		var a = el.value.split( '/' );
		a = a[ a.length - 1 ].split( '\\' );
		document.getElementById( 'sayAddExternal_imgName' ).innerHTML = a[ a.length - 1 ];
	},
	
	sayEditExternalSubmit:function( form ){
		var node = document.getElementById( 'sayAddExternal_members' );
		if( node && Say.ac ) node.value = Say.ac.value().join( ',' );
		var node = document.getElementById( 'sayAddExternal_rules' );
		if( node && ! node.checked ){
			blink( {
				node:document.getElementById( 'sayAddExternal_rulesCapt' )
			} );
			return false;
		}
		var node = document.getElementById( 'sayAddExternal_private' );
		if( node && node.checked ){
			return;
		}
		var node2 = document.getElementById( 'sayAddExternal_name' );
		if( empty( node2.value ) ){
			blink( {
				node:node2
			} );
			return false;
		}
		var node2 = document.getElementById( 'sayAddExternal_about' );
		if( empty( node2.value ) ){
			blink( {
				node:node2
			} );
			return false;
		}
	},
	
	showLogin:function(){
		InfoBox.iframe( '/say/login.php', { width:600, height:500 } );
		return false;
	},
	
	onChangePoster:function( el, formId ){
		var node = document.getElementById( 'sayLeftCol' );
		/*
		if( node ){
			if( empty( el.value ) ){
				rq( '/say/rq/fr_posts.php', node );
			} else {
				var u = el.value.split( '_' );
				rq( '/say/rq/user_posts.php?uType=' + u[ 0 ] + '&uid=' + u[ 1 ], node );
			}
		} else {*/
			if( empty( el.value ) ){
				document.location.href = '/say/?selectedTab=3';
			} else {
				var u = el.value.split( '_' );
				document.location.href = '/say/?selectedTab=4&uType=' + u[ 0 ] + '&uid=' + u[ 1 ];
			}
		//}
	},
	
	rules:function(){
		InfoBox.open( '/say/rq/external_rules.php', {} );
		return false;
	},
	
	addExtPostOnkeyup:function( el ){
		var node = document.getElementById( 'SayAddExtPostChars' );
		node.innerHTML = Say.MAX_POST_LENGTH - el.value.length;
	},
	
	windowInfoBoxClose:function(){
		if( window.parent !== window && window.parent.InfoBox ){
			window.parent.InfoBox.close();
		}
		window.close();
		return false;
	},
	
	scrollIntoView:function( id ){
		var node = document.getElementById( id );
		$( '#outermost' ).animate( {
			scrollTop:( node ? node.offsetTop - 40 : 0 )
		} );
	},
	
	recommend:function( par ){
		var resp = function( re ){
			var node = document.getElementById( 'sayRecommend' + par.pid );
			if( ! node ){
				return;
			}
			var parent = node.parentNode;
			removeNode( node );
			mkE( {
				tag:'img',
				prop:{
					src:pimg + 'img/g/star_on_sm.png'
				}
			} ).append( parent );
			mkE( {
				tag:'span',
				text:' ' + re.suggestions_count
			} ).append( parent );
		};
		if( Say.extProfiles && ( ! empty( Say.extProfiles[ 1 ] ) || ! empty( Say.extProfiles[ 2 ] ) ) ){
			var node = mkE( {
				tag:'div',
				className:'sayRecommendMenu',
				prop:{
					style:{
						top:D.mouseY - 10 + 'px',
						left:D.mouseX - 10 + 'px'
					},
					onmouseout:function(){
						this.style.display = 'none';
					},
					onmouseover:function(){
						this.style.display = '';
					}
				}
			} ).append( document.body );
			var get = new Get( '' );			
			for( var i = 0; i < 6; i ++ ){
				if( Say.extProfiles[ i ] ){
					for( var k in Say.extProfiles[ i ] ){
						mkE( {
							tag:'a',
							prop:{
								href:'#',
								_drSayExternal:i + '_' + Say.extProfiles[ i ][ k ].uid,								
								onclick:function(){
									node.remove();
									Say.rpc.send(
										'recommend',
										{
											pid:par.pid,
											external:this._drSayExternal
										},
										resp
									);
									return false;
								}, // onclick
								innerHTML:Say.extProfiles[ i ][ k ].name
							}
						} ).append( node );
					}
				}
			}
			return false;
		}
		Say.rpc.send(
			'recommend',
			{
				pid:par.pid
			},
			resp
		);
		return false;
	}
};

var Get = function( url ){
  this.__ = '';      
  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] ) ] = ( b[1] ? decodeURIComponent( b[1] ) : '' );            
      } 
    }
  }
};
Get.prototype.toStr = function(){
  return this.toStrItem( '', '', this._ );
};
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 ) : '' );
	}
};
Get.prototype.str = function(){
	return this.toStr();
};
Get.prototype.toUrl = function( k ){
  return this.__ + '?' + this.toStr();
};
Get.prototype.add = function( k, v ){
  this._[ k ] = ( v ? v : '' );
  return this;
};
Get.prototype.remove = function( k ){
  delete this._[ k ];
  return this;
};
Get.prototype.v = function( k ){
	return ( this._[ k ] ? this._[ k ] : false )
};


function blink( par ){  
  if( par.node.fx_blink ) return;
  if( ! par.color1 ) par.color1 = '';
  if( ! par.color2 ) par.color2 = '#F00';
  if( ! par.sk ) par.sk = 2;
  var sk = 0;
  par.node.style.background = par.color1;
  par.node.fx_blink = true;
  function blink_(){  
    setTimeout( function(){      
      if( ! par.type || par.type == 'background' ) par.node.style.background = par.color2;
      if( par.type == 'border' ) par.node.style.borderColor = par.color2;
			if( par.type == 'className' ) addClassName( par.node, par.className || 'blink' );
      setTimeout( function(){
        sk ++;      
        if( ! par.type || par.type == 'background' ) par.node.style.background = par.color1;
        if( par.type == 'border' ) par.node.style.borderColor = par.color1;
				if( par.type == 'className' ) removeClassName( par.node, par.className || 'blink' );
        if( sk < par.sk ) blink_(); else par.node.fx_blink = false;       
      }, 100 );
    }, 80 );
  }
  blink_();
} // function blink
