var scrolledMoved=false;
var m1=null;
var m2=null;
var imgX=0, imgY=0, imgH=90, imgW=90;
$(document).ready(function(){
	var container = new Container('wrapper');
	var footer = new Container('footer');
	var topBar = new Container('top-bar');
	$('.select-box > .select-data').click(showSelect);
	SwitchingNews.initialize();
	m1=new MultyContent('multy-content-articles');
	setInterval(viewMoved, 500);
	setColumnsHeight();
	$('#main-menu-first-level').find('.menu-arrow').click(showInnerMenu);
	$('#main-menu-first-level').find('.menu-link').click(goLink);
	$('.banner-out > div').css('margin','0 auto');
	ReportForm.initialize();
	$('#reg-link').click(showRegistrationForm);
	fuckIE6();
	var datepickerJSON;
	var test='{"1":{"dateBegin":"12.12.2010","dateEnd":"15.12.2010","title":"title 1","preview":"preview text 1","link":"http://www.brainity.ru?ipv=12&id=2"},"2":{"dateBegin":"13.12.2010","dateEnd":"16.12.2010","title":"title 2","preview":"preview text 2","link":"www.brainity.ru"}}';
	try {
		datepickerJSON = jQuery.parseJSON(test);
	} catch(e) {
		alert(e);
	}
	$(function() {
		$('#datepicker').datepicker({
			changeMonth: true,
			changeYear: true,
			onSelect: function(dateText, inst) { window.location.href='//brainity.ru/know/events/?date='+dateText; }//,
			//beforeShowDay: function(date){}
		});
	});
	var yourAreaTime;
	$('#your-area').mouseenter(function(){
		yourAreaTime=setTimeout(function(){
			$('#your-area-text').fadeIn();
		},300);
	}).mouseout(function(){
		clearTimeout(yourAreaTime);
		$('#your-area-text').fadeOut();
	});
	$(".anons-menu div").hover(function(){
		$(".anons-menu .select-anons").attr("class","not-select-anons");
		$(this).attr("class","select-anons");
		$(".postlist").css("display","none");
		$("div#post"+$(this).attr("theme")).css("display","block");
	});
});

var datepickerJSON;
var test='{"1":{"dateBegin":"12.12.2010","dateEnd":"15.12.2010","title":"title 1","preview":"preview text 1","link":"http://www.brainity.ru?ipv=12&id=2"},"2":{"dateBegin":"13.12.2010","dateEnd":"16.12.2010","title":"title 2","preview":"preview text 2","link":"www.brainity.ru"}}'
function sendImgInfo() {
	$('#RESIZE_IMG_HEIGHT').attr('value',imgH);
	$('#RESIZE_IMG_WIDTH').attr('value',imgW);
	$('#RESIZE_IMG_X').attr('value',imgX);
	$('#RESIZE_IMG_Y').attr('value',imgY);
	var cords=$('#load-img-form-action').serialize();
	alert(cords);
}



/**********margin for containers**************/
//get's body width
function bodyWidth() {
	return document.body.clientWidth;
}
//constructor of container object
function Container(id) {
	this.id = id;
	this.margin = 0;
	this.width = 'auto';
	this.setMargin();
	this.setWidth();
}

//method set left and right margins for container object
Container.prototype.setMargin = function() {
}
//method set width for container object
Container.prototype.setWidth = function() {
	this.width = 'auto';
	$('#'+this.id+' > .inner').width(this.width);
}

