//Created and developed by jj
//kamil.surowka@gmail.com

function get_css(obj,value){
	var w;

	if (!window.getComputedStyle) {
		window.getComputedStyle = function(el, pseudo) {
			this.el = el;
			this.getPropertyValue = function(prop) {
				var re = /(\-([a-z]){1})/g;
				if (prop == 'float') prop = 'styleFloat';
				if (re.test(prop)) {
					prop = prop.replace(re, function () {
						return arguments[2].toUpperCase();
						});
					};
				return el.currentStyle[prop] ? el.currentStyle[prop] : null;
				};
			return this;
			};
	};

	w = window.getComputedStyle(obj,null).getPropertyValue(value);

	return parsePX(w);
}

function parsePX(value) {
	value.replace('px','');
	return parseInt(value);
};

function parseToPX(value) {
	var s = new String(value);
	return s+'px';
};
function Queue(elements) {
	this.elements = elements;
};

Queue.prototype.shiftLeft = function() {
	var temp_list = new Array();
	for ( var i = 0; i < this.elements.length-1; i++) {
		var elem = this.elements[i];
		temp_list[i+1] = elem;
	}
	temp_list[0] = this.elements[this.elements.length-1];
	this.elements = temp_list;
};

function ScrollNewsController(parent_div,step,step_time,space,elements) {
	this.elements = elements;
	this.parent_div_id = parent_div;
	this.step = step;
	this.step_time = step_time;
	var diff = space;
	this.scroll_news_tab = [];
	for ( var i = 0; i < this.elements.length; i++) {
		var elem = this.elements[i];
		this.scroll_news_tab[i] = new ScrollNews(this.parent_div_id,this.step,this.step_time,elem,i*diff,this);
	}
}

ScrollNewsController.prototype.stop = function() {

	for ( var i = 0; i < this.scroll_news_tab.length; i++) {
		var elem = this.scroll_news_tab[i];
		elem.stop();
	}
};

ScrollNewsController.prototype.start = function() {
	
	for ( var i = 0; i < this.scroll_news_tab.length; i++) {
		var elem = this.scroll_news_tab[i];
		elem.start();
	}
};


function ScrollNews(display_elem_id,step,step_time,text,actual_pos,controller) {
	this.controller = controller;
	this.display_elem_id = display_elem_id;
	this.display_elem = document.getElementById(this.display_elem_id);
	var div = document.createElement('div');
	div.innerHTML = text;
	this.display_elem_parent = this.display_elem;
	this.display_elem.appendChild(div);
	this.display_elem = div;
	this.stop_start = false;
	this.display_elem.style.position = 'relative';
	this.display_elem.style.float = 'left';
	this.text = text;
	this.step = step;
	this.step_time = step_time;

	this.static_pos = actual_pos;
	if(actual_pos == undefined){
		this.actual_left_pos = 0;
	}else{
		this.actual_left_pos = actual_pos;
	}
	this.addEvent();
	this.shiftLeft(this.step_time);
	this.width = get_css(this.display_elem,'width');

};

ScrollNews.prototype.stop = function() {
	this.stop_start = true;
};

ScrollNews.prototype.start = function() {
	this.stop_start = false;
};

ScrollNews.prototype.stopFun = function() {
	setTimeout(function() {
			
	}, 100);
};

ScrollNews.prototype.addEvent = function(){
	var parent = this;
	this.display_elem.onmouseover = function() {
		parent.controller.stop();
	};
	this.display_elem.onmouseout = function() {
		parent.controller.start();
	};
};

ScrollNews.prototype.shiftLeft = function(time_ms) {
	var parent = this;
	var st = setTimeout(function() {
		
		if(!parent.stop_start){
			parent.periodFun();
		}
		
		parent.shiftLeft(time_ms);
	}, time_ms);
};

ScrollNews.prototype.get_parent_div_width = function() {
	return get_css(this.display_elem_parent, 'width');
};

ScrollNews.prototype.periodFun = function() {
	this.actual_left_pos += this.step;
	this.set_display_elem_pos(this.actual_left_pos);

	if(this.actual_left_pos > (this.get_parent_div_width()) ){
		this.actual_left_pos = -this.width;
	}
};



ScrollNews.prototype.get_display_elem_pos = function() {
	return parsePX(this.display_elem.style.left);
};

ScrollNews.prototype.set_display_elem_pos = function(pos) {
	this.display_elem.style.left = pos+'px';
};

