document.observe("dom:loaded", initialize);
var navSelected = null;
var navImage = null;
var navLinks = new Array("news", "shows", "about", "artists", "construction", "construction");
var buttons = new Array();

function initialize() {
	setupNav();
}

function setupNav() {
	navs = document.getElementsByClassName('navButton')
	for(i = 0; i < navs.length; i++){
		nn = new navButton(navs[i])
		buttons.push(nn);
	}
	changePage("jump/index.html", setupJump);
}

var navButton = Class.create()

navButton.prototype = {
	initialize: function(host) {
		this.host = host;
		this.content = host.href;
		this.buttonNum = host.rel;
		Event.observe(host, 'click', this.select.bindAsEventListener(this), false);
		host.onclick = function(){return false};

	},
	select: function() {
		amount = "28px";
		if(this.buttonNum > 6) {
			changePage("construction/index.html");
			closeSelected();
			return;
		}
		if(this.buttonNum == 0) {
			changePage("jump/index.html", setupJump);
			closeSelected();
			return;
		}
		image = $('nav' + this.buttonNum).getElementsByTagName('img')[0]
		numB = this.buttonNum
		if(image != navImage) {
			this.host.getElementsByTagName('div')[0].className = "navCenter o";
			image.src = "images/spikes/o.png";
			if(this.buttonNum == 1)	{	
				changePage("news/index.html", loadNews);
			} else if (this.buttonNum == 2) {
				changePage("shows/index.html", loadShows);
			} else if (this.buttonNum == 3) {
				changePage("about/index.html", loadAbout);
			} else if (this.buttonNum == 4) {
				changePage("artists/index.html", loadArtists);
			} else if (this.buttonNum == 5) {
				changePage("classes/index.html", loadClasses);
			} else if (this.buttonNum == 6) {
				changePage("apply/index.html", loadApply);
			}
			
			closeSelected();

			new Effect.Parallel([
				new Effect.Move(image, {y:28, mode: 'absolute', sync: true})
			], { 
				duration: 0.2, 
				transition: Effect.Transitions.sinoidal,
				beforeStart : function () {
					navImage = image;
					navSelected = numB;
				}
			});	
		}
	}
}

function closeSelected() {
	if(navImage) {
		//navImage.host.getElementsByTagName('div')[0].className = "navCenter o";
		//$(navImage).morph('margin-top: -28px;', {duration:0.2, transition: Effect.Transitions.sinoidal});
		div =  $('nav' + navSelected).getElementsByTagName('div')[0];
		div.className = "navCenter dg";
		navImage.src = "images/spikes/dg.png";
		new Effect.Move($(navImage), {y:-28, mode: 'absolute', duration:0.2});
		navImage = null;
	}
}

function changeSelected(id) {
	image = $('nav' + buttons[id].buttonNum).getElementsByTagName('img')[0]
	numB = buttons[id].buttonNum	
	buttons[id].host.getElementsByTagName('div')[0].className = "navCenter o";
	image.src = "images/spikes/o.png";	
	closeSelected();	
	new Effect.Parallel([new Effect.Move(image, {
		y: 28,
		mode: 'absolute',
		sync: true
	})], {
		duration: 0.2,
		transition: Effect.Transitions.sinoidal,
		beforeStart: function(){
			navImage = image;
			navSelected = numB;
		}
	});	
	
}

function wrap(start) {
	if(navImage) {
 		//$(navImage).morph('margin-top: -28px;', {duration:0.2});
		new Effect.Move($(navImage), {y:-28, mode: 'relative', duration:0.2});
	}

	image = $('nav' + start).getElementsByTagName('img')[0]
	//$(image).morph('margin-top: 0px;', {duration:0.2, afterFinish: function () {
	new Effect.Move($(image), {y:28, mode: 'relative', duration:0.2, afterFinish: function() {
			if (start == 6) {
				wrap(1);
			} else {
				wrap(start+1);
			}
	}});
	navImage = image;
}

function changePage(replacement, finishedFunc, funcArgs) { 
	new Effect.Parallel([
		new Effect.Morph("main", {style: 'opacity: 0.0', sync: true})
	], {
		duration: 0.1,
		transition: Effect.Transitions.sinoidal,
		afterFinish: function() {
			AjaxHandler.sendRequest(replacement, function(results) {
				$('main').innerHTML = results;
				AjaxHandler.sendRequest("footer.html", function(results) {
					$('main').innerHTML += results;					
					new Effect.Morph("main", {style: 'opacity: 1.0', duration: 0});
					if(typeof(finishedFunc) == "function") {
						if (funcArgs != undefined) {
							finishedFunc(funcArgs);
						}
						else {
							finishedFunc();
						}
					}	
				});
			});
		}
	});
}

