var arrStdPopupFeatures = [
	['menubar', 'no'],
	['toolbar', 'no'],
	['resizable', 'yes'],
	['scrollbars', 'yes'],
	['status', 'yes']
]; 

function getPopupSizeRelatedFeatures( iWinW, iWinH, strUserFeatures ) {
	var strSizeRelatedFeatures = '';
	var iWinWidth = ( iWinW )? iWinW : 540;
	var iWinHeight = ( iWinH )? iWinH : 600;
	if ( screen ) {
		var iScrWidth = ( screen.width )? screen.width : 0;
		var iScrHeight = ( screen.height )? screen.height : 0;
		var bNeedScroll = false;
		if ( iScrWidth < iWinWidth + 50 ) { bNeedScroll = true; iWinWidth = iScrWidth - 50; }
		if ( iScrHeight < iWinHeight + 100 ) { bNeedScroll = true; iWinHeight = iScrHeight - 100; }
		if ( !(strUserFeatures && strUserFeatures.indexOf('scrollbars') >= 0 ) ) {
			strSizeRelatedFeatures += ( bNeedScroll )? ',scrollbars=yes' : ',scrollbars=no';
		}
		var iPosX = Math.round( ( iScrWidth - iWinWidth ) / 2 );
		var iPosY = Math.round( ( ( iScrHeight - 70 ) - iWinHeight ) / 2);
		strSizeRelatedFeatures += ( document.all )? ',left=' + iPosX + ',top=' + iPosY : ',screenX=' + iPosX + ',screenY=' + iPosY;
	}
	strSizeRelatedFeatures += ',width=' + iWinWidth + ',height=' + iWinHeight;

	return strSizeRelatedFeatures;
} 

function getStdFeatures( strUserFeatures ) {
	var strStdFeatures = '';
	for ( var i = 0; i < arrStdPopupFeatures.length; i++) {
		if ( !(strUserFeatures && strUserFeatures.indexOf( arrStdPopupFeatures[i][0] ) >= 0 ) ) {
			strStdFeatures += ',' + arrStdPopupFeatures[i][0] + '=' + arrStdPopupFeatures[i][1];
		}
	}
	return strStdFeatures;
}

function cmnPopup(strFileUrl, strUserWinName, iWinW, iWinH, strUserFeatures) {
	var strAllFeatures = strUserFeatures + getPopupSizeRelatedFeatures( iWinW, iWinH, strUserFeatures );
	strAllFeatures += getStdFeatures( strAllFeatures );
	var strWinName = ( strUserWinName )? strUserWinName : 'popupWin';
	var popupWin = window.open(strFileUrl, strWinName, strAllFeatures);
	if ( popupWin ) popupWin.focus();
}


function jsFind(){
	this.fnd	= $('.fnd');
	if(this.fnd){
		this.init();
	}
}

jsFind.prototype = {
	init : function(){
		var self	= this;
		this.field	= this.fnd.find('.input');
		if(!this.field.hasClass('disable')){
			this.change	= true;
			this.old	= this.field.val();
		}else{
			this.change	= false;
			this.old	= '';
		}
		this.field.focus(function(){self.enterField()}).blur(function(){self.leaveField()}).keyup(function(e){self.changeField(e)});
		this.fnd.submit(function(){return self.verify()});
	},
	enterField : function(){
		if(!this.change){
			this.field.val('');
		}
	},
	leaveField : function(){
		if(!this.change){
			this.field.val(this.old || 'Поиск по сайту');
		}
	},
	changeField : function(e){
		var str	= this.field.val();
		if(str != this.old){
			this.change	= true;
		}else{
			this.change	= false;
		}
		if(str != ''){
			this.field.removeClass('disable');
		}else{
			this.field.addClass('disable');
		}
	},
	verify : function(){
		var str	= this.field.val();
		if(str == 'Поиск по сайту') str = '';
		if(str.length < 3){
			alert('Искомый текст не может быть менее длиной менее трех букв');
			return false;
		}else{
			return true;
		}
	}
}

$(document.body).ready(function(){var js_find	= new jsFind()});
