// copyright(c) 2009 HearYourPeople.com
//var hyp_path = "http://localhost/hearyourpeople/youtube/";
var hyp_path = "http://hearyourpeople.com/";



var hyp_maxwait = 300; // 30 seconds =  300 x 100ms

//	hyp_init();
var hyp_initialized;
var hyp_busy;
var hyp_cookie_ready;
var hyp_cookie_is;
var hyp_error_str;
var wait_timer;
var hyp_id;
var hyp_preview;

var hyp_str;
var hyp_del_str;



if (!hyp_instance) {
	var hyp_instance = new Array();
}else {
}

function hyp_init(aid,eid){
//alert("init");
	var hyp_attributes = { rm:'' };
	hyp_init_attr(aid,eid,hyp_attributes);
}

// store them in a queue, keep revisiting the queue until empty
function hyp_init_attr(aid,eid,attributes){
//alert("hyp_init_attr");
	if (false){ // hyp_busy){
		window.setTimeout("hyp_init("+aid+","+eid+","+attributes+")",100);
		return;
	}
	hyp_busy = true;
	
	hyp_add_attributes(aid,attributes);
	
	if (!hyp_initialized){
		var ecss=document.createElement("link");
		ecss.setAttribute("rel","stylesheet");
		ecss.setAttribute("type","text/css");
		ecss.setAttribute("href",hyp_path+"ex2.css");
		document.getElementsByTagName("head")[0].appendChild(ecss);

		var hyp_css=document.createElement("link");
		hyp_css.setAttribute("rel","stylesheet");
		hyp_css.setAttribute("type","text/css");
		hyp_css.setAttribute("href",hyp_path+"hyp.css");
		document.getElementsByTagName("head")[0].appendChild(hyp_css);
	}

	hyp_initialized = true;
	hyp_cookie = hyp_getCookie("hypid");
	hyp_display(aid,eid,1,null,hyp_cookie);
}

function hyp_add_attributes(aid,attributes){
	// store instance attributes
	var keystr = aid+"_";
	if (attributes['numperpage'])
		hyp_instance[keystr+'numperpage'] = attributes['numperpage'];
	else
		delete hyp_instance[keystr+'numperpage'];
	if (attributes['margin'])
		hyp_instance[keystr+'margin'] = attributes['margin'];
	else
		delete hyp_instance[keystr+'margin'];
	if (attributes['color_border'])
		hyp_instance[keystr+'color_border'] = attributes['color_border'];
	else
		delete hyp_instance[keystr+'color_border'];
	if (attributes['color_background'])
		hyp_instance[keystr+'color_background'] = attributes['color_background'];
	else
		delete hyp_instance[keystr+'color_background'];
	if (attributes['width'])
		hyp_instance[keystr+'width'] = attributes['width'];
	else
		delete hyp_instance[keystr+'width'];
}

function hyp_cat_attributes(aid,url){
	// add instance attributes to url get variables
	var keystr = aid+"_";
	if (hyp_instance[keystr+'numperpage'])
		url = url+'&numperpage='+hyp_instance[keystr+'numperpage'];
	if (hyp_instance[keystr+'margin'])
		url = url+'&margin='+hyp_instance[keystr+'margin'];
	if (hyp_instance[keystr+'color_border'])
		url = url+'&color_border='+hyp_instance[keystr+'color_border'].replace('#','_lb_');
	if (hyp_instance[keystr+'color_background'])
		url = url+'&color_background='+hyp_instance[keystr+'color_background'];
	if (hyp_instance[keystr+'width'])
		url = url+'&width='+hyp_instance[keystr+'width'];
	return url;
}

// mod is never used in calls from js or php files
function hyp_display(aid,eid,page,link_elm_id,hyp_cookie,mod){
//alert("hyp_display");
	hyp_busy = true;
	hyp_cookie_ready = 0;
	if (!page){
		page = 1;
	}

	if (link_elm_id==null || link_elm_id==undefined){
	} else {
		var link_elm = document.getElementById(link_elm_id);
		link_elm.innerHTML = 'Loading...';
	}
	if (!hyp_cookie){
		hyp_cookie = hyp_getCookie('hypid');
	}
	var currenturl = window.location.href.replace(/\&/g,"__38__");
	var url;
	var script_id_str = "hyp_script_"+aid+"_"+eid;
	
	if (mod)
		url = hyp_path+'HearYourPeople.php?jsid='+script_id_str+'&aid='+aid+'&entry='+eid+'&page='+page+'&hypid='+hyp_cookie+'&mod='+mod+'&url='+currenturl+'&rnd='+Math.random();
	else
		url = hyp_path+'HearYourPeople.php?jsid='+script_id_str+'&aid='+aid+'&entry='+eid+'&page='+page+'&hypid='+hyp_cookie+'&url='+currenturl+'&rnd='+Math.random();
	
	url = hyp_cat_attributes(aid,url);
	hyp_addscript(url,script_id_str);
	wait_timer = 0;
	//var nextfunc = "hyp_display2('"+script_id_str+"')";
//	window.setTimeout(nextfunc,100);
}

