try{
	$j = jQuery.noConflict();
}catch(e){
	
}


if(!TBG){ var TBG = new Object();};

/*
	Paginator Module
	------------------
	Provides functionallity for javascript pagination of html elements
	
	Usage: Apply the class 'paginated' to any elements you wish to be paginated and include this script in your header.
*/
TBG.Paginator = {
	pages: [],
	curPageIndex: 0,
	curPage: null,
	initialize: function(){
		this.pages = $$(".paginated");
		if(this.pages.length > 1){
			this.curPage = this.pages.first();

			new Insertion.After(this.pages.last(), this.controls());
			$$(".paginator_tools").first().down().next().addClassName("currentPageLink");
			this.pages.each(function(p, i){
				if(i != 0){
					p.hide();
				}
			});	
		}
	},
	controls: function(){
		var html = '<div class="paginator_tools">';
		html += '<a onclick="javascript:TBG.Paginator.previousPage();">back</a> '
		this.pages.each(function(p, i){
			html += ' <a onclick="TBG.Paginator.loadPageLink('+i+', this);">'+Number(i+1)+'</a> ';
		});
		html += '<a onclick="TBG.Paginator.nextPage();">next</a>';
		html += '</div>';
		return html;
	},
	nextPage: function(){
		this.adjustPage(+1);
	},
	previousPage: function(){
		this.adjustPage(-1);
	},
	adjustPage: function(offset){
		this.curPageIndex += offset;
		this.curPageIndex = (this.curPageIndex < 0 ? 0 : this.curPageIndex);
		this.curPageIndex = (this.curPageIndex > this.pages.length -1 ? this.pages.length -1 : this.curPageIndex);
		this.loadPage(this.curPageIndex);
	},
	loadPageLink: function(pageIndex, link){
		this.loadPage(pageIndex);
	},
	loadPage: function(pageIndex){
		this.curPage.hide();
		this.curPageIndex = pageIndex;
		this.curPage = this.pages[pageIndex];
		this.curPage.show();
		this.updateControls();
	},
	updateControls: function(){
		$$(".currentPageLink").first().removeClassName("currentPageLink");
		$$(".paginator_tools").first().immediateDescendants()[this.curPageIndex + 1].addClassName("currentPageLink");
	}
	
};

TBG.TabControl = Class.create();

TBG.TabControl.prototype = {
	id: null,
	initialize: function(control_id){
		this.id = control_id;
		this.setup_links();
	},
	setup_links: function(){
		$$('#'+this.id+' ul.tabs li a').each(function(a) {
			var page = a.getAttribute('href').match(/[-_\w]+$/i)[0];
			if (a == $$('#'+this.id+' ul.tabs li a').first()) {$(a.parentNode).addClassName('active') }
			else { $(page).hide() }

	    Event.observe(a, 'click', function(e) {
	     $$('#'+this.id+' ul.tabs li.active').each(function(e) { e.removeClassName('active'); })
	     $$('#'+this.id+' .tab_page[id!='+page+']').each(function(e) { e.hide() });
	     $(a.parentNode).addClassName('active');
	     $(page).show();
	     Event.stop(e);
	    }.bind(this));
		}.bind(this));
	}
};

TBG.FlashController = {
	initialize: function(){
		Event.observe('header', 'mouseout', onNavigationMouseOut, false);
		Event.observe('content', 'mouseover', onNavigationMouseOut, false);
		
	},
	setFlashHeight: function(id, new_height){
		$(id).style.height = new_height+"px";
	},
	setFlashWidth: function(id, new_width){
		$(id).style.width = new_width+"px";
	},
	setFlashSize: function(id, new_width, new_height){
		this.setFlashHeight(id, new_height);
		this.setFlashWidth(id, new_width)
	},
	doTest: function(p1, p2){
		alert(arguments);
	},
	getSWF: function(id){
		if(navigator.appName.indexOf("Microsoft") != -1){
			return window[id];
		}
		else{
			return document[id];
		}
	}
};

TBG.SharingHelper = {
	title: "",
	description: "",
	url: location.href,
	initialize: function(title, description){
		this.title = title;
		this.description = description;
	},
	post_to: function(network_id){
		switch(network_id){
			case "delicious":
				this.post_popup('http://del.icio.us/post?v=4&noui&jump=close&url=' + encodeURIComponent(this.url) + '&title=' + encodeURIComponent(this.title), 'del.icio.us', 'toolbar=0,status=0,height=450,width=650,scrollbars=yes,resizable=yes')
				break;
			case "stumble.upon":
				this.post_popup('http://www.stumbleupon.com/submit?url=' + encodeURIComponent(this.url) + '&title=' + encodeURIComponent(this.title), 'stumble.upon', 'toolbar=0,status=0,height=450,width=650,scrollbars=yes,resizable=yes')
				break;
			case "digg":
				this.post_popup( 'http://digg.com/submit?phase=2&url=' + encodeURIComponent(this.url) + '&title='+ encodeURIComponent(this.title) + '&bodytext=' + encodeURIComponent(this.description) + '&topic=design', 'digg', 'toolbar=0,status=0,height=450,width=650,scrollbars=yes,resizable=yes');
				break;
			case "email":
				document.location.href = 'mailto:?subject='+this.title+'&Body=Check out this page at NewMuseum.org:%0a%0a'+this.url+'%0a%0aHere\'s an excerpt to get you interested...%0a%0a'+this.description;
				break;
		}
	},
	post_popup: function(url, name, params) {
		var win = window.open(url, name, params);
	}
};

function initTBGPaginator(){
	TBG.Paginator.initialize();	
	TBG.FlashController.initialize();
}


function onNavigationMouseOut() {
	TBG.FlashController.getSWF("navigation").onMouseOut();
}

Event.observe(window, 'load', initTBGPaginator, false);


var InlineSelect = {
  // An AJAX free method for adding resource's to a record
  // via the record's collection_id's method
  add : function(collection_name){
    
    var data = {source : $(collection_name)}
    data.name = collection_name
    data.id = data.source.value
    data.label = data.source[data.source.selectedIndex].text
    
    this._render_add(data)
  },
  
  remove : function(name, id){
    var id_collection = this._id_collection(name)
    var ids = id_collection.value.split(",")
    
    //Remove the id from the list
    ids.splice(ids.indexOf(id), 1)
    id_collection.value = ids.join(",")

    //Remove the label
    $(name+"_"+id).remove()
  }, 
  
  _id_collection: function(name){
    //signularize categories
    name = name.replace(/categories/,"category")
    return $(name+"_ids")
  },
  
  _render_add: function(data){
    var target = $('list_'+data.name)
    var id_collection = this._id_collection(data.name)
    var ids = id_collection.value.split(",")
    
    // If the id is already in the hidden array
    if(ids.indexOf(data.id) > -1) {return false}
    
    var id = data.name+"_"+data.id
    var method = "InlineSelect.remove(\""+data.name+"\", "+data.id+")"
    target.innerHTML += "<div id='"+id+"'><a href='#' onclick='"+method+"; return false'><img src='/images/trash.gif'/></a>"+data.label+"</div>"
    
    
    ids.push(data.id)
    // remove any invalid ids
    ids = ids.reject(function(i){return !(i > 0)})
    id_collection.value = ids.compact().join(",")
  }
}
