//GET SIZE OF PAGE
var imageViewerHeight;
function getSizes(size) {
	var pageWidth = $(window).width();
	var pageHeight = $(window).height();
	
	//----------------------------SIZE AND CSS-----------------------------\\
	
	columnSize = pageWidth - 986;
	columnSize = columnSize / 2;
	columnSize = columnSize + "px";
	
	headerInitHeight = '300px';
	headerAniHeight = '100px';
	if(pageHeight > 400 && pageHeight <= 700) {
		contentInitHeight = pageHeight-300;
		contentAniHeight = pageHeight-100;
	} else if(pageHeight > 700) {
		contentInitHeight = 400;
		contentAniHeight = 600;
	} else {
		contentInitHeight = 250;
		contentAniHeight = 450;
	}
	mainContentInitHeight = contentInitHeight-20;
	mainContentAniHeight = contentAniHeight-20;
	imageViewerHeight = mainContentAniHeight;
	if(size == "pgHT") {
		return pageHeight;
	}
	if(size == "coIn") {
		return contentInitHeight;
	}
	if(size == "coAn") {
		return contentAniHeight;
	}
	if(size == "mcIn") {
		return mainContentInitHeight;
	}
	if(size == "mcAn") {
		return mainContentAniHeight;
	}
}

var page = 0;
var botPos = 0;
var topPos = 0;
var numPages = 0;

