﻿var siteURL = "http://www.dataspacescotland.co.uk";

// menubar animation and headline replacement
window.addEvent('domready', function()
{
	SetMenu(document.location.pathname);
	
	// turn website into AJAX website
	var ajaxDiv = document.createElement("div");
	ajaxDiv.setAttribute('id', "ajaxframe");
	$("footer").appendChild(ajaxDiv);
	$$("a").forEach(function(lnk) {	
		if (lnk.href.substring(0,19) == "http://www.dataspac" && lnk.href.slice(-13) != "stocklist.php") // only for internal links (ex. home) / Skips the file extensions listed
		{
			var aPath = lnk.href.split('/');
			$(lnk).setProperty('title', lnk.href);
			$(lnk).setProperty('href', "javascript:nolink();");
			$(lnk).addEvent('click', function(){LoadPage(this)});
			
		}
	});

});

var accordion;

function nolink() {}

function SetMenu(sPath)
{
	// if accordian exists, get rid of it
	if ($defined(accordion))
	{
		accordion = null;
	}
	
	//first find out which folder we're in
	sPath = sPath.replace(siteURL, "");
	dir = sPath.substr(sPath.indexOf('/', 0)+1, sPath.indexOf('/', 1)-1);
	var curSection = -1; // this 'default' value will close all sections
	switch (dir)
	{
		case 'records-management':
			curSection = 0;
			$("mb-records").addClass('current');
			break;
		case 'data-centre':
			curSection = 1;
			$("mb-datacentre").addClass('current');
			break;
		case 'offsite-storage':
			curSection = 2;
			$("mb-ods").addClass('current');
			break;
	}
	
	var file = sPath.substr(sPath.indexOf('/', 1)+2, sPath.length);

	$$("#topmenu a").forEach(function(lnk) {
		lnk.removeClass("current");
	});

	switch(file)
	{
		case '':
			$("tplHome").addClass('current');
			break;
		case 'index.php':
			$("tplHome").addClass('current');
			break;
		case 'about-us.php':
			$("tplAboutUs").addClass('current');
			break;
		case 'find-us.php':
			$("tplFindUs").addClass('current');
			break;
		case 'contact.php':
			$('tplContact').addClass('current');
			break;
		case 'news.php':
			$('tplNews').addClass('current');
			break;
		case 'sponsorship.php':
			$('tplSpons').addClass('current');
			break;
	}
	
	// then apply that knowledge so the accordion while open up the correct section
	accordion = new Accordion("div.mbdiv", "div.mbexpand", {opacity:true, show:curSection}, $('menubar'));
	
	// now let's find the current page and apply the 'current' style to it
	$$("div.mbexpand a").forEach(function(lnk) {
		lnk.removeClass("current");
	});
	
	var page = sPath.substr(sPath.indexOf('/', 1)+1);

	// on ajax
	if ($defined($("ajaxframe")))
	{
		$$('div.mbexpand a').each(function(el) {
			if (page == el.getProperty("title").replace(siteURL, "").split('/')[2]) {
				el.addClass('current');
			}
		});
	} else { // // on first load
		$$('div.mbexpand a').each(function(el) {
			if (page == el.href.replace(siteURL, "").split('/')[2]) {
				el.addClass('current');
			}
		});
	}
}

function LoadPage(el)
{
	$("content").innerHTML = "";
	$("ajaxframe").innerHTML = "";
	SetMenu(el.getProperty("title"));
	new Ajax(el.getProperty("title"), {method:"get", onComplete:DisplayPage}).request();
}

function DisplayPage(sHtml)
{
	$("ajaxframe").innerHTML = sHtml.replace("<div id=\"content\">", "<div id=\"ajaxcontent\">").replace("<div id=\"sidebar\">", "<div id=\"ajaxsidebar\">");
	$("content").innerHTML = $("ajaxcontent").innerHTML;
	$("sidebar").innerHTML = $("ajaxsidebar").innerHTML;
}


function RefreshFrame( pFrameId ) 
{
    var wController = 'http://stockpso.onlinestocksupply.com/cgi/en/vstock.search/';
    var wSearchString = $('SearchBox').value;
    var wClientNoString = "storge";
	window.open (wController + wSearchString + '-' + wClientNoString + '-br',
		"vstock","location=0,status=0,scrollbars=1,width=800,height=400"); 
}


