function eraseInitialText(element, text) {
	if (element.value == text) {
		element.value = '';
	}
}

function hoverImg(img, xval, yval) {
	img.style.backgroundPosition = '' + xval + 'px ' + yval + 'px';
}

/* фича для IE исключающая мерцание фона */

if (document.uniqueID && document.compatMode && !window.XMLHttpRequest && document.execCommand)
{
	document.execCommand("BackgroundImageCache", false, true);
}

function switchElementVisibility(id, s) {
	if (s == 0) {
	 document.getElementById(id).style.visibility = 'hidden';
	} else {
	 document.getElementById(id).style.visibility = 'visible';
	}
}


function closeLoginBox() {
	switchElementVisibility('login-box', 0);
	document.getElementById('enter-link').style.textDecoration = 'underline';
}

function openLoginBox() {
	switchElementVisibility('login-box', 1);
	document.getElementById('enter-link').style.textDecoration = 'none';
}

function closeCallBackBox() {
	switchElementVisibility('callback-box', 0);
	document.getElementById('call-link').style.textDecoration = 'underline';
}

function openCallBackBox() {
	switchElementVisibility('callback-box', 1);
	document.getElementById('call-link').style.textDecoration = 'none';
}

function bookmarksite(url, title) {
	// alert('x');
 if (window.sidebar) {// firefox
	// alert('q');
	window.sidebar.addPanel(title, url, "");
 }
 else if(window.opera && window.print) { // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
 } 
 else if(document.all) { // ie 
	window.external.AddFavorite(url, title);
 }
 return false;
}


function upd_form(id) /* функция для отправки формы; вызывается по событию для отправки AJAX-запроса, отличного от обычной отправки формы. Вызов  AJAX-запроса производится по событию onsubmit соответствующей формы */
 {
	if(document.getElementById(id).onsubmit() != false)
	{
		document.getElementById(id).submit();	       		
	}
}



/**
 * Popup-search object
 **/

pSearch = {
	last: "",
	input: undefined,
	block: undefined,
	num: undefined,
	selected: undefined,
	init: function(callback) {
		this.input = $(".search input.txt");
		this.input.attr('autocomplete', 'off');
		this.block = $("#search-popup");
		this.block.dropShadow({left: 1, top: 2, blur: 2, opacity: 0.2});
		this.recalcList = callback;
		var ooo = this;
		var keyblistener = function(ev) {
			switch (ev.keyCode) {
				case 27: // esc 
					ooo.closePopup();
					return;
				case 40: // down 
					if (ooo.block.css('display') == 'none') {
						ooo.reload();
					}
					if (ooo.selected === undefined) {
						ooo.select(0);
					} else {
						if (ooo.selected < ooo.num-1) {
							ooo.select(ooo.selected+1);
						}
					}
					return;
				case 38: // up
					if (ooo.selected > 0) {
						ooo.select(ooo.selected-1);
					}
					return;
				case 13: // enter
					if (ooo.selected !== undefined) {
						loc = $("#search-popup #pitem"+ooo.selected).attr('href');
						if (loc != '') {
							document.location.href = loc;
							// alert(loc);
						}
					} else {
						$("#searchform").submit();	
					}
					return;
				default:
					if (ooo.input.val().length > 3) {
						if ( ooo.input.val() != ooo.last ) {
							ooo.reload();
						}
					} else {
						ooo.closePopup();
					}
					return;
			} // end switch
		};
		this.input.keyup( keyblistener );
		this.input.keydown(function(e) {
			if (e.keyCode == 13) {
				e.preventDefault();
			}
		});
		this.block.keyup( keyblistener );
		return this;
	},
	recalcList: undefined,
	reload: function() {
		var ooo = this;
		if(this.req)
		{
			this.req.abort();
		}
		this.req = $.ajax({
		  type: "GET",
		  cache: true,
		  async: true,
		  url: "/search/ajax_search.php",
		  data: {q: $("#searchstring").val() },
		  success: function(data) {
			ooo.block.html(data);
			ooo.num = $("#search-popup a.item").size();
			ooo.last = ooo.input.attr('value');
			ooo.block.css('left', ''+( ooo.input.offset().left-ooo.block.width()+ooo.input.width()+28 )+'px');
			if (ooo.block.css('display') == 'none') {
				ooo.select(undefined);
				ooo.block.slideDown("fast", function() {
					ooo.block.redrawShadow();
					$("#search-popup_dropShadow").show();
				});
			} else {	
				ooo.block.redrawShadow();
			}
		  }
		});
	},
	
	closePopup: function() {
		if (this.block.css('display') != 'none') {
			$("#search-popup_dropShadow").hide();
			$("#"+this.block.attr('id')+":visible")
				.slideUp(100, function() {
//				this.last = '';
			});
			this.select(undefined);
		}
	},
	select: function(n) {
		$("#search-popup .sel").removeClass("sel");
		if (n !== undefined) {
			$("#search-popup #pitem"+n).addClass("sel");
		}
		this.selected = n;
	}
};



/**
 * Object for Client Ribbon
 * AJAX should be added into object ;))
 **/