$(document).ready(function() {
	
	getSizes();
	
	pageHeight = getSizes('pgHT');
	contentInitHeight = getSizes('coIn');
	contentAniHeight = getSizes('coAn');
	mainContentInitHeight = getSizes('mcIn');
	mainContentAniHeight = getSizes('mcAn');
	
	columnHeight = pageHeight + "px";
	
	contentInitHeight=contentInitHeight+"px";
	contentAniHeight=contentAniHeight+"px";
	mainContentInitHeight=mainContentInitHeight+"px";
	mainContentAniHeight=mainContentAniHeight+"px";
	
	
	$("#content").css({height: contentInitHeight});
	$("#mainColumn").css({height: mainContentInitHeight});
	$("#statementColumn").css({height: contentAniHeight});
	$("#workColumn").css({height: contentAniHeight});
	$("#leftPageCol").css({width: columnSize, height: columnHeight});
	$("#rightPageCol").css({width: columnSize, height: columnHeight});
	
	
	//Set variables--------------------------------------
	var alreadyAni = 0;
	var showTwitter = 0;
	var showHome = 0;
	var showWork = 0;
	var showStatement = 0;
	var rcAni = 0;
	var prevLink = "";
	var twitCode = "";
	var column;
	var closeHeader = 0;
	var loadString = "<div style='width:702px; height: " + contentAniHeight + "px; margin:auto;'><table width='702px' height='" + contentAniHeight + "'><tr><td style='vertical-align:middle; text-align:center;'><img src='/Assets/Images/loading.gif' /></td></tr></table></div>";
	
	//LINKS-----------------------------------------------------
	$('#navHome').click(function() { showPage('ldHome'); });
	$('#navNews').click(function() { showPage('ldNews'); });
	$('#navStatement').click(function() { showPage('ldStatement');});
	$('#navWork').click(function() { showPage('ldWork');});
	$('#navContact').click(function() { showPage('ldContact');});
	$('#nextSketch').click(function() { nextImage();});
	
	$(".thumbWork").click(function() {
		workdiv = this.id;
		workID = workdiv.substring(3);
		showPic(workID);
	});
	
	//ANIMATIONS-------------------------------------------------
	//Show Page and ANIMATION UP
	function showPage(link) {
		if(link != "") {
			if(link != prevLink) {
				//IF CLICK ON TITLE, SHOW HOME PAGE
				if(link == "ldHome") {
					$("#fbShareMe").css({display: "none"});
					aniHeaderDown(link);
				} else {
					//IF ALREADY ANIMATED SHOW CONTENT
					if(alreadyAni != 0) {
						displayRC(link);
					} else {
						//IF ANIMATION HAS NOT HAPPENED, SHOW ANIMATION
						document.getElementById("mainColumnContent").innerHTML = loadString;
						$("#mainColumnContent").animate({width: '702px'},1500);
						$("#header").animate({height: headerAniHeight},1500); 
						//AFTER HEIGHT CHANGES, CHANGE RC IF NEEDED
						$("#content").animate({height: contentAniHeight},1500, function() {displayRC(link);}); 
						$("#mainColumn").animate({height: mainContentAniHeight},1500);
					
						//CALL DISPLAY MAIN
						alreadyAni = 1;
					}
				}
			}
		}
	}
	
	//Close Current Column
	function closeCol(link) {
		document.getElementById("mainColumnContent").innerHTML = loadString;
		if(link != prevLink) {
			if(prevLink == "ldWork") {
				$("#workColumn").animate({width: "0px"},1000, function() {aniRc(link, 'workColumn');});
			}
			if(prevLink == "ldNews") {
				$("#newsColumn").animate({height: "0px"},1000, function() {aniRc(link, 'newsColumn');});
			}
			if(prevLink == "ldContact") {
				$("#contactColumn").animate({height: "0px"},1000, function() {aniRc(link, 'contactColumn');});
			}
			if(prevLink == "ldStatement") {
				$("#facebookBox").css({display: "none"});
				$("#statementColumn").animate({width: "0px"},1000, function() {aniRc(link, 'statementColumn');});
			}
			if(prevLink == "") {
				aniRc(link, '');
			}
		}
	}
	
	//ANIMATE RIGHT COLUMN
	function aniRc(link, prev) {
		if(closeHeader == 0) {
				if(prev != "") {
					$("#" + prev).css({display: "none"});
				}
				if(link == "ldNews") {
					$("#newsColumn").css({display: "block"});
					$("#newsColumn").animate({height: contentAniHeight},1500, function() {checkTwitter(link);});
				} else if(link == "ldWork") {
					$("#workColumn").css({display: "block"});
					$("#workColumn").animate({width: "262px"},1500, function() {checkTwitter(link);});
				} else if(link == "ldContact") {
					$("#contactColumn").css({display: "block"});
					$("#contactColumn").animate({height: contentAniHeight},1500, function() {checkTwitter(link);});			
				} else if(link == "ldStatement") {
					$("#statementColumn").css({display: "block"});
					$("#statementColumn").animate({width: "262px"},1500, function() {showFace(link);});
				}
				prevLink = link;
				if(link == "ldWork") {
					//Need to get most recent here
					displayFirst();
				} else {
					displayMain(link);
				}
		} else {
			closeHeader = 0;
			headerAnimate(link);
		}
	}
	
	//SHOW FACEBOOK
	function showFace(link) {
		$("#facebookBox").css({display: "block"});
		checkTwitter(link);
	}
	
	//ANIMATION DOWN
	function aniHeaderDown(link) {
		closeHeader = 1;
		closeCol(link);
	}
	
	function headerAnimate(link) {
		if(link == "ldHome") {
			rcol = "";
			displayMain(link, rcol);
		}
		if(alreadyAni == 1) {
			$("#header").animate({height: headerInitHeight},1500); 
			$("#content").animate({height: contentInitHeight},1500); 
			$("#mainColumn").animate({height: mainContentInitHeight},1500);
			$("#mainColumnContent").animate({width: '966px'},1500);
			alreadyAni = 0;
		}
	}
	
	//SHOW TWITTER
	function updateTwitter(link) {
		$("#fbShareMe").css({display: "none"});
		if(twitCode == "") {
			twitCode = document.getElementById("ldTwitter").innerHTML;
			document.getElementById("ldTwitter").innerHTML = "";
		}
		document.getElementById("newsColumn").innerHTML = twitCode;
		$("#twtr-widget-1").css({display: "none"});
		closeCol(link);
	}
	
	function checkTwitter(link) {
		if(link == "ldNews") {
			$("#newsColumn").css({diplay: "block"});
			$("#twtr-widget-1").css({display: "block"});
		}
	}
	
	//--------------------------------------------------
	//UPDATE CONTENT------------------------------------
	var rightLink;

	function displayMain(mainLink) {
		var url = mainLink + ".php";
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				onComplete: updateMain
			});
	}

	function updateMain(originalRequest) {
		//$('#mainColumn').innerHTML = originalRequest.responseText;
		var res = originalRequest.responseText;
		document.getElementById("mainColumnContent").innerHTML = res;
		initLytebox();
	}

	function displayRC(mainLink) {
		if(mainLink != "ldNews") {
			var url = mainLink + "RC.php";
			rightLink = mainLink;
			var myAjax = new Ajax.Request(
				url, 
				{
					method: 'get', 
					onComplete: updateRC
				});
		} else {
			updateTwitter(mainLink);
		}
		column = mainLink;
	}

	function updateRC(originalRequest) {
		//$('#mainColumn').innerHTML = originalRequest.responseText;
		$("#fbShareMe").css({display: "none"});
		var res = originalRequest.responseText;
		if(column == "ldNews") {
			document.getElementById("newsColumn").innerHTML = res;
		}
		if(column == "ldWork") {
			document.getElementById("workColumn").innerHTML = res;
			tooltip();
			//showPic('1', 'Assets/Art/Works/AlltheWrongPlaces.jpg');
		}
		if(column == "ldContact") {
			document.getElementById("contactColumn").innerHTML = res;
		}
		if(column == "ldStatement") {
			document.getElementById("statementColumn").innerHTML = res;
			FB.init("b1ee1d23497d70b4f62988a8979b22b2");
		}
		initLytebox();
		closeCol(rightLink);
	}
});