// unused
function hyp_display2(script_id_str){
	if (hyp_cookie_ready){
		hyp_delscript(script_id_str);
	} else {
		wait_timer = wait_timer + 1;
		if (wait_timer < hyp_maxwait){
			var nextfunc = "hyp_display2('"+script_id_str+"')";
			window.setTimeout(nextfunc,100);
		}
	}
	hyp_busy = false;
}

function hyp_display_parent(aid,parentid,childid){
//alert("hyp_display");
	hyp_busy = true;
	hyp_cookie_ready = 0;
	if (!hyp_cookie){
		hyp_cookie = hyp_getCookie('hypid');
	}
	var currenturl = window.location.href.replace(/\&/g,"__38__");
	var url;
	var script_id_str = "hyp_script_"+aid+"_"+eid;
	
	if (mod)
		url = hyp_path+'HearYourPeople.php?jsid='+script_id_str+'&aid='+aid+'&entry='+eid+'&child='+childid+'&hypid='+hyp_cookie+'&mod='+mod+'&url='+currenturl+'&rnd='+Math.random();
	else
		url = hyp_path+'HearYourPeople.php?jsid='+script_id_str+'&aid='+aid+'&entry='+eid+'&child='+childid+'&hypid='+hyp_cookie+'&url='+currenturl+'&rnd='+Math.random();
	
	url = hyp_cat_attributes(aid,url);
	hyp_addscript(url,script_id_str);
}

// add the display as an external script
function hyp_addscript(url,id_str){
	var fileref=document.createElement('script');
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", url);
	fileref.setAttribute("id", id_str);
	document.getElementsByTagName("head")[0].appendChild(fileref);
}
function hyp_delscript(id_str){
	var x=document.getElementById(id_str);
	x.parentNode.removeChild(x);
}

function hyp_ajax_return() {
//alert("in hyp_ajax_return()");
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		var all_str = xmlHttp.responseText;
//alert("ajax_return:"+all_str);
		hyp_str = all_str.split('+++');	//
		var id_str = "hyp_div_"+hyp_str[0]+"_0";
		// [0]=aid,[1]=eid,[2]=html
		var hyp_response = document.getElementById(id_str);
		hyp_response.innerHTML = hyp_str[2];
		hyp_busy = false;
	}
}

function hyp_setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function hyp_showCookie(c_name,id_name){
	var hyp_cookie = document.getElementById(id_name);
	var str = "cookie:"+hyp_getCookie(c_name)+"<br>";
	hyp_cookie.innerHTML=str;
}


