//User Grid Toggle
function UserGrid(i) {
	$('networkGrid','networkTile').invoke('toggle');
	jar = new CookieJar({domain: '.artician.com', path: '/networks'},"ArticianNetworks_");
	jar.empty();
	if (i==0) { jar.put('view', 1); }
	else { jar.put('view', 0); }
	if (i==1) { $('NetUser').className='ugl'; $('networkGrid').hide(); $('networkTile').show(); }
	else { $('NetUser').className='utl'; $('networkTile').hide(); $('networkGrid').show();}
}

//Browse Node 01
function b1Node(id) {new Ajax.Updater('ExplorebNode1', '/modules/nodes/browse/design.php?browse='+id,{asynchronous:true,evalScripts:true });	}
function b1NodeTab(id) {
	tab = new Array('b1Node_1','b1Node_2','b1Node_3','b1Node_4','b1Node_5','b1Node_6','b1Node_7','b1Node_8','b1Node_9');
	activeClass = 'current';
	inactiveClass = null;
	for(i=0;i<tab.length;i++) {
		if(i==(id-1)) {setObjClass(tab[i],activeClass);}
		else {setObjClass(tab[i],inactiveClass);}
	}
}

//Browse Node 02
function b2Node(id) {new Ajax.Updater('ExplorebNode2', '/modules/nodes/browse/photography.php?browse='+id,{asynchronous:true,evalScripts:true });	}
function b2NodeTab(id) {
	tab = new Array('b2Node_1','b2Node_2','b2Node_3','b2Node_4','b2Node_5','b2Node_6','b2Node_7','b2Node_8');
	activeClass = 'current';
	inactiveClass = null;
	for(i=0;i<tab.length;i++) {
		if(i==(id-1)) {setObjClass(tab[i],activeClass);}
		else {setObjClass(tab[i],inactiveClass);}
	}
}

function spreadSlider(id){
	//$(id).className='';
	vpw = document.viewport.getWidth();
	
	if(vpw < 1120){
		$(id).addClassName('resA'); $(id).removeClassName('resB'); $(id).removeClassName('resC');
	}
	else if(vpw > 1129 && vpw < 1240){
		$(id).addClassName('resB'); $(id).removeClassName('resA'); $(id).removeClassName('resC');
	}
	else if(vpw > 1240){
		$(id).addClassName('resC'); $(id).removeClassName('resA'); $(id).removeClassName('resB');
	}
}

function goToBlogCat(url) {parent.location=url}

/**
 * @author Bruno Bornsztein <bruno@missingmethod.com>
 * @copyright 2007 Curbly LLC
 * @package Glider
 * @license MIT
 * @url http://www.missingmethod.com/projects/glider/
 * @version 0.0.3
 * @dependencies prototype.js 1.5.1+, effects.js
 * @author Brandon Lis <knightar.artician.com>
 */
/* Edited for use with Ajax View Widget - Brandon Lis */
/*  Thanks to Andrew Dupont for refactoring help and code cleanup - http://andrewdupont.net/  */

