// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function cmenu_info(text){
	$('cmenu_info').innerHTML = String(text);
}

function toggle_tip(tip_id){
	// Element.toggle('tip_link_' + String(tip_id));
	Element.toggle('tip_' + String(tip_id));
}

function toggle_instructions(){
	Element.toggle('tutorial');
}

var swap_promo = function(file, caller){
	
	$$('#image_switcher ul li').each(function(link){link.removeClassName("selected_image_button")})
	caller.parentNode.addClassName("selected_image_button")

	img = $('home_promo_img')
	img.src = "/images/h_promo_"+file+".png"
}

function check_login_availability(){
	var login = $('user_login').value;
	return(new Ajax.Request(('/account/check/?login=' + encodeURI(login)), {asynchronous:true, evalScripts:true}))
}

function choose_suggested_login(text){
	$('user_login').value = text; 
	$('suggest_login').style.display='none'; 
	$("availability_link").innerHTML = "Available?";
	$('user_login').style.border = '3px solid lightgreen';
	$('user_email').focus();
	
}

function popup(mylink, windowname){
	if (! window.focus)return true;
		var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;
		
	window.open(href, windowname, 'width=800,height=600,scrollbars=yes');
	return false;
}


function toggle_debate_info_2() {
	Element.toggle("debate_info_2_inner"); 
	if ($('debate_info_2_toggle_link').innerHTML == 'Hide') {
		Element.update("debate_info_2_toggle_link","Show Contents");
	}
	else 
	{
		Element.update("debate_info_2_toggle_link","Hide");
	}
}

function toggle_debate_toc_arguments() {
	Element.toggle('debate_toc_side_0_arguments_more'); 
	Element.toggle('debate_toc_side_0_toggle_arguments_link');
	Element.toggle('debate_toc_side_1_arguments_more'); 
	Element.toggle('debate_toc_side_1_toggle_arguments_link');
	return false;
}


//  Browser Detection

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();



/******************************************************************* 
* 
* File    : JSFX_Browser.js © JavaScript-FX.com
* 
* Created : 2000/07/15 
* 
* Author  : Roy Whittle www.Roy.Whittle.com 
* 
* Purpose : To create a cross browser "Browser" object.
*		JSFX.Browser library will allow scripts to query parameters
*		about the current browser window.
* 
* History 
* Date         Version        Description 
* 2001-03-17	2.0		Converted for javascript-fx
***********************************************************************/  
if(!window.JSFX)
	JSFX=new Object();

if(!JSFX.Browser)
	JSFX.Browser = new Object();

if(navigator.appName.indexOf("Netscape") != -1)
{
	JSFX.Browser.getCanvasWidth	= function() {return innerWidth;}
	JSFX.Browser.getCanvasHeight	= function() {return innerHeight;}
	JSFX.Browser.getWindowWidth 	= function() {return outerWidth;}
	JSFX.Browser.getWindowHeight	= function() {return outerHeight;}
	JSFX.Browser.getScreenWidth 	= function() {return screen.width;}
	JSFX.Browser.getScreenHeight	= function() {return screen.height;}
	JSFX.Browser.getMinX		= function() {return(pageXOffset);}
	JSFX.Browser.getMinY		= function() {return(pageYOffset);}
	JSFX.Browser.getMaxX		= function() {return(pageXOffset+innerWidth);}
	JSFX.Browser.getMaxY		= function() {return(pageYOffset+innerHeight);}

}
else 	if(document.all) 	{
	JSFX.Browser.getCanvasWidth	= function() {return document.body.clientWidth;}
	JSFX.Browser.getCanvasHeight	= function() {return document.body.clientHeight;}
	JSFX.Browser.getWindowWidth 	= function() {return document.body.clientWidth;}
	JSFX.Browser.getWindowHeight	= function() {return document.body.clientHeight;}
	JSFX.Browser.getScreenWidth	= function() {return screen.width;}
	JSFX.Browser.getScreenHeight	= function() {return screen.height;}
	JSFX.Browser.getMinX		= function() {return(document.body.scrollLeft);}
	JSFX.Browser.getMinY		= function() {return(document.body.scrollTop);}
	JSFX.Browser.getMaxX		= function() {
		return(document.body.scrollLeft
			+document.body.clientWidth);
	}
	JSFX.Browser.getMaxY		= function() {
			return(document.body.scrollTop
				+document.body.clientHeight);
	}
} 
/*** End  ***/




function select_message(id){
	$$('.message-list-link').each(function(link){
		link.setStyle({background: '#fff'})
	})
	$('message_' + id).setStyle({background: '#dcecf7'})
}


function png_to_gif(el){
	old_bg = el.getStyle('background-image')
	re = /\.png/
	new_bg = old_bg.replace(re,'.gif')
	el.setStyle({backgroundImage: new_bg})
}


