/*	RealArcade.com Script, version 0.1.0.
 *	(c) 2008 RealArcade.com
 *
 *--------------------------------------------------------------------------*/

// START - Unobtrusive Events //
// ************************** //
document.observe("dom:loaded", function() {
	new Tabs('home_carousel_nav');
});

Event.observe(window, 'load', function() {
	observeClickArray($$('.carousel_next_button_top_games img'),carouselNextButtonTop);
	observeClickArray($$('.carousel_previous_button_top_games img'),carouselPrevButtonTop);
	observeClickArray($$('.carousel_next_button_community_games img'),carouselNextButtonComm);
	observeClickArray($$('.carousel_previous_button_community_games img'),carouselPrevButtonComm);
	observeClickArray($$('.carousel_next_button_skill_games img'),carouselNextButtonSkill);
	observeClickArray($$('.carousel_previous_button_skill_games img'),carouselPrevButtonSkill);
	observeClickArray($$('.carousel_next_button_free_forever img'),carouselNextButtonFree);
	observeClickArray($$('.carousel_previous_button_free_forever img'),carouselPrevButtonFree);
	observeClickArray($$('.accordion_toggle'),homeAccordion,1);
});

// ************************ //
// END - Unobtrusive Events //

// START initializers //
// ****************** //
function carouselNextButtonTop() { moveCarousel('next', 'top_games_01'); return false; }
function carouselPrevButtonTop() { moveCarousel('prev', 'top_games_01'); return false; }
function carouselNextButtonComm() { moveCarousel('next', 'community_games_01'); return false; }
function carouselPrevButtonComm() { moveCarousel('prev', 'community_games_01'); return false; }
function carouselNextButtonSkill() { moveCarousel('next', 'skill_games_01'); return false; }
function carouselPrevButtonSkill() { moveCarousel('prev', 'skill_games_01'); return false; }
function carouselNextButtonFree() { moveCarousel('next', 'free_forever_01'); return false; }
function carouselPrevButtonFree() { moveCarousel('prev', 'free_forever_01'); return false; }
// **************** //
// END initializers //


// START Event Observe Functions //
// ***************************** //


function observeClickStringId(elementIdString, funct) {
/*{ "Summary": "Attached an element to a function for unobtrusive events.",
	"Arguments": {
		"elementIdString": "Accepts a string for an element ID.",
		"funct": "Accepts a function.",
	}
}  */
	if (Object.isString(elementIdString))
	{
		if (Object.isElement($(elementIdString))) { Event.observe($(elementIdString),'click',funct); }
		else { return false; }
	}
	else { return false; }
}


function observeClickArray(elementArray, funct) {
/*{ "Summary": "Attached an element to a function for unobtrusive events.",
	"Arguments": {
		"elementArray": "Accepts an array of elements.",
		"funct": "Accepts a function.",
		"bind": "If set to 1 creates a event binding to the element that triggered the event."
	}
}  */
	if (Object.isArray(elementArray) && Object.isElement(elementArray[0]))
	{
		for(var i=0;i < elementArray.length;i++) { 
			Event.observe(elementArray[i],'click',funct.bindAsEventListener(this));
		}
	}
	else { return false; }
}


function observeClickElement(element, funct) {
/*{ "Summary": "Attached an element to a function for unobtrusive events.",
	"Arguments": {
		"element": "Accepts an element.",
		"funct": "Accepts a function.",
	}
}  */
	if (Object.isElement(element)) { Event.observe(element,'click',funct); }
	else { return false; }
}


// **************************** //
// END	Event Observe Functions //

