/* RedRC main javascript. uses a lot of code that is copied from tweakers.net, but also from ProtoType framework. */


// Simply method that eliments the use of document.getElementById(); also does cachining.
var idCache = {};
function getById(id) {
	if ( ! (id in idCache)) {
		idCache[id] = document.getElementById(id);
	}
	return idCache[id];
}


// Add a extend method to Object. This makes it easy to add methods (see below).
Object.extend = function(dest, source, allowOverwrite) {
	for (var prop in source)
	{
		if (source.hasOwnProperty(prop) && (allowOverwrite || !dest.hasOwnProperty(prop))) {
			dest[prop] = source[prop];
		}
	}
	return dest;
}



Object.extend(Array.prototype,
{
	indexOf: function(searchElement, fromIndex) {
		var l = this.length, i = 0;
		if (fromIndex) {
			i = fromIndex;
			if (i < 0) {
				i += l;
				if (i < 0) i = 0;
			}
		}

		while (i < l) {
			if (this[i] === searchElement) return i;
			i++;
		}
		return -1;
	},
	forEach: function(func, obj) {
		for (var i = 0, l = this.length; i < l; i++) {
			if (i in this) func.call(obj, this[i], i, this);
		}
	},
	filter: function(func, obj) {
		var res = [], val;
		for (var i = 0, l = this.length; i < l; i++) {
			if (i in this) {
				val = this[i]; // in case func mutates this
				if (func.call(obj, val, i, this)) res.push(val);
			}
		}
		return res;
	}
});

['forEach', 'filter', 'slice'].forEach(
	function(func) {
		if (!(func in Array)) {
			Array[func] = function(obj) {
				return this.prototype[func].apply(obj, Array.prototype.slice.call(arguments, 1));
			}
		}
	}
);


Object.extend(Function.prototype, {
	bind: function() {
		var handler = this;
		var args = Array.slice(arguments, 0);
		var obj = args.shift();
		return function() {
			return handler.apply(obj, args.concat(Array.slice(arguments, 0)));
		}
	}
});


function FeaturedItems() {
	this.liItems = [];
	this.imageItems = [];
	this.activeItem = 0;
	this.fadeTimer = null;
	this.nextItem = null;
	this.fadeOpacity = 0;

	this.liDiv = getById("FeaturedItems");

	this.init();
}

Object.extend(FeaturedItems.prototype, {
	init: function() {
		liDiv = getById("FeaturedItems");
		this.liItems = liDiv.getElementsByTagName('li');

		for (i = 0; i < this.liItems.length; i++) {
			li = this.liItems[i];
			featuredItemId = li.getAttribute("id").substr(3);
			this.imageItems[i] = getById(featuredItemId);
			
			// Initalize them all to none.
			if (i != this.activeItem) {
				this.imageItems[i].style.display="none";
			} else {
				this.liItems[i].setAttribute("class", "active");
				this.liItems[i].setAttribute("className", "active");
			}
			this.imageItems[i].style.opacity = "0";
			this.imageItems[i].style.filter = "alpha(opacity=0)";
			if (li.addEventListener) {
				li.addEventListener("click", this.mouseClick.bind(this, i), false);
			} else {
				li.attachEvent("onclick", this.mouseClick.bind(this, i));
			}
		}

		this.fadeTimer = setTimeout(this.fadeIn.bind(this), 12);

	},
	mouseClick: function(itemNr) {
		this.nextItem = itemNr;
		if (itemNr != this.activeItem) {
			this.fadeOut();
		}
	},
	setOpacity: function () {
		if (this.fadeOpacity < 10) {
			this.imageItems[this.activeItem].style.opacity = "0.0" + this.fadeOpacity;
		} else {
			this.imageItems[this.activeItem].style.opacity = "0." + this.fadeOpacity;
		}
		this.imageItems[this.activeItem].style.filter = "alpha(opacity=" + this.fadeOpacity + ")";
	}, 
	setFadeTimer: function(method, time) {
		if (this.fadeTimer) {
			clearTimeout(this.fadeTimer);
			this.fadeTimer = null;
		}

		if (method == "out") {
			this.fadeTimer = setTimeout(this.fadeOut.bind(this), time);
		} else {
			this.fadeTimer = setTimeout(this.fadeIn.bind(this), time);
		}
	},
	fadeOut: function() {
		if (this.nextItem != null && this.fadeOpacity > 3) {
			this.fadeOpacity--;
		}
		this.fadeOpacity--;
		this.setOpacity();

		if (this.fadeOpacity <= 0) {
			this.imageItems[this.activeItem].style.display="none";
			this.imageItems[this.activeItem].style.visibility = "hidden";
			this.liItems[this.activeItem].setAttribute("class", "");
			this.liItems[this.activeItem].setAttribute("className", "");
			if (this.nextItem != null) {
				this.activeItem = this.nextItem;
			} else {
				this.activeItem++;
				if ( ! this.imageItems[this.activeItem]) {
					this.activeItem = 0;
				}
			}
			this.setFadeTimer("in", 100);

			this.imageItems[this.activeItem].style.display="";
			this.imageItems[this.activeItem].style.visibility = "visible";
			this.liItems[this.activeItem].setAttribute("class", "active");
			this.liItems[this.activeItem].setAttribute("className", "active");
		} else {
			this.setFadeTimer("out", 5);
		}
	}, 
	fadeIn: function() {
		if (this.nextItem != null && this.fadeOpacity < 98) {
			this.fadeOpacity++;
		}
		this.fadeOpacity++;
		this.setOpacity();
		
		if (this.fadeOpacity >=99) {
			this.nextItem = null;
			this.setFadeTimer("out", 4500);
		} else {
			this.setFadeTimer("in", 5);
		}
	}
});


