var slid = false;
var curShow = true;
var jump = true;
var ieOffset = 0;
var noNextShow = false;
var curShowDetails = null;
var nextShowDetails = null;

function setupJump() {
	curShow = true;
	if(curShow) {	
		new Effect.Opacity("nextJBInfo", {from: 1, to: 0, duration:0});	
	}
	setupPositions();
	setInfo();
	if (Prototype.Browser.IE) {
		ieOffset = 90;
	}
}

function setInfo() {
	slid = false;
	
	new Ajax.Request("scripts/getCurrentShow.php", {
		method: 'post',
		onComplete: processShowResponse,
		onFailure: function(){ alert('Something went wrong with the database. Refresh the page or try again in a little while')}
	});
	
	/*AjaxHandler.sendRequest("currentShow/detailsShort.html", function(results) {
		lines = results.split('\n');
		$("jbInfo1").innerHTML = lines[0] + "<br />" + lines[1];
		$("jbInfo2").innerHTML = lines[2];
		
	});
	AjaxHandler.sendRequest("nextShow/detailsShort.html", function(results) {
		lines = results.split('\n');
		$("nextJBInfo1").innerHTML = lines[0] + "<br />" + lines[1];
		$("nextJBInfo2").innerHTML = lines[2];
	});*/
	/*AjaxHandler.sendRequest("currentShow/detailsFull.html", function(results) {
		lines = results.split('\n');
		//var text = 	"<p class='jumpShowName'>" +lines[0] +"<br />" +
		//		lines[1]+"</p>" +
		//		"<p class='jumpArtistName'>"+lines[2]+"</p>" +
		//		"<p class='jumpShowOpening'>"+lines[3]+"</p>" +
		//		"<p class='jumpShowBlurb'>" +lines[4]+
		//		"</p>"
				var text = 	"<p class='featureTitleM'> <span class='featureTitle'>" +lines[0] + "</span></p>" +
						"<p class='featureCreditsM'><span class='featureCredits'>"+lines[1]+"</span></p>" +
						"<p class='featureCreditsM'><span class='featureCredits'>"+lines[2]+"</span></p>" +
						"<p class='featureBlurb'>"+lines[3]+"</p>" +
						"<p class='featureCreditsM'><span class='featureCredits'>" +lines[4]+
						"</span></p>"
		$('jumpInfo').innerHTML = text;
	});*/
}