/************end margins**************/
var isShowSelect=false;
function showSelect(e) {
	var e=e||event;
	var src=e.target||e.srcElement;
	while(src.className!='select-box') {
		src=src.parentNode;
	}
	var arr=new Array();
	var tmp=src.getElementsByTagName('span');
	var linkToSDT;
	var linkToButton;
	for(i=0;i<tmp.length;i++) {
		if(tmp[i].className=='select-data-text')
			linkToSDT=tmp[i];
		else if(tmp[i].className=='select-button')
			linkToButton=tmp[i];
	}
	var src2=src.getElementsByTagName('ul')[0];
	if(!isShowSelect) {
		$(src2).slideDown('fast');
		$(linkToButton).css('background-position','0 -20px');
		isShowSelect=true;
	} else {
		$(src2).slideUp('fast');
		$(linkToButton).css('background-position','0 0');
		isShowSelect=false;
	}
	$(src.getElementsByTagName('li')).mouseenter(function(e){
		var e=e||event;
		var srcLoc=e.target||e.srcElement;
		$(srcLoc).css('background-color','#bebebe');
	}).mouseleave(function(e){
		var e=e||event;
		var srcLoc=e.target||e.srcElement;
		$(srcLoc).css('background-color','#fff');
	}).click(function(e){
		var e=e||event;
		var srcLoc=e.target||e.srcElement;
		$(src2).slideUp('fast');
		isShowSelect=false;
		$(linkToSDT).text($(srcLoc).text());
		$(src.getElementsByTagName('input')[0]).attr('value',$(srcLoc).text());
		$(linkToButton).css('background-position','0 0');
	});
};
var isShow=false;
function showLoginForm() {
	if($('#login-form').css('display')=='block') {
		$('#login-form').slideUp();
		$('#login-link').css('text-decoration','underline');
	} else {
		$('#login-form').slideDown();
		if($('#registration-box').css('display')=='block') {
			$('#registration-box').slideUp();
			$('#reg-link').css('text-decoration','underline');
		}
		$('#login-link').css('text-decoration','none');
	}
}
var SwitchingNews = {
	containerId: 'switching-news',
	tabsId: 'news-tabs',
	controlsId: 'news-controls',
	leftArrow: 'left-arrow',
	rightArrow: 'right-arrow',
	panelId: 'switching-news-panel',
	linkToContainer: null,
	arrConten: null, //массив контента
	conWidth: 0,//ширина блока - устанавливается из функции
	linkToTabs: null,
	curPosition: 0, //текущая новость
	countDown: 0,//счетчик прокрутки
	arTabs: 0, //массив табов
	tabsLeft: 0, //позиция смещения табов
	tabHeight: 0, //высота - устанавливается из функции
	tabWidth: 0,//ширина таба - устаравливается из функции
	tabsCount: 0,//количество табов - устанавливается из функции
	tabsMargin: 1,
	showTabsCount: 3,
	scrollWidth: 0,//на сколько прокручивать - устанавливается из функции
	conTabsWidth: 0,//ширина контэйнера табов - устанавливается из функции
	src: null,
	initialize: function() {
		SwitchingNews.linkToContainer=document.getElementById(SwitchingNews.containerId);
		SwitchingNews.linkToTabs=document.getElementById(SwitchingNews.tabsId);
		SwitchingNews.arrContent=$(SwitchingNews.linkToContainer).find('.content');
		var src=$(SwitchingNews.linkToTabs).children();
		SwitchingNews.setParam();
		SwitchingNews.tabsLeft=0;
		SwitchingNews.arTabs=new Array();
		for(i=0;i<src.length;i++)
			SwitchingNews.arTabs[i]=src[i];
		$(src).mouseenter(SwitchingNews.switchNews);
		$(src).mouseleave(SwitchingNews.switchNews);
		$('#'+SwitchingNews.controlsId).children().click(SwitchingNews.scrollTabs);
		for(var i=0;i<SwitchingNews.tabsCount;i++) {
			if(src[i].className=='selected')
				SwitchingNews.curPosition=i;
		}
	},
	setParam: function () {
		SwitchingNews.conWidth=$(SwitchingNews.linkToContainer).innerWidth();
		var src=$(SwitchingNews.linkToTabs).children();
		SwitchingNews.tabsCount=src.length;
		SwitchingNews.tabWidth=(SwitchingNews.conWidth-SwitchingNews.showTabsCount*SwitchingNews.tabsMargin)
								/SwitchingNews.showTabsCount;
		$(src).find('img').width(SwitchingNews.tabWidth);
		SwitchingNews.scrollWidth=(SwitchingNews.tabWidth+SwitchingNews.tabsMargin)*SwitchingNews.showTabsCount;
		SwitchingNews.conTabsWidth=(SwitchingNews.tabWidth+SwitchingNews.tabsMargin)*SwitchingNews.tabsCount;
		$(SwitchingNews.linkToTabs).width(SwitchingNews.conTabsWidth);
		SwitchingNews.tagHeight=$(SwitchingNews.linkToTabs).innerHeight();
		var panelTop=SwitchingNews.tagHeight-$('#'+SwitchingNews.panelId).innerHeight();
	},
	switchNews: function(e) {
		var e=e||event;
		var src=e.target||e.srcElement;
		var tmp=0;
		while(src.tagName!='LI') {
			src=src.parentNode;
		}
		if(e.type=="mouseenter"){
			while(src!=SwitchingNews.arTabs[tmp]) {
				tmp++;
			}
			SwitchingNews.unSelect(SwitchingNews.curPosition);
			SwitchingNews.curPosition=tmp;
			SwitchingNews.select(SwitchingNews.curPosition);
		}
	},
	select: function (elem) {
		$(SwitchingNews.arrContent[elem]).show();
		SwitchingNews.arTabs[elem].className='selected';
	},
	unSelect: function (elem) {
		$(SwitchingNews.arrContent[elem]).hide();
		SwitchingNews.arTabs[elem].className=' ';
	},
	scrollTabs: function(e) {
		var e=e||event;
		var src=e.target||e.srcElement;
		var tmp;
		if(src.className==SwitchingNews.rightArrow) {
			if(SwitchingNews.tabsLeft<SwitchingNews.conTabsWidth-SwitchingNews.scrollWidth) {
				SwitchingNews.countDown++;
				SwitchingNews.tabsLeft=SwitchingNews.scrollWidth*SwitchingNews.countDown;
				SwitchingNews.showHide();
				$(SwitchingNews.linkToTabs).animate({left: -SwitchingNews.tabsLeft+'px'}, 'slow');
			}
		} else if(src.className==SwitchingNews.leftArrow) {
			if(SwitchingNews.tabsLeft!=0) {
				SwitchingNews.countDown--;
				SwitchingNews.tabsLeft=SwitchingNews.scrollWidth*SwitchingNews.countDown;
				SwitchingNews.showHide();
				$(SwitchingNews.linkToTabs).animate({left: -SwitchingNews.tabsLeft+'px'}, 'slow');
			}
		}
	},
	showHide: function () {
		$(SwitchingNews.linkToTabs).stop(true, true);
		$(SwitchingNews.arrContent[SwitchingNews.curPosition]).hide();
		SwitchingNews.arTabs[SwitchingNews.curPosition].className=' ';
		SwitchingNews.curPosition=SwitchingNews.showTabsCount*SwitchingNews.countDown;
		$(SwitchingNews.arrContent[SwitchingNews.curPosition]).show();
		SwitchingNews.arTabs[SwitchingNews.curPosition].className='selected';
	}
}
//multycontent
function MultyContent(id) {
	this.containerId=id;
	this.leftArrow='left-arrow';
	this.rightArrow='right-arrow';
	this.curPosition=0;
	this.countDown=0;
	this.linkToContainer=document.getElementById(this.containerId);
	this.linkToContent=$(this.linkToContainer).find('.slider-content');
	this.linkToUl=$(this.linkToContent).find('ul');
	this.ulWidth=0;
	this.containerWidth=0;
	this.contentWidth=0;
	this.contentHeight=0;
	this.elemWidth=0;
	this.contentCount=0;
	this.arrContent=$(this.linkToContainer).find('.content');
	this.elemPadding=8;
	this.setParam();
	$(this.linkToContainer).find('.'+this.leftArrow).click(scrollContent);
	$(this.linkToContainer).find('.'+this.rightArrow).click(scrollContent);
}
MultyContent.prototype = {
	setParam: function() {
	}
}
var scrollLeft=0;
function scrollContent(e) {
	var e=e||event;
	var src=e.target||e.srcElement;
	var src2=src.parentNode;
	var tmp=$(src2).find('.content');
	var scrollWidth=$(src2).find('.slider-content').innerWidth();
	var num=tmp.length;
	$(src2).find('ul').stop(true, true);
	if(src.className=='right-arrow') {
		var f = document.createDocumentFragment();
		//scrollLeft+=scrollWidth;
		$(src2).find('ul').animate({marginLeft: -scrollWidth+'px'},'slow',function(){
		for(var i=0;i<3;i++)
			f.appendChild(tmp[i]);
		$(src2).find('ul').append(f);
		$(src2).find('ul').css('margin-left', '0');
		});
	} else if(src.className=='left-arrow') {
		var f = document.createDocumentFragment();
		for(var i=num-3;i<num;i++)
			f.appendChild(tmp[i]);
		$(src2).find('ul').css('margin-left',-scrollWidth+'px');
		$(src2).find('ul').prepend(f);
		scrollLeft=0;
		$(src2).find('ul').animate({marginLeft: scrollLeft+'px'},'slow',function(){
			
		});
	}
}
var timeOutForMulty;
function setParamsAllScrolled() {
	SwitchingNews.setParam();
}
function viewMoved() {
	if(scrolledMoved) {
		setTimeout(function() {
			SwitchingNews.setParam();
			scrolledMoved=false;
		},500);
	}
}
function switchTabs(src) {
	var tmp=src;
	var tabId=$(src).attr('href');
	while (tmp.tagName!='TABLE')
		tmp=tmp.parentNode;
	var prevLoc=$(tmp).find('.selected').find('a').attr('href');
	if(prevLoc!=tabId) {
		$(prevLoc).stop(true,true);
		$(src).stop(true,true);
		$(tmp).find('TD').removeClass('selected');
		$(prevLoc).slideUp();
		$(src).parent().addClass('selected');
		$(tabId).slideDown();
	}
}
function setSearch(e) {
	if(e.value.length==0)
		e.value='Поиск по сайту';
}
function setColumnsHeight() {
	var arCol=[];
	arCol=$('.columns');
	var max=$(arCol[0]).height();
	for(i=1;i<arCol.length;i++) {
		if(max<$(arCol[i]).height())
			max=$(arCol[i]).height();
	}
	$(arCol).height(max);
}
function changeFontSize(src) {
	var tmp=src.getAttribute('class');
	var tmp='.'+tmp.substr(tmp.indexOf(' ')+1,tmp.length);
	var size=$(src).css('font-size');
	while(!$(src).hasClass('widget-content'))
		src=src.parentNode;
	$(src).css('font-size',size);
	$('.rss-feed').css('font-size','12px');
	$(src).find('.change-font-size').removeClass('selected');
	$(src).find('.page-toolbox').find(tmp).addClass('selected');
}
function showAllert(msg,type) {
	if(!document.getElementById('allert-msg-box')) {
		var src='<div id="allert-msg-box"><div class="panel"><span class="msg-text">'
		+type+'</span><span class="close-msg">x</span><div class="clear"></div></div><div class="msg-content">'
		+'<p><b>'+type+'</b></p>'
		+'<p>'+msg+'</p></div><span class="submit">Ok</span></div>';
		$('#wrapper').append(src);
		var bTop, bLeft;
		bLeft=($('#wrapper').width()/2-$('#allert-msg-box').outerWidth()/2);
		bTop=$(window).scrollTop()+$(window).height()/2-$('#allert-msg-box').outerHeight()/2;
		$('#allert-msg-box').css('top',bTop+'px');
		$('#allert-msg-box').css('left',bLeft+'px');
		$('#allert-msg-box').find('.close-msg').click(function(){$('#allert-msg-box').remove();});
		$('#allert-msg-box').find('.submit').click(function(){$('#allert-msg-box').remove();});
	}
}
function showInnerMenu(e) {
	var e=e||event;
	var src=e.target||e.srcElement;
	if(src.tagName=='SPAN') {
		if(!$(src.parentNode).hasClass('clicked')){
			$('#main-menu-first-level').find('a').removeClass('clicked');
			$(src.parentNode).addClass('clicked');
			$('#main-menu-first-level').find('.inner-menu').slideUp('fast');
			$(src.parentNode.parentNode).find('ul').slideDown('fast');
		} else {
			$(src.parentNode).removeClass('clicked');
			$(src.parentNode.parentNode).find('ul').slideUp('fast');
		}
	}
}
function goLink(e) {
	var e=e||event;
	var src=e.target||e.srcElement;
	if(src.tagName=="A") {
		document.location=src.href;
	}
}
/*
var ReportForm = {
	posTop: 0,
	posLeft: 0,
	width: 0,
	height: 0,
	initialize: function() {
		ReportForm.width=$('#form-report').outerWidth();
		ReportForm.height=$('#form-report').outerHeight();
		ReportForm.posTop=ReportForm.height+$('#form-report-button').outerHeight()-15;
		ReportForm.posLeft=ReportForm.width-$('#form-report-button').outerWidth()+15;
		$('#form-report').css('left',-ReportForm.posLeft+'px');
		$('#form-report').css('top',-ReportForm.posTop+'px');
		$('#report-message-text').click(function(){
			if($(this).attr('value').length<1) {
				$(this).attr('value','');
				$(this).removeClass('default');
			}
		});
	},
	hideForm: function() {
		if($('#form-report').css('display')=='none') {
			$('#form-report').show();
			var URI=document.location;
			$('#ERROR_URL').attr('value',URI);
		} else {
			$('#form-report').hide();
		}
	}
}
*/

