document.observe('dom:loaded', function(){
	$$('.hot_line').each(function(element){
		window.onresize = function(){ scrollReset(); };
		scrollReset();
	});
	$$('.show_form').each(function(element){
		element.onclick = function(){
			$('c_form').show();
			$('dark').observe('click', function(){ $('c_form').hide(); });
			$$('a[class="close"]').each(function(element){ element.onclick = function(){ $('c_form').hide(); return false; } });
			return false;
		}
	});
});

function sendMessageText(){
	var text = trim($('se_text').value);
	if (text!=''){
		$('se_email').writeAttribute('disabled',true);
		$('se_name').writeAttribute('disabled',true);
		$('se_text').writeAttribute('disabled',true);
		$('messagetextbutt').hide();
		$('messagetextwait').show();
		var a = new ajaxInit('/inc/sendmessage.php','sendMessageTextOnLoad();','');
		a.sendPost(preparePostVars({'name':trim($('se_name').value),'text':trim(text),'email':trim($('se_email').value)}));
	}
}
function sendMessageTextOnLoad(){
	$('messagetextwait').hide();
	$('messagetextsend').show();
	setTimeout("sendMessageTextReady()",5000);
}
function sendMessageTextReady(){
	$('se_text').value = '';
	$('se_email').writeAttribute('disabled',false);
	$('se_name').writeAttribute('disabled',false);
	$('se_text').writeAttribute('disabled',false);
	$('messagetextsend').hide();
	$('messagetextbutt').show();
}
function getActiveRubric(){
	var retval = false;
	var list = $('hot_scroll').childElements();
	for (var i=0; i<list.length; i++) if (list[i].style.display!='none') retval = list[i].id;
	return retval;
}

function hotChangeRubric(obj){
	var rubric = obj.rel;
	var activeRubric = getActiveRubric();
	if (activeRubric){
		$(activeRubric+'_li').removeClassName('active');
		$(activeRubric).hide();
	}
	$(rubric+'_li').addClassName('active');
	$(rubric).show();
	scrollReset();
}

function trim(s){ return s.replace(/^(\s*)/,"$`").replace(/(\s*)$/,"$'"); }
function ajaxInit(url,oncomplete,onerror){
	url += ((url.indexOf('?')>-1)?'&':'?')+'uniqueID=' + (new Date()).getTime();
	var req = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
	req.onreadystatechange = processRequest;
	function processRequest(){
		if (req.readyState == 4){
			if (req.status == 200){
				var response = req.responseText;
				if (oncomplete!='') eval(oncomplete);
			}
			else eval(onerror);
		}
	}
	this.sendPost = function(postStr){
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(postStr);
	}
}
function preparePostVars(postVars){
	var first = new Boolean(true);
	var postStr = '';
	for (var pkey in postVars) {
		if (!first) postStr += "&";
		postStr += pkey + "=" + encodeURI(postVars[pkey]);
		first = false;
	}
	return postStr;
}
addEvent = function(obj,event,func){
	if(obj.addEventListener){ obj.addEventListener(event,func,false); return true; }
	else{ if(obj.attachEvent){ obj.attachEvent("on"+event,func); return true; } }
	return false;
}

var scrollDirection;
var scrollSpeed = 10;

function startScroll(){
	if (scrollDirection == 'left'){
		if (parseInt($('hot_scroll').style.left,10) < 0) {
			$('hot_scroll').style.left = parseInt($('hot_scroll').style.left,10)+scrollSpeed+'px';
		}
	}
	if (scrollDirection == 'right'){
		if (parseInt($('hot_scroll').style.left,10) > -maxScroll) {
			$('hot_scroll').style.left = parseInt($('hot_scroll').style.left,10)-scrollSpeed+'px';
		}
	}
}

function scrollReset(){
	$('hot_scroll').style.left = '0px';
	maxScroll = $(getActiveRubric()).getWidth() - $('hot_line').getWidth();
	if (maxScroll > 0){
		$('hl_left').show();
		$('hl_right').show();
	}else{
		$('hl_left').hide();
		$('hl_right').hide();
	}
}

function sLeft(){
	scrollDirection = 'left';
	scroller = new PeriodicalExecuter(startScroll, 0.01);
}
function sLeftStop(){
	scroller.stop();
}
function sRight(){
	scrollDirection = 'right';
	scroller = new PeriodicalExecuter(startScroll, 0.01);
}
function sRightStop(){
	scroller.stop();
}

/* jQuery */

var timer;
var maxScroll;
var sto;
var direction;

function setTimer() {
	timer = (jQuery("#hot_scroll table:visible td").length+1)*1000;
}

function setMaxScroll() {
	if (jQuery("#hot_scroll").width() > jQuery("#hot_scroll table:visible").width()) {
		maxScroll = 0
	} else {
		maxScroll = jQuery("#hot_scroll table:visible").width() - jQuery("#hot_scroll").width();
	}
}

function autoScrollRight() {
	jQuery(function() {
		jQuery("#hot_scroll").animate({left: -1*maxScroll}, timer, function() {direction = "left"; clearTimeout(sto); sto = setTimeout(autoScrollLeft, 5000)});
	});
}

function autoScrollLeft() {
	jQuery(function() {
		jQuery("#hot_scroll").animate({left: 0}, timer, function() {direction = "right"; clearTimeout(sto); sto = setTimeout(autoScrollRight, 5000)});
	});
}

jQuery(document).ready(function() {
	setTimer();
	setMaxScroll();
	direction = "right";
	setTimeout(autoScrollRight, 5000);
	
	jQuery("#hl_left").click(function() {
		jQuery("#hot_scroll").stop();
		clearTimeout(sto);
		if (direction == "left") {
			sto = setTimeout(autoScrollLeft, 5000);
		} else {
			sto = setTimeout(autoScrollRight, 5000);
		}
		if (jQuery("#hot_scroll").position().left >= 0) {
			clearTimeout(sto);
			sto = setTimeout(autoScrollRight, 5000);
			direction = "right";
		}
	});
	jQuery("#hl_right").click(function() {
		jQuery("#hot_scroll").stop();
		clearTimeout(sto);
		if (direction == "left") {
			sto = setTimeout(autoScrollLeft, 5000);
		} else {
			sto = setTimeout(autoScrollRight, 5000);
		}
		if (jQuery("#hot_scroll").position().left <= -1*maxScroll) {
			clearTimeout(sto);
			sto = setTimeout(autoScrollLeft, 5000);
			direction = "left";
		}
	});
	jQuery(".hot-cats").click(function() {
		clearTimeout(sto);
		jQuery("#hot_scroll").stop();
		setTimer();
		setMaxScroll();
		jQuery("#hot_scroll").css({left: 0});
		sto = setTimeout(autoScrollRight, 5000);
		direction = "right";
	});
})

jQuery(window).resize(function() {
	setMaxScroll();
	clearTimeout(sto);
	jQuery("#hot_scroll").stop();
	if (direction == "left") {
		autoScrollLeft();
	} else {
		autoScrollRight();
	}
})
