
function $A(arrayish) {
var arr = [];
for (var i=0; i<arrayish.length; i++) {
arr.push(arrayish[i]);
};
return arr;
}


Array.prototype.each = function(callback) {
for (var i=0; i<this.length; i++) {
callback(this[i], i);
};
}

Evt = new Object();
Evt.extend = function(e) {
e.stop = function() {
cancelBubble(this);
cancelEvent(this);
}
return e;
}
Evt._handlers = {};
Evt.add = function(obj, type, fn) {
addEvent(obj, type, fn);
}
function addEvent(obj, type, fn) {
var wrappedFn = function(e) { fn(Evt.extend(e)); };
Evt._handlers[fn] = wrappedFn;
_addEvent(obj, type, wrappedFn);
}
function _addEvent(obj, type, fn) {
if (!obj) { return; };



if (obj instanceof Array) {
obj.each(function(item) { addEvent(item, type, fn) });
return;
};

if (type instanceof Array) {
type.each(function(item) { addEvent(obj, item, fn); });
return;
};
if (obj.addEventListener) {
switch (obj.tagName) { 
case "body":
var mappings = { mousewheel: "DOMMouseScroll", load: "DOMContentLoaded" };
default:
var mappings = { mousewheel: "DOMMouseScroll" };
};
if (mappings[type]) {
type = mappings[type];
};
obj.addEventListener( type, fn, false );
} else if (obj.attachEvent) {
var eName = "e"+type+fn;
var name = type+fn;

while (obj[eName]) {
eName += "_";
name += "_";
};
obj[eName] = fn;
obj[name] = function() { obj[eName]( window.event ); }
obj.attachEvent( "on" + type, obj[name] );
}
}
function cancelBubble(e) {
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
};
}
function cancelEvent(e) {
if (e.preventDefault) { 
e.preventDefault();
} else {
e.returnValue = false;
};
}
function getEventTarget(e) {
var target;
if (e.target) {
target = e.target;
} else if (e.srcElement) {
target = e.srcElement;
};
if (target.nodeType == 3) { // fix for Safari bug
target = target.parentNode;  
};
return target;
}
function removeEvent(obj, type, fn) {
var wrappedFn = Evt._handlers[fn];
if (obj.removeEventListener) {
if (type == "mousewheel") {
type = "DOMMouseScroll";
};
obj.removeEventListener( type, wrappedFn, false );
} else if (obj.detachEvent) {
obj.detachEvent( "on"+type, obj[type+wrappedFn] );
obj[type+wrappedFn] = null;
obj["e"+type+wrappedFn] = null;
}
};


function $(id) {
return document.getElementById(id);
}
function $T(id, tag) {
return $(id).getElementsByTagName(tag);
}
function $F(id) {
return $(id).value;
}
var page = new Object();
page.m = new Object();
page.v = new Object();
page.c = new Object();

page._forms = {};
page._inputFeatures = [];
page.addForm = function(id, form) {
page._forms[id] = form;
var target = form.getTarget();
if (target) {
addEvent($(target), "load", function(e) { form.onTargetLoad(e); });
};
}
page.getForm = function(id) {
return page._forms[id];
}
page.addInputFeature = function(feature) {
page._inputFeatures.push(feature);
}

page._components = {};
page._componentTypes = {};
page.addComponent = function(type, name, component) {
page._components[name] = component;
if (!page._componentTypes[type]) {
page._componentTypes[type] = [];
};
page._componentTypes[type].push(component);
}
page.getComponentsByType = function(type) {
return page._componentTypes[type] || [];
}
page.getComponent = function(name) {
return page._components[name];
}






page._loadHandlers = [];
page.c.onload = function(callback) {
var env = { m: {}, v: {}, c: {} };
page._loadHandlers.push({ callback: callback, env: env });
}
page.c.onLoad = function() {

};
page.c.onUnload = function() {

};
page.getXMLRPCGateway = function() {
return "xmlrpc.php";
};
addEvent(window, "unload", page.c.onUnload );
page.init = function() {
if (page._done) return;
page._done = true;
page._loadHandlers.each(function(handler) {
handler.callback(handler.env);
});
};
/* Mozilla/Firefox/Opera 9 */
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", page.init, false);
}
/* Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=\"__ie_onload\" defer=\"defer\" src=\"javascript:void(0)\"><\/script>");
var script = document.getElementById("__ie_onload");
if (script) {
script.onreadystatechange = function() {
if (this.readyState == "complete") {
page.init(); 
}
};
};
/*@end @*/
/* Safari */
if (/WebKit/i.test(navigator.userAgent)) {
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
clearInterval(_timer);
page.init();
}
}, 10);
}
window.onload = page.init;



Array.prototype.map = function(callback) {
var result = [];
this.each(function(item) {
result.push(callback(item));
});
return result;
}