//SCROLL THUMBNAILS
function getPage(numThumbs) {
	numPages = numThumbs / 6;
	numPages = numPages + 0.4;
	numPages = Math.round(numPages);
	page = 1;
}

function scrollUp(numThumbs) {
	//alert(numThumbs);
	if(page == 0) {
		getPage(numThumbs);
	}
	if(page > 0) {
		if(page != numPages) {
			//alert(numPages);
			//alert(page);
			topPos = page * 300;
			topPos = topPos * -1;
			topPX = topPos + 'px';
			//alert(topPX);
			if(numPages > 1) {
				//alert("Greater");
				if(page < numPages) {
					//alert("less");
					$("#thumbList").animate({top: topPX},1500);
					page = page + 1;
					//alert(page);
					//alert(numPages);
					//numOneLess = numPages - 1;
					if(page == numPages) {
						document.getElementById("arrowUP").innerHTML = '<img src="Assets/Images/t_down_none.jpg" />';
						document.getElementById("arrowDOWN").innerHTML = '<img src="Assets/Images/t_up.jpg" />';
					} else {
						document.getElementById("arrowDOWN").innerHTML = '<img src="Assets/Images/t_down.jpg" />';
						document.getElementById("arrowUP").innerHTML = '<img src="Assets/Images/t_up.jpg" />';
					}
					//alert(page);
				}
			}
		}
	}
}
	
function scrollDown(numThumbs) {
	//alert(numThumbs);
	if(numThumbs != 0) {
		if(page > 1) {
			numPages = numThumbs / 6;
			numPages = numPages + 0.4;
			numPages = Math.round(numPages);
			//alert(numPages);
			//alert(page);
			topPos = topPos + 300;
			if(topPos == 0) {
				botPX = '0px';
			} else {
				botPX = topPos + 'px'
			}
			//alert(botPX);
			if(numPages > 1) {
				if(page > 0) {
					$("#thumbList").animate({top: botPX},1500);
					page = page - 1;
					//alert(page);
					//alert(numPages);
					if(page == 1) {
						//alert("test");
						document.getElementById("arrowDOWN").innerHTML = '<img src="Assets/Images/t_up_none.jpg" />';
						document.getElementById("arrowUP").innerHTML = '<img src="Assets/Images/t_down.jpg" />';
					} else {
						document.getElementById("arrowDOWN").innerHTML = '<img src="Assets/Images/t_down.jpg" />';
						document.getElementById("arrowUP").innerHTML = '<img src="Assets/Images/t_up.jpg" />';
					}
				}
			}
		}
	}
}

//SORT THUMBNAILS IN RIGHT WORK COLUMN
function updateArrows(type, year) {
	page = 0;
	$.ajax({
		type: "POST",
		url: "updateArrows.php",
		data: "type=" + type + "&year=" + year,
		success: function(msg){
                document.getElementById("arrows").innerHTML = (msg);
				tooltip();
            }
	});
}

//FIRST IMAGE FOR WORKS
	function displayFirst() {
		var url = "getFirst.php";
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				onComplete: updateFirst
			});
	}

	function updateFirst(originalRequest) {
		var res = originalRequest.responseText;
		displaySecond(res);
	}
	
	function displaySecond(firstID) {
		$.ajax({
		type: "POST",
		url: "getFirst.php",
		data: "id=y",
		success: function(msg){
                    showPic(firstID, msg);
				}
		});
}
	

