//determine the initial gallery content & arrow links 
var mainImage;
var galleryFeature;

var arrowLeft;
var arrowRight;
var arrowLeftImg;
var arrowRightImg;
var captionLeft;
var captionRight;
var moreInfo;

var nextImage;
var previousImage;

var currentImage;

var ursartPrefix = "http://www.ursart.com/video/";


function loadFirstImage() {
	
	initVars();
    initDeeplinkingDefault();
	
	var _loc = window.location;

	if (_loc.indexOf("#") != -1){
		var _page = _loc.substr(_loc.indexOf("#") + 1,_loc.length);
		changeImage(_page);
	}
}

function initVars(){
	mainImage = document.getElementById("mainImage");
    galleryFeature = document.getElementById("galleryFeature");
    arrowLeft = document.getElementById("arrowLeft");
    arrowRight = document.getElementById("arrowRight");
    arrowLeftImg = document.getElementById("arrowLeftImg");
    arrowRightImg = document.getElementById("arrowRightImg");
    captionLeft = document.getElementById("captionLeft");
    captionRight = document.getElementById("captionRight");
    moreInfo = document.getElementById("moreInfo");
	
}

function initDeeplinkingDefault() {
    SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
}

function handleChange(event) {

    var page;
    if (event.pathNames[0]) {
        page = event.pathNames[0];
    } else {
        page = undefined;
    }
    changeImage(page);
}

function drawGallery(start, stop) {

if (stop >=images.length){
	stop = images.length - 1;
}

    for (g = start; g < stop; g++) {

        thumbnail = "<a href='#" + images[g] + "'>";

        //check to see if the thumbnail is in the no outline array
        for (h = 0; h < imagesNoOutline.length; h++) {
            if (imagesNoOutline[h] == images[g]) break;
        }
        // determine the thumbnail style	
        if (images[g] == imagesNoOutline[h]) {
            thumbnail += "<img class='galHoverWht galleryThumbnail' src='"
        } else {
            thumbnail += "<img class='galHoverGry galleryThumbnail' src='"
        }

        thumbnail += imgDirectory
        thumbnail += images[g]
        thumbnail += "_sm.jpg'>";
        thumbnail += "</a>";

        document.write(thumbnail);
    }
}

//change the content

function changeImage(newImage) {
    if (newImage != undefined) {
        for (i = 0; i < images.length; i++) {
            if (images[i] == newImage) break;
        }
		 //pageTracker._trackPageview(newImage);
	   _gaq.push(['_trackEvent', 'image', 'click', newImage]);
    } else {
		var _randomIndex = Math.round(Math.random() *(images.length-1));
        newImage = images[_randomIndex];
	    i = _randomIndex;
	   
	   _gaq.push(['_trackEvent', 'image', 'click', newImage + " (random selection)"]);
	  // pageTracker._trackPageview(newImage + " (random selection)");
    }

	if (currentImage != newImage){
    mainImage.src = imgDirectory + newImage + ".jpg";
    captionLeft.innerHTML = textLeft[i];
    captionRight.innerHTML = textRight[i];
    moreInfo.innerHTML = additionalText[i];

    //change the left arrow link
    if (i == 0) {
        previousImage = images[images.length - 1];
    } else {
        previousImage = images[i - 1];
    }

    arrowLeft.href = "#" + previousImage;

    //change the right arrow link
    if (i == images.length - 1) {
        nextImage = images[0];
    } else {
        nextImage = images[i + 1];
    }

    arrowRight.href = "#" + nextImage;

    //check to see if we should get rid of the outline
    if (imagesNoOutline) {
        for (j = 0; j < imagesNoOutline.length; j++) {
            if (imagesNoOutline[j] == newImage) break;
        }
        if (imagesNoOutline[j] == newImage) {
            mainImage.style.color = "#ffffff";
        } else {
            mainImage.style.color = "#a9a9a9";
        }
    }
	}

}


//THE FOLLOWING FUNCTIONS PERTAIN ONLY TO THE THE GALLERIS DISPLAYING ANIMATION/VIDEO/FLASH
//load the first animation option in the gallery

