var projectImages;
var projectLabels;
var currentImage = 0;
var bWorking = false;


//WEB SERVICE DATA COLLECTION

function getItemCount( sInputString )
{
	return sInputString.split("|||||").length;
}

function getItem ( nIndex, sInputString )
{
	return sInputString.split("|||||")[nIndex];
}

function getItemVariable ( nIndex, sVarName, sInputString )
{
	myItem = getItem(nIndex, sInputString);
	index = myItem.indexOf("[[" + sVarName + ":");
	if ( index == -1 )
		return "";
	
	return myItem.substring(index).split("]]")[0].replace("[[" + sVarName + ":", "");
}

//END OF WEB SERVICE DATA COLLECTION


//PAGING STUFF
var projectImages;
var currentImage = 0;

function getCurrentPage()
{
	var sPath = window.location.pathname;
	//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	if ( sPage == "" )
		sPage = "default.aspx";
	return sPage;
}

function initProjectImages( imageList )
{
	projectImages = imageList;
	currentImage = 0;
	hideLeftNav();
	
	if ( !hasNextImage() )
	{
		hideRightNav();
	} else {
		showRightNav();
	}
	displayImage();
	
	if ( $.browser.msie && parseInt($.browser.version) == 6 )
	{
		setTimeout("displayImage()", 1000);
	}
	
	updateNumberPanel();
}

function initProjectLabels(imageList) {
    projectLabels = imageList;
//    currentImage = 0;
//    hideLeftNav();

//    if (!hasNextImage()) {
//        hideRightNav();
//    } else {
//        showRightNav();
//    }
//    displayImage();

//    if ($.browser.msie && parseInt($.browser.version) == 6) {
//        setTimeout("displayImage()", 1000);
//    }

//    updateNumberPanel();
}

function hideLeftNav()
{
		$(".navcontainer .prev a").css("display", "none");
		$(".navcontainer .prev").animate({
		width: "0px"}, 500 );
}

function hideRightNav()
{
		$(".navcontainer .next a").css("display", "none");
		$(".navcontainer .next").animate({
		width: "0px"}, 500 );
}

function showLeftNav()
{
		$(".navcontainer .prev a").css("display", "inline");
		$(".navcontainer .prev").animate({
		width: "42px"}, 500 );
}

function showRightNav()
{
		$(".navcontainer .next a").css("display", "inline");
		$(".navcontainer .next").animate({
		width: "42px"}, 500 );
}

function hasNextImage()
{
	if ( currentImage >= (imageCount()-1) )
	{
		return false;
	} else {
		return true;
	}
}

function hasPrevImage()
{
	if ( currentImage > 0 )
	{
		return true;
	} else {
		return false;
	}
}

var recallCount = 0;

function showNextImage() {


	if ( hasNextImage() ) {

	    recallCount = 0;
		currentImage++;
		displayImage();
		if ( !hasNextImage() )
			hideRightNav();
		
		if ( hasPrevImage() )
			showLeftNav();
		
		updateNumberPanel();
	}
}

function showPrevImage()
{
	if ( hasPrevImage() ) {

	    recallCount = 0;
		currentImage--;
		displayImage();

		if ( !hasPrevImage() )
			hideLeftNav();
		
		if ( hasNextImage() )
			showRightNav();
		
		updateNumberPanel();
	}
}

function updateNumberPanel()
{
	
	var nIMGCount = imageCount() + "";
	var nCurrentImage = (currentImage + 1) + "";
	
	$(".pagingContainer .paging").text(nCurrentImage + " of " + nIMGCount);
}

function imageCount()
{
	return projectImages.split(";").length;
}

function displayImage()
{
    $(".imgContainer").css("background-image", "url(" + projectImages.split(";")[currentImage] + ")");
    $(".label").text(projectLabels.split(";")[currentImage]);

    if ($.browser.msie && parseInt($.browser.version) == 6 && recallCount <= 2) {
        recallCount += 1;
        setTimeout("displayImage()", 2500);
    }
}


//END OF PAGING STUFF

function showModalContent()
{
	$("#modal").jqmShow();
	setModalPosition();
	$('.modalContent').jScrollPane({ scrollbarWidth: 11 });
	initProjectImages("images/project_images/img1.jpg;images/project_images/img2.jpg;images/project_images/img3.jpg");
}

function displayModalContent ( projectID )
{
	$("#modal").jqmShow();
	setModalPosition();	
	getModalContent ( projectID );
}

function getModalContent ( projectID )
{
	
  $.ajax({
    type: "POST",
    url: "WebServices/Data.asmx/getProjectData",
    data: "{'projectID':'" + projectID + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
			populateModalContent(msg.d);
			//populateBackground(msg.d, documentHeight, documentWidth);
    }
  });
	
}

function populateModalContent( content )
{
	
	$("#modal .modalContentContainer h4").text(getItemVariable ( 0, "Heading", content ));
	$("#modal .modalContentContainer .modalContent").html(getItemVariable ( 0, "Content", content ));
	initProjectLabels(getItemVariable(0, "ImageLabels", content));
	initProjectImages(getItemVariable(0, "Images", content));
	
	$('.modalContent').jScrollPaneRemove();
	
	$("#modal .imgContainer").css("display", "block");
	$("#modal .modalContent").css("height", "165px");	
	
	$('.modalContent').jScrollPane({ scrollbarWidth: 11 });
	
}