function moveCarousel(direction, id) {
/*{ "Summary": "Scrolls carousel panels.",
	"Arguments": {
		"direction": "Direction of scroll, accepts "prev" and "next".",
		"id": "The ID of the element to scroll."
	}
}  */
	var carousel = $(id);
	var slides = carousel.childElements();
	var total = slides.length;
	var width = $(slides[0]).getWidth();
	// If element is not offset set x to 0 otherwise set x to offset amount.
	var x = ($(id).getStyle('left') == null) ? '0px' : $(id).getStyle('left');
	x = String(x.substring(0, (x.length - 2)));
	// detect next button click and set slide number.
	if (direction == 'next') { 
		var slide = (x == 0) ? 2 : ((Math.abs(x) / width) + 2);
	}
	// detect previous button click and set slide number.
	else if (direction == 'prev') { 
		var slide = (Math.abs(x) / width);
	}
	else {
		return false;
	}
	if (slide != Math.round(slide)) return false;
	// If at beginning or end of slide sequence return false.
	if (!$(slides[slide - 1])) {
		if (x !== '0') {

			new Effect.MoveBy(id, 0, 0 , { x: (width*total)-width, y: 0, duration: 0.7,	transition: Effect.Transitions.sinoidal});
			return true;
		}
		else {
			new Effect.MoveBy(id, 0, 0 , { x: -(width*total)+width, y: 0, duration: 0.7,	transition: Effect.Transitions.sinoidal});
			return true;
		}
	}
	if (direction == 'next') {
		if (x > -(width * (total - 1))) {
			new Effect.MoveBy(id, 0, 0 , { x: -width, y: 0, duration: 0.7,	transition: Effect.Transitions.sinoidal});
			return true;
		}
	} 
	else if (direction == 'prev') {
		if (x < 0) {
			new Effect.MoveBy(id, 0, 0 , { x: width, y: 0, duration: 0.7,  transition: Effect.Transitions.sinoidal});
			return true;
		}
	} 
	else {
		return false;
	}
	return false;
}


function accordion(e) {
/*{ "Summary": "Left nav drawer constructor.",
	"Arguments": {
		"e": "An event."
	}
}  */
	this.e = e;
	this.triggers = $$('.accordion_toggle');
	this.containers = $$('.accordion_content');
	this.isClick = false;
	if (this.e !== undefined) {
		// find the object that the event was fired in.
		this.t = Event.element(this.e);
		this.isClick = true;
	}
	
	this.accordionSetView = accordionSetView;
}

function homeAccordion(e) {
	var thisAccordion = new accordion(e);
	thisAccordion.accordionSetView();
}

function accordionSetView() {
	if (this.t.next().hasClassName('on')) {
		return;
	}
	for(var i=0;i < this.triggers.length;i++) {
		this.headerID = this.triggers[i];
		this.drawerID = this.triggers[i].next();
		// On + Click
		if (this.drawerID.hasClassName('on') && (this.t == this.triggers[i])) {
			this.headerID.addClassName('head_on');
			this.drawerID.addClassName('on');
		}
		// On + Not Click
		if (this.drawerID.hasClassName('on') && (this.t !== this.triggers[i])) {
			this.headerID.removeClassName('head_on');
			this.drawerID.removeClassName('on');
		}
		// Off + Click
		if (!this.drawerID.hasClassName('on') && (this.t == this.triggers[i])) {
			this.headerID.addClassName('head_on');
			this.drawerID.addClassName('on');
		}
		// Off + NotC lick
		if (!this.drawerID.hasClassName('on') && (this.t !== this.triggers[i])) {
			this.headerID.removeClassName('head_on');
			this.drawerID.removeClassName('on');
		}
	}
}

var Tabs = Class.create();
Tabs.prototype = {
	initialize : function(element) {
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('li'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "li");
		Event.stop(ev);
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
	},
	hide : function(elm) {
		$(elm).removeClassName('active_tab');
		$(this.tabID(elm)).removeClassName('active_tab_body');
	},
	show : function(elm) {
		$(elm).addClassName('active_tab');
		var tabIdBody = this.tabID(elm) + "_body";
		$(this.tabID(elm)).addClassName('active_tab_body');
	},
	tabID : function(elm) {
		return elm.identify().gsub('_tab', '');
	},
	getInitialTab : function() {
		return this.menu.first();
	}
}