
window.onload = init;

function init(){
	replacepop();
	replacebuttons();
	/*if(document.getElementById("featureditem")){
		setfeaturefunc();
	}*/
	if(document.getElementById("contentorder")){
		addcontentorder();
	}
	if (document.getElementById("fsaver")){
		controlsave();
	}
	if (document.getElementById("fsearch")){
		searchset();
	}
}
$(document).ready(function()
  {
$('#featureditem').hover(function() {
        $('#itemdesc1').removeClass('hidden');
      }, function() {
        $('#itemdesc1').addClass('hidden');
      });
})
/*=============================================================
AJAX support
=============================================================*/
var xmlHttp;
function GetXmlHttpObject(){
	var xmlHttp=null;
	try{// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}//catch
		}//catch
	}//catch
	return xmlHttp;
}
/*=============================================================
Help function support
=============================================================*/
function helper(theobj){
	if(theobj.className=="help"){
		theobj.className="helpopen";
	}else{
		theobj.className="help";
	}
}
/*=============================================================
add an up/down button to a text field
=============================================================*/
function addcontentorder(){
	var ccontent = document.getElementById("contentorder").innerHTML;
	var maxrec = parseInt(document.getElementById("fmaxorder").value);
	var ncontent = "<a href='#' class='arrowl' onclick='changeorder(\"up\","+maxrec+");'><abbr title='Move up'>&lt;</abbr></a> ";
	ncontent = ncontent + ccontent;
	ncontent  = ncontent + "<a href='#' class='arrow' onclick='changeorder(\"down\","+maxrec+");'> <abbr title='Move down'>&gt;</abbr></a>";
	//alert(ncontent);
	document.getElementById("contentorder").innerHTML = ncontent;
}
function changeorder(whichway,maxrec){
	var inputval = parseInt(document.getElementById("forder").value);
	if(whichway=="up"){
		if (inputval <= 1){
			alert("It is already at the top.");
		}else{
			inputval=(inputval-1);
		}
	}else if(whichway=="down"){
		if (inputval == maxrec){
			alert("The content is now at the bottom, you can however move it further down. e.g. if you plan to add something before it");
		}
		inputval=(inputval+1);
	}
	document.getElementById("forder").value = inputval;
	document.getElementById("forder").focus();
}

/*random image placer*/
function replacerndimg(){
	var imgArray = new Array();
	var imgArray2 = new Array();
	var images=document.images;
	
	var currentdate = new Date();
	var core =currentdate.getSeconds();
	var core2=currentdate.getSeconds();
	
	for (x in images){
		if (images[x].className=="replace fullimage"){
			imgArray[0] = 'big_femail_violinis.jpg';
			imgArray[1] = 'big_jazz.jpg';
			imgArray[2] = 'big_recorder.jpg';
			imgArray[3] = 'big_trombone.jpg';
			imgArray[4] = 'big_trumpet.jpg';
			imgArray[5] = 'big_violinist.jpg';
			ran = 60/imgArray.length;
			core = Math.floor(core/ran);
			images[x].src="images/common/home/"+imgArray[core];
		}else if(images[x].className=="replace thinimage"){
			imgArray2[0] = 'small_drummer.jpg';
			imgArray2[1] = 'small_flute.jpg';
			imgArray2[2] = 'small_guitar.jpg';
			imgArray2[3] = 'small_singing.jpg';
			imgArray2[4] = 'small_violinist.jpg';
			ran2 = 60/imgArray2.length;//array length
			core2 = Math.floor(core2/ran2);
			images[x].src="images/common/home/"+imgArray2[core2];
		}
	}
}
/*replace button input elements with stylable a tags*/
function replacebuttons(){
	if (!document.getElementsByTagName) return false;
		var links = document.getElementsByTagName("div");
		for(x in links){
			if (links[x].className=="replacebutton"){
				var el =links[x].firstChild;
				var elid = el.id;
				var formid = document.getElementById(elid).form.id;
				var temp = "<a href='#' onclick='document.forms[\""+formid+"\"].submit();'";
				if (el.className !=""){
					temp = temp +" class='"+el.className+"' ";
				}
				temp = temp+">"+el.value+" ";
				if (links[x].lastChild.className =="arrow"){
					temp = temp +"<span class='arrow'>.</span> ";
				}
				temp = temp+"</a>";
				links[x].innerHTML = temp;
			}
			
		}
}
/***********create functionality for featured item**********************/
//onhover and onfocus sets main text to its text, onclick sets image and text
var itemdescswap;
var itemset;
itemset=0;
      