cRibbon = {
	// you can modify this value 
	PRincr: 1.0,
	PXincr: undefined,
	num: undefined,
	step: 2,
	// private vars 
	animtime: undefined,
	callBackLeft: undefined,
	callBackRight: undefined,	
	tbl: undefined,
	timerID: undefined,
	// methods 
	init: function(anim, c1, c2) {
		this.num = $(".client-ribbon #rib0 table .rib-s").size(); 
		$(".client-ribbon #rib0 table").css('width', ''+(this.num*25)+'%');
		$(".client-ribbon #rib0 table").css('left', '-'+(Math.floor(this.num/3)*25)+'%');
		$(".client-ribbon table .a2 .rib-s").css('width', ''+(100.0/this.num)+'%');
		this.tbl = $(".client-ribbon #rib0 table");
		this.animtime = anim;
		this.PXincr = Math.floor(this.PRincr*this.tbl.width()/100);
		this.callBackLeft = c1;
		this.callBackRight = c2;
		if (jQuery.browser.msie) { // fix small design hack for css
			$(".client-ribbon .paddcr").css('height', '10px');
		}
		return this;
	      },
	rollLeft: function() {
		var ooo = this;
		if (this.tbl.css('left').match(/\-?[0-9]+\%/)) {
			if ( (this.timerID !== undefined) || 
			     (1*this.tbl.css('left').replace(/\%/, '') >= 0) ) {
				return false;
			}
			var startval = 1*ooo.tbl.css('left').replace(/\%/, '');
			var stopval  = startval + ooo.step*25;
			if (stopval > 0) {stopval = 0;}
			ooo.timerID  = setInterval( function() {
				startval = startval + ooo.PRincr;
				ooo.tbl.css('left', startval+'%');
				if (startval >= stopval) {
					clearInterval(ooo.timerID);
					ooo.timerID = undefined;
					ooo.callBackLeft();
				}
			}, ooo.animtime);
		} else {
			if ( (this.timerID !== undefined) || 
			     (1*this.tbl.css('left').replace(/px/, '') >= 0) ) {
				return false;
			}
			var startval = 1*ooo.tbl.css('left').replace(/px/, '');
			var stopval  = startval + ooo.step*ooo.tbl.width()/ooo.num;
			if (stopval > 0) { stopval = 0; }
			ooo.timerID = setInterval( function() {
				startval = startval + ooo.PXincr;
				ooo.tbl.css('left', startval+'px');
				if (startval >= stopval) {
					clearInterval(ooo.timerID);
					ooo.timerID = undefined;
					ooo.callBackLeft();
				}
			}, ooo.animtime);
		}
		return false;
	},
	rollRight: function() {
		var ooo = this;
		if (this.tbl.css('left').match(/\-?[0-9]+\%/)) {			   
			if ( (this.timerID !== undefined) || 
			     (1*this.tbl.css('left').replace(/\%/, '') <= 25*(4.0-ooo.num) ) ) {
				return false;
			}
			var startval = 1*ooo.tbl.css('left').replace(/\%/, '');
			var stopval  = startval - ooo.step*25;
			if (stopval < 25*(4.0-ooo.num) ) { stopval = 25*(4.0-ooo.num); }
			ooo.timerID = setInterval( function() {
				startval = startval - ooo.PRincr;
				ooo.tbl.css('left', startval+'%');
				if (startval <= stopval ) {
					clearInterval(ooo.timerID);
					ooo.timerID = undefined;
					ooo.callBackRight();					
				}
			}, ooo.animtime);
		} else { 
			if ( (this.timerID !== undefined) || 
			     (this.tbl.css('left').replace(/px/, '') <= ooo.tbl.width()*(4.0/ooo.num-1) ) ) {
				return false;
			} 
			var startval = 1*ooo.tbl.css('left').replace(/px/, '');
			var stopval  = startval - ooo.step*ooo.tbl.width()/ooo.num;
			if (stopval < ooo.tbl.width()*(4/ooo.num-1) ) { stopval = ooo.tbl.width()*(4/ooo.num-1); }
			ooo.timerID = setInterval( function() {
				startval = startval - ooo.PXincr;
				ooo.tbl.css('left', startval+'px');
				if (startval <= stopval) {
					clearInterval(ooo.timerID);
					ooo.timerID = undefined;
					ooo.callBackRight();
				}
			}, ooo.animtime); 
		}
		return false;
	}
};

/**
 * CLient ribbon ending
 **/


$(document).ready( function() {

	var UriFlagExist = /new_search=Y/i;
   var UriString = document.location;
   if( UriFlagExist.exec(UriString) != null ) {
      var ajp = pSearch.init(function() {this.num = 0; this.req=null; return;});
   } // End if

	/**
	 * Usage of cRibbon class
	 */
	var crb = cRibbon.init(
		5,  // animation time
		function() { return; },
		function() { return; }
	);

	$("#cr-rollleft").click(function() {
		return crb.rollLeft();
	});	

	$("#cr-rollright").click(function() {
		return crb.rollRight();
	});
});



function closeCallbackBox() {
	switchElementVisibility('callback-box', 0);
	document.getElementById('call-link').style.textDecoration = 'underline';
}

function openCallbackBox() {
	switchElementVisibility('callback-box', 1);
	document.getElementById('call-link').style.textDecoration = 'none';
}