function hyp_getCookie(c_name){
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function hyp_GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0) {
			alert("This example doesn't work in Opera") 
			return 
	}
	
	if (navigator.userAgent.indexOf("MSIE")>=0){ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
			strName="Microsoft.XMLHTTP"
		} 
		try { 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 
		catch(e){ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0){
//alert("in hyp_getxmlhttpobject");
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
}


//
// Global variables
var curleft;
var curtop;
var glbl_refreshid;
var glbl_aid;
var glbl_eid;
var glbl_page;
var xmlHttp
var taobj
var tiobj
var selectedEntryID	= 0;	// the EntryID gets saved everytime an item is selected
var savedEntryID = 0;		// 'click here' to rank item, copies selectedEntryID here


function hyp_login_alert(frm) {
	alert('Please Log In to Vote and Add New Entries.');
	frm.rank.value = 0;
}

function hyp_insert_url(textarea) {
	var obj = document.getElementById(textarea);
	var str = obj.value;
	obj.value = str+"[url]put link here [display]link name[/url]";
}
function hyp_insert_bold(textarea) {
	var obj = document.getElementById(textarea);
	var str = obj.value;
	obj.value = str+"[b]bold text here[/b]";
}

function hyp_limit_textarea(textareafield, maxchars, counterfield) {
	var obj = document.getElementById(textareafield);
	var length = obj.value.length;
	if (length > maxchars) {
		length = maxchars;
		var str = obj.value.substring(0,maxchars);
		obj.value = str;
	}
	document.getElementById(counterfield).innerHTML=maxchars - length;
}

function hyp_scroll_to_id(elementid) {
	var ta = document.getElementById(elementid);
	var ypos = hyp_findPos(ta); // hyp_get_scroll_position();
	window.scrollTo(0,ypos);
}

function hyp_scroll_to(ypos) {
	window.scrollTo(0,ypos);
}

function hyp_get_scroll_position() {
	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0) {
		if (window.pageYOffset)
			ScrollTop = window.pageYOffset;
		else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return ScrollTop;
}

function hyp_findPos(obj) {
	curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	var bodyleft = document.body.offsetLeft;
	var bodytop = document.body.offsetTop;
//alert("top:"+curtop+" left:"+curleft+" bodytop:"+bodytop+" bodyleft:"+bodyleft);
	return curtop; // [curleft,curtop];
}


function hyp_forgot_password(emailid) {
	var emailval = document.getElementById("LogInEmail").value;
	window.location= hyp_path+"ForgotPassword.php?email="+emailval+"&rnd="+Math.random();
}

// HERE - add preview

// mode {'preview', 'submit'}
function hyp_preview(aid,type,textarea_id){
	var userinfo_str;
	userinfo_str = hyp_build_newentry_str(textarea_id);
	userinfo_str = "&aid="+aid+"&type="+type+userinfo_str;
//alert('userinfo_str:'+userinfo_str);
	window.open(hyp_path+"preview.php?"+userinfo_str,'_blank','titlebar=no,toolbar=no,location=no,menubar=no,width=600,height=400,scrollbars=yes,directories=no,status=no,copyhistory=no');
}

function hyp_build_newentry_str(textarea_id){
	var entry = escape(document.getElementById(textarea_id).value);
//alert('entry:'+entry);
	var video;
	var video_elm = document.getElementById(textarea_id + "_video_embed");
	if (video_elm)
		video = escape(video_elm.value);
	else
		video = '';
	
	var image;
	var image_elm = document.getElementById(textarea_id + "_image_url");
	if (image_elm)
		image = escape(image_elm.value);
	else
		image = '';

	var audio;
	var audio_elm = document.getElementById(textarea_id + "_audio_url");
	if (audio_elm)
		audio = escape(audio_elm.value);
	else
		audio = '';

	var audio_name;
	var audio_name_elm = document.getElementById(textarea_id + "_audio_name");
	if (audio_name_elm)
		audio_name = escape(audio_name_elm.value);
	else
		audio_name = '';
	
	var link_url;
	var link_url_elm;
	var link_name;
	var link_name_elm;
	var link_str='';
	var i;
	var id;
	for (i=1;true;i++) {
		id = textarea_id + "_link_url_" + i;
//alert('linkid:'+id);
		link_url_elm = document.getElementById(textarea_id + "_link_url_" + i);
		if (link_url_elm) {
			link_url = escape(link_url_elm.value);
//alert('url:'+link_url);
			if (link_url != '') {
				link_str = link_str+'&link_url_'+i+'='+link_url;
				link_name_elm = document.getElementById(textarea_id + "_link_name_" + i);
				if (link_name_elm) {
					link_name = escape(link_name_elm.value);
					if (link_name != '')
						link_str = link_str+'&link_name_'+i+'='+link_name;
					else
						link_str = link_str+'&link_name_'+i+'='+link_url;
				}
			}
		} else {
			break;
		}
	}
//alert(link_str);
	var urlvars = "&entry="+entry+
		"&video="+video+
		"&image="+image+
		"&audio="+audio+
		"&audio_name="+audio_name+
		link_str;
//alert('urlvars:'+urlvars);
	return urlvars;

}
// builds a string of the get variables from the userinfo inputs
function hyp_build_userinfo_str(textarea_id){
//alert('textarea_id:'+textarea_id);
	var email;
	var firstname;
	var lastname;
	var address1;
	var address2;
	var city;
	var state;
	var zipcode;
	
	var email_elm = document.getElementById(textarea_id + "_email");
//alert('add_entry 2');
	if (email_elm)
		email = escape(email_elm.value);
	else
		email = '';
	var firstname_elm = document.getElementById(textarea_id + "_first_name");
	if (firstname_elm)
		firstname = escape(firstname_elm.value);
	else
		firstname = '';
	var lastname_elm = document.getElementById(textarea_id + "_last_name");
	if (lastname_elm)
		lastname = escape(lastname_elm.value);
	else
		lastname = '';
	var address1_elm = document.getElementById(textarea_id + "_address1");
	if (address1_elm)
		address1 = escape(address1_elm.value);
	else
		address1 = '';
	var address2_elm = document.getElementById(textarea_id + "_address2");
	if (address2_elm)
		address2 = escape(address2_elm.value);
	else
		address2 = '';
	var city_elm = document.getElementById(textarea_id + "_city");
	if (city_elm)
		city = escape(city_elm.value);
	else
		city = '';
	var state_elm = document.getElementById(textarea_id + "_state");
	if (state_elm)
		state = escape(state_elm.value);
	else
		state = '';
	var zipcode_elm = document.getElementById(textarea_id + "_zipcode");
	if (zipcode_elm)
		zipcode = escape(zipcode_elm.value);
	else
		zipcode = '';
	
	var urlvars = "&email="+email+
		"&firstname="+firstname+
		"&lastname="+lastname+
		"&address1="+address1+
		"&address2="+address2+
		"&state="+state+
		"&zipcode="+zipcode;
//alert('urlvars:'+urlvars);
	return urlvars;
}

function hyp_addentry(aid,parentid,type,textarea_id,button_id,page,mode){
	
//alert("textarea_id:"+textarea_id);
	hyp_cookie_ready = 0;
	hyp_cookie_is = '';

	glbl_aid = aid;
	glbl_refreshid = parentid;
	
	var button = document.getElementById(button_id);
	button.style.color = 'red';
	button.disabled = 'disabled';
	button.innerHTML = 'Submitting...';

// create the GET variables
	if (!page)
		glbl_page = 1;
	else
		glbl_page = page;
	
	if (!mode)
		var mode = '';
	
	var hypid = hyp_getCookie("hypid");
//alert("hypid:"+hypid);
	var scroll = hyp_get_scroll_position();
	
	var userinfo_str;
	userinfo_str = hyp_build_newentry_str(textarea_id)+hyp_build_userinfo_str(textarea_id);
//alert('userinfo_str:'+userinfo_str);
	
	var url = hyp_path+"hyp_addnewentry.php?aid="+aid+"&parent="+parentid+"&type="+type+
		"&hypid="+hypid+"&scroll="+scroll+
		userinfo_str+
		"&rnd="+Math.random();
//alert("addentry("+url+")");

	var script_id_str = "hyp_script_"+aid+"_"+parentid;
//alert("url:"+url+" script_id_str:"+script_id_str);
	hyp_addscript(url,script_id_str);	// make the call to add the entry
	//alert("after addscript");
	wait_timer = 0;
	var nextfunc = "hyp_castvote2('"+script_id_str+"',"+aid+","+parentid+","+page+")";
//alert("about to call - "+nextfunc);
	window.setTimeout(nextfunc,100);
}

function hyp_castvote(aid,entryid,refreshid,entrytype,frm,page)
{
	hyp_cookie_ready = 0;
	hyp_cookie_is = '';
//alert("hyp_castvote("+aid+","+entryid+","+refreshid+","+entrytype+",frm,"+page+")");
	var hypid = hyp_getCookie("hypid");
//alert("hypid:"+hypid);
	glbl_aid = aid;
	glbl_eid = entryid;
	glbl_refreshid =refreshid;
	if (!page)
		glbl_page = 1;
	else
		glbl_page = page;
//alert("refreshid:"+refreshid);
	var rankval;
	if (entrytype != 'C') {
		rankval = document.getElementById("rank_" + aid + "_" + entryid).value;
	} else {
		rankval = '0';
	}

	// button's id
	var ackidstr = 'aid_'+aid+'_eid_'+entryid;
	// read radio
	var voteval = 'u';
	if (entrytype == 's' || entrytype == 'S' ) {
		var yvote = document.getElementById('yvote_'+ackidstr);
		var nvote = document.getElementById('nvote_'+ackidstr);
		if (yvote != null && yvote.checked){
			voteval = 'f';
		}
		if (nvote != null && nvote.checked){
			voteval = 'a';
		}
	}

	var scroll = hyp_get_scroll_position();
	// refreshid is the parentid
	
	if (entrytype != 'C') {
		var url = hyp_path+"castvote.php?aid="+aid+"&eid="+entryid+"&rank="+rankval+"&vote="+voteval+"&hypid="+hypid+"&scroll="+scroll+"&rnd="+Math.random();
	} else {
		var url = hyp_path+"castvote.php?aid="+aid+"&eid="+entryid+"&type=C&parent="+refreshid+"&hypid="+hypid+"&vote=f&scroll="+scroll+"&rnd="+Math.random();
	}
	// find button
	var eid_id = document.getElementById(ackidstr);
	
	//  indicate busy by changing button color 
	eid_id.style.color = 'red';
	eid_id.disabled = 'disabled';
	eid_id.innerHTML = 'Voting..';
	
	var script_id_str = "hyp_script_"+aid+"_"+entryid;

	hyp_cookie_ready=0;
	hyp_addscript(url,script_id_str);	// cast the vote

	wait_timer = 0;
	var nextfunc = "hyp_castvote2('"+script_id_str+"',"+aid+","+refreshid+","+page+")";
	window.setTimeout(nextfunc,100);
}

function hyp_castvote2(script_id_str,aid,refreshid,page){
//alert("hyp_cookie_ready:"+hyp_cookie_ready);
	if (hyp_cookie_ready==1){
//alert("hyp_castvote2('"+script_id_str+"',"+aid+","+refreshid+","+page+"),hyp_cookie_ready:"+hyp_cookie_ready);
		var hypid = hyp_cookie_is;
//alert("hypid:"+hypid);
		hyp_setCookie('hypid',hypid,100);
//alert("after setCookie");
		hyp_delscript(script_id_str);
//alert("after delscript, before hypdisplay, aid:"+aid+" page:"+page+" refreshid:"+refreshid);
		hyp_display(aid,refreshid,page,null,hypid);
//alert("after display");
	} else {
		wait_timer = wait_timer + 1;
//alert('wait_timer'+wait_timer);
		if (wait_timer < hyp_maxwait){
//alert('really wait');
			var nextfunc = "hyp_castvote2('"+script_id_str+"',"+aid+","+refreshid+","+page+")";
			window.setTimeout(nextfunc,100);
		}
	}
}


function hyp_ajax_castvote(url){ 
//alert(url);
	xmlHttp=hyp_GetXmlHttpObject(hyp_wait_vote_finished);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}

function hyp_wait_vote_finished()
{
//var newentryTA = document.getElementById("newentryTA");
	var mystr;

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		mystr = xmlHttp.responseText.split('+++');
//newentryTA.value = mystr;
//alert(mystr);
//alert(mystr[0]);	// 'voted'/'reviewtime'
//alert(mystr[1]);	// hyp_cookie value
//alert(mystr[2]);	// unused
		hyp_setCookie('hypid',mystr[1],100);

		if (mystr[0]=="reviewtime"){
			hyp_review_request_js(glbl_refreshid, false);
		} else if (mystr[0]=="voted" || mystr[0]=="success"){
			hyp_display(glbl_aid,glbl_refreshid);
			// make vote_ack invisible again incase of multiple forums
//			var vack = document.getElementById("vote_ack");
//			vack.style.visibility = 'invisible';
			//window.location= "ArgueDebate.php?entry="+glbl_refreshid+"&rnd="+Math.random();
		} else {
			alert(mystr[0]);
		}
	}
}


function hyp_review_request_js(refreshid, alt) {
	if (confirm("Shared Responsibility Time.\n\nAre you willing to review a new entry at this time?\n \n")) {
		window.location=hyp_path+"ReviewEntry.php?entry="+refreshid+"&alt="+alt+"&rnd="+Math.random();
	} else {
		if (alt) {
			window.location= hyp_path+"AltArgueDebate.php?entry="+refreshid+"&rnd="+Math.random();
		} else {
			window.location= hyp_path+"ArgueDebate.php?entry="+refreshid+"&rnd="+Math.random();
		}
	}
}

//	var currenturl = window.location.href.replace(/\s/g,"%20").replace(/\&/g,"%38");


