﻿// JScript File

var imagebase = "/projects/projectImages/";
var section = "Gallery";
var current ="";
var imageOffset = 0;
var imagesPerPage = 8;
var imagesResult;
var doScrollLeft = false;
var doScrollRight = false;

//takes a project id and an image id and uses them to load the project into the main viewing section
function showcontent(id, imageid)
{
    currentSubImage = "image0";
    document.getElementById(current).className = document.getElementById(current).className.replace("focus", "");
    document.getElementById(imageid).className += " focus";
    current = imageid;
    var ic = document.getElementById("item_content");
    ic.style.display = "block";
    displayLoader();
    ajaxFetcher.FetchProject(id, section, showcontent_callback);
}

//attach mouseover, mouseout, and onclick events to the images
function setListener(event) {
	setSelected((event.target || event.srcElement).id);
}
function unsetListener(event) {
	unsetSelected((event.target || event.srcElement).id);
}
function clickListener(event) {
	var pid = ((event.target || event.srcElement).id).replace("a_","").replace("img_","");
	var iid = (event.target || event.srcElement).id;
	showcontent(pid, iid);
}

//Creates an instance of the project image and attaches it to the projectHeroImages
function createImage(project, i)
{
	var heros = document.getElementById("projectHeroImages");
	var a = document.createElement("a");
	a.href="#";
	a.id = "a_" + project.ProjectID;


	
	if (project.imageSource != 0)
	{
		/*var image = new Image(133, 75);
		image.src = imagebase + obj.ShoeImage;
		image.id = "shoe_" + obj.ShoeId;*/

		var image = document.createElement("img");
		image.src = imagebase + project.imageSource;
		image.id = "img_" + project.ProjectID;
		if(i%8 == 0)
		{
		    image.className = "focus";
		    current = image.id;
		}
		if(i%2 == 0)
		{
		    image.className += " leftImage";
		}
		//check if they are using moz-mac
		var d = detectMacXFF();
        if (d) 
        {
            image.className += " moz-mac";
        }
		if (typeof document.implementation != 'undefined' && document.implementation.hasFeature('Events', '2.0'))
	    {
		    a.addEventListener('mouseover', setListener, false);
		    a.addEventListener('mouseout', unsetListener, false);
		    a.addEventListener('click', clickListener, false);
	    }
	    else if (typeof document.attachEvent != 'undefined')
	    {
		    a.attachEvent('onmouseover', setListener);
		    a.attachEvent('onmouseout', unsetListener);
		    a.attachEvent('onclick', clickListener);
	    }
    		
		    a.appendChild(image);
	}
	heros.appendChild(a);
    if(i == 0)
        showcontent(project.ProjectID,image.id);
}

//goes through the ncaProject[] array that it is passed and call createImage() for each project in the array
function showproject_callback(res)
{

	hideLoader();
	if (res.value)
	{
	    imagesResult = res.value;
		doImageLoop();
	}
	else
	{
	    //show no projetc label
	    clearcurrent();
	    
	     var d = document.getElementById("item_content"); 
        while (d.hasChildNodes()) 
        { 
          d.removeChild(d.firstChild); 
        }
        var e = document.createElement("label");
        e.innerHTML = "I'm sorry, there are no projects in this category";
        d.appendChild(e);
	    imagesResult = (0);
	    
	}
}

function showproject_callback_single(res)
{

	hideLoader();
	if (res.value)
	{
		    imagesResult = res.value;
		doImageLoop();

	}
	else
	{
	    //show no project label
	}
}

//function showproject_callback_single(res)
//{
//	hideLoader();
//	if (res.value)
//	{
//		    clearcurrent();
//		    createImage(res.value, 0);

//	}
//	else
//	{
//	    //show no project label
//	}
//}


//the following 3 functions allow for scrolling images on the gallery page
function doImageLoop()
{
    clearcurrent();
    if(imagesResult.length > 0)
    {
        var loopBound = imagesPerPage + imageOffset;
        if(loopBound >= imagesResult.length)
            loopBound = imagesResult.length;
        for(var i = imageOffset; i < loopBound; i++)
		    {
		            createImage(imagesResult[i], i);
		    }
        if(imagesPerPage == 8)
        {
            if(imageOffset <= 0)
            {
                imageOffset = 0;
                document.getElementById("backButton").style.visibility ="hidden";
            }
            else
                document.getElementById("backButton").style.visibility ="visible";
            if(imageOffset + imagesPerPage >= imagesResult.length)
                document.getElementById("moreButton").style.visibility ="hidden";
            else
                document.getElementById("moreButton").style.visibility ="visible";
        }
    }
    
}


function leftScroll()
{
    if(doScrollLeft)
    {
        imageOffset -= imagesPerPage;
        if(imageOffset < 0)
            imageOffset = 0;
        doImageLoop();		
    }
}

function rightScroll()
{
    if(doScrollRight)
    {
        imageOffset += imagesPerPage;
        if(imageOffset >= imagesResult.length)
            imageOffset -= imagesPerPage;
        doImageLoop();	
    }		
}


function open()
{
    imagesPerPage = 4;
    doImageLoop();
    document.getElementById("backButton").style.visibility ="hidden";	
    document.getElementById("moreButton").style.visibility ="hidden";
    doScrollLeft = false;
    doScrollRight = false;
}

function close()
{
    imagesPerPage = 8;
    doImageLoop();
     doScrollLeft = true;
    doScrollRight = true;
}

function openArch()
{
    imagesPerPage = 4;
    doImageLoop();
    document.getElementById("backButton").style.visibility ="hidden";	
    document.getElementById("moreButton").style.visibility ="hidden";
    doScrollLeft = false;
    doScrollRight = false;
    document.getElementById("projectHeroImages").style.top = "240px";
}

function closeArch()
{
    imagesPerPage = 8;
    doImageLoop();
     doScrollLeft = true;
    doScrollRight = true;
    document.getElementById("projectHeroImages").style.top = "110px";
}





//clear all images from the left panel
function clearcurrent() 
{ 
     var d = document.getElementById("projectHeroImages"); 
     while (d.hasChildNodes()) 
     { 
          d.removeChild(d.firstChild); 
     } 
}

function detectMacXFF() 
{
    var userAgent = navigator.userAgent.toLowerCase();
    if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
        return true;
    }
//    if (userAgent.indexOf('firefox')!=-1) {
//        return true;
//    }
}