Glider = Class.create();
Object.extend(Object.extend(Glider.prototype, Abstract.prototype), {
	initialize: function(wrapper, options){
		this.scrolling  = false;
		this.wrapper    = $(wrapper);
		this.scroller   = this.wrapper.down('div.scroller');
		this.sections   = this.wrapper.getElementsBySelector('div.section');
		this.content	= this.scroller.down('div.content');
		this.options    = Object.extend({ prefix:'', preloaded: new Hash({}), amount: 3, duration: 1.0, frequency: 3, totalindex: 1, csubmission: 0, user: 0, currentindex: 1, path: '/ajax/view_widget2/' }, options || {});
		
		var x = 1;
		while (x <= this.options.totalindex){
			$(this.content).insert({ bottom: '<div id="'+this.options.prefix+'section'+x+'" class="section"></div>' });
			x++;
		}
		
		this.currentIndex = this.options.currentindex;
		this.options.initialSection=this.options.prefix+'section'+this.currentIndex;
		
		this.ajaxNext = this._ajaxNext.bindAsEventListener(this);
		this.ajaxPrevious = this._ajaxPrevious.bindAsEventListener(this);
		this.unhide = this._unhide.bindAsEventListener(this);
		this.jumpto = this._jumpto.bindAsEventListener(this);
		this.ajaxJump = this._ajaxJump.bindAsEventListener(this);
		
		var data = this.options.preloaded.get('section'+this.currentIndex);
		if (typeof(data) == 'string' && data.length>0) { $(this.options.prefix+'section'+this.currentIndex).update(data); }
		
		if ($(this.options.prefix+'section'+this.currentIndex).empty()) {
			new Ajax.Updater($(this.options.prefix+'section'+this.currentIndex),this.options.path, {
				method: 'post',
				parameters: {'a': this.options.amount, 'u': this.options.userid,'c': this.currentIndex, 's': this.options.csubmission, 'd': 'current'},
				onSuccess:this.unhide,
				onComplete:this.jumpto
			});
		}
		
		this.sections.each( function(section, index) {
		  section._index = index;
		});

		this.events = {
		  click: this.click.bind(this)
		};

		this.addObservers();
		if(this.options.autoGlide) this.start();
	},
	_unhide: function() {
		$(this.options.prefix+'section'+this.currentIndex).show()
	},
	
	_jumpto: function() {
		this.moveTo(this.options.prefix+'section'+this.currentIndex, this.scroller, { duration:this.options.duration });
	},
	
	indexSections: function() {
		this.sections   = this.wrapper.getElementsBySelector('div.section');
		this.sections.each( function(section, index) {
		  section._index = index;
		});
	},
	addObservers: function() {
		var controls = this.wrapper.getElementsBySelector('div.controls a');
		controls.invoke('observe', 'click', this.events.click);
	},	

	click: function(event) {
		this.stop();
		var element = Event.findElement(event, 'a');
		if (this.scrolling) this.scrolling.cancel();

		this.moveTo(element.href.split("#")[1], this.scroller, { duration:this.options.duration });     
		Event.stop(event);
	},

	moveTo: function(element, container, options){
		this.current = $(element);
		Position.prepare();
		var containerOffset = Position.cumulativeOffset(container),
		elementOffset = Position.cumulativeOffset($(element));

		this.scrolling 	= new Effect.SmoothScroll(container, 
		{duration:options.duration, x:(elementOffset[0]-containerOffset[0]), y:(elementOffset[1]-containerOffset[1])});
		return false;
	},
	
	jump: function(section) {
		if (section > 0 && section <= this.options.totalindex) {
			var data = this.options.preloaded.get('section'+section);
			if (typeof(data) == 'string' && data.length>0) { $(this.options.prefix+'section'+section).update(data); }
			if ($(this.options.prefix+'section'+section).empty()) {
				this.jumptosection = section;
				new Ajax.Updater($(this.options.prefix+'section'+section),this.options.path, {
					method: 'post',
					parameters: {'a': this.options.amount, 'u': this.options.userid, 's': this.options.csubmission, 'c': section, 'd': 'next'},
					onSuccess: this.ajaxJump,
					onComplete: this.jumpto
				});
				
			}
			else {
				this.currentIndex=section;
				this.moveTo(this.options.prefix+'section'+this.currentIndex, this.scroller, { duration:this.options.duration });
			}
			
		}
	},
	
	_ajaxJump: function() {
		this.currentIndex=this.jumptosection;
		this.moveTo(this.options.prefix+'section'+this.jumptosection, this.scroller, { duration:this.options.duration });
	},
	
	next: function(){
		var currentindex = this.currentIndex;
		
		var users = $(this.options.prefix+'section'+currentindex).select('a');
		var id = users[users.length-1];
		var lastid = $(id).readAttribute('ref').replace('glider-','');
		
		currentindex++;
		if (this.currentIndex <= (this.options.totalindex-1)) {
			var data = this.options.preloaded.get('section'+currentindex);
			if (typeof(data) == 'string' && data.length>0) { $(this.options.prefix+'section'+currentindex).update(data); }
			if ($(this.options.prefix+'section'+currentindex).empty()) {
				new Ajax.Updater($(this.options.prefix+'section'+currentindex),this.options.path, {
					method: 'post',
					parameters: {'l':lastid, 'a': this.options.amount, 'u': this.options.userid, 's': this.options.csubmission, 'c': currentindex,'d': 'next'},
					onSuccess: this.ajaxNext,
					onComplete: this.jumpto
				});
			}
			else {
				this.currentIndex++;
				this.moveTo(this.options.prefix+'section'+this.currentIndex, this.scroller, { duration:this.options.duration });
			}
		}
		else {
			this.jump(1);
		}
	},
	
	previous: function(){
		var currentindex = this.currentIndex;
		
		var users = $(this.options.prefix+'section'+currentindex).select('a');
		var id = users[users.length-1];
		var lastid = $(id).readAttribute('ref').replace('glider-','');
		
		currentindex--;
		if (currentindex > 0) {
			if ($(this.options.prefix+'section'+currentindex).empty()) {
				var data = this.options.preloaded.get('section'+currentindex);
				if (typeof(data) == 'string' && data.length>0) { $(this.options.prefix+'section'+currentindex).update(data); }
				new Ajax.Updater($(this.options.prefix+'section'+currentindex),this.options.path, {
					method: 'post',
					parameters: {'l':lastid, 'a': this.options.amount, 'u': this.options.userid, 's': this.options.csubmission, 'c': currentindex,'d': 'prev'},
					onSuccess: this.ajaxPrevious,
					onComplete: this.jumpto
				});
			}
			else {
				this.currentIndex--;
				this.moveTo(this.options.prefix+'section'+this.currentIndex, this.scroller, { duration:this.options.duration });
			}
		}
		else { this.jump(this.options.totalindex); }
	},
	
	_ajaxPrevious: function(transport){
		this.currentIndex--;
	},
	
	_ajaxNext: function(transport){
		this.currentIndex++;
	},
	
	stop: function()
	{
		clearTimeout(this.timer);
	},
	
	start: function()
	{
		this.periodicallyUpdate();
	},
		
	periodicallyUpdate: function()
	{ 
		if (this.timer != null) {
			clearTimeout(this.timer);
			this.next();
		}
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency*1000);
	}

});