function displayModalPageContent ( pageURL )
{
	$("#modal").jqmShow();
	setModalPosition();	
	getModalPageContent ( pageURL );
}

function getModalPageContent ( pageURL )
{
	
  $.ajax({
    type: "POST",
    url: "WebServices/Data.asmx/getModalData",
    data: "{'pageURL':'" + pageURL + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
			populateModalPageContent(msg.d);
			//populateBackground(msg.d, documentHeight, documentWidth);
    }
  });
	
}

function populateModalPageContent( content )
{
	
	$("#modal .modalContentContainer h4").text(getItemVariable ( 0, "Heading", content ));
	$("#modal .modalContentContainer .modalContent").html(getItemVariable ( 0, "Content", content ));
	
	$('.modalContent').jScrollPaneRemove();	
	
	$("#modal .imgContainer").css("display", "none");
	$("#modal .modalContent").css("height", "505px");	

	$('.modalContent').jScrollPane({ scrollbarWidth: 11 });
	
}





function resetPositions()
{
	var documentHeight = $(document).height();
	var documentWidth = $(document).width() - 1;
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var interfaceHeight = $("#container").height();
	var interfaceWidth = $("#container").width();

	var projectLabelWidth = $("#projectLabel h4").width();
	if ( projectLabelWidth == 0 || projectLabelWidth == null )
	{
		projectLabelWidth = $("#projectLabel h3").width();
	}
	
	var containerWidth = $("#container").width();
	
	var minFooterTopPosition = $("#container").height();
	
	var interfacePositionTop;
	var interfacePositionLeft;
	var projectLabelPositionLeft;
	var footerPositionTop;
	
	interfacePositionTop = (windowHeight - interfaceHeight) / 2;
	if ( interfacePositionTop < 0 )
		interfacePositionTop = 0;
	interfacePositionLeft = (windowWidth - interfaceWidth);
	
	projectLabelPositionLeft = (containerWidth - (projectLabelWidth + 25));
	
	$("#container").css("top", interfacePositionTop + "px");
	$("#container").css("left", interfacePositionLeft + "px");
	
	if ( $("#projectLabel h4").width() != null )
	{
		$("#projectLabel h4").css("top", "467px");
		$("#projectLabel h4").css("left", projectLabelPositionLeft + "px");
	} else {
		$("#projectLabel h3").css("top", "467px");
		$("#projectLabel h3").css("left", projectLabelPositionLeft + "px");
	}
	
	if ( (windowHeight - $("#footer").height()) < minFooterTopPosition )
	{
		footerPositionTop = minFooterTopPosition;
	} else {
		footerPositionTop = windowHeight - $("#footer").height();
	}			
		
	$("#footer").css("top", footerPositionTop + "px");	
	
	if ($("#modal") != undefined && $("#modal").css("display") != "none")
		setModalPosition();
	
}

function setModalPosition()
{
	var footerHeight = 600;
	var footerWidth = 500;
	var minHeight = 595;
	var documentHeight = $(document).height();
	var documentWidth = $(document).width() - 1;
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var interfaceHeight = $("#modal").height();
	var interfaceWidth = $("#modal").width();

	var footerPositionTop;
	var footerPositionLeft;

	$("#modal").css("left", (windowWidth - interfaceWidth)/2 + "px");
	$("#modal").css("top", (windowHeight - interfaceHeight)/2 + "px");
	
	if (( (windowHeight - interfaceHeight)/2) < 0 )
		$("#modal").css("top", "0px");
	
	if (( (windowWidth - interfaceWidth)/2) < 0 )
		$("#modal").css("left", "0px");

}

function getEmail ( ph, mailbox, domain )
{
	sBuild = "";
	sBuild += ('<a href=\"mailto:' + mailbox + '@' + domain + '\">');
	sBuild += (mailbox + '@' + domain + '</a>');
	$("#" + ph).html(sBuild);
}

function setBackgroundImage()
{
	if ( bWorking )
		return;
	
	bWorking = true;
	//alert("Setting background");
	var documentHeight = $(document).height();
	var documentWidth = $(document).width() - 1;

  $.ajax({
    type: "POST",
    url: "WebServices/Data.asmx/getBackgroundImage",
    data: "{'pageURL':'" + getCurrentPage() + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
			populateBackground(msg.d, documentHeight, documentWidth);
    }
  });

	bWorking = false;
	
}

function populateBackground( casestudyID, height, width )
{
	var backgroundImage = casestudyID + "_" + width + "_" + height;
	setTimeout("$('body').css('background-image', 'url(" + backgroundImage + ".bgimg)')", 500);
}

		$(document).ready(
			function() {

				resetPositions();
				setBackgroundImage();
				//Because of the scrollbars being on the screen for a touch longer 
				//than we'd like, we need to run this bad boy twice or we'll get 
				//a gap where the scrollers were
				if ( $.browser.mozilla )
				{
					setTimeout("resetPositions()", 3000);
				} else {
					setTimeout("resetPositions()", 3000);
				}

				$(window).resize(function() {
					resetPositions();
					setTimeout("resetPositions()", 1000);
					setBackgroundImage();
				});
				
				if($("#modal").css("display") != undefined)
					$("#modal").jqm();
				if ( $.browser.msie && parseInt($.browser.version) == 8 )
				{
					setTimeout("$('h2').css('display', 'none')", 100);
					setTimeout("$('h2').css('display', 'block')", 150);
				}
				
				getCurrentPage();
			}
		)
