// массив картинок для предзагрузки:
// массив картинок для предзагрузки:
var picsPreload=new Array();
// добавляет картинку в массив, если ее еще там нет:
function addPicture(picName){
	for (i=0;i<picsPreload.length; i++){
		if (picName==picsPreload[i])
			return;
	}
	 picsPreload.push(picName);
}
// вернуть имя картинки с суффиксом, например из "btn-1.jpg" сделать "btn1-blur.jpg" и наоборот, кстати, можно:
function getNameWithSuffix(fileName,suffix){
	var reg = /(http.+).(png|jpg|gif)/;
	var arr=reg.exec(fileName);
	var name=arr[1];
	var ext="."+arr[2];
	var pics=Array();
	if (name.substr(name.length-4,name.length)=="-act")
		name=name.substr(0,name.length-4);
	if (name.substr(name.length-5,name.length)=="-blur")
		name=name.substr(0,name.length-5);
	if (suffix!='')
		name=name+'-';
	return name+suffix+ext; 
}
// заточка getNameWithSuffix под конкретный суффикс:
function getBlurName(fileName){
	return getNameWithSuffix(fileName,'blur');
}
function getActName(fileName){
	return getNameWithSuffix(fileName,'act');
}
function getSimpleName(fileName){
	return getNameWithSuffix(fileName,'');
}
// запихнуть картинку в массив предзагрузки, берем элемент, анализируем бэкграунд и пихаем в массив, если такой еще там нет:
function pushPreload(element,suffix){
	var backgroundImage=jQuery.css(element,'background-image');
	if (backgroundImage=='none')
		backgroundImage=element.src;
	var reg = /(http.+).(png|jpg|gif)/;
	var arr=reg.exec(backgroundImage);
	if (arr!=null && arr.length>1)
	{
		var name=arr[1];
		var ext="."+arr[2];
		var pics=Array();
		if (name.substr(name.length-4,name.length)=="-act")
		{
			name=name.substr(0,name.length-4);
			addPicture(name+ext);
		}
		addPicture(name+'-'+suffix+ext);
	}
}
// здесь надо отрефакторить, можно вообще убрать эти дурацкие ф-ции:
function pnMenuFirstItemAct(event){
	jQuery('div#profile-menu table td.tab').removeClass('tab-act');
	jQuery(event).addClass('first-act');		
	jQuery(event).removeClass('first-blur');		
	showTabPage(0);
}
function showTabPage(index){	
	if (jQuery('div#profile-menu div.switcher').size()>0 && !jQuery('div#profile-menu div.switcher').hasClass('open')){
		pnMenuSwitch();
	}
	jQuery('div#profile-menu div.tab-item').removeClass('act');
	var el=jQuery('div#profile-menu div.tab-item').get(index);
	jQuery(el).addClass('act');
}
function pnMenuItemAct(event){
	jQuery('div#profile-menu table td.first-tab').removeClass('first-act');
	jQuery('div#profile-menu table td.tab').removeClass('tab-act');
	jQuery(event).addClass('tab-act');		
	showTabPage(jQuery('div#profile-menu table td.tab').index(event)+1);
}
function pnMenuItemBlur(event){
	jQuery(event).toggleClass('tab-blur');		
}
function pnMenuItemUnblur(event){
	jQuery(event).toggleClass('tab-blur');		
}
function pnMenuFirstItemBlur(event){
	if (!jQuery(event).hasClass('first-act'))
		jQuery(event).addClass('first-blur');		
}
function pnMenuFirstItemUnblur(event){
	jQuery(event).removeClass('first-blur');		
}
function prepareImages(){
	// подсвечиваемые:
	jQuery('.blurable').each(function (i) {
		pushPreload(this,'blur');
	});
	// активные:
	jQuery('.actable').each(function (i) {
		pushPreload(this,'act');
	});	
	// загружаем невидимые картинки:
	for (i=0;i<picsPreload.length;i++){
		var pic=new Image();
		pic.src=picsPreload[i];
	}
}
function pnMenuSwitch(){	
	var swBtn=jQuery('div#profile-menu div.switcher');
	var img=jQuery('div#profile-menu div.switcher img')[0];
	if (jQuery(swBtn).hasClass('open')){
		var el=jQuery('div#profile-menu div.body-small');
		jQuery(el).removeClass('body-small');
		jQuery(el).addClass('body-hidden');
		jQuery(swBtn).removeClass('open');
		img.src='/bitrix/images/front/pn-small-open.png';
	}
	else{
		var el=jQuery('div#profile-menu div.body-hidden');
		jQuery(swBtn).addClass('open');
		jQuery(el).removeClass('body-hidden');
		jQuery(el).addClass('body-small');
		img.src='/bitrix/images/front/pn-small-close.png';
	}
}
function icoBlur(event){
	event.src=getBlurName(event.src);
}
function icoUnblur(event){
	event.src=getSimpleName(event.src);
}
function setProfile(){
	var start = location.href.indexOf("#");
	var end = location.href.length;
	if (start>0){
		var anchor = location.href.substring(start+1, end);
		var index = anchor.substring(2, 3);
		index=index-1;
		if (index>0){
			var element=jQuery('div#profile-menu table td.tab')[index];
			pnMenuItemAct(element);
		}
	}
}
var popupMenu = {}
	popupMenu.currentItem=-1;
	popupMenu.currentSubItem=-1;
	popupMenu.itemFunc;
	popupMenu.subItemFunc;
	popupMenu.clearAll=function(){
		for (i=0;i<popupMenu.itemFunc.length;i++){
			clearInterval(popupMenu.itemFunc[i]);
//			jQuery('div#page-content').html(jQuery('div#page-content').html()+i+' ');
		}
	}
	popupMenu.init=function(){
		popupMenu.itemFunc=Array(jQuery('div#top-menu table td.item div').size());
	}
	popupMenu.setHideInterval=function(index){
		clearInterval(popupMenu.itemFunc[index]);
		if (popupMenu.currentItem!=-1)
			popupMenu.itemFunc[index]=setInterval('popupMenu.hideItems();',300);
	}
	popupMenu.setHideInterval=function(index){
		clearInterval(popupMenu.itemFunc[index]);
		if (popupMenu.currentItem!=-1)
			popupMenu.itemFunc[index]=setInterval('popupMenu.hideItems();',300);
	}
	popupMenu.setHideSubInterval=function(){
		popupMenu.subItemFunc=setInterval('popupMenu.hideSubItems();',300);
	}
	popupMenu.hide=function(){
		popupMenu.setHideInterval(popupMenu.currentItem);
	}
	popupMenu.hideSubItems=function(){	
		clearInterval(popupMenu.subItemFunc);
		popupMenu.currentSubItem=-1;
		jQuery('div.pm-subitems').hide();
	}
	popupMenu.hideItems=function(){
		jQuery('div#top-menu table td.item').removeClass('blur');
		popupMenu.clearAll();
		jQuery('div.popup-menu').hide();
		popupMenu.currentItem=-1;
		popupMenu.currentSubItem=-1;
	}
	popupMenu.showItem=function(){
		jQuery('div.popup-menu').css('display','none');
		jQuery('div#top-menu table td.item').removeClass('blur');
		var size=jQuery('div#top-menu table td.item div').size();
		var el=jQuery('div.item-'+popupMenu.currentItem);
		jQuery(jQuery('div#top-menu table td.item').get(popupMenu.currentItem)).addClass('blur');
		var item=jQuery('div#top-menu table td.item div').get(popupMenu.currentItem);
		var position=jQuery(item).offset();
		var menuPosition=jQuery('div#top-menu').offset();
		var left=position.left;
		var top=position.top+19;
		var width=jQuery(el).css('width').replace('px','');
		if (popupMenu.currentItem>=0){
			if (popupMenu.currentItem<size-1){
//				var elPrevPosition=jQuery(jQuery('div#top-menu table td.item div').get(popupMenu.currentItem-1)).offset();
	//			var elNextPosition=jQuery(jQuery('div#top-menu table td.item div').get(popupMenu.currentItem+1)).offset();
				left=position.left-15;
			}
			else
				left=menuPosition.left+950-width+1;
		}
		else
			left=menuPosition.left;
		jQuery(el).css('left',left);
		jQuery(el).css('top',top);
		jQuery(el).show();
	}
	popupMenu.show=function(event){
		popupMenu.hideSubItems();
		var newCurrentItem=jQuery('div#top-menu table td.item').index(jQuery(event));
		if (newCurrentItem==popupMenu.currentItem)
			return;
		popupMenu.currentItem=newCurrentItem;
		if (jQuery('div.item-'+popupMenu.currentItem).size()==0)
			return;
		popupMenu.clearAll();
		popupMenu.showItem();
	}
	popupMenu.showSubItems=function(event){
		popupMenu.clearAll();
		clearInterval(popupMenu.subItemFunc);	
		if (event==null){
			return;
		}
		var newCurrentSubItem=jQuery('div.item-'+popupMenu.currentItem+' table tr.menuitem').index(jQuery(event));
		if (newCurrentSubItem==popupMenu.currentSubItem)
			return;
		popupMenu.currentSubItem=newCurrentSubItem;
		var subItem='div.subitem-'+popupMenu.currentItem+'-'+newCurrentSubItem;
		jQuery('div.pm-subitems').hide();
		if (jQuery(subItem).size()>0){
			var item=jQuery('div.item-'+popupMenu.currentItem+' table tr.menuitem').get(popupMenu.currentSubItem);
			var position=jQuery(item).offset();
			jQuery(subItem).css('left',(position.left+206)+'px');
			jQuery(subItem).css('top',(position.top-1)+'px');
			jQuery(subItem).show();
		}
	}
	function switchPhoto(event){
		var index=jQuery(event).attr('selectedIndex');
		var id=jQuery(jQuery(event).get(index)).val();
		var span=jQuery('div#feedback span.spec-name').get(index);
		var mailSpan=jQuery('div#feedback span.spec-mail').get(index);
		if(index>-1){
			jQuery('div#feedback div.spec-name').html(jQuery(span).html());
			jQuery('div#feedback img.spec-photo').attr('src',jQuery(jQuery('div#feedback img.spec-photo-item').get(index)).attr('src'));
			jQuery('div#feedback input[name=form_text_21]').val(id);
			jQuery('div#feedback input[name=form_text_20]').val(jQuery(span).html());
			jQuery('div#feedback input[name=form_text_32]').val(jQuery(mailSpan).html());
		}
		else{
			jQuery('div#feedback input[name=form_text_21]').val('');
			jQuery('div#feedback input[name=form_text_20]').val('');
			jQuery('div#feedback input[name=form_text_32]').val('');
			jQuery('div#feedback div.spec-name').html('');
			jQuery('div#feedback img.spec-photo').attr('src','/bitrix/images/front/fb-no-photo.jpg');
		}
	}
	
	var searchText;
	var loginText;
	var passText;
	var fileNameText;
	function inputFocus(event,text){
		if (jQuery(event).val()==text){
			jQuery(event).val('');
		}
	}
	function inputBlur(event,text){
		if (jQuery(event).val()==""){
			jQuery(event).val(text);
		}
	}
	function setInputTexts(){
		searchText=jQuery('div#search input[name=q]').val();
		loginText=jQuery('div#profile-menu input[name=USER_LOGIN]').val();
		passText=jQuery('div#profile-menu input[name=USER_PASSWORD]').val();
		fileNameText=jQuery('input#pm-file-name').val();
	}
	function aIcoBlur(event){
		var index=jQuery('div.switch div.ico img').index(jQuery(event));
		var src=jQuery(jQuery('div.switch div.ico img').get(index)).attr('src');
		jQuery(jQuery(event)).attr('src',jQuery(jQuery('div.switch div.ico-blur img').get(index)).attr('src'));
		jQuery(jQuery('div.switch div.ico-blur img').get(index)).attr('src',src);
	}
	function authSubmit(event){	
		jQuery('form#auth-form')[0].submit();
	}
	function checkPmBtn(){	
		if(jQuery('input#pm-file-name').val()!='' && jQuery('input#pm-file').val()!='')
			jQuery('input#pm-file-btn').attr('disabled',false);
		else
			jQuery('input#pm-file-btn').attr('disabled',true);
	}
	function setFileName(){
//		if (jQuery('input#pm-file-name').val()==fileNameText){
			var fileName=jQuery('input#pm-file').val();
			var reg = /[a-z0-9а-яА-я\.\s]+\.[a-z0-9а-яА-я]+/;
			fileName=reg.exec(fileName);
			jQuery('input#pm-file-name').val(fileName);
			
			jQuery('input#pm-file-name').addClass('enter');
	//	}
	}
	function confirmDelete(){
		return (confirm("Вы уверены, что хотите удалить файл?"));
	}
