/* javascript functions for mypropaganda
   these are the bits I wrote. There is a library of stuff other wonderful people
   wrote at /lib.js. I did modify moo.fx slightly to add a .resize() function
   though.
*/

/* here were are doing various things when the page has loaded including
   setting up moo.fx height effect and a scroll effect. We also execute
   some functions that turn nice happy html links into horrible evil
   javascript ones. This stops linux nerds complaining that the site doesn't
   work in lynx
*/


var collapsed;
var current = 0;


function initialise(){
	collapsed = true;
	slideSearch = new fx.Height('searchwindow', {duration:400, onComplete: function()
 		{
			if(collapsed){
				collapsed = false;
			}else if(slideSearch.now == 0){
				collapsed = true;
			}
		}});

	scrollComment = new fx.Scroll({duration: 800});
	changesearch('/date/',search_date);
	changesearch('/tag/',search_tag);
	changelink('/archivesearch/',archivesearch);	
}

var search_date = function(search_terms){
	var post = new Object;
	post["search_type"] = "date";
	post["search1"] = search_terms;
	var jsonpost = JSON.stringify(post);
	x_search(jsonpost,search_cb);
}

var search_tag = function(search_terms){
	var post = new Object;
	post["search_type"] = "tag";
	post["search1"] = search_terms;
	var jsonpost = JSON.stringify(post);
	x_search(jsonpost,search_cb);
}

var search_terms = function(){
	document.getElementById('search_submit').src = "http://beta.mypropaganda.co.uk/t/mp5/images/searching.gif";
	var post = new Object;
	post["search_type"] = "normal";
	post["search1"] = document.getElementById("search_string").value;
	var jsonpost = JSON.stringify(post);
	x_search(jsonpost,search_cb);
	return false;
}

function search_cb(result){
	eval("var customarray = "+result);
	document.getElementById("search_results").innerHTML = customarray.returnhtml;
	document.getElementById('search_submit').src = "http://beta.mypropaganda.co.uk/t/mp5/images/search.png";
	slideSearch.resize();
}

function archivesearch(){
	slideSearch.toggle();
}

function changesearch(linktype,functionname){
	clinks = document.getElementsByTagName('a');
	for(i=0; i<clinks.length; i++){
    	cl = clinks[i];
 		if (cl.href && cl.href.indexOf(linktype) != -1) {
	 		cl.onclick = function(){
				lid = this.href.split('/');
		 		//this.href = "javascript:void(0)";
				if(linktype == "/date/"){
		 			functionname(lid[(lid.length-3)]+"_"+lid[(lid.length-2)]);
	 			}else{
		 			functionname(lid[(lid.length-2)]);
	 			}		 			
				return false;
			}
		}
	}
}

function changelink(linktype,functionname){
	clinks = document.getElementsByTagName('a');
	for(i=0; i<clinks.length; i++){
    	cl = clinks[i];
 		if (cl.href && cl.href.indexOf(linktype) != -1) {
	 		cl.onclick = function(){
				lid = this.href.split('/');
		 		this.href = "javascript:void(0)";
				functionname(lid[(lid.length-2)]);
			}
		}
	}
}   

/* addcomment - this is what's executed if you submit the add comment form. We use JSON to encode
   all our form fields into once nice big expression to go ajaxing with. The php on the other side
   uses a JSON object to decode it back into seperate fields for working with.
*/
function addcomment(){
	document.getElementById('comment_submit').src = "http://beta.mypropaganda.co.uk/t/mp5/images/saving.gif";
	var f=document.forms['add_c'];
	var post1 = new Object;
	for (i=0;i<f.length;i++){           
		post1[f[i].id]  = f[i].value; 
	}
	var strPOST = JSON.stringify(post1);  
	x_add_comment(strPOST, add_comment_cb);
}

/* add_comment_cb - once again, this is what's executed when we get results back from ajaxing. The
   JSON encoded data returned is sorted out with eval. There is a status variable sent back from the
   php which is checked. 0 = the name field wasn't filled out. Some javascript highlights this fact
   to the user. 1 = successful. Their comment is added to the end of the existing comments, the
   comments section is resized and the comments form is cleared. 2 = flooding. Some javascript 
   highlights this fact to the user.
*/
function add_comment_cb(result){
	document.getElementById('comment_submit').src = "http://beta.mypropaganda.co.uk/t/mp5/images/submit.png";	
	var f=document.forms['add_c'];
	var c=document.getElementById('comments');
	var e=document.getElementById('error');
	var n=document.getElementById('name');
	eval("var customarray = "+result);
	if(customarray.status == 1){
		e.style.display = "none";
		e.innerHTML = "";
		n.style.background = '#FF5BBD';
		n.style.border = '0';
		n.onfocus = function(){
			n.style.background = '#FFB3D9';
			n.style.color = '#FF3399';
		}
		n.onblur = function(){
			n.style.background = '#FF5BBD';
			n.style.color = 'white';
		}			
		if(customarray.commentcount == 0){
			c.innerHTML= customarray.returnhtml;
		}else{
			c.innerHTML= c.innerHTML+customarray.returnhtml;
		}			
		for (i=0;i<f.length;i++){
			f[i].value = "";
		}
		f['update_id'].value = customarray.update_id;
	}else if(customarray.status == 0){
		e.style.display = "block";
		e.innerHTML = customarray.error;
		n.style.background = '#CC0066';
		n.style.border = '0';
	}else if(customarray.status == 4){
		e.style.display = "block";
		e.innerHTML = customarray.error;
		n.style.background = '#FF5BBD';
		n.style.border = '0';
		n.onfocus = function(){
			n.style.background = '#FFB3D9';
			n.style.color = '#FF3399';
		}
		n.onblur = function(){
			n.style.background = '#FF5BBD';
			n.style.color = 'white';
		}				
	}else{
		e.style.display = "block";
		e.innerHTML = customarray.error;
		n.style.background = '#CC0066';
		n.style.border = '0';
		n.onfocus = function(){
			n.style.background = '#CC0066';
			n.style.color = '#FF3399';
		}
		n.onblur = function(){
			n.style.background = '#FF5BBD';
			n.style.color = 'white';
		}					
	}	
}