function show_hidden_comment(id) {
       $('comment_' + id).removeClassName('post_hidden');
       $('show_comment_' + id + '_link').remove();
}

// Accepts the argument ID
function cancel_argument_comment(id) {
	comment_div = $('argument_' + id + '_new_comment');
	comment_buttons = $('argument_' + id + '_new_comment_button');
	if (comment_div != null ) {
		cancel_argument_comment_by_el(comment_div);
		comment_buttons.hide();
	}
}


function set_comment_stand(id,stand) {
	if (stand > 1 || stand < -1) { stand = 0;}
	comment_div = $('argument_' + id + '_new_comment');
	comment_entry = $$('#argument_' + id + '_new_comment textarea')[0];
	if (comment_div != null ) {
		remove_comment_stand(comment_div.id);
		stand_class = ["new_comment_con","new_comment_neutral","new_comment_pro"][stand + 1];
		comment_entry.addClassName(stand_class);
		procon = $('comment_' + id + "_procon");
	    procon.value = stand;
		comment_entry.focus();
	}
	$$('div#argument_' + id + '_new_comment ul.new_comment_type_links li a').each(function(x){x.removeClassName("active").addClassName("inactive")});
	$('argument_' + id + '_' + stand + '_link').addClassName("active").removeClassName("inactive");
}

function remove_comment_stand(comment_id) {
	comment_div = $(comment_id);
	comment_entry = $$('div#' + comment_id + ' textarea')[0];
	if (comment_div != null ) {
		procon_inputs = $$('#' + comment_div.id + " input#comment_procon");
		if (procon_inputs.size() == 1) {
			procon = procon_inputs[0];
			procon.value = "0";
		}
		comment_entry.removeClassName("new_comment_neutral");
		comment_entry.removeClassName("new_comment_pro");
		comment_entry.removeClassName("new_comment_con");
	}	
}

function new_comment_login_prompt(id) {
	login_div = $('argument_' + id + '_new_comment_login_prompt');
	comment_div = $('argument_' + id + '_new_comment');
	comment_form = $('argument_' + id + '_new_comment_form_p');
	if (comment_div != null && login_div != null && comment_form != null) {
		login_div.show();
		comment_div.hide();
		comment_form.hide(); 
	} else { }
}

function debate_filter_select_category() {
	s = $("category_select"); 
	if (s != null) {
		v = s.options[s.selectedIndex].value;
		if (v != null) { new Ajax.Request(('/filter/category/' + v), {asynchronous:true, evalScripts:true}); }
	}
}

function show_login() {
	lc = $('login_container');
	hll = $('header_login_link');
	prl = $('forgot_password_link');
	prl.show();
	hll.hide();
	lc.setStyle({display: 'inline'});
        lcc = $('nav_ul');
        if ((BrowserDetect.browser == "Explorer") && (BrowserDetect.version < 7))
            lcc.setStyle({margin: '14px, 0, 0, 0'});
	// new Effect.Grow('login_container',{direction: 'top-left', duration: '2.0'});
}

function recent_activity_toggle() {
	var filter = get_filter();
	if (filter != "" )
	    var selected = filter;
	else 
	{
    	var	activity = $("activity_select"); 
    	var	selected = activity.value;
	}

	if (selected == "all"){
		$$('div#home_recent_activity li.posting').each(function(x){x.show();})
	} 
	else
	{
		$$('div#home_recent_activity li.posting').each(function(x){x.setStyle({display: "none"});})
		$$('div#home_recent_activity li.posting_' + selected).each(function(x){x.setStyle({display: "block"});})	
	}

}


function get_filter(){
	var	activity = $("activity_select"); 
	var	selected = activity.value;
	return selected;
}

function cancel_profile_comment(){
	$('enter_comment').innerHTML = '';
}

function show_vote_login_prompt(updown,id) {
	id_string = 'vote_' + updown + '_' + id;
	Effect.Appear(id_string);
}

function hide_vote_login_prompt(updown,id) {
	id_string = 'vote_' + updown + '_' + id;
	Effect.Fade(id_string);
}


function filter_select_tags(tags) {
    tags.each(function(t){filter_tag_select(t);})
}

function reset_all_filter_tag_status() {
    $$('div#home_tag_cloud_tags a').each(function(t){t.removeClassName('active_tag');})
}

function filter_tag_select(tagname) {
    $$('a#tag_' + tagname).each(function(t){t.addClassName('active_tag')})
}

function filter_tag_unselect(tagname) {
    $$('a#tag_' + tagname).each(function(t){t.removeClassName('active_tag')})
}

function insert_filter_tag_if_missing(tagname, html){
    if ($$('a#tag_' + tagname).size() == 0) {
        new Insertion.Top('home_tag_cloud_tags',html);
    } else {
        console.log($$('tag_' + tagname));
    }
}



