﻿/**
 * Contains Javascript functions common to multiple pages
 */

  //function to get value from URL parameter string
  function getQueryValue(name) {
      var value = "";
      if ( (i=location.search.indexOf('?'+name))==-1 && (i=location.search.indexOf('&'+name,3))==-1 )
        return null;
      else
        i += name.length+2;
      if ( (j=location.search.indexOf("&",i))!=-1 )
        value = location.search.substring(i,j);
      else
        value = location.search.substring(i,location.search.length);
      for ( i=0; i<value.length; i++ )
        if ( value.charAt(i)=='+' )
         value = value.substring(0,i)+' '+value.substring(i+1,value.length);
      return unescape(value);
  }

  //function to show story's titles
  function showTitles() {
    with (document) {
      getElementById('story_title_large').innerHTML = titleLargeArray[currIndexForThumbnails];
      getElementById('story_title_small').innerHTML = titleSmallArray[currIndexForThumbnails];
    }
  }
  
  //hover processing of navigation buttons
  function processHover(control,baseImageName,ext,mode) {
    if (mode == 1) {
      if (control.id == "up_button") {
        if (currIndex > 0) {
          control.src = "images/" + baseImageName + "_hover." + ext;
        }
      }
      if (control.id == "down_button") {
        if (currIndex < storyCount-1) {
          control.src = "images/" + baseImageName + "_hover." + ext;
        }
      }
      if ((control.id == "pageprev_button") || (control.id == "bottom_pageprev_button")) {
        if (currPage > 1) {
          control.src = "images/" + baseImageName + "_hover." + ext;
        }
      }
      if ((control.id == "pagenext_button") || (control.id == "bottom_pagenext_button")) {
        if (currPage < Math.ceil(catalogThumbCount/16)) {
          control.src = "images/" + baseImageName + "_hover." + ext;
        }
      }
    } else if (mode == 0) {
      if (control.id == "up_button") {
        if (currIndex > 0) {
          control.src = "images/" + baseImageName + "_enabled." + ext;
        }
      }
      if (control.id == "down_button") {
        if (currIndex < storyCount-1) {
          control.src = "images/" + baseImageName + "_enabled." + ext;
        }
      }
      if ((control.id == "pageprev_button") || (control.id == "bottom_pageprev_button")) {
        if (currPage > 1) {
          control.src = "images/" + baseImageName + "_enabled." + ext;
        }
      }
      if ((control.id == "pagenext_button") || (control.id == "bottom_pagenext_button")) {
        if (currPage < Math.ceil(catalogThumbCount/16)) {
          control.src = "images/" + baseImageName + "_enabled." + ext;
        }
      }
    }
  }
  
  //process story to story navigation
  function navigate(offset) {
    if ((currIndex+offset < 0) || (currIndex+offset >= storyCount)) {
      return;
    }
    currIndex += offset;
    with (document) {
      getElementById('story_thumb').src = "story/thumb/"+thumbArray[currIndex];
      
      var buttonPhase = pageName == "default" ? "on" : "off"; 
      if (currIndex > 0) {
        getElementById('up_button').src = "images/20_uparrow_"+buttonPhase+"_enabled.gif";
      } else {
        getElementById('up_button').src = "images/20_uparrow_"+buttonPhase+"_disabled.gif";
      }
      if (currIndex < storyCount-1) {
        getElementById('down_button').src = "images/21_downarrow_"+buttonPhase+"_enabled.gif";
      } else {
        getElementById('down_button').src = "images/21_downarrow_"+buttonPhase+"_disabled.gif";
      }
    }
  }
  
  //pops up the about frame
  function showAbout(anchor) {
    with (document) {
      var newPos;
      newPos = documentElement.scrollTop+10;
      getElementById('pop_frame').style.top = Math.round(newPos).toString()+"px";
      newPos = ((body.clientWidth-1077)/2)+192;
      getElementById('pop_frame').style.left = Math.round(newPos).toString()+"px";
      getElementById('pop_frame').style.visibility = "visible";
      getElementById('pop_frame').src = "about.aspx?anchor=" + anchor;
      var catalogGrid = getElementById('catalog_grid');
      if (catalogGrid) {
        catalogGrid.style.filter = "alpha (opacity=50)";
        catalogGrid.style.opacity = 0.5;
      }
    }
  }
      
  //hides the about frame
  function hideAbout() {
    with (document) {
      getElementById('pop_frame').style.visibility = "hidden";
      getElementById('pop_frame').src = "";
      var catalogGrid = getElementById('catalog_grid');
      if (catalogGrid) {
        catalogGrid.style.filter = "alpha (opacity=100)";
        catalogGrid.style.opacity = 1.0;
      }
    }
  }