//SHOW PICS IN GALLERY
function showPic(workID, pu) {
	if(workID != "") {
		$.ajax({
		type: "POST",
		url: "updateWork.php",
		data: "workID=" + workID + "&galleryHeight=" + imageViewerHeight,
		success: function(msg){
                    document.getElementById("mainColumnContent").innerHTML = (msg);
					initLytebox();
					shareFBButton(pu);
					initAddThis();
                 }
		});
	}
}
$(document).ready(function() {
	showPic('');
});

//Show FB Share Button
function shareFBButton(pu) {
	document.getElementById("fbShareMe").innerHTML = "<a style='cursor:pointer;' onClick=sharePic('" + pu + "');><img height='18px' src='Assets/Images/fblogo.png'/></a>";
	$("#fbShareMe").css({display: "block"});
}

//START ADD THIS
function initAddThis(){
$("#mainColumnContent .addthis_button").each(function(){
$(this).attr("onclick", "return addthis_open(this, '', '"+$(this).attr('addthis:url')+"', '"+$(this).attr('addthis:title')+"');");
});
}

//SUB NAV FOR WORKS NAV
var prevSub = '';
function showSubNav(subDiv, cD1, cD2) {
	if(subDiv == "subWork") {
		sortThumbs('painting', 'all');
	} else if(subDiv == "subSketches") {
		sortThumbs('sketch', 'all');
	} else if(subDiv == "subPhotos") {
		sortThumbs('photo', 'all');
	}
	if(prevSub != '') {
		$("#" + prevSub).animate({height: '0px'},100, function() {aniSubNav(subDiv, cD1, cD2);});
	} else {
		aniSubNav(subDiv, cD1, cD2);
	}
}

function aniSubNav(subDiv, cD1, cD2) {
	$("#" + cD1).css({display: 'none'});
	$("#" + cD2).css({display: 'none'});
	$("#" + subDiv).css({display: "block"});
	$("#" + subDiv).animate({height: '20px'},1500);
	prevSub = subDiv;
}
$(document).ready(function() {
	showSubNav('');
});


//SORT THUMBNAILS IN RIGHT WORK COLUMN
function sortThumbs(type, year) {
	var firstLetter = type.substr(0, 1);
    var newTitle = firstLetter.toUpperCase() + type.substr(1);
	if(newTitle == "Sketch") {
		newTitle = newTitle + "es";
	} else {
		newTitle = newTitle + "s";
	}
	if(year == "all") {
	document.getElementById("thumbDesc").innerHTML = "All " + newTitle;
	} else {
	document.getElementById("thumbDesc").innerHTML = newTitle + " from " + year;
	}
	$.ajax({
		type: "POST",
		url: "updateWorkRC.php",
		data: "type=" + type + "&year=" + year,
		success: function(msg){
                document.getElementById("thumbContainer").innerHTML = (msg);
				updateArrows(type, year);
            }
	});
}

//Open comments form
function openComForm() {
	$('#comPost').animate({height: 250},1500);
	$('#openComForm').css({display: "none"});
	$('#comError').css({display: "none"});
	$('#closeComForm').css({display: "block"});
}

function closeComForm() {
	$('#comPost').animate({height: 15},1500);
	$('#closeComForm').css({display: "none"});
	$('#openComForm').css({display: "block"});
}

//Comments form post
function subForm() {
	var name = $("#name").val();
	var email = $("#email").val();
	var text = $("#text").val();
	
	//name = name.replace("'", "XquotX");
	//email = email.replace("'", "XquotX");
	//text = text.replace("'", "XquotX");
	
	var dataString = 'name='+ name + '&email=' + email + '&text=' + text;

	if(name=="" || text=='') {
		$('#comError').css({display: "block"});
	} else {
		$.ajax({
		type: "POST",
		url: "submitComment.php",
		data: dataString,
		success: function(msg){
			if(msg != "") {
				$('#comPost').css({display: "none"});
				$('#comSuccess').fadeIn(200).show();
				updateCommentsDisplay();
			}
			}
		});
	}
	return false;
}

function updateCommentsDisplay() {
	var comFrame = document.getElementById('comFrame');
	comFrame.contentWindow.location.reload(true);
}