var TabArea = Class.create();
TabArea.prototype = {
	initialize: function(ta,doScroll){
		this.ta = ta;
		this.ta_element = $(ta);
		this.doscroll = doScroll;
		this.tabs = $$('#'+ta+' .TA_tabs a.tab');
		this.contents = $$("#"+ta+' .TA_contents > div');
		this.active_tab = $$("#"+ta+' .TA_tabs a.activetab')[0];
		this.setup();
	},
	setup: function(){
		this.tabs.invoke("observe",'click',this.open_tab.bind(this));
		this.contents.invoke('hide');
		idx = this.tabs.indexOf(this.active_tab);
		if (this.doscroll == true) {
			this.scroller = new articleScroller(this);
			this.scroller.start(this.ta_element.id);
		} else {
			this.contents[idx].show();
		}
		
	},
	open_tab: function(e){
		this.active_tab = Event.element(e);
		if (this.doscroll == true) {
			idx = this.tabs.indexOf(this.active_tab);
			this.scroller.show(idx);
		} else {
			this.call_open_tab(e)
		}
	},
	call_open_tab: function(tab_el){
		this.active_tab = tab_el;
		this.tabs.each(function(el){el.removeClassName("activetab")});
		this.active_tab.addClassName('activetab');
		tab_idx = this.tabs.indexOf(this.active_tab);
		this.contents.invoke('hide');
		this.contents[tab_idx].show();
	}
}


function articleScroller(tabarea){
	instance= null;
	articleInterval= 10000;
	tabber = tabarea;
	articleContainer= tabarea.ta_element;
	articles= new Array();
	tabs = new Array();
	current=null;
	contentFinder=null;
	tabFinder=null;
	__instance__ = null;

};
articleScroller.getInstance = function(id){
	if (this.__instance__ == null){
		this.__instance__ = new articleScroller(id)
	}
	return this.__instance__
};
articleScroller.prototype.hideAll = function(){
	this.articles.each(function(el){el.hide();});
	this.tabs.each(function(el){el.removeClassName('activetab');});
};
articleScroller.prototype.showCurrent = function(){
	if(this.current==null){
		this.current = 0;
	}
	if(this.current < 0){
		this.current = this.articles.length -1;
	}
	if(this.current >= this.articles.length){
		this.current = 0;
	}
	// content = this.articles[this.current];
	tab = this.tabs[this.current];
	tabber.call_open_tab(tab);
};
articleScroller.prototype.start = function(id){
	this.contentFinder = '#' + id + ' div.TA_contents div.tab_content';
	this.tabFinder = "#" + id + ' div.TA_tabs a.tab';
	this.articles = $$(this.contentFinder);
	this.tabs = $$(this.tabFinder);
	this.hideAll();
	this.show(0);
	this.articleInterval = setInterval(this.rotate.bind(this), 10000)
};
articleScroller.prototype.stop = function(){
	clearInterval(this.articleInterval)
};
articleScroller.prototype.prev = function(){
	this.stop();
	this.current--;
	this.showCurrent();
};
articleScroller.prototype.next = function(){
	this.stop();
	this.rotate();
};
articleScroller.prototype.show = function(id){
	this.stop();
	this.current = id;
	this.showCurrent();
};
articleScroller.prototype.rotate = function(){
	this.current++;
	this.showCurrent();
};




function add_observers_for_watched_length() {
	$$(".watch_length").each(function(x){
			add_entry_observer(x.id);
		})
}

function add_entry_status(id) {
	if ($('status_' + id) == null ) {
		status_html = '<span class="entry_status" id="status_' + id + '"></span><br>';
		$(id).insert({before: status_html});
		update_length_status_span(id);
	}
}

function add_entry_observer(id) {
	add_entry_status(id);
	e = $(id);
	Event.observe(e,'keyup', update_length_status);
}

function get_entry_length(id) {
	l = $(id).value.length;
	return l;
}

function get_entry_max_length(id) {
	ml = $(id).getAttribute("max_length");
	if (ml != null) {
		ml = parseInt(ml);
	} else {
		ml = 255;
	}
	return ml;
}

function update_length_status(e) {
	id = Event.element(e).id;
	update_length_status_span(id);
}

function update_length_status_span(id) {
	se = $('status_' + id)
	chars_left = get_entry_max_length(id) - get_entry_length(id);
	se.innerHTML = "(" + chars_left + " characters left)"
	// se.innerHTML = "(" + get_entry_length(id) + "/" + get_entry_max_length(id) + ' characters)';
	if (get_entry_length(id) >= get_entry_max_length(id)) {
		se.setStyle({color: 'red'});
		e = $(id);
		e.value = e.value.substring(0,get_entry_max_length(id));
	} else {
		se.setStyle({color: 'green'});
	}
}