﻿var gMaxImagesToLookFor = 24;

window.onload = Main;

var gZone = '';											// found in query string
var gStoreName = '';
var gDirectory = '';
var gStoreNum = '';
var gDates = '';
var gCurrentPage = 1;
var gSection = null;

var gWeDate = new Date();
//var gInitializationComplete = false;

var gTotalPages = 0;
var gCurrentTestingPage = 0;
var gSettingsDisplayed = false;
var gTOCDisplayed = false;

gImgPageThumbs = new Array(gMaxImagesToLookFor);        // this doesn't need to be global if we're not using thumbs for display
gImgPages = new Array(gMaxImagesToLookFor);



function Main()
{
	if ($('hfChooseStore').value == 1)							// if we're supposed to choose a store
	{
		StartOver();
		$('divModalClose').style.display = 'none';
	}
	else
	{
		if (!TestAndParseQueryString())
			StartOver();
		else
			FinishInitialLoad();
		//TestAndParseQueryString
	}
}

// if something failed on initial load
function StartOver()
{
	$('divLoading').style.display = 'none';
	ToggleSettings();
}


// ----------------------------------------------------
// 		 Initial Setup
// ----------------------------------------------------

function TestAndParseQueryString()
{
	var noofgoodargs = 0;
	args = document.location.search.split('&');
	args[0] = args[0].replace('?', '');
	for (x = 0; x < args.length; x++)
	{
		keyvalue = args[x].split('=');
		
		switch (keyvalue[0])
		{
			case 'z':	keyvalue[1] = keyvalue[1].replace('%25', '');
						gZone = keyvalue[1];
						noofgoodargs++;
						break;

	        case 's':   keyvalue[1] = FixQueryStringPart(keyvalue[1]);
	                    // TODO: validate store name here?
	                    // if valid continue
	                    gStoreName = keyvalue[1];
	                    noofgoodargs++;
	                    break;

			case 'ad':	gDirectory = keyvalue[1];
						noofgoodargs++;
						break;
						
			case 'n':	gStoreNum = keyvalue[1];
						noofgoodargs++;
						break;
						
			case 'd':	gDates = FriendlyDate(FixQueryStringPart(keyvalue[1]))
						noofgoodargs++;
						break;

			case 'pg': // optional argument - don't increment noofgoodargs
						gCurrentPage = keyvalue[1];
						gViewType = 2; 				// default to single page view
						break;

			case 'vw': // optional argument - don't increment noofgoodargs
						gViewType = +keyvalue[1];	// may not be used in all media (i.e. iPhone)
						break;

			case 'section': gSection = keyvalue[1];
			
			default:	break; //bad argument handling
		}
	}

	if (noofgoodargs == 5)
		return true;
	else
		return false;
}

function FixQueryStringPart(inputString)
{
    for (counter = 1; counter <= 4; counter++)
        inputString = inputString.replace('%20', ' ').replace('+', ' ').replace('%2F', '/');
    return inputString;
    //return decodeURI(inputString);
}

function FriendlyDate(inputString)
{
	addate = new Date(inputString);
	dateStr = monthName(addate.getMonth()) + ' ' + addate.getDate() + suffix(addate.getDate()) + ' thru ';
	wedate = new Date(Date.parse(addate) + (6 * 24 * 60 * 60 * 1000) + (1 * 60 * 60 * 1000));
	gWeDate = wedate;
	if (wedate.getMonth() != addate.getMonth())
		dateStr += monthName(wedate.getMonth()) + ' ';
	dateStr += wedate.getDate() + suffix(wedate.getDate())
	return dateStr;
}



// ----------------------------------------------------
// 		 Display
// ----------------------------------------------------

function HighlightArrow(arrow)
{
	SetOpacity(arrow, 1);
}

function UnHighlightArrow(arrow)
{
	SetOpacity(arrow, .82);
}


// ----------------------------------------------------
// 		 Utility
// ----------------------------------------------------

//	sets the opacity on the specified object
function SetOpacity(object, value) {
	object.style.opacity = value;
	object.style.filter = 'alpha(opacity=' + value * 100 + ')'; 	// this would be for less standards-complient browsers..
}

var gFaderClearArray = new Array(); // only use this if only one thing fades at a time.
var gHideClear;

function Fader(objectID, duration, startingOpacity, endingOpacity, frequency, delay) {
	SetOpacity(document.getElementById(objectID), startingOpacity);
	var counter = 0;
	var differenceOpacity = endingOpacity - startingOpacity;
	while (frequency * counter < duration) {
		counter++;
		gFaderClearArray[counter - 1] = window.setTimeout('SetOpacity(document.getElementById("' + objectID + '"), "' + (startingOpacity + ((frequency * counter / duration) / (1 / differenceOpacity))) + '");', counter * frequency + delay);
	}
}

function StopCurrentFade() {
	for (var index = 0; index < gFaderClearArray.length; index++)
		window.clearTimeout(gFaderClearArray[index]);
	window.clearTimeout(gHideClear); // clearing the display:none at the end
}

function monthName(which)
{
	switch (which)
	{
		case 0:		return "January";
		case 1:		return "February";
		case 2:		return "March";
		case 3:		return "April";
		case 4:		return "May";
		case 5:		return "June";
		case 6:		return "July";
		case 7:		return "August";
		case 8:		return "September";
		case 9:		return "October";
		case 10:	return "November";
		case 11:	return "December";
	}
}

function suffix(which)
{
	if (((which > 4) && (which < 21)) || ((which > 23) && (which < 31)))
	{
		return "th";
	}
	else
	{
		which = which % 10;
	}
	switch (which)
	{
		case 1:
			return "st";
		case 2:
			return "nd";
		case 3:
			return "rd";
		default:
			return "th";
	}
}

function $(elementName)
{
	return document.getElementById(elementName);
}

function IsNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}

function InsertAfter(referenceNode, newNode)
{
	referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function mouseX(evt)
{
	if (!evt) evt = window.event;
	var x;
	if (evt.pageX) x = evt.pageX;
	else if (evt.clientX)
		x = evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	else x = 0;
	
	return x;
}
function mouseY(evt)
{
	if (!evt) evt = window.event;
	var y;
	if (evt.pageY) y = evt.pageY;
	else if (evt.clientY)
		y = evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	else y= 0;
	
	return y;
}

// checking the zip code length
function SubmitCheck()
{
	var textZip = $('txtZip');
	if (textZip.value.length != 5)
	{
		alert('Please enter a five digit zip code.');
		return false;
	}

	if($('divSpinner') != null) $('divSpinner').style.display = 'block';

	if ($('gvZip') != null) $('gvZip').style.display = 'none';
	if ($('spanChooseStoreMessage') != null) $('spanChooseStoreMessage').style.display = 'none';
}