Array.prototype.reduce = function(callback, seed) {
var result = seed;
this.each(function(item) {
result = callback(result, item);
});
return result;
}

Array.prototype.flatten = function() {
return this.reduce(function(result, item) {
return result.concat(item);
}, []);
}



Array.prototype.unique = function() {
var result = this.reduce(function(res, item) {
if (!res.found[item]) {
res.values.push(item);
res.found[item] = 1;
};
return res;
}, { values: [], found: {}});
return result.values;
}

function $A(item) {
if (item instanceof Array) {
return item;
};
var newItem = [];
for (var i=0; i<item.length; i++) {
newItem.push(item[i]);
};
return newItem;
} 



Array.prototype.filter = function(predicate) {
if (!predicate) {
predicate = function(item) { return item; };
};
var filtered = this.reduce(function(result, item) {
if (predicate && predicate(item) || !predicate && item) {
result.push(item);
};
return result;
}, []);
return filtered;
}
function isAncestorOf(parent, child) {
if (child.parentNode == parent) {
return true;
};
if (!child.parentNode) {
return false;
};
return isAncestorOf(parent, child.parentNode);
}







var Selector = {

find: function(compositeSelector, context) {
var selectorParts = compositeSelector.split(/\s*,\s*/);
var matches = selectorParts.map(function(simpleSelector) { return Selector.findSimple(simpleSelector, context); });
return matches.flatten();
},

findSimple: function(simpleSelector, context) {
var subselectors = simpleSelector.split(/\s+/);
return subselectors.reduce(Selector.applySubselector, [context || document.body])
},
applySubselector: function(nodes, subselector) {
var matches = subselector.match(/^(.*?)(?:([.#])(.*?))?$/);
var tagName = matches[1];
var subselectorType = matches[2];
var subselectorData = matches[3];
if (tagName != "") {
var keptNodes = nodes.filter(function(node) { return node.tagName == tagName; });
nodes = nodes.map(function(node) { return $A(node.getElementsByTagName(tagName)); }).flatten();
nodes = nodes.concat(keptNodes);
};
switch (subselectorType) {
case ".":
var matchRegexp = new RegExp("\\b" + subselectorData + "\\b");
nodes = nodes.filter(function(node) { 
return node.className.match(matchRegexp);
});
break;
case "#":
nodes = nodes.map(function(node) { 
if (node.getAttribute("id") == subselectorData) {
return [node];
};
var newNode = $(subselectorData);


if (!newNode ||
!isAncestorOf(node, newNode)) {
return [];
};
return [newNode];
}).flatten().unique();
break;
};
return nodes;
}
}

function $$(selector, context) {
var matches = Selector.find(selector, context);
return matches.length > 0 ? matches[0] : null;
}


Object.prototype.each = function(callback) {
for (var i in this) {
if (!Object.prototype[i] || 
Object.prototype[i] != this[i]) {
callback(this[i], i);
};
};
}


var Cls = {};
Cls.inherit = function(subClass, baseClass, methods) {
function inheritance() {};
inheritance.prototype = baseClass.prototype;
subClass.prototype = new inheritance();
subClass.prototype.constructor = subClass;
subClass.baseConstructor = baseClass;
subClass.superClass = baseClass.prototype;
if (methods) { 
this.extend(subClass, methods);
};
}

Cls.extend = function(destination, source) {
source.each(function(value, key) {
destination.prototype[key] = value;
});  
}

function Autoscroll(eContainer, eScroll, eData) {
this._eContainer = eContainer;
this._eData = eData;
this._eScroll = eScroll;
eScroll.style.display = "block";
eScroll.style.position = "relative";
eScroll.style.width = "10000px";
eData.style.cssFloat = "left";
eData.style.width = "auto";
this._pos = 0;
this._stepValue = -1;
this._timeoutValue = 1000 / 20; // 1/20 of second
this._timeoutHandle = null;
}
Cls.inherit(Autoscroll, Object, {
run: function() {
this.stop();
if (this._eContainer.offsetWidth < this._eData.offsetWidth) {
this.step();
};
},
step: function() {
var scroll = this;
this._pos += this._stepValue;
if (this._eData.offsetWidth < -this._pos) {
this._pos = this._eContainer.clientWidth;
};
this._eScroll.style.left = this._pos.toString() + "px";
this._timeoutHandle = setTimeout(function() { scroll.step(); }, this._timeoutValue);
},
stop: function() {
if (this._timeoutHandle) {
clearTimeout(this._timeoutHandle);
};
this._timeoutHandle = null;
}
})



page.c.onload(function(e) {
var newsRoot = $$("span.hotnews");
new Autoscroll($$("span.content", newsRoot), $$("span.scrollable", newsRoot), $$("span.data", newsRoot)).run();
});

