/**
 * site.js
 * JavaScript used on all top level ianritchiearchitects.co.uk pages
 * this file depends on the jQuery javascript library to function
 */

/* main popup function to reveal/populate hidden iframes */
function popup()
{
		var a = arguments;
		if (a.length > 0) {
    		var pageURL = a[0];
    		if (a[0].indexOf("../") == 0) {
    		    pageURL = a[0].substring(3);
    		}
    		var baseURL = 'http://'+window.location.host+'/';
				if (window.location.pathname.indexOf("/beta") == 0) {
				    baseURL += 'beta/';
				}
    		if (pageURL.indexOf("menus") != -1) {
    		    window.frames['menu'].location.href = baseURL + pageURL;
    				$('#menu-window').css('visibility', 'visible');
    		} else {
    				var anchorID = false;
    		    if (pageURL.indexOf("#") != -1) {
    						anchorID = pageURL.split("#")[1];
    				    pageURL = pageURL.split("#")[0];
    				}
    		    if (window.frames['data'].location.href.indexOf(pageURL) != -1 && anchorID) {
    						window.frames['data'].scrollDivToAnchor(anchorID);
    				} else if (anchorID) {
    				    window.frames['data'].location = baseURL + pageURL + '?scrollto=' + anchorID;
    				} else if (window.location.search != '') {
    				    window.frames['data'].location = baseURL + pageURL + window.location.search;
    				} else {
        		    window.frames['data'].location = baseURL + pageURL;
    				}
    				$('#data-window').css('visibility', 'visible');
    		}
		}
}
/* object to track history of data window */
var pageHistory = new Array();
var currentIndex = -1;
/* retrieves data from session cookies about page history and current position */
function getHistory()
{
    if (testSessionCookie()) {
        if (pageHistoryData = getCookieValue('pageHistory')) {
		        pageHistory = pageHistoryData.split(',');
		    }
		    if (currentIndexData = getCookieValue('currentIndex')) {
		        currentIndex = parseInt(currentIndexData);
    		}
    }
		//alert("getHistory call result:\n\npageHistory:\n"+pageHistory.join("\n")+"\ncurrentIndex: "+currentIndex);
}
/* saves data to session cookies about page history and current position */
function saveHistory()
{
    if (testSessionCookie()) {
        writeSessionCookie('currentIndex', currentIndex);
		    writeSessionCookie('pageHistory', pageHistory.join(','));
    }
}
/* adds an entry to the pageHistory array/cookie */
function addToHistory(url)
{
    return;
		getHistory();
    if (currentIndex == (pageHistory.length - 1) && pageHistory[currentIndex] != url) {
		    currentIndex = pageHistory.length;
        pageHistory[currentIndex] = url;
		}
		saveHistory();
}
/* button swapping function for "window" controls */
function swapButton(btn, o)
{
    var img = btn.indexOf('close') == 0? 'close':btn;
		if (btn == 'back' || btn == 'forward') {
		    getHistory();
		    if (pageHistory.length == 0) return;
				if (currentIndex == pageHistory.length && btn == 'forward') return;
				if (currentIndex == 0 && btn == 'back') return;
		}
		if (o) {
		    $('#'+btn+'button').src('images/buttons/'+img+'b-over.gif');
		} else {
		    $('#'+btn+'button').src('images/buttons/'+img+'b.gif');
		}
}
/* function to close either data or menu "windows" */
function closeWindow(win)
{
    $('#'+win+'-window').css('visibility','hidden');
    window.frames[win].location = '/threads/blank.html';
    
}
/* function to go back in the history of the data "window" */
function goBack()
{
    getHistory();
		if (pageHistory.length > 1 && currentIndex != 0) {
        currentIndex--;
				window.frames['data'].location.href = pageHistory[currentIndex];
		}
		saveHistory();
}
/* function to go forward in the history of the data "window" */
function goForward()
{
    getHistory();
    if (currentIndex != pageHistory.length) {
		    currentIndex++;
        window.frames['data'].location.href = pageHistory[currentIndex];
		}
		saveHistory();
}
/* function to print the content of the data "window" */
function printWindow(win)
{
    window.frames[win].focus();
    window.frames[win].print();
}
/* function to write the current month and year next to the news star */
function writeNewsLink()
{
    /* write the current date in the news link */
    var now = new Date();
    var year = now.getFullYear();
    var months = ["january","february","march","april","may","june","july","august","september","october","november","december"];
    var month = months[now.getMonth()];
    $('.newslink').append('<span id="newstext">news&nbsp;&nbsp;&nbsp;&nbsp;'+month+'&nbsp;'+year+'</span>');
}
/* pre-caching for the star rollover */
var star_anim = new Image(20,20);
star_anim.src = 'images/star_anim_blue.gif';
var star_static = new Image(20,20);
star_static.src = 'images/star_static_blue.gif';
var staticstar = false;
/* rollover for the news/star */
function showNews(x,star)
{
    if (x) {
		    $('#newstext').show();
        if ($('#news_star').src().indexOf('images/star_static_blue.gif') != -1) {
				    staticstar = true;
            $('#news_star').src('images/star_anim_blue.gif');
		    }
		} else {
		    $('#newstext').toggle();
				if (staticstar) {
            $('#news_star').src('images/star_static_blue.gif');
		    }
		}
}
/*==============================================================================

    Routines written by John Gardner - 2003 - 2005

    See www.braemoor.co.uk/software for information about more freeware
    available.

/*==============================================================================

Routine to write a session cookie

    Parameters:
        cookieName        Cookie name
        cookieValue       Cookie Value
    
    Return value:
        true              Session cookie written successfullly
        false             Failed - persistent cookies are not enabled

   e.g. if (writeSessionCookie("pans","drizzle") then
           alert ("Session cookie written");
        else
           alert ("Sorry - Session cookies not enabled");
*/

function writeSessionCookie (cookieName, cookieValue) {
  if (testSessionCookie()) {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
    return true;
  }
  else return false;
}

/*==============================================================================

Routine to get the current value of a cookie

    Parameters:
        cookieName        Cookie name
    
    Return value:
        false             Failed - no such cookie
        value             Value of the retrieved cookie

   e.g. if (!getCookieValue("pans") then  {
           cookieValue = getCoookieValue ("pans2);
        }
*/

function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

/*==============================================================================

Routine to see if session cookies are enabled

    Parameters:
        None
    
    Return value:
        true              Session cookies are enabled
        false             Session cookies are not enabled

   e.g. if (testSessionCookie())
           alert ("Session coookies are enabled");
        else
           alert ("Session coookies are not enabled");
*/

function testSessionCookie () {
  document.cookie ="testSessionCookie=Enabled";
  if (getCookieValue ("testSessionCookie")=="Enabled")
    return true 
  else
    return false;
}