function openArtist(featureId) {
	changePage("artists/index.html", loadArtists, featureId);	
	changeSelected(4);
}

function openApply() {
	changePage('apply/index.html', loadApply);
	changeSelected(6);
}

function loadArtists(featureId) {
	new Ajax.Request("scripts/getProfiles.php", {
		method: 'post',
		onComplete: function(transport) {
			 processLoad(transport, featureId);
		},
		onFailure: function(){ alert('Something went wrong with the database. Refresh the page or try again in a little while')}
	});
}

function loadShows() {
	new Ajax.Request("scripts/getShows.php", {
		method: 'post',
		onComplete: showsProcessLoad,
		onFailure: function(){ alert('Something went wrong with the database. Refresh the page or try again in a little while')}
	});
}

function loadNews() {
	new Ajax.Request("scripts/getNews.php", {
		method: 'post',
		onComplete: newsProcessLoad,
		onFailure: function(){ alert('Something went wrong with the database. Refresh the page or try again in a little while')}
	});
}

function loadApply() {
	new Ajax.Request("scripts/getApply.php", {
		method: 'post',
		onComplete: applyProcessLoad,
		onFailure: function(){ alert('Something went wrong with the database. Refresh the page or try again in a little while')}
	});	
}

function loadClasses() {
	new Ajax.Request("scripts/getClasses.php", {
		method: 'post',
		onComplete: classesProcessLoad,
		onFailure: function(){ alert('Something went wrong with the database. Refresh the page or try again in a little while')}
	});	
}

function loadAbout() {
	new Ajax.Request("scripts/getAbout.php", {
		method: 'post',
		onComplete: aboutProcessLoad,
		onFailure: function(){ alert('Something went wrong with the database. Refresh the page or try again in a little while')}
	});
}