function loadFirstAnimation() {
	//alert("loadFirstAnimation");
	initVars();
    initDeeplinkingAnimation();
	
	var _loc = window.location;

	if (_loc.indexOf("#") != -1){
		var _page = _loc.substr(_loc.indexOf("#") + 1,_loc.length);
		updateAnimationContent(_page);
	}else{
		updateAnimationContent(undefined);
	}
}

function initDeeplinkingAnimation() {
    SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChangeAnimation);
}

function handleChangeAnimation(event) {

    var page;
    if (event.pathNames[0]) {
        page = event.pathNames[0];
    } else {
        page = undefined;
    }
    //changeAnimation(page);
	updateAnimationContent(page);
}

//change the content when clicked

function changeAnimation(newImage) {
	window.location = "#" + newImage;
}

function updateAnimationContent(newImage){
	//alert("updateAnimationContent " + currentImage + " " + newImage);
	
	if (newImage != undefined) {
        for (i = 0; i < images.length; i++) {
            if (images[i] == newImage) break;
        }
		
		_gaq.push(['_trackEvent', 'animation', 'click', newImage]);
		//pageTracker._trackPageview(newImage);
    } else {
		newImage = images[0];
        i = 0;
		_gaq.push(['_trackEvent', 'animation', 'click', newImage + " (random selection)"]);
		//pageTracker._trackPageview(newImage + " (random selection)");
    }

	if (currentImage != newImage) {

        for (i = 0; i < images.length; i++) {
            if (images[i] == newImage) {
				
				break;
			}
        }

        galleryFeature.innerHTML = mainContent[i];
        captionLeft.innerHTML = textLeft[i];
        captionRight.innerHTML = textRight[i];
        moreInfo.innerHTML = additionalText[i];
		

        //change the left arrow link
        if (i == 0) {
            previousImage = images[images.length - 1];
        } else {
            previousImage = images[i - 1];
        }
        arrowLeft.href = "#" + previousImage;


        //change the right arrow link
        if (i == images.length - 1) {
            nextImage = images[0];
        } else {
            nextImage = images[i + 1];
        }

        arrowRight.href = "#" + nextImage;

        // if coolericons is chosen, create the icons table			
        if (newImage == "coolerIcons") {
            writeEmoticons();
        }

        currentImage = images[i];
        document.getElementById("srcCode").style.display = "none";
        document.getElementById("srcLinkImg").src = "images/buttons/viewSrc_off.gif";

    }
	
	
}

//change the swf when clicked

function swapSwf(newTheme) {
    if (newTheme == "dance") {
        galleryFeature.innerHTML = mainContent[0];
        document.getElementById("swapVideoLink").onclick = function () {
            swapSwf("lids");
			return false;
        }
    } else {
        galleryFeature.innerHTML = additionalViagra;
        document.getElementById("swapVideoLink").onclick = function () {
            swapSwf("dance");
			return false;
        }
    }
}

//load the correct movie when the user chooses file format

function playMovie(fileName) {
	
//	var _html = "<EMBED SRC=" + ursartPrefix + fileName + " WIDTH=240 HEIGHT = 196 AUTOPLAY=true CONTROLLER=true LOOP=false PLUGINSPAGE=http://www.apple.com/quicktime/>";
	galleryFeature.innerHTML = window[fileName];
//	galleryFeature.innerHTML = this[fileName];
	switch(fileName){
		case "scythe_quickHiBand":
			galleryFeature.innerHTML =scythe_quickHiBand;
		
		break;
		case "scythe_quickLoBand":
		
		galleryFeature.innerHTML =scythe_quickLoBand;
		break;
		case "scythe_winHiBand":
		galleryFeature.innerHTML =scythe_winHiBand;
		
		break;
		case "scythe_winLoBand":
		
		galleryFeature.innerHTML =scythe_winLoBand;
		break;
		case "iceAge_quickHiBand":
		galleryFeature.innerHTML =iceAge_quickHiBand;
		
		break;
		case "iceAge_quickLoBand":
		galleryFeature.innerHTML =iceAge_quickLoBand;
		
		break;
		case "iceAge_winHiBand":
		galleryFeature.innerHTML =iceAge_winHiBand;
		
		break;
		case "iceAge_winLoBand":
		
		galleryFeature.innerHTML =iceAge_winLoBand;
		break;
		case "jack_quickHiBand":
		galleryFeature.innerHTML =jack_quickHiBand;
		
		break;
		case "jack_quickLoBand":
		galleryFeature.innerHTML =jack_quickLoBand;
		
		break;
		case "jack_winHiBand":
		galleryFeature.innerHTML =jack_winHiBand;
		
		break;
		case "jack_winLoBand":
		
		galleryFeature.innerHTML =jack_winLoBand;
		break;
		case "finalMoments_quickHiBand":
		galleryFeature.innerHTML =finalMoments_quickHiBand;
		
		break;
		case "finalMoments_quickLoBand":
		
		galleryFeature.innerHTML =finalMoments_quickLoBand;
		break;
		case "finalMoments_winHiBand":
		galleryFeature.innerHTML =finalMoments_winHiBand;
		
		break;
		case "finalMoments_winLoBand":
		
		galleryFeature.innerHTML =finalMoments_winLoBand;
		break;
			
		
	}




}

