﻿//Global Variables
var photoIndex_Parent = 0;  //index of the current picture being shown
var theWindow = "Parent";   //which window is in use... the main gallery window (Parent) or the pop-up expanded gallery iframe (Child)

/*Preload images*/
    /*Conventional Oil & Gas*/
    var og_photo_ary = new Array();
    
    og_photo_ary[0]  = new Image(1024,768);
    og_photo_ary[0].src = "Images/Gallery/oil-gas/Compressor Stations.JPG";
    
    og_photo_ary[1] = new Image(750,410);
    og_photo_ary[1].src = "Images/Gallery/oil-gas/Gas Processing and Dehydration.jpg";
    
    og_photo_ary[2] = new Image(920,616);
    og_photo_ary[2].src = "Images/Gallery/oil-gas/Pipelines.jpg";
    
    og_photo_ary[3] = new Image(795,623);
    og_photo_ary[3].src = "Images/Gallery/oil-gas/Surface Facilities.jpg";
    
    og_photo_ary[4] = new Image(790,524);
    og_photo_ary[4].src = "Images/Gallery/oil-gas/Pipelines .JPG";
    
    og_photo_ary[5] = new Image(814,527);
    og_photo_ary[5].src = "Images/Gallery/oil-gas/Compressor Stations .jpg";
    
    og_photo_ary[6] = new Image(800,600);
    og_photo_ary[6].src = "Images/Gallery/oil-gas/Compressor Facilities.JPG";
    
    og_photo_ary[7] = new Image(806,605);
    og_photo_ary[7].src = "Images/Gallery/oil-gas/Power Generation.JPG";        


    /*Heavy Oil*/
    var ho_photo_ary = new Array();
    
    ho_photo_ary[0] = new Image(750,563);
    ho_photo_ary[0].src = "images/Gallery/Heavy-oil/Gathering and Distribution Systems.jpg";
    
    ho_photo_ary[1] = new Image(750,563);
    ho_photo_ary[1].src = "images/Gallery/Heavy-oil/Hydrogen Plants.jpg";

    ho_photo_ary[2] = new Image(640,480);
    ho_photo_ary[2].src = "images/Gallery/Heavy-oil/Pad Facilities.JPG";


    /*Modular*/
    var mod_photo_ary = new Array();
    mod_photo_ary[0]  = new Image(735,486);
    mod_photo_ary[0].src  = "images/Gallery/modular/Alpine Production Facilities, Alaska.JPG";
        
    mod_photo_ary[1]  = new Image(1472,1168);
    mod_photo_ary[1].src  = "images/Gallery/modular/Arctic Drillship Gulf Kulluk, Russia.jpg";
    
    mod_photo_ary[2]  = new Image(825,546);
    mod_photo_ary[2].src  = "images/Gallery/modular/Large Module Transportation Alpine, Alaska.JPG";
    
    mod_photo_ary[3]  = new Image(813,634);
    mod_photo_ary[3].src = "images/Gallery/modular/Sakhalin II - Lunskoye, Russia.jpg";

    mod_photo_ary[4]  = new Image(900,600);
    mod_photo_ary[4].src = "images/Gallery/modular/Sakhalin II - Lunskoye Sailout, Russia.jpg";
    
    mod_photo_ary[5]  = new Image(811,727);
    mod_photo_ary[5].src = "images/Gallery/modular/Sakhalin II - Molikpaq Drilling Rig, Russia.jpg";

    mod_photo_ary[6]  = new Image(637,478);
    mod_photo_ary[6].src = "images/Gallery/modular/Sakhalin II - Molikpaq Under Tow, Russia.JPG";

    mod_photo_ary[7]  = new Image(800,600);
    mod_photo_ary[7].src = "images/Gallery/modular/Sakhalin II - Waterflood Module, Russia.jpg";


