function usrActionReg(which,action) {
   if (action == 20) {
      window.print();
   }
   else if (action == 19) {
      location.href='mailto:?subject='+encodeURIComponent(document.title)+'&body='+encodeURIComponent(location.href);
   }
}
function google(){
    var a=document,b=a.createElement('script'),w=window,g={};
    g.altWindow=w.open('','_blank','height=370px,width=720px,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
    setTimeout(function(){g.altWindow.focus()},1000);
    function f(o){try{var d=new google.share.EmailWidget(g);
    d.display()}catch(e){if(o>20)return;
    setTimeout(function(){f(o+1)},o<10?1<<o:1000)}};
    b.src='http://www.google.com/s2/sharing/js?script=socialsharing&key=AAAAJ--gdtJYHC_fJptEAHT5Bf3vmtgiNr9evPGAdn6ffo9GbIBy2zBLrN-zLyA5QdWh-A';
    a.body.appendChild(b);
    f(0)
}
function FaceBook()
{
    var d=document,
    f='http://www.facebook.com/share',
    l=d.location,
    e=encodeURIComponent,
    p='.php?src=bm&v=4&i=1208544185&u='+e(l.href)+'&t='+e(d.title);1;
    try{if (!/^(.*\.)?facebook\.[^.]*$/.test(l.host))throw(0);
    share_internal_bookmarklet(p)}catch(z) {a=function() {if (!window.open(f+'r'+p,'sharer','toolbar=0,status=0,resizable=0,width=626,height=436'))l.href=f+p};
    if (/Firefox/.test(navigator.userAgent))setTimeout(a,0);
    else{a()}}void(0)
}
function yahoo()
{
    var d=document,f='http://bookmarks.yahoo.com/toolbar/savebm?',l=d.location,e=encodeURIComponent,p='u='+e(l.href)+'&t='+e(d.title)+'&opener=bm&ei=UTF-8';1;a=function(){if(!window.open(f+p,'popup','toolbar=0,status=0,resizable=1,scrollbars=1,width=450,height=480'))l.href=f+p};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}void(0)
}
function bnet()
{
    location.href = "http://www.bnet.com/5262-13093-0.html?link=" + encodeURIComponent(location.href) + "&title=" + encodeURIComponent(document.title) + "&path=" + encodeURIComponent(location.href);
}
function twitter()
{
    location.href = "http://twitter.com/home?status=" + encodeURIComponent(document.title) + " " +  encodeURIComponent(location.href);
}
function delicious()
{
    location.href="http://del.icio.us/post?url=" + encodeURIComponent(location.href) + "&title=" + encodeURIComponent(document.title);
//&tags=bnet    
}
function stumbleupon(){
    location.href= "http://www.stumbleupon.com/submit?url=" + encodeURIComponent(location.href);
}
function newsvine()
{
    window.open('http://www.newsvine.com/_wine/save?popoff=1&u=' + encodeURIComponent(location.href) ,'newsvine','toolbar=no,width=590,height=600,resizable=yes,scrollbars=yes');
}
function linkedin()
{
	location.href = "http://www.linkedin.com/shareArticle?mini=true&url=" + encodeURIComponent(location.href) + "&title=" + encodeURIComponent(document.title);
//&source=BNET
}
function digg()
{
    location.href="http://digg.com/submit?url=" + encodeURIComponent(location.href) + "&title=" + encodeURIComponent(document.title);
}
function technorati()
{
    location.href = "http://technorati.com/faves?add=" + encodeURIComponent(location.href);
}
function reddit()
{
	location.href = "http://reddit.com/submit?url=" + encodeURIComponent(location.href) + "&title=" + encodeURIComponent(document.title);
}
function toolbar() {

	if ($$('.social')) {
     	var social = $$('.social');
		social.each(function(el, i){ 
			el.addEvent('mouseenter', function(e){ 
				social[i].addClass('drill'); 
			});	
			el.addEvent('mouseleave', function(e){ 
				social[i].removeClass('drill'); 
			});
		})
	} // end social

	if ($('embed')) {
		var link = $$('#embed a');
		var cont = $('embed');
		link.addEvent('click', function(e){ 
			e = new Event(e).stop();
			cont.toggleClass('drill');
		});
	}
}