function processLoad(transport, featuredId) {
	//fname, lname, profileImage, medium, description, type, link, isCurrent, cv, id
	transport.responseText = transport.responseText.replace(/'/, "");
		categories = transport.responseText.split('^^&^^');
        entries = categories[0].split("**&**");
        recentExhibitors = categories[1].split("**&**");
		var hasFeature = 0;
		if (typeof(featuredId) == "number" || typeof(featuredId) == "string") {
			hasFeature = 1;
		} else {
			randomId = Math.floor(entries.length*Math.random());		
		}
        artistDetails = entries[1].split('&^&');
		featureDetails = artistDetails;
        for(i=0;i<entries.length-1;i++){
                artistDetails = entries[i].split('&^&');
			    if(hasFeature == 1 && artistDetails[9] == featuredId) {
			        featureDetails = artistDetails;
				}
				if(hasFeature == 0 && i == randomId) {
			        featureDetails = artistDetails;					
				}
                injectArtist(artistDetails);
        }
        for(i=0;i<recentExhibitors.length-1;i++){
                artistDetails = recentExhibitors[i].split('&^&');
                injectExhibitor(artistDetails);
        }        
        injectFeature(featureDetails);		
}

function applyProcessLoad(transport) {
	//id, title, content, dl1name, dl2name, link1name, link1url, link2name, link2url, hasImage, hasDl1, hasDl2 
	transport.responseText = transport.responseText.replace(/'/, "");
    entries = transport.responseText.split('**&**');
   	for(i=0;i<entries.length-1;i++){
    	applyDetails = entries[i].split('&^&');
        injectApply(applyDetails);
    }
}

function classesProcessLoad(transport) {
	//id, title, content, dl1name, dl2name, link1name, link1url, link2name, link2url, hasImage, hasDl1, hasDl2 
	transport.responseText = transport.responseText.replace(/'/, "");
    entries = transport.responseText.split('**&**');
   	for(i=0;i<entries.length-1;i++){
    	classesDetails = entries[i].split('&^&');
        injectClass(classesDetails);
    }
}

function newsProcessLoad(transport) {
	//id, title, content
	transport.responseText = transport.responseText.replace(/'/, "");
    entries = transport.responseText.split('**&**');
   	for(i=0;i<entries.length-1;i++){
    	newsDetails = entries[i].split('&^&');
        injectNews(newsDetails);
    }
}

function aboutProcessLoad(transport) {
    //id, title, content
    transport.responseText = transport.responseText.replace(/'/, "");
    entries = transport.responseText.split('**&**');
    for(i=0;i<entries.length-1;i++){
    	aboutDetails = entries[i].split('&^&');
        injectAbout(aboutDetails);
    }
}

function showsProcessLoad(transport) {
	//showName, id, summary, description, hasImage, imageExt, startDate, endDate, status, gallery, linkName, linkUrl, openingDate, artists
	transport.responseText = transport.responseText.replace(/'/, "").substring(0,transport.responseText.lastIndexOf("**&**"));
    entries = transport.responseText.split('**&**');
    //entries.sort(cmpDates);
    featureShowDetails = entries[0].split("&^&");
    for(i=0;i<entries.length;i++){
    	showDetails = entries[i].split('&^&');
		if(showDetails[8] == "0") {
   			featureShowDetails = showDetails;
 		}
		injectShow(showDetails);
		//attachArtists(showDetails, injectShow);
    }
	injectFeatureShow(featureShowDetails);
    //attachArtists(featureShowDetails, injectFeatureShow);
}

/*function attachArtists(showDetails, functionName) {
	var params = "id=" + showDetails[1];
	new Ajax.Request("scripts/getShowArtists.php", {
		method: 'post',
		parameters: params,
		onComplete: function(transport) {
			artists = transport.responseText.split("**&**");	
			functionName(showDetails, artists);
		},
		onFailure: function() {alert("Failed to retrieve show artists. Refresh the page or try again in a little while")}
	});			
}*/

function injectExhibitor(artistDetails) {
	parentd = $("recontent");	
	thumblink = new Element('a', {'href': '#'});
	thumbd = new Element('div', {'class': 'artistThumb'});
	thumbd.className = "artistThumb";
	fname = new Element('p', {'className': 'artistThumbFirst'});
	fname.className = "artistThumbFirst";
	lname = new Element('p', {'class': 'artistThumbLast'});
	lname.className = "artistThumbLast";
    fname.innerHTML = artistDetails[0];
    lname.innerHTML = artistDetails[1];
    link = (artistDetails[2].replace(/^..\//, ''));
    ext = link.split(".")[1];
    bits = link.split('/');
    link = ""
    for(j=0;j<bits.length-1;j++){
    	link += bits[j] + "/";
	}
	linkT = "http://cococo.net.au/backend/" + link + "thumb." + ext;
    thumb = new Element('img', {'src': linkT});
	Event.observe(thumblink, 'click', injectAdapter.bindAsEventListener(thumblink), false);
	thumblink.setAttribute('rel', artistDetails[0]+"&^&"+artistDetails[1]+"&^&"+artistDetails[2]+"&^&"+artistDetails[3]+"&^&"+artistDetails[4]+"&^&"+artistDetails[5]+"&^&"+artistDetails[6]+"&^&"+artistDetails[7]+"&^&"+artistDetails[8]);
    thumbd.appendChild(fname);
    thumbd.appendChild(lname);
    thumbd.appendChild(thumb);
	thumblink.appendChild(thumbd);
    parentd.appendChild(thumblink);
}

function injectApply(applyDetails) {
	//id, title, content, dl1name, dl2name, link1name, link1url, link2name, link2url, hasImage, hasDl1, hasDl2 
	parentDiv = $("applyContent");
	blockDiv = new Element('div', {'class': 'block'});
	blockDiv.className = 'block';
	blockTitleDiv = new Element('div', {'class':'blockTitle'});
	blockTitleDiv.className = 'blockTitle';
	blockContentDiv = new Element('div', {'class':'blockContent'});
	blockContentDiv.className = 'blockContent';
	leftDiv = new Element('div', {'class':'applyLeft'});
	leftDiv.className = "applyLeft";
	rightDiv = new Element('div', {'class':'applyRight'});
	rightDiv.className = "applyRight";	
	
	headerOne = new Element('h1');
	bodyTextP = new Element('p', {'class':'bodyTextApply'});
	bodyTextP.className = 'bodyTextApply';
		
	headerOne.innerHTML = applyDetails[1];
	var value = applyDetails[2].replace(/[`]/g, "'");	
	bodyTextP.innerHTML = value;

	linkBase = "http://cococo.net.au/backend/apply/images/" + applyDetails[0] + "/";

	if(applyDetails[9] == 1) {
		icon = new Element('img', {'class':'applyImage'});
		icon.className = "applyImage";
		icon.src = linkBase + "thumb";
		rightDiv.appendChild(icon);	
	}	
	
	linksWrapper = new Element('p', {'class':'featureCreditsM'});
	linksWrapper.className = "featureCreditsM";
	links = new Element('span', {'class':'featureCredits'});
	links.className = "featureCredits";	
	
	if(applyDetails[5] != "" && applyDetails[6] != "") {		
		link1 = new Element('a');	
		link1.innerHTML = applyDetails[5];
		link1.href = applyDetails[6];		
		links.appendChild(link1);	
		links.appendChild(new Element('br'));
	}
	if(applyDetails[7] != "" && applyDetails[8] != "") {		
		link2 = new Element('a');
		link2.innerHTML = applyDetails[7];
		link2.href = applyDetails[8];	
		links.appendChild(link2);	
		links.appendChild(new Element('br'));			
	}
	links.appendChild(new Element('br'));				
			
	if(applyDetails[10] == 1 && applyDetails[3] != "") {
		link3 = new Element('a');
		link3.innerHTML = applyDetails[3];
		link3.href = linkBase + "dl1.pdf";
		links.appendChild(link3);		
		links.appendChild(new Element('br'));							
	}
	if(applyDetails[11] == 1 && applyDetails[4] != "") {
		link4 = new Element('a');
		link4.innerHTML = applyDetails[4];
		link4.href = linkBase + "dl2.pdf";
		links.appendChild(link4);		
		links.appendChild(new Element('br'));							
	}			
	leftDiv.appendChild(headerOne);
	leftDiv.appendChild(bodyTextP);
	linksWrapper.appendChild(links);
	rightDiv.appendChild(linksWrapper);	
		
	blockContentDiv.appendChild(leftDiv);
	blockContentDiv.appendChild(rightDiv);	
	
	blockDiv.appendChild(blockTitleDiv);
	blockDiv.appendChild(blockContentDiv);
	parentDiv.appendChild(blockDiv);
	
	
	/*	<div class="applyLeft">
			<h1> An example application title </h1>
			<p class="bodyTextApply"></p>
		</div>
		<div class="applyRight">
			<!--The image<div style="width:200px;height:200px;background-color:black;"></div>-->
			<p class="featureCreditsM">
				<span class="featureCredits">
					<a href="http://paulkrix.com"> my space </a> <br />
					<a href="http://paulkrix.com"> workshop website </a> <br />
					<br />
					<a href="http://paulkrix.com"> application form </a> <br />
					<a href="http://paulkrix.com"> national cremetorium </a> <br />
					
				</span>
			</p>
							
		</div>
*/


}

function injectClass(classDetails) {
	//id, title, content, dl1name, dl2name, link1name, link1url, link2name, link2url, hasImage, hasDl1, hasDl2 
	parentDiv = $("classContent");
	blockDiv = new Element('div', {'class': 'block'});
	blockDiv.className = 'block';
	blockTitleDiv = new Element('div', {'class':'blockTitle'});
	blockTitleDiv.className = 'blockTitle';
	blockContentDiv = new Element('div', {'class':'blockContent'});
	blockContentDiv.className = 'blockContent';
	leftDiv = new Element('div', {'class':'applyLeft'});
	leftDiv.className = "applyLeft";
	rightDiv = new Element('div', {'class':'applyRight'});
	rightDiv.className = "applyRight";	
	
	headerOne = new Element('h1');
	bodyTextP = new Element('p', {'class':'bodyTextApply'});
	bodyTextP.className = 'bodyTextApply';
		
	headerOne.innerHTML = classDetails[1];
	var value = classDetails[2].replace(/[`]/g, "'");
	bodyTextP.innerHTML = value;
	linkBase = "http://cococo.net.au/backend/classes/images/" + classDetails[0] + "/";

	if(classDetails[9] == 1) {
		icon = new Element('img', {'class':'applyImage'});
		icon.className = "applyImage";
		icon.src = linkBase + "thumb";
		rightDiv.appendChild(icon);	
	}	
	
	linksWrapper = new Element('p', {'class':'featureCreditsM'});
	linksWrapper.className = "featureCreditsM";
	links = new Element('span', {'class':'featureCredits'});
	links.className = "featureCredits";	
	
	if(classDetails[5] != "" && classDetails[6] != "") {		
		link1 = new Element('a');	
		link1.innerHTML = classDetails[5];
		link1.href = classDetails[6];		
		links.appendChild(link1);	
		links.appendChild(new Element('br'));
	}
	if(classDetails[7] != "" && classDetails[8] != "") {		
		link2 = new Element('a');
		link2.innerHTML = classDetails[7];
		link2.href = classDetails[8];	
		links.appendChild(link2);	
		links.appendChild(new Element('br'));			
	}
	links.appendChild(new Element('br'));				
			
	if(classDetails[10] == 1 && classDetails[3] != "") {
		link3 = new Element('a');
		link3.innerHTML = classDetails[3];
		link3.href = linkBase + "dl1.pdf";
		links.appendChild(link3);		
		links.appendChild(new Element('br'));							
	}
	if(classDetails[11] == 1 && classDetails[4] != "") {
		link4 = new Element('a');
		link4.innerHTML = classDetails[4];
		link4.href = linkBase + "dl2.pdf";
		links.appendChild(link4);		
		links.appendChild(new Element('br'));							
	}			
	leftDiv.appendChild(headerOne);
	leftDiv.appendChild(bodyTextP);
	linksWrapper.appendChild(links);
	rightDiv.appendChild(linksWrapper);	
		
	blockContentDiv.appendChild(leftDiv);
	blockContentDiv.appendChild(rightDiv);	
	
	blockDiv.appendChild(blockTitleDiv);
	blockDiv.appendChild(blockContentDiv);
	parentDiv.appendChild(blockDiv);

}



function injectNews(newsDetails) {
	parentDiv = $("newsContent");
	blockDiv = new Element('div', {'class': 'block'});
	blockDiv.className = 'block';
	blockTitleDiv = new Element('div', {'class':'blockTitle'});
	blockTitleDiv.className = 'blockTitle';
	blockContentDiv = new Element('div', {'class':'blockContent'});
	blockContentDiv.className = 'blockContent';
	headerOne = new Element('h1');
	headerThree = new Element('h3');
	bodyTextP = new Element('p', {'class':'bodyText'});
	bodyTextP.className = 'bodyText';
	//dateUL = new Element('ul');
	//dateLI = new Element('li', {'class':'newsDate'});
		
	//headerOne.innerHTML = newsDetails[1] + " - " + newsDetails[3];
	headerOne.innerHTML = newsDetails[1];
	bodyTextP.innerHTML = newsDetails[2];
	//dateLI.innerHTML = newsDetails[3];
	headerThree.innerHTML = newsDetails[3];
	
	//dateUL.appendChild(dateLI);
	
	blockTitleDiv.appendChild(headerThree);	
	blockContentDiv.appendChild(headerOne);
	blockContentDiv.appendChild(bodyTextP);
	
	blockDiv.appendChild(blockTitleDiv);
	blockDiv.appendChild(blockContentDiv);
	parentDiv.appendChild(blockDiv);

}

function injectAbout(aboutDetails) {
	parentDiv = $("aboutContent");
	blockDiv = new Element('div', {'class': 'block'});
	blockDiv.className = 'block';
	blockTitleDiv = new Element('div', {'class':'blockTitle'});
	blockTitleDiv.className = 'blockTitle';
	blockContentDiv = new Element('div', {'class':'blockContent'});
	blockContentDiv.className = 'blockContent';
	headerOne = new Element('h1');
	bodyTextP = new Element('p', {'class':'bodyText'});
	bodyTextP.className = 'bodyText';
	
	headerOne.innerHTML = aboutDetails[1];
	bodyTextP.innerHTML = aboutDetails[2];

	blockTitleDiv.appendChild(headerOne);
	blockContentDiv.appendChild(bodyTextP);
	
	blockDiv.appendChild(blockTitleDiv);
	blockDiv.appendChild(blockContentDiv);
	parentDiv.appendChild(blockDiv);

}

function injectArtist(artistDetails) {
	
	if (artistDetails[5] == 0){
		return;
	} else {
		if(artistDetails[7] == 1){
		    parentd = $("tcontent");
		} else {
			parentd = $("ptcontent");
		}
	}
	thumblink = new Element('a', {'href': '#'});
	thumbd = new Element('div', {'class': 'artistThumb'});
	thumbd.className = "artistThumb";
	fname = new Element('p', {'className': 'artistThumbFirst'});
	fname.className = "artistThumbFirst";
	lname = new Element('p', {'class': 'artistThumbLast'});
	lname.className = "artistThumbLast";
    fname.innerHTML = artistDetails[0];
    lname.innerHTML = artistDetails[1];
    link = (artistDetails[2].replace(/^..\//, ''));
    ext = link.split(".")[1];
    bits = link.split('/');
    link = ""
    for(j=0;j<bits.length-1;j++){
    	link += bits[j] + "/";
	}
	linkT = "http://cococo.net.au/backend/" + link + "thumb." + ext;
    thumb = new Element('img', {'src': linkT});
	Event.observe(thumblink, 'click', injectAdapter.bindAsEventListener(thumblink), false);
	thumblink.setAttribute('rel', artistDetails[0]+"&^&"+artistDetails[1]+"&^&"+artistDetails[2]+"&^&"+artistDetails[3]+"&^&"+artistDetails[4]+"&^&"+artistDetails[5]+"&^&"+artistDetails[6]+"&^&"+artistDetails[7]+"&^&"+artistDetails[8]);
    thumbd.appendChild(fname);
    thumbd.appendChild(lname);
    thumbd.appendChild(thumb);
	thumblink.appendChild(thumbd);
    parentd.appendChild(thumblink);
}


function injectShow(showDetails) {
	//artists format: fname+" "+lname
	//showDetails format: showName, id, summary, description, hasImage, imageExt, startDate, endDate, status, gallery, linkName, linkUrl, openingDate, artists
	galName = new Element('p', {'class': 'showThumbGallery'});
	galName.className = "showThumbGallery";		
	if (showDetails[8] == "0") { //Deal with current shows
		parentd = $("content");
		if(showDetails[9] == "0"){
			thumblink = $('mainSpaceLink');
			thumbd = $("mainSpace");
			galName.innerHTML = "main space";
		} else if(showDetails[9] == "1") {
			thumblink = $('backSpaceLink');			
			thumbd = $("backSpace");		
			galName.innerHTML = "backspace";
		} else if(showDetails[9] == "2") {
			thumblink = $('foyerSpaceLink');			
			thumbd = $("foyerSpace");		
			galName.innerHTML = "foyer space";
		}
		thumbd.style.display = "block";
		while(thumbd.childNodes[0]) {
			thumbd.removeChild(thumbd.childNodes[0]);
		}
	} else if (showDetails[8] == "1") { //Deal with future shows
		return;
	} else { //Deal with past shows
		parentd = $('pcontent');
		thumbd = new Element('div', {'class': 'galleryThumb'});
		if(showDetails[9] == "0"){
			thumbd.className = "galleryThumb mainSpaceThumb"
		} else if(showDetails[9] == "1") {
			thumbd.className = "galleryThumb backSpaceThumb"
		} else if(showDetails[9] == "2") {
			thumbd.className = "galleryThumb foyerSpaceThumb"
		}						
		//thumbd.className = "galleryThumb";	
		thumblink = new Element('a', {'href': '#'});				
		parentd.appendChild(thumblink);		
	}
	showName = new Element('p', {'class': 'showThumbName'});
	showName.className = "showThumbName";
    showName.innerHTML = showDetails[0];
    link = ("http://cococo.net.au/backend/shows/images/"+showDetails[1]+"/thumb." + showDetails[5]);   
    thumb = new Element('img', {'src': link});
	Event.observe(thumblink, 'click', injectAdaptorShow.bindAsEventListener(thumblink), false);
	thumblink.setAttribute('rel', showDetails[0]+"&^&"+showDetails[1]+"&^&"+showDetails[2]+"&^&"+showDetails[3]+"&^&"+showDetails[4]+"&^&"+showDetails[5]+"&^&"+showDetails[6]+"&^&"+showDetails[7]+"&^&"+showDetails[8]+"&^&"+showDetails[9]+"&^&"+showDetails[10]+"&^&"+showDetails[11]+"&^&"+showDetails[12]+"&^&"+showDetails[13]+"&^&"+showDetails[14]);
	thumbd.appendChild(galName);
    thumbd.appendChild(showName);
    thumbd.appendChild(thumb);
	thumblink.appendChild(thumbd);	

}

function injectAdaptorShow(){
	showDetails = this.rel.split("&^&");
	//attachArtists(showDetails,injectFeatureShow);
	injectFeatureShow(showDetails);
}

function injectAdapter(){
	artistDetails = this.rel.split("&^&");
	injectFeature(artistDetails);
}

function injectFeature(artistDetails) {
	for(i=0;i<artistDetails.length;i++){
		artistDetails[i] = artistDetails[i].trim();
	}
    $("featureImg").setStyle({'display': 'none'});
    link = (artistDetails[2].replace(/^..\//, ''));
    ext = link.split(".")[1];
	bits = link.split('/');
    link = ""
    for(j=0;j<bits.length-1;j++){
        link += bits[j] + "/";
    }
    //link = "../backend/" + link + "profile." + ext;
    link = "http://cococo.net.au/backend/" + link + "profile." + ext;
	fimg = $("featureImg");
	fimg.onload = function(){
        	fimg.setStyle({'display': 'block'});
	}
	if(fimg.src == link) {
		fimg.setStyle({'display': 'block'});
	} else {
	    fimg.src = link;
	}
	//if (fimg.complete) {
    //	fimg.setStyle({'display': 'block'});
	//}
        parentd = $("featureInfo");
        children = parentd.childElements();
        for(j=0;j<children.length;j++){
        	parentd.removeChild(children[j])
        }
		namep = new Element('p', {'class': 'featureTitleM'});
		namep.className = "featureTitleM";
        names = new Element('span', {'class': 'featureTitle'});
		names.className = "featureTitle";
        names.innerHTML = artistDetails[0] + " " + artistDetails[1];
		namep.appendChild(names);
    	parentd.appendChild(namep);
        if(artistDetails[3] != "") {
       		mediump = new Element('p', {'class': 'featureCreditsM'});
			mediump.className = "featureCreditsM";
       		mediums = new Element('span', {'class': 'featureCredits'});
			mediums.className = "featureCredits";
       		mediums.innerHTML = artistDetails[3];
			mediump.appendChild(mediums);
       		parentd.appendChild(mediump);
	}
	if(artistDetails[4] != "") {
       	desc = new Element('p', {'class': 'featureBlurb'});
		desc.className = "featureBlurb";
      	desc.innerHTML = nl2br(artistDetails[4]);
       	parentd.appendChild(desc);       		
	}

	
    linkp = new Element('p', {'class': 'featureCreditsM'});
	linkp.className = "featureCreditsM";
   	images = new Element('span', {'class': 'featureCredits'});
	images.className = "featureCredits";
    images.innerHTML = "<a href = \"http://cococo.net.au/" + fixImageLink(artistDetails[2]) + "\"> IMAGE </a>";
    if(artistDetails[6] != "") {
       	websites = new Element('span', {'class': 'featureCredits'});
		websites.className = "featureCredits";
       	websites.innerHTML = "<a href = \"" + toAbs(artistDetails[6]) + "\">WEBSITE</a>";
		linkp.appendChild(websites);
	}
    if(artistDetails[8] != "") {
       	cvs = new Element('span', {'class': 'featureCredits'});
		cvs.className = "featureCredits";
       	cvs.innerHTML = "<a href = \"" + fixImageLink(artistDetails[8]) + "\">CV</a>";
		linkp.appendChild(cvs);
	}
	linkp.appendChild(images);
    parentd.appendChild(linkp);
}

function injectFeatureShow(showDetails) {
	//artists format: fname+" "+lname
	//showDetails format: showName, id, summary, description, hasImage, imageExt, startDate, endDate, status, gallery, linkName, linkUrl, openingDate, artists
	for(i=0;i<showDetails.length;i++){
		showDetails[i] = showDetails[i].trim();
	}
	artists = showDetails[14].split("?^?");
    $("featureImg").setStyle({'display': 'none'});
    link = (showDetails[1].replace(/^..\//, ''));
    ext = link.split(".")[1];
	bits = link.split('/');
    link = ("http://cococo.net.au/backend/shows/images/"+showDetails[1]+"/feature." + showDetails[5]);
	fimg = $("featureImg");
	fimg.onload = function(){
        	fimg.setStyle({'display': 'block'});
	}
	if(fimg.src == link) {
		fimg.setStyle({'display': 'block'});
	} else {
	    fimg.src = link;
	}
    parentd = $("featureInfo");
    children = parentd.childElements();
    for(j=0;j<children.length;j++){
    	parentd.removeChild(children[j]);
    }
	namep = new Element('p', {'class': 'featureTitleM'});
	namep.className = "featureTitleM";
    names = new Element('span', {'class': 'featureTitle'});
	names.className = "featureTitle";
    names.innerHTML = showDetails[0];
	namep.appendChild(names);
    parentd.appendChild(namep);
    if(showDetails[2] != "") {
    	summaryp = new Element('p', {'class': 'featureCreditsM'});
		summaryp.className = "featureCreditsM";
    	summarys = new Element('span', {'class': 'featureCredits'});
		summarys.className = "featureCredits";
    	summarys.innerHTML = showDetails[2];
		summaryp.appendChild(summarys);
    	parentd.appendChild(summaryp);
	}
	if (artists.length - 1 > 0) {
	
	    artistsp = new Element('p', {'class': 'featureCreditsM'});
		artistsp.className = "featureCreditsM";
		for (j = 0; j < artists.length - 1; j++) {
			artistss = new Element('span', {
				'class': 'featureCredits'
			});
			artistss.className = "featureCredits";
			/*if (artists[j].split("%^%")[2] == 0) {
			artistss.innerHTML = artists[j].split("%^%")[1];
			} else {
				artistss.innerHTML = "<a href='#' onclick='openArtist(\"" + artists[j].split("%^%")[0] + "\")'>" + artists[j].split("%^%")[1] + "</a>";
			}*/
			artistss.innerHTML = "<a href='#' onclick='openArtist(\"" + artists[j].split("%^%")[0] + "\")'>" + artists[j].split("%^%")[1] + "</a>";

			artistsp.appendChild(artistss);			
		}
    	parentd.appendChild(artistsp);			
	
	/*
		artistsp = new Element('p', {
			'class': 'featureCreditsM'
		});
		artistsp.className = "featureCreditsM";
		artistss = new Element('span', {
			'class': 'featureCredits'
		});
		artistss.className = "featureCredits";
		artistsStr = "By ";
		if (artists.length - 1 > 3) {
			artistsStr = "Multiple Artists";
		} else {
			for (j = 0; j < artists.length - 1; j++) {
				artistsStr += artists[j];
				if (j < artists.length - 2) {
					artistsStr += ", ";
				}
			}
			if (artistsStr.length > 40) {
				artistsStr = "Multiple Artists";
			}
		}
    	artistss.innerHTML = artistsStr;
		artistsp.appendChild(artistss);
    	parentd.appendChild(artistsp);
    */	
    }
	if(showDetails[3] != "") {
       	desc = new Element('p', {'class': 'featureBlurb'});
		desc.className = "featureBlurb";
      	desc.innerHTML = nl2br(showDetails[3]);
       	parentd.appendChild(desc);       		
	}
	durationp = new Element('p', {'class': 'feauteCreditsM'});
	durationp.className = "featureCreditsM";
	durations = new Element('span', {'class': 'featureCredits'});
	durations.className = "featureCredits";
	durationp.appendChild(durations);
	durations.innerHTML = "Runs: " + showDetails[6] + " - " + showDetails[7];
	parentd.appendChild(durationp);
	openingp = new Element('p', {'class': 'feauteCreditsM'});
	openingp.className = "featureCreditsM";
	openings = new Element('span', {'class': 'featureCredits'});
	openings.className = "featureCredits";
	openingp.appendChild(openings);
	hours = showDetails[12].substring(0,2);
	date = showDetails[12].substring(3);
	if (hours > 12){
		hours = (hours-12) + "PM";
	} else {
		hours = hours + "AM";
	}
	openings.innerHTML = "Opening event: " + hours + ", " + date;
	parentd.appendChild(openingp);	
    if(showDetails[10] != "" && showDetails[11] != "") {
	    linkp = new Element('p', {'class': 'featureCreditsM'});
		linkp.className = "featureCreditsM";
       	websites = new Element('span', {'class': 'featureCredits'});
		websites.className = "featureCredits";
       	websites.innerHTML = "<a href = \"" + toAbs(showDetails[11]) + "\">"+showDetails[10]+"</a>";
		linkp.appendChild(websites);
		parentd.appendChild(linkp);

	}
	
}


//The nl2br and trim functions aren't mine, but im not sure who the authors are. They were both GPL or something like it
function nl2br (str) {
	var breakTag = '';
	breakTag = '<br />'
	return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag + '\n');
}

String.prototype.trim = function () {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

function toAbs(link) {
  var lparts = link.split('/');
  if (/http:|https:|ftp:/.test(lparts[0])) {
      // already abs, return
      return link;
  }
  return "http://"  + lparts.join('/');
}
function dateToArray(date) {
	year = date.substr(4,4);
	month = date.substr(2,2);
	day = date.substr(0,2);
	return new Array(day,month,year);
}

function cmpDates(a, b) {
	//less than 0, a should be before b. more than 0, b should be before a
    showDetailsA = a.split('&^&');
    showDetailsB = b.split('&^&');
	dateA = showDetailsA[6].replace(/-/g, "");
	dateB = showDetailsB[6].replace(/-/g, "");
	dateArrayA = dateToArray(dateA);
	dateArrayB = dateToArray(dateB);;
	if (dateArrayA[2] < dateArrayB[2]) {
		return -1;
	}
	if (dateArrayB[2] < dateArrayA[2]) {
		return 1;
	}
	if (dateArrayA[1] < dateArrayB[1]) {
		return -1;
	}
	if (dateArrayB[1] < dateArrayA[1]) {
		return 1;
	}
	if (dateArrayA[0] < dateArrayB[0]) {
		return -1;
	}
	if (dateArrayB[0] < dateArrayA[0]) {
		return 1;
	}
	return 0;
}

function cmpNames(a, b) {
	//less than 0, a should be before b. more than 0, b should be before a
    showDetailsA = a.split('&^&');
    showDetailsB = b.split('&^&');
	showNameA = showDetailsA[0];
	showNameB = showDetailsB[0];	
	if (showNameA < showNameB) {
		return -1;
	} 
	return 1;
}

function fixImageLink(link) {
	link = (link.replace(/^..\//, ''));
        link = "../backend/" + link;
	return link;
}
var featureSlid = true;
function slideFeature(){
	if (featureSlid) {
                $('featureInfo').morph('margin-left: 320px;', {duration:0.2, transition: Effect.Transitions.sinoidal});
                featureSlid = false;
	} else {        
		$('featureInfo').morph('margin-left: 0px;', {duration:0.2, transition: Effect.Transitions.sinoidal});
	        featureSlid = true;
	}
}