/*===============================================================================
Functions:
===============================================================================*/
    
    /*------------------------------------
        nextPhoto(page):
            Takes in a string representing the page that called it, chooses a photo array based on that, and
        displays the next picture in that array. Returns nothing.
    ------------------------------------*/
    function nextPhoto(page)
    {
        detectCallingWindow();
        var curAry = new Array();
        if(page == 'heavyoil')
        {
            curAry = ho_photo_ary;
        }
        else if(page == 'oilgas')
        {
            curAry = og_photo_ary;
        }
        else if(page == 'modular')
        {
            curAry = mod_photo_ary;
        }        
        
        photoIndex_Parent += 1; //next photo
        if(photoIndex_Parent >= curAry.length) //wrap around to the beginning of the array
        {
            photoIndex_Parent = 0;
        }
        //change the picture in both windows to the newly selected one.
        top.document.getElementById("bigPicParent").setAttribute("src",curAry[photoIndex_Parent].src);
        top.frames[0].document.getElementById("bigPicChild").setAttribute("src",curAry[photoIndex_Parent].src);
        
        //wideOrTall();
        writePhotoCaption();
    }


    /*------------------------------------------
        prevPhoto(page):
            Takes in a string representing the page that called it, chooses a photo array based on that, and
        displays the previous picture in that array. Returns nothing.
    ------------------------------------------*/
    function prevPhoto(page)
    {
        detectCallingWindow();
        var curAry = new Array();
        if(page == 'heavyoil')
        {
            curAry = ho_photo_ary;
        }
        else if(page == 'oilgas')
        {
            curAry = og_photo_ary;
        }
        else if(page == 'modular')
        {
            curAry =mod_photo_ary;
        }

        photoIndex_Parent -= 1; //previous photo
        if(photoIndex_Parent < 0)//wrap around to the end of the array
        {
            photoIndex_Parent = curAry.length-1;
        }
        //change the picture in both windows to the newly selected one.
        top.document.getElementById("bigPicParent").setAttribute("src",curAry[photoIndex_Parent].src);
        top.frames[0].document.getElementById("bigPicChild").setAttribute("src",curAry[photoIndex_Parent].src);

        //wideOrTall();
        writePhotoCaption();
    }
    
    
    /*------------------------------------------
        writePhotoCaption():
            Takes nothing and returns nothing. Calls helper functions to update the photo's titles to match
            the newly selected picture. One of the helper functions changes the title in the parent window, and
            the other, in the child window
    ------------------------------------------*/
    function writePhotoCaption()
    {
         writePhotoCaptionParent();
         writePhotoCaptionChild();
    }
    
    
    /*------------------------------------------
        writePhotoCaptionParent():
            Takes nothing and returns nothing. Detects the name of the picture being shown and 
            changes the title under it accordingly. It does this on the parent window.
    ------------------------------------------*/
    function writePhotoCaptionParent()
    {
        var theName = new String(top.document.getElementById('bigPicParent').getAttribute('src'));
        var lastSlashAt = theName.lastIndexOf("/");
        theName = theName.substr(lastSlashAt+1,theName.length - (lastSlashAt+1));
        var lastDotAtIndex = theName.lastIndexOf(".");
        theName = theName.substr(0,lastDotAtIndex);
         
         while(theName.lastIndexOf("%20") != -1)
         {
            theName = theName.replace(new RegExp("(%20)+"),"&nbsp;");
         }
         top.document.getElementById("photoNameColParent").innerHTML = theName;
    }
   
    /*------------------------------------------
        writePhotoCaptionChild():
            Takes nothing and returns nothing. Detects the name of the picture being shown and 
            changes the title under it accordingly. It does this on the child window.
    ------------------------------------------*/
    function writePhotoCaptionChild()
    {
        //Get the path of the picture being shown
        var theName = new String(top.frames[0].document.getElementById('bigPicChild').getAttribute('src'));
        
        //Extract the picture's name from its path
        var lastSlashAt = theName.lastIndexOf("/");
        theName = theName.substr(lastSlashAt+1,theName.length - (lastSlashAt+1));
        var lastDotAtIndex = theName.lastIndexOf(".");
        theName = theName.substr(0,lastDotAtIndex);
         
         while(theName.lastIndexOf("%20") != -1)
         {
            theName = theName.replace(new RegExp("(%20)+"),"&nbsp;");
         }

        //Replace the old picture title with the new one.
        top.frames[0].document.getElementById("photoNameColChild").innerHTML = theName;
    }
   
   
   function detectCallingWindow()
   {
        if(top.document.getElementById("lgGallery").style.visibility  == 'visible')
        {
            theWindow = 'Child';
        }
        else
        {
            theWindow = 'Parent';
        }
   } 
    
    
    