function selector() {
	var selector = $$('.selector');
	var selectBox = $$('.selectBox');
	var remember = true; //cookie
	var warehouse = [];
	
	selector.each(function(el,i){
		var selectTabs = el.getElements('li');
		var name = selectTabs[0].get('text').slice(0,10).replace(/[ \t]/g, "")+"_"+document.URL.slice(document.URL.length - 15, document.URL.length - 5);
		var pos = 0;
		var locateBox = [];
		warehouse.push(locateBox);
		if (el.getElements('.external') != ""){
			remember = false;
		}

		selectTabs.each(function(ela, ia){
			
			locateBox.push(selectBox[0]);
			selectBox.splice(0,1);
			
			ela.addEvent('click', function(e){	
				e = new Event(e).stop();
				selectTabs[pos].removeClass('on');
				warehouse[i][pos].removeClass('on');
				selectTabs[ia].addClass('on');
				warehouse[i][ia].addClass('on');
				pos = ia;
				if (remember) {
					Cookie.write(name, ia, {duration: 12});
				}
			});
		});//selectTabs.each
		
		if (remember) {
			var tab = Cookie.read(name);
			if (tab) {
				pos = tab;
				selectTabs[0].removeClass('on');
				selectTabs[pos].addClass('on');
				warehouse[i][0].removeClass('on');
				warehouse[i][pos].addClass('on');
			}
		}
	}); //selector.each
} //selector

var CustomTags = {
	truncate: function(amount){
		$$('.tags').each(function(el){
			var links = el.get('html').split(',');
			if (links.length > amount) {
				var hiddenLinks = links.splice(amount,links.length - amount);
				hiddenLinks = hiddenLinks.join(', ');
				links = links.join(', ');
				el.set('html',links);
				var ellipsis = new Element ('span', {'class': 'ellipsis'}).set('html','...').inject(el,'bottom');
				var hiddenCont = new Element ('span', {'class': 'more'}).set('html',',' + hiddenLinks).inject(el,'bottom');
				el.addEvents({
					'mouseenter': function(){
						ellipsis.setStyle('display','none');
						hiddenCont.setStyle('display','inline');
					},
					'mouseleave': function(){
						ellipsis.setStyle('display','inline');
						hiddenCont.setStyle('display','none');
					}
				});
			}
		});
	}
};

var SearchSwitcher = {
	init: function(start){
		this.bnetSearch = $('bnetSearch');
		this.searchoptions = $$('#search ul li');
		this.querybox = $('searchbox');
		this.fa = false;
		this.faSearch = $('faSearch');
		this.faSearch.addEvent('submit', function(e) {
			e.stop();
			var query = this.faSearch.toQueryString();
			var queryUrl = query.replace("%20","+");
			window.location = "http://findarticles.com/p/search?" + queryUrl + "&x=0&y=0";
		}.bind(this));

	if (start == 'fa') {
		this.fa = true;
		this.bnetSearch.setStyle('display','none');
	} else {
		this.faSearch.setStyle('display','none');
	}

		this.searchoptions.each(function(el,i){
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				this.setOff();
				this.setOn(el);
			}.bind(this));
		}.bind(this));
	},
	
	setOn: function(el){
		el.addClass('on');
		this.setParams(el.get('id'));

		if (!this.fa) {
			this.bnetSearch.set({
				'styles': {
					'display': 'block'
					},	
				'action' : this.action
			});
			this.querybox.set('name',this.query);
		}
	},

	setOff: function(){
		this.searchoptions.each(function(el,i){
			el.removeClass('on');
		}.bind(this));

		if ($chk(this.hidden)) {
			this.hidden.destroy();
		}

		if (this.fa) {
			this.faSearch.setStyle('display','none');
			this.fa = false;
		}
	},

	setParams: function(id){

		switch (id) {
			case 'srchAll':
				this.action = "http://resources.bnet.com/index.php";
				this.query = "q";
				break;
			case 'srchArt':
				// function to hide BNET and show FA search
				this.fa = true;
				this.bnetSearch.setStyle('display','none');
				this.faSearch.setStyle('display','block');
				break;
			case 'srchLib':
				this.action = "http://jobfunctions.bnet.com/search.aspx";
				this.query = "q";
				break;
			case 'srchStks':
				this.action = "http://finance.bnet.com/bnet";
				this.query = "q";
				this.hidden = new Element ('input', {
					'type': 'hidden',
					'name': 'Page',
					'value': 'Quote'
					}).inject(this.bnetSearch,'bottom');				
				break;
			case 'srchDict':
				this.action = "http://dictionary.bnet.com/index.php";
				this.query = "d";
				break;
			} // switch
		} // setParams
};


/* Dropdown */

