var newCompanysInit = {
	data: [],
	current_position: 0,
	button_left : false,
	button_right : false
};

newCompanysInit.run = function(data) {
	this.data = data;
	if(!data.length) {
		$("#newCompanysTitle").remove();
		$("#newCompanysBlock").remove();
		return;
	}
	this.initButtons();
	this.makeButtons();
	this.makeCompany();
	this.makeHiddenInput();
};

newCompanysInit.makeHiddenInput = function() {
	this.hidden_input = $("<input type=\"text\">");
	this.hidden_input.css({
		'position' : 'absolute',
		'top' : $("#newCompanysBlockLogo").position().top,
		'left' : '-90px',
		'width' : '1px'
	});
	this.hidden_input.appendTo($("body"));
};

newCompanysInit.hideFocus = function() {
	this.hidden_input.get(0).focus();
};

newCompanysInit.makeCompany = function() {
	if(this.data[this.current_position][3] != "0") {
		$("#newCompanysBlockLogo")
			.attr('src', "/i/images/" + this.data[this.current_position][3] + "_0.png")
			.css('display', 'inline');
		$("#newCompanysBlock .title_company").css('display', 'none');
	} else {
		$("#newCompanysBlockLogo").css('display', 'none');
		$("#newCompanysBlock .title_company")
			.css('display', 'block')
			.text(this.data[this.current_position][1]);
	}
	$("#newCompanysBlockName").text(this.data[this.current_position][1]);
	$("a.newCompanysBlockLink").attr('href', this.data[this.current_position][2]);
	$("#newCompanysBlockWww")
		.attr('href', '/ref.php?u=http://' + this.data[this.current_position][4] + '&cid=' + this.data[this.current_position][0])
		.text(this.data[this.current_position][4])
		.css('display', (this.data[this.current_position][4] != '')? 'inline' : 'none');
};

newCompanysInit.initButtons = function() {
	var _this = this;
	
	$("#newCompanysBlockLeftButton").hover(function(){
		if(newCompanysInit.button_left) {
			$(this).css('background-position', '0px -96px');
		}
	}, function(){
		if(newCompanysInit.button_left) {
			$(this).css('background-position', '0px -48px');
		}
	}).mousedown(function(){
		if(newCompanysInit.button_left) {
			$(this).css('background-position', '0px -144px');
		}
		_this.hideFocus();
	}).mouseup(function(){
		if(newCompanysInit.button_left) {
			$(this).css('background-position', '0px -96px');
		}
		_this.hideFocus();
	}).click(function(){
		if(newCompanysInit.button_left) {
			newCompanysInit.leftButtonClick();
		}
		_this.hideFocus();
	}).focus(function(){
		_this.hideFocus();
	});
	
	$("#newCompanysBlockRightButton").hover(function(){
		if(newCompanysInit.button_right) {
			$(this).css('background-position', '0px -96px');
		}
	}, function(){
		if(newCompanysInit.button_right) {
			$(this).css('background-position', '0px -48px');
		}
	}).mousedown(function(){
		if(newCompanysInit.button_right) {
			$(this).css('background-position', '0px -144px');
		}
		_this.hideFocus();
	}).mouseup(function(){
		if(newCompanysInit.button_right) {
			$(this).css('background-position', '0px -96px');
		}
		_this.hideFocus();
	}).click(function(){
		if(newCompanysInit.button_right) {
			newCompanysInit.rightButtonClick();
		}
		_this.hideFocus();
	}).focus(function(){
		_this.hideFocus();
	});
};

newCompanysInit.makeButtons = function() {
	// leftButton
	if(this.current_position == 0) {
		$("#newCompanysBlockLeftButton")
			.css('cursor', 'default')
			.css('background-position', '0px 0px');
		this.button_left = false;
	} else {
		$("#newCompanysBlockLeftButton")
			.css('cursor', 'pointer')
			.css('background-position', '0px -48px');
		this.button_left = true;
	}
	
	// rightButton
	if(this.current_position != (this.data.length - 1)) {
		$("#newCompanysBlockRightButton")
			.css('cursor', 'pointer')
			.css('background-position', '0px -48px');
		this.button_right = true;
	} else {
		$("#newCompanysBlockRightButton")
			.css('cursor', 'default')
			.css('background-position', '0px 0px');
		this.button_right = false;
	}
};

newCompanysInit.leftButtonClick = function() {
	this.current_position --;
	this.makeButtons();
	this.makeCompany();
};

newCompanysInit.rightButtonClick = function() {
	this.current_position ++;
	this.makeButtons();
	this.makeCompany();
};