// после загрузки страницы расставляем события и делаем начальную обработку:
jQuery(function(){
	// Профильное меню:
	jQuery('div#profile-menu table td.tab').bind('mouseover',function(event){pnMenuItemBlur(this);});
	jQuery('div#profile-menu table td.tab').bind('mouseout',function(event){pnMenuItemUnblur(this);});
	jQuery('div#profile-menu table td.first-tab').bind('mouseover',function(event){pnMenuFirstItemBlur(this);});
	jQuery('div#profile-menu table td.first-tab').bind('mouseout',function(event){pnMenuFirstItemUnblur(this);});
	jQuery('div#profile-menu table td.tab').bind('click',function(event){pnMenuItemAct(this);});
	jQuery('div#profile-menu table td.first-tab').bind('click',function(event){pnMenuFirstItemAct(this);});
	// Верхнее меню:
	jQuery('div#top-menu table td.item').bind('mouseover',function(event){popupMenu.show(this);});
	jQuery('div#top-menu table td.item').bind('mouseout',function(event){popupMenu.hide();});
	// Popup-menu:
	jQuery('div.popup-menu').bind('mouseout',function(event){popupMenu.hide();});
	jQuery('div.popup-menu').bind('mouseover',function(event){popupMenu.clearAll(this);});
	jQuery('div.popup-menu table tr.menuitem').bind('mouseout',function(event){popupMenu.setHideSubInterval();});
	jQuery('div.popup-menu table tr.menuitem').bind('mouseover',function(event){popupMenu.showSubItems(this);});
	jQuery('div.pm-subitems').bind('mouseout',function(event){popupMenu.setHideSubInterval();});
	jQuery('div.pm-subitems').bind('mouseover',function(event){popupMenu.showSubItems();});
	// 
	jQuery('tr.blurable').bind('mouseout',function(event){jQuery(this).removeClass('blur')});
	jQuery('tr.blurable').bind('mouseover',function(event){jQuery(this).addClass('blur')});
	jQuery('div.blurable').bind('mouseover',function(event){jQuery(this).toggleClass('blur')});
	jQuery('div.blurable').bind('mouseout',function(event){jQuery(this).toggleClass('blur')});
	jQuery('img.blurable').bind('mouseover',function(event){icoBlur(this)});
	jQuery('img.blurable').bind('mouseout',function(event){icoUnblur(this)});
	//jQuery('div#profile-menu div').bind('click',function(event){alert(jQuery(this).attr('class'))});

	jQuery('div.switcher').bind('click',function(event){pnMenuSwitch(this)});
	jQuery('div#feedback select.spec-position').bind('change',function(event){switchPhoto(this)});

	jQuery('div#search input[name=q]').bind('focus',function(event){inputFocus(this,searchText)});
	jQuery('div#search input[name=q]').bind('blur',function(event){inputBlur(this,searchText)});

	jQuery('div#profile-menu input[name=USER_LOGIN]').bind('focus',function(event){inputFocus(this,loginText)});
	jQuery('div#profile-menu input[name=USER_LOGIN]').bind('blur',function(event){inputBlur(this,loginText)});
	
	jQuery('div#profile-menu input[name=USER_PASSWORD]').bind('focus',function(event){inputFocus(this,passText)});
	jQuery('div#profile-menu input[name=USER_PASSWORD]').bind('blur',function(event){inputBlur(this,passText)});

	jQuery('input#pm-file-name').bind('focus',function(event){inputFocus(this,fileNameText);jQuery(this).addClass('enter');});
	jQuery('input#pm-file-name').bind('blur',function(event){inputBlur(this,fileNameText);jQuery(this).removeClass('enter');});

	jQuery('input#pm-file-name').bind('keyup',function(event){checkPmBtn()});
	jQuery('input#pm-file').bind('change',function(event){checkPmBtn(); setFileName()});

	jQuery('form.del-file').bind('submit',function(event){return confirmDelete()});
	
	jQuery('div.switch div.ico img').bind('mouseover',function(event){aIcoBlur(this)});
	jQuery('div.switch div.ico img').bind('mouseout',function(event){aIcoBlur(this)});
	
	jQuery('a#auth-link').bind('click',function(event){authSubmit(this)});

	setInputTexts();
	prepareImages();
	setProfile();
	popupMenu.init();	
});