function setfeaturefunc(){
	if (!document.getElementById) return false;
	var i1=document.getElementById('item1');
	/*var i2=document.getElementById('item2');
	var i3=document.getElementById('item3');*/
	var a1=i1.getElementsByTagName('a');
	/*var a2=i2.getElementsByTagName('a');
	var a3=i3.getElementsByTagName('a');*/
	//var as= new Array(a1[0],a2[0],a3[0]);
	//set attributes of first link only rathr than checking because it speeds up performance.

			a1[0].setAttribute('onfocus','featuredfocus(1)');
			a1[0].setAttribute('onmouseover','featuredfocus(1)');
			/*a1[0].setAttribute('onmouseout','featureddefocus(1)');
			a1[0].setAttribute('onblur','featureddefocus(1)');
			//a1[0].setAttribute('onclick','featureset(1);return false;');*/


			/*a2[0].setAttribute('onfocus','featuredfocus(2)');
			a2[0].setAttribute('onmouseover','featuredfocus(2)');
			a2[0].setAttribute('onmouseout','featureddefocus(2)');
			a2[0].setAttribute('onblur','featureddefocus(2)');
			a2[0].setAttribute('onclick','featureset(2);return false;');


			a3[0].setAttribute('onfocus','featuredfocus(3)');
			a3[0].setAttribute('onmouseover','featuredfocus(3)');
			a3[0].setAttribute('onmouseout','featureddefocus(3)');
			a3[0].setAttribute('onblur','featureddefocus(3)');
			a3[0].setAttribute('onclick','featureset(3);return false;');*/
			return false;

}

function featuredfocus(it){
	//alert("yay");
	itemset=1;
	var desc =document.getElementById('itemdesc'+it);
	desc.setAttribute('class','featureditemdesc');
	desc.setAttribute('onmouseout','featureddefocus(1)');
	desc.setAttribute('onblur','featureddefocus(1)');
	/*var itemdesc=document.getElementById('itemdesc'+it).innerHTML;
	itemdescswap=document.getElementById('itemdesc1').innerHTML;
	document.getElementById('itemdesc1').innerHTML=itemdesc;*/
}
function featureddefocus(it){
	if (itemset==1 && document.getElementById('itemdesc1').onmouseout==true){
		var t=setTimeout("document.getElementById('itemdesc1').setAttribute('class','featureditemdesc hidden')",500);
	}
	itemset=0;
}
function featureset(it){
	//collect contents - itemdescswap contains 1 itemdesc1 contains the rolled item
	var itemfrom=document.getElementById('item'+it).innerHTML;
	var itemdesc=document.getElementById('itemdesc'+it).innerHTML;
	//alert(itemdesc);
	itemset=1;
	itemto=document.getElementById('item1').innerHTML;
	//now swap over the contents
	document.getElementById('item'+it).innerHTML=itemto;
	document.getElementById('itemdesc'+it).innerHTML=itemdescswap;
	document.getElementById('item1').innerHTML=itemfrom;
	document.getElementById('itemdesc1').innerHTML=itemdesc;
	itemdescswap=itemdesc;
	//alert(itemdesc);
}
/*******************admin delete and save functions*******************/
function adbutton (storyid, dmode, smode){
		document.getElementById("saved").value = storyid;
		document.getElementById("mode").value = dmode;
		document.getElementById("smode").value = smode;
		if (document.getElementById("deleted")){
			document.getElementById("deleted").value = "";
		}
}
function delbutton (delid, storyid, dmode, smode){
		document.getElementById("deleted").value = delid;
		document.getElementById("saved").value = storyid;
		document.getElementById("mode").value = dmode;
		document.getElementById("smode").value = smode;
}

/**************************************************************/
function check(storyid){

	if (document.getElementById("check"+storyid).className =="delvisible"){
		document.getElementById("check"+storyid).className = "hidden";
	}else{
		document.getElementById("check"+storyid).className = "delvisible";
	}
}
/******************************************************************/