//button rollovers
var request;
//load button imgs
if (document.images) {
    var viewSrcOff = new Image();
    viewSrcOff.src = "http://www.ursart.com/images/buttons/viewSrc_off.gif";
    var viewSrcOn = new Image();
    viewSrcOn.src = "http://www.ursart.com/images/buttons/viewSrc_on.gif";
    var hideSrcOff = new Image();
    hideSrcOff.src = "http://www.ursart.com/images/buttons/hideSrc_off.gif";
    var hideSrcOn = new Image();
    hideSrcOn.src = "http://www.ursart.com/images/buttons/hideSrc_on.gif";
}

var srcLinkImg = document.getElementById("srcLinkImg");
var srcLink = document.getElementById("srcLink");
var srcCode = document.getElementById("srcCode");

function highlightViewSrc() {
    srcLinkImg.src = viewSrcOn.src;
}

function dimViewSrc() {
    srcLinkImg.src = viewSrcOff.src;
}

function highlightHideSrc() {
    srcLinkImg.src = hideSrcOn.src;
}

function dimHideSrc() {
    srcLinkImg.src = hideSrcOff.src;
}

function getSrc(url, lines) {

    srcLink.onmouseover = function () {
        highlightHideSrc();
    }
    srcLink.onmouseout = function () {
        dimHideSrc();
    }
    srcLink.onclick = function () {
        hideSrc(url, lines);
    }

    srcCode.innerHTML = "<center>loading... please wait a moment</center>";
    srcCode.style.display = "block";

    if (!window.XMLHttpRequest) {
        XMLHttpRequest = function () {
            try {
                return new ActiveXObject("Msxml2.XMLHTTP.6.0")
            } catch (e) {}
            try {
                return new ActiveXObject("Msxml2.XMLHTTP.3.0")
            } catch (e) {}
            try {
                return new ActiveXObject("Msxml2.XMLHTTP")
            } catch (e) {}
            try {
                return new ActiveXObject("Microsoft.XMLHTTP")
            } catch (e) {}
            throw new Error("Could not find an XMLHttpRequest alternative.")
        }
    }
    request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (request.readyState == 4 && request.status == 200) {
            //lineNumbers = makeNumbers(1, lines);
            allSrcTxt = "<table border='0' padding='0' margin='0'><tr><td valign='top' class'flashActions'><div id='codeLines'>NUM</div></td><td class='flashActions' valign='top'>" + request.responseText + "</td></tr></table>";
            srcCode.innerHTML = allSrcTxt;
            makeNumbers(1, lines);
        }
    };
    request.open("GET", url, true);
    request.send(null);
}


function showSrc() {
    srcLinkImg.src = viewSrcOn.src;
    srcLink.onmouseout = function () {
        srcLinkImg.src = hideSrcOff.src;
        srcLink.onmouseover = function () {
            hideSrc();
        }
    }
    srcCode.style.display = "block";
}

function hideSrc(_url, _lines) {
    srcCode.style.display = "none";
    srcLink.onmouseover = function () {
        highlightViewSrc();
    }
    srcLink.onmouseout = function () {
        dimViewSrc();
    }
    srcLink.onclick = function () {
        getSrc(_url, _lines);
    }

}

function initSlideshow(){
	document.getElementById("att_network_slideshow").style.display = "block";
    $('.slideshow').cycle({
		fx: 'fade',
		speed:    300, 
   		timeout:  2000 
	});
}

//YES IT FINALLY FUCKING WORKS