/*------------------------------------------
    wideOrTall():
        Adjusts the size of the picture being shown so that it doesn't exceed
    the space reserved for it on the page. This calls two helper functions,
    one for the parent window and one for the child window.
------------------------------------------*/
function wideOrTall()
{
	wideOrTallParent();
	wideOrTallChild();
}


/*------------------------------------------
    wideOrTallParent():
        Adjusts the size of the picture being shown so that it doesn't exceed
    the space reserved for it on the page. This function adjusts the picture in
    the parent window.
------------------------------------------*/
function wideOrTallParent()
{
//Reset the picture's width and height to their default values... will depend on current picture
top.document.getElementById("bigPicParent").style.width  = 'auto';
top.document.getElementById("bigPicParent").style.height = 'auto';

//Save the current picture's width and height
var theParentWidth  = top.document.getElementById("bigPicParent").offsetWidth;	
var theParentHeight = top.document.getElementById("bigPicParent").offsetHeight;	

//Calculate the current picture's width-to-height ratio
var wthRatio  = theParentWidth/theParentHeight; /*need non-zero height... width too*/
/* The ratio will be the same for the photo in the parent window and in the child window */

//Set the maximum width and height values that the picture may have
var MAX_PARENT_WIDTH  = 480;
var MAX_PARENT_HEIGHT = 480;

    //If the picture is too wide, clamp its width to the maximum 
    //set above and calculate new height in order to keep the aspect ratio unchanged
	if(theParentWidth > MAX_PARENT_WIDTH)
	{
		theParentWidth = MAX_PARENT_WIDTH;
		theParentHeight = theParentWidth/wthRatio;
	}

    //If the picture is too tall, clamp its height to the maximum 
    //set above and calculate new width in order to keep the aspect ratio unchanged	
	if(theParentHeight > MAX_PARENT_HEIGHT)
	{
		theParentHeight = MAX_PARENT_HEIGHT;
		theParentWidth  = theParentHeight*wthRatio;
	}

    //Assign the new width and height values to the current picture 
	top.document.getElementById("bigPicParent").style.width  = theParentWidth;
	top.document.getElementById("bigPicParent").style.height = theParentHeight;
}


/*------------------------------------------
    wideOrTallChild():
        Adjusts the size of the picture being shown so that it doesn't exceed
    the space reserved for it on the page. This function adjusts the picture in
    the child window.
------------------------------------------*/
function wideOrTallChild()
{
//Reset the picture's width and height to their default values... will depend on current picture
top.frames[0].document.getElementById("bigPicChild").style.width  = 'auto';
top.frames[0].document.getElementById("bigPicChild").style.height = 'auto';

//Save the current picture's width and height
var theChildWidth  = top.frames[0].document.getElementById("bigPicChild").offsetWidth;	
var theChildHeight = top.frames[0].document.getElementById("bigPicChild").offsetHeight;	

//Calculate the current picture's width-to-height ratio
var wthRatio  = theChildWidth/theChildHeight; /*non-zero height... width too*/
/*The ratio will be the same for the photo in the parent window and in the child window*/

//Set the maximum width and height values that the picture may have
var MAX_CHILD_WIDTH  = 700;
var MAX_CHILD_HEIGHT = 700;


	//If the picture is too wide, clamp its width to the maximum 
    //set above and calculate new height in order to keep the aspect ratio unchanged
	if(theChildWidth > MAX_CHILD_WIDTH)
	{
		theChildWidth = MAX_CHILD_WIDTH;
		theChildHeight = theChildWidth/wthRatio;
	}
	
	//If the picture is too tall, clamp its height to the maximum 
    //set above and calculate new width in order to keep the aspect ratio unchanged	
	if(theChildHeight > MAX_CHILD_HEIGHT)
	{
		theChildHeight = MAX_CHILD_HEIGHT;
		theChildWidth  = theChildHeight*wthRatio;
	}
	
	//Assign the new width and height values to the current picture 
	top.frames[0].document.getElementById("bigPicChild").style.width  = theChildWidth;
	top.frames[0].document.getElementById("bigPicChild").style.height = theChildHeight;
}