function ChangeFont(size) {
	SetCookie("RedRcArticleFontSize", size);

	var rows = document.getElementsByTagName('link');
	for(var i=0, row; row = rows[i]; i++)
	{
		if (row.getAttribute("type") == "text/css") {
			title = row.getAttribute("title");
			if (title.indexOf("Font") != -1) {
				row.disabled = true;
			}
		}
	}
	for(var i=0, row; row = rows[i]; i++)
	{
		if (row.getAttribute("type") == "text/css") {
			title = row.getAttribute("title");
			if (title == size) {
				row.disabled = false;
			}
		}
	}
}

function SetCookie(name, value, days) {
	if (days == null) {
		days = 365;
	}

	var expireDate=new Date();
	expireDate.setDate(expireDate.getDate()+days);

	document.cookie = name + "=" + escape(value) + ";expires=" + expireDate.toGMTString() + ";path=/;domain=redrc.net;";
}

function GetCookie(name)
{
	if (document.cookie.length > 0)
	{
		start=document.cookie.indexOf(name + "=");
		if (start != -1)
		{ 
			start = start + name.length + 1; 
			end = document.cookie.indexOf(";", start);
			if (end == -1) {
				end=document.cookie.length;
			}
			return unescape(document.cookie.substring(start,end));
		} 
        }
	return "";
}


function SetupFontsize() {
	currentFont = GetCookie("RedRcArticleFontSize");
	if (currentFont != null && currentFont != "") {
		ChangeFont(currentFont);
	}
}

/* Dynacmic width stuff */

function getBrowserWidth(){
	if (window.innerWidth) 
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0) 
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	return 1000;
}



function SetWidth() {
	var width = getBrowserWidth();

	if (width > 1200) {
		changeLayout("wide");
	} else if (width <= 1200) {
		changeLayout("default");
	}

}


// http://www.alistapart.com/articles/alternate/
function changeLayout(description){
	var rows = document.getElementsByTagName('link');
	for(var i=0, row; row = rows[i]; i++)
	{
		if (row.getAttribute("type") == "text/css") {
			row.disabled = true;
			if (row.getAttribute("title") == description || row.getAttribute("title") == "default") {
				row.disabled = false;
			}
		}
	}
}


if (window.addEventListener) {
	window.addEventListener("resize", SetWidth, false);
} else {
	window.attachEvent("onresize", SetWidth);
}

// Set the width of the page, this used to be done int he onload but that made it very slow.
SetWidth();
SetupFontsize();