function processShowResponse(transport) {
	//showName, id, summary, description, hasImage, imageExt, startDate, endDate, status, gallery, linkName, linkUrl, openingDate, showType, artists
	transport.responseText = transport.responseText.replace(/'/, "");
	entries = transport.responseText.split('**&**');
	curShowDetails = entries[0].split("&^&");
	curArtists = curShowDetails[14].split("?^?");
	link = "http://cococo.net.au/backend/shows/images/" + curShowDetails[1] + "/show." + curShowDetails[5];
	$('currentShow').src = link;
	if(curShowDetails[13] == "1") {
		curArtistStr = "Special Event";
	} else {	
		if (curArtists.length-1 < 1) {
			curArtistStr = "Artist Unknown";
		} else if (curArtists.length-1 == 1) {
			if (curArtists[0].split("%^%")[2] == "0") {
				curArtistStr = "<a href='#' onclick='openArtist("+curArtists[0].split("%^%")[0]+")'>" + curArtists[0].split("%^%")[1] + "</a>";
			} else { 
				curArtistStr = "<a href='#' onclick='openArtist(\"" + curArtists[0].split("%^%")[0] + "\")'>" + curArtists[0].split("%^%")[1] +"</a>";
			}
		} else if (curArtists.length-1 == 2) {
			curArtistStr = "Joint Show";
		} else {
			curArtistStr = "Group Show";
		}
	}
	
	if(entries[1] != "") {
	    nextShowDetails = entries[1].split("&^&");
		nextArtists = nextShowDetails[14].split("?^?");	    
		if(nextShowDetails[13] == "1") {
			nextArtistStr = "Special Event";
		} else {			
			if (nextArtists.length-1 < 1) {
				nextArtistStr = "Artist Unknown";
			} else if (nextArtists.length-1 == 1) {
				if (nextArtists[0].split("%^%")[2] == "0") {
					nextArtistStr = nextArtists[0].split("%^%")[1];
				} else { 
					nextArtistStr = "<a href='#' onclick='openArtist(\"" + nextArtists[0].split("%^%")[0] + "\")'>" + nextArtists[0].split("%^%")[1] +"</a>";
				}
			} else if (nextArtists.length-1 == 2) {
				nextArtistStr = "Joint Show";
			} else {
				nextArtistStr = "Group Show";
			}
		}		
		$('nextJBInfo1').innerHTML = "<a href='#' onclick='changePage(\"shows/index.html\", loadShows);'>" + nextShowDetails[0] + "<br />"  + nextArtistStr + "</a>";
		$('nextJBInfo2').innerHTML = nextShowDetails[6] + " / " + nextShowDetails[7];			
		link = "http://cococo.net.au/backend/shows/images/" + nextShowDetails[1] + "/show." + nextShowDetails[5];
		$('nextShow').src = link;		
	} else {
		noNextShow = true;
		$('curShowButton').hide();
		$('nextShowButton').hide();		
	}
	
	$('jbInfo1').innerHTML = "<a href='#' onclick='changePage(\"shows/index.html\", loadShows);'>" + curShowDetails[0] + "<br /> " + curArtistStr + "</a>";
	$('jbInfo2').innerHTML = curShowDetails[6] + " / " + curShowDetails[7];	

	openingStr = "";
	openingTime = curShowDetails[12].substring(0,2);
	if(openingTime > 12) {
		openingStr = (openingTime-12) + "PM, ";
	} else {
		openingStr = openingTime + "AM, ";
	}
	openingStr += curShowDetails[12].substring(3);
	text = 	"<p class='featureTitleM'> <span class='featureTitle'>" +curShowDetails[0] + "</span></p>" +
			"<p class='featureCreditsM'><span class='featureCredits'>"+curArtistStr+"</span></p>" +
			"<p class='featureCreditsM'><span class='featureCredits'> Closes "+ curShowDetails[7]+"</span></p>" +
			"<p class='featureBlurb'>"+curShowDetails[3]+"</p>" +
			"<p class='featureCreditsM'><span class='featureCredits'> Opening event: "+ openingStr +" </span></p>"
	$('jumpInfo').innerHTML = text;
	//alert(entries[0]);
	//alert(entries[1]);
}

function jumpSlide() {
	if (slid) {
		$('jumpInfo').morph('margin-right: -320px;', {duration:0.2, transition: Effect.Transitions.sinoidal});
		slid = false;
	} else {
		$('jumpInfo').morph('margin-right: 0px;', {duration:0.2, transition: Effect.Transitions.sinoidal});
		slid = true;
	}
}

function jumpSlideOut() {
	if (slid) {
		$('jumpInfo').morph('margin-right: -320px;', {duration:0.2, transition: Effect.Transitions.sinoidal});
		slid = false;
	}
}

function setupPositions() {
	if(curShow) {
		placement = 400;
		selected = "curJBInfo";
		notSelected = "nextJBInfo";
		ieScalar = 1;
	} else {
		placement = -400;
		selected = "nextJBInfo";
		notSelected = "curJBInfo";
		ieScalar = 0.4;
	}
	new Effect.Opacity(notSelected, {from: 1, to: 0, duration:0});	
	new Effect.Move(notSelected, {x:placement+ieScalar*ieOffset, mode: 'absolute', duration:0});	
	new Effect.Move(selected, {x:0+ieScalar*ieOffset, mode: 'absolute', duration:0});	
}

function checkPositions() {
	if(curShow) {
		placement = 300;
		selected = "curJBInfo";
		notSelected = "nextJBInfo";
	} else {
		placement = -300;
		selected = "nextJBInfo";
		notSelected = "curJBInfo";
	}
	new Effect.Move(notSelected, {x:placement, mode: 'absolute', duration:0});	
	new Effect.Move(selected, {x:0, mode: 'absolute', duration:0});	
}


function slideShows(tag) {
	if (curShow) {
		if(noNextShow) {
			return;
		}
		if(tag == "cur") {
			return;
		}
		details = nextShowDetails;
		//infoPath = "nextShow/detailsFull.html";
		amount = "-780px";
		msign = -1;
		selectedShow = "currentShow";
		unselectedShow = "nextShow"
		selectedInfo = "curJBInfo";
		unselectedInfo = "nextJBInfo";
		ieScalar = 0.4;
	} else {
		if(tag == "next") {
			return;
		}
		details = curShowDetails;
		//infoPath = "currentShow/detailsFull.html";
		msign = 1;
		amount = "780px";
		selectedShow = "nextShow";
		unselectedShow = "currentShow"
		selectedInfo = "nextJBInfo";
		unselectedInfo = "curJBInfo";
		ieScalar = 1;
	}
	jumpSlideOut();
	new Effect.Parallel([
		new Effect.Morph(selectedShow, {style: 'margin-left: ' + amount, sync: true}),
		new Effect.Morph(unselectedShow, {style: 'margin-left: 0px', sync: true}),
		new Effect.Move(selectedInfo, {x:msign*400 + msign*ieScalar*ieOffset, mode: 'absolute', sync: true}),
		new Effect.Move(unselectedInfo, {x:0 + ieScalar*ieOffset, mode: 'absolute', sync: true}),
		new Effect.Opacity(selectedInfo, {from: 1, to: 0, sync: true}),
		new Effect.Opacity(unselectedInfo, {from: 0, to: 1, sync: true})
	], { 
		duration: 0.8, 
		transition: Effect.Transitions.sinoidal,
		//beforeStart: function() {
		//	setupPositions();
		//},
		afterFinish: function() {
			/*AjaxHandler.sendRequest(infoPath, function(results) {
				lines = results.split('\n');
				var text = 	"<p class='featureTitleM'> <span class='featureTitle'>" +lines[0] + "</span></p>" +
						"<p class='featureCreditsM'><span class='featureCredits'>"+lines[1]+"</span></p>" +
						"<p class='featureCreditsM'><span class='featureCredits'>"+lines[2]+"</span></p>" +
						"<p class='featureBlurb'>"+lines[3]+"</p>" +
						"<p class='featureCreditsM'><span class='featureCredits'>" +lines[4]+
						"</span></p>"
				$('jumpInfo').innerHTML = text;
			});*/
			openingStr = "";
			openingTime = details[12].substring(0,2);
			if(openingTime > 12) {
				openingStr = (openingTime-12) + "PM, ";
			} else {
				openingStr = openingTime + "AM, ";
			}
			openingStr += details[12].substring(3);
			if(details[13] == "1") {
				artistStr = "Special Event";
			} else {			
				artists = details[14].split("?^?");
				if (artists.length-1 < 1) {
					artistStr = "Artist Unknown";
				} else if (artists.length-1 == 1) {
					artistStr = "By " + artists[0].split("%^%")[1];
				} else if (artists.length-1 == 2) {
					artistStr = "Joint Show";
				} else {
					artistStr = "Group Show";
				}
			}

			text = 	"<p class='featureTitleM'> <span class='featureTitle'>" +details[0] + "</span></p>" +
					"<p class='featureCreditsM'><span class='featureCredits'>"+artistStr+"</span></p>" +
					"<p class='featureCreditsM'><span class='featureCredits'> Closes "+ details[7]+"</span></p>" +
					"<p class='featureBlurb'>"+details[3]+"</p>" +
					"<p class='featureCreditsM'><span class='featureCredits'> Opening event: "+ openingStr +" </span></p>"
			$('jumpInfo').innerHTML = text;
		}
	});	
	if (curShow) {
		curShow = false;
	} else {
		curShow = true;
	}
}

function slideJumpOut(replacement) { //not jumpSlideOut()
	if(jump) {
		jump = false;
		new Effect.Parallel([
			new Effect.Morph("currentShowDiv", {style: 'height: 100px; margin-top: 350px; opacity: 0.0', sync: true}),
			new Effect.Morph("moreDiv", {style: 'height: 0px; margin-top: 90px; opacity: 0.0', sync: true})
		], {
			duration: 0.1,
			transition: Effect.Transitions.sinoidal,
			afterFinish: function() {
				AjaxHandler.sendRequest(replacement, function(results) {
					$('main').innerHTML = results;
				});
			}
		});
	}
}