function replacesearch() {
	/*check browser capabilities*/
	if (!document.getElementsByTagName) return false;

	if (document.getElementById("searchlink")){
		var links = document.getElementById("searchlink");
				links.onclick =function() {
			var val=document.getElementById("search").value;
			var linkshref = links.getAttribute("href");
			links.href= linkshref+"?search="+val;
		}

	}
}
/********************************************************************/
function newwin(winfile,winname,attribs) {
	newwindow=window.open(winfile,winname,attribs);
	if (window.focus) {newwindow.focus()}
	return false;
}
function replacepop() {
	if (!document.getElementsByTagName){ alert("Your browser doesn't suport this.");return false;}
	var x = document.getElementsByTagName('a');
	for (var i=0;i<x.length;i++) {
		if (x[i].className == 'popup') {
			x[i].onclick = function () {
				return newwin(this.href, 'newwindow','location=yes,status=yes,scrollbars=yes,resizable=yes,width=600,height=800,top=10,left=10');
			}
			x[i].title += ' (Popup)';
		}
	}
}
/********************************************************************/
function controlsave() {
	if (! document.getElementById) {return false;}
	var saver = parseInt(document.getElementById("fsaver").value);
	var fs = document.getElementsByTagName("fieldset");
	for (var i=0;i<fs.length;i++) {
		if (i < saver){
			fs[i].className="deemphasize";
		}else if(i > saver){
			fs[i].className="hidden";
		}else if(i == saver){
			var theid =fs[i].id;
			document.getElementById(theid).focus();
		}
	}
	
}
function savercontrol() {
	document.getElementById("fsaver").value= parseInt(document.getElementById("fsaver").value)+1;
	
}
/********************************************************************/
function searchset(){
	var it=document.getElementById("fsearch");
	it.setAttribute('onfocus','checksearch()');
	it.setAttribute('onblur','resetsearch()');
}
function checksearch(){
	var it=document.getElementById("fsearch");
	it.setAttribute('style','color:#000;font-style:normal;');
	if(it.value=="search"){
		it.value="";
	}
}
function resetsearch(){
	var it=document.getElementById("fsearch");
	it.setAttribute('style','color:#333;font-style:italic;');
	if(it.value==""){
		it.value="search";
	}
}
/********************************************************************/
function addsection(){
	var curHTML = document.getElementById("substories").innerHTML;//old html
	//alert("hello" + document.getElementById("substories").innerHTML);
	var newHTML = "";//to hold html to insert
	var icount=0;//to hold count of sections
	if((curHTML.search(/class="section"/i))>0){
		var sects = document.getElementById("substories").getElementsByTagName("div");
		for (var i=0; i<sects.length; i++) {
			if (sects[i].className=="section"){
				icount++;
			}
		}
	}
	newHTML='<div class="section"><div class="formrow"><label for="fstitle'+icount+'" class="inputlabel">Story title: </label><input type="text" id="fstitle'+icount+'" name="fstitle'+icount+'" value="" class="input50" /></div><div class="formrow"><label for="fstext'+icount+'" class="inputlabel">Story teaser (255 chars max): </label><input type="text" id="fstext'+icount+'" name="fstext'+icount+'" value="" class="input50" /></div><div class="formrow"><label for="fslink'+icount+'" class="inputlabel">Story link: (<span class="note">e.g. http://www.me.com/</span>)</label><input type="text" id="fslink'+icount+'" name="fslink'+icount+'" value="" class="input50" /></div><div class="formrow"><iframe id="imgupload'+icount+'" src="admin/uploadfile.php?field='+icount+'" frameborder="0" class="iframe imgupload"></iframe></div><div class="formrow"><label for="fimage'+icount+'" class="inputlabel">Picture:</label> <input class="input50 disabled" name="fimage'+icount+'" type="text" id="fimage'+icount+'" value="" /></div></div>';
	document.getElementById("substories").innerHTML=curHTML+newHTML;
	document.getElementById("newsubstory").value = parseInt(parseInt(document.getElementById("newsubstory").value)+1);
	document.getElementById('f'+icount).focus();
}