var ReportForm = {
	posTop: 0,
	posLeft: 0,
	width: 0,
	height: 0,
	initialize: function() {
		ReportForm.width=$('#form-report').outerWidth();
		ReportForm.height=$('#form-report').outerHeight();
		ReportForm.posTop=ReportForm.height+$('#form-report-button').outerHeight()-15;
		ReportForm.posLeft=ReportForm.width-$('#form-report-button').outerWidth()+15;
		$('#form-report').css('left',-ReportForm.posLeft+'px');
		$('#form-report').css('top',-ReportForm.posTop+'px');
		$('#report-form-submit').click(ReportForm.getContent);
	},
	hideForm: function() {
		if($('#form-report').css('display')=='none') {
			$('#form-report').show();
			var URI=document.location;
			$('#ERROR_URL').attr('value',URI);
		} else {
			$('#form-report').hide();
		}
	},
	getContent: function() {
	var str = $("form[name='error_message']").serializeArray();
	var ajxFile = "/bitrix/components/brainity/form.result.new/component.php";
	$.post(ajxFile,str,
		function(data){
		$('#form-report').hide();
		$('#report-message-text').val('');
		if (data.length > 0)
			showAllert("Спасибо за то, что вы нам помогаете! Редакция BRAINITY", 'Сообщение');
		});
	}
}



function showRegistrationForm() {
	if($('#registration-box').css('display')=='none') {
		$('#registration-box').slideDown();
		$('#reg-link').css('text-decoration','none');
		if($('#login-form').css('display')=='block') {
			$('#login-form').slideUp();
			$('#login-link').css('text-decoration','underline');
		}
	}
	else {
		$('#registration-box').slideUp();
		$('#reg-link').css('text-decoration','underline');
	}
}
function fuckIE6() {
	var app=navigator.appVersion;
	if(app.indexOf('MSIE 6.0')>0)
		showAllert('Для комфортного просмотра сайта, рекомендуем использовать более современный браузер.<br />Например Firefox 3, Chrome, Opera 10 или Internet Explorer 8.','Обратите внимание');
}