var Dropdown = new Class({
	Implements: [Chain, Options],
	options: {
		speed:  300
	},
	
	initialize: function(element,options){
		this.setOptions(options);
		this.element = element;
		this.height = this.element.getScrollSize().y;

		if (!this.options.open && this.element.get('height') != '0') {
			this.element.setStyles({
				visibility: 'hidden', //set this in the css also
		    	height: 0,
				overflow: 'hidden'
			});
		}
		this.fx = new Fx.Morph(this.element, {duration: this.options.speed, link: 'ignore'});
	},
	
	toggle: function(){
		(this.element.hasClass('on')) ? this.close() : this.open();
	},
	
	open: function(){
		this.element.setStyle('visibility', 'visible');
		this.callChain();
		this.fx.start({
			'height': this.height
		});
		this.element.addClass('on');
	},
	
	close: function(){
		this.fx.start({
			'height': 0
		}).chain(function(){
			this.element.setStyle('visibility', 'hidden');
			this.callChain();
		}.bind(this));
		this.element.removeClass('on');
	}
});

CNBTruncate = new Class({
	//Todo: add format = words and more/less options
	Implements: Options,
	options: {
		format: 'characters', 
		length: 100,
		showMore: false,
		truncEnd: '...',
		moreText: {
			more: '+more',
			less: '-less'
		}
	},

	initialize: function(content,options){
		this.setOptions(options);
		
		if ($type(content) != 'array') { content = [content]; }
		
		content.each(function(el){ 
			var text = el.get('text');
			
			if (text.length > this.options.length) {
				// regex determines if it\'s the middle of a word or space
				var trunc = text.substring(0, this.options.length).replace(/\w+$/, '').replace(/\s+$/, '');

				this.replaceText(el,trunc);
			}
		}.bind(this));

	},
	
	replaceText: function(el,text){
		el.set('html',text + this.options.truncEnd);
	}
});


InputFocus = new Class({

	Implements: Options,
	options: {
		container: null,
		className: 'focus'
	},

	initialize: function(options){
		this.setOptions(options);
		$$('.'+this.options.className).each(function(el,i){
			el.addClass('unfocus');
			el.removeClass('focus');
			var original = el.get('value');
			el.addEvent('focus', function(e){
				if (el.hasClass('unfocus')) {
					el.removeClass('unfocus');
					el.set('value','');
				}
			}.bind(this));
			el.addEvent('blur', function(e){	
				if(!$chk(el.get('value'))) {
					el.addClass('unfocus');
					el.set('value',original);
				}
			}.bind(this));
		}.bind(this));
	}
});


function feedbackPop(email) {
	var load = window.open('http://blogs.zdnet.com/emailform.php?email='+email,'','scrollbars=no,menubar=no,height=640,width=400,resizable=no,toolbar=no,location=no,status=no');
	load.document.close();
}

function viewChanger() {

	$$('.view-changer').each(function(el, i){ 
		var items = el.getElements('li');
		var target = el.get('rel');
		var views = $$("'." + target + "'");
		
		items.each(function(el2){
			var viewClass = el2.get('class').replace("changer","view");
			
			el2.addEvent('click', function(e){	
				e = new Event(e).stop();
				items.each(function(el3){
					el3.getElements('a').removeClass('on');
				});
				el2.getElements('a').addClass('on');
				
				views.each(function(el4){
					var findClass = el4.get('class').split(' ');
					findClass.each(function(el5){
						if (el5.contains('view')) el4.removeClass(el5);
					});
					el4.addClass(viewClass);
				});
				Cookie.write('viewChanger', viewClass, {duration: 5});
			});
		});
		
		var pastView = Cookie.read('viewChanger');
		if (pastView) {
			items.each(function(el3){
				el3.getElements('a').removeClass('on');
				if (el3.get('class').replace("changer","view") == pastView) {
					el3.getElements('a').addClass('on');
				}

			});

			views.each(function(el4){
				var findClass = el4.get('class').split(' ');
				findClass.each(function(el5){
					if (el5.contains('view')) el4.removeClass(el5);
				});
				el4.addClass(pastView);
			});
		}
	});
};


window.addEvent('domready', function() {
	if ($$('.toolbar') && $$('.toolbar') != "") toolbar();
	if ($$('.selector') && $$('.selector') != "") selector();
	if ($$('.focus') != "") new InputFocus();
	if ($$('.tags') != "" && typeof(Tags) == 'undefined') CustomTags.truncate(5);
	if ($$('.view-changer') != "") {viewChanger();}
});