function removesection(thesection){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="/admin/delstory.php";
	url=url+"?q="+thesection;
	url=url+"&sid="+Math.random();
	//when the server finishes call the js to delete the section from the page
	xmlHttp.onreadystatechange=delsect(thesection);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function delsect(thesection){
	//if (xmlHttp.readyState==4){
		var p = document.getElementById("substories");//parent element
		var c = document.getElementById("sect"+thesection);//parent element
		p.removeChild(c);
		document.getElementById("newsubstory").value = parseInt(parseInt(document.getElementById("newsubstory").value)-1);
	//}else{
	//	alert("umm... "+xmlHttp.responseText);
	//}
}
//add event images
function addimage (thesection, theimage, thealt, themain){//called from uploadfile
	var newHTML = "";//to hold html to insert
	var icount=1;//to hold count of sections
	var container;//to hold cotainer obj
	var orignum;//to hold original numver obj
	var newnum;//to hold new numver obj
	container = document.getElementById("imglistcontainer");
	orignum=document.getElementById("oimgnum").value;
	newnum=parseInt(document.getElementById("imgnum").value)+1;
	var curHTML = container.innerHTML;//old html
		icount=newnum;
	if(themain=="true"){
		var check = 'checked="checked" ';
	}
	newHTML='<tr class="'+icount+'"><td>'+theimage+'</td><td>'+thealt+'</td><td><input id="fmain'+icount+'" class="checkinput" type="checkbox" '+check+' value="1" name="fmain'+icount+'" onchange="unmain(this,'+thesection+', \''+theimage+'\', \''+thealt+'\', '+themain+')" /></td><td><input id="assoc'+icount+'" type="button" value="Dis-associate"  onclick="dissaveimg('+thesection+', \''+theimage+'\', \''+thealt+'\', '+themain+')" /></td></tr>';
	container.innerHTML=curHTML+newHTML;
	newnum = parseInt(newnum+1);
	checkmain();
	document.getElementById('fimage0').focus();
}
function checkmain(){//check for more than one checked main image
	if (! document.getElementById) return false;
	
	var fs = document.getElementsByTagName("input");
	var checkcount =0;
	for (x in fs){
		xid=fs[x].id+'';		
		if (xid.search(/fmain/i)){
			if(fs[x].checked==true){
				checkcount++;
			}
		}
	}
	//alert(checkcount+" "+checkcount>2);
	if(parseInt(checkcount) >2){
		alert("Please only choose 1 main image per event.");
	}
}
//saves the image info to db
function saveimg(thesection, theimage, thealt, themain){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="/admin/delstory.php";
	url=url+"?s="+thesection+"&img="+theimage+"&a="+thealt+"&m="+themain;
	url=url+"&sid="+Math.random();
	//when the server finishes call the js to delete the section from the page
	xmlHttp.onreadystatechange=addimage(thesection, theimage, thealt, themain);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
//unmain buttons over
function unmain(that,thesection, theimage, thealt, themain){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	  var val;
	 if(that.checked){
		 val=2;
	 }else{
		 val=1;
	 }
	var url="/admin/delstory.php";
	url=url+"?s="+thesection+"&img="+theimage+"&a="+thealt+"&m="+themain;
	url=url+"&unmain="+val;
	url=url+"&dis="+Math.random();
	url=url+"&sid="+Math.random();
	//when the server finishes call the js to delete the section from the page
	xmlHttp.onreadystatechange=checkmain();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
//dis-associate the image from the  event
function dissaveimg(thesection, theimage, thealt, themain){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="/admin/delstory.php";
	url=url+"?s="+thesection+"&img="+theimage+"&a="+thealt+"&m="+themain;
	url=url+"&dis="+Math.random();
	url=url+"&sid="+Math.random();
	//when the server finishes call the js to delete the section from the page
	xmlHttp.onreadystatechange=removeimage(themain);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
//remove table row
function removeimage(themain){
	var p = document.getElementById("imglistcontainer");//parent element
	/*for (var i = 0; i < p.children.length; i++) {
		if (p.children[i].tagName == "table") {
			var c = p.children[i];//child element-table
		}
	}
	for (var i = 0; i < c.children.length; i++) {
		if (c.children[i].tagName == "tbody") {
			var t = c.children[i];//child elements-tbody
		}
	}
	var tr = p.children;//trs
	alert(tr);*/
	tr=p.getElementsByTagName("tr");
	for (var i = 0; i < tr.length; i++) {
		if (tr[i].className == themain) {
			p.removeChild(tr[i]);
		}
	}
	document.getElementById("imgnum").value = parseInt(parseInt(document.getElementById("imgnum").value)-1);
}
/********************************************************************/
function changeheight (){
	if (document.getElementById) {
		var lheight = document.getElementById('col2');
		var cheight = document.getElementById('brown');
		var diff =  (lheight.offsetHeight - cheight.offsetHeight);
		if (diff > 0) {
			var copyheight = document.getElementById('brown');
			copyheight.style.height = (copyheight.offsetHeight + diff - 79) + 'px';
			copyheight.style.width = 442 + 'px';
		}
		
		diff = (cheight.offsetHeight - lheight.offsetHeight);

		if (diff > 0) {

			var navheight = document.getElementById('col2');
			navheight.style.height = (navheight.offsetHeight + diff -4) + 'px';
			
		}
	}
}
function changeheightie (){
	if (document.getElementById) {
		var lheight = document.getElementById('col2');
		var cheight = document.getElementById('brown');
		var diff =  (lheight.offsetHeight - cheight.offsetHeight);
		if (diff > 0) {
			var copyheight = document.getElementById('brown');
			copyheight.style.height = (copyheight.offsetHeight + diff - 65) + 'px';
			copyheight.style.width = 440 + 'px';
		}
		
		diff = (cheight.offsetHeight - lheight.offsetHeight);

		if (diff > 0) {

			var navheight = document.getElementById('col2');
			navheight.style.height = (navheight.offsetHeight + diff -4) + 'px';
			
		}
	}
}
function changeheightie7 (){
	if (document.getElementById) {
		var lheight = document.getElementById('col2');
		var cheight = document.getElementById('brown');
		var diff =  (lheight.offsetHeight - cheight.offsetHeight);
		if (diff > 0) {
			var copyheight = document.getElementById('brown');
			copyheight.style.height = (copyheight.offsetHeight + diff - 40) + 'px';
			copyheight.style.width = 443 + 'px';
		}
		
		diff = (cheight.offsetHeight - lheight.offsetHeight);

		if (diff > 0) {

			var navheight = document.getElementById('col2');
			navheight.style.height = (navheight.offsetHeight + diff -4) + 'px';
			
		}
	}
}
