﻿///<reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5-vsdoc.js" />
///<reference path="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js" />
///<reference path="/debugversions/browserdetect.js" />
///<reference path="/debugversions/underscore.js" />
///<reference path="/debugversions/jquery.ba-hashchange.js" />
///<reference path="/debugversions/parseUri.js" />
///<reference path="/Scripts/BondiTouch.js" />
///<reference path="/Scripts/BondiTouch.Carousel.Objects.js" />
///<reference path="/Scripts/BondiTouch.Workflow.js" />
///<reference path="/Scripts/BondiTouch.Utility.js" />
///<reference path="/Scripts/BondiTouch.Core.js" />

//TODO: This file seems like an odds and ends grab bag of functions that could probably be elsewhere...
//TODO: Refactor to reduce/eliminate dependencies on other modules

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core = {
	Initializer: {},
	jumpLinks: {},
	pageListing: (_b.Carousel) ? new _b.Carousel.PageListing() : null
};

// Use cache: setting this to false disables both saving and loading to the cache, 
// for purposes of testing raw startup time.
_b.Core.useCache = true; 

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.constants = {
	///<summary>Constants should be changed here directly in the javascript file.</summary>
	imageContainer: "#filmStrip",
	thumbnailContainer: "#ftrItemWrapper",
	spreadMode: "s",	// AKA landscape
	pageMode: "p",		// AKA portrait
	modeRatio: .74,
	pageMargin: 5,
	carouselEdgeLeft: -3000,	// how far left (px) the carousel's DOM elements go
	carouselEdgeRight: 4000,	// how far right (px) the carousel's DOM elements go
	imageryEdgeLeft: -1,		// how far left (# spreads) non-thumb-res imagery loads
	imageryEdgeRight: 1,		// how far right (# spreads) non-thumb-res imagery loads
	tapRegion: 100,
	issueSubCache: "issue",
	thumbnailHeight: 90,
	browserTag: "-webkit-",
	appVersion: "1.3.0",
	version: "2.0.0.5854"
};

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.navState = {
	///<summary>The navState object contains values set and checked during navigation</summary>
	mode: "init",
	windowW: 0,
	windowCtrX: 0,
	windowH: 0,
	currentOffset: 0,
	currentIndex: 0,
	page: undefined,
	navWidth: 0,
	issueKey: 0,
	itemsInTransition: 0,
	translateStart: "",
	translateEnd: "",
	vendorSpec: "webkit",
	backstopTimerID: -1,
	hash: "",
	thumbnailLoadSeq: 0,
	thumbOffset: 0,
	headerVisible: false,
	footerVisible: false,
};

/*== Underscore.js settings ________________________________________________________________________________________________________________________________________________________________*/
//TODO: Replace with jquery.tmpl
_.templateSettings = {
	///<summary>Change the delimiter for Underscore's template engine from <%=foo%> to {{foo}} to avoid confusion with asp.net
	///<para>NOTE: example _.template usage: _.template("the answer to {{question}} is {{answer}}", { question: "the Ultimate Question of Life, etc...", answer: 42 });</para></summary>
	start: '{{',
	end: '}}',
	interpolate: /\{\{(.+?)\}\}/g
};

/*add isInteger to Underscore, as required by the TOC _______________________________________________________________________________________________________________________________________*/
_.mixin({
	isInteger: function (value) {
		///<summary>Determines if a given object can be parsed as an integer</summary>
		///<returns>Returns true if integery, false if not</returns>
		return !isNaN(parseInt(String(value), 10));
	}
});

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.Initializer.beforeDomReady = function () {
	///<summary>Sets values and performs startup tasks that can't wait for DOMReady.</summary>
	_b.Toc.init();
	_b.IssueListing.init();

	$(".closeButton").click(function () {
		_b.Tasks.hideOverlays();
		_b.Tasks.displayFooter();
	});
};

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.Initializer.startup = function () {
	///<summary>Sets values and performs startup tasks.</summary>
	this.setCompatibility();

	this.loginOnReady();

	_b.Events.init();
	_b.Workflow.executeIssueLoad();

	loadTestScript();
};

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.Initializer.setCompatibility = function () {
	///<summary>Set touch and webkit compatibility values.</summary>
	var brwsr = BrowserDetect.browser;
	var brwsrVer = BrowserDetect.dataOS;
	var os = BrowserDetect.OS;
	var use3dTransform = true;

	//TODO: Use best of breed browser detection logic from iScroll, Modernizr, etc...

	// If we're on iPad or iPhone use 3d, all else use 2d
	if (brwsr === "Safari" && os === "Windows") {
		use3dTransform = false;
	};
	if (brwsr === "Firefox") {
		use3dTransform = false;
	};
	// Translate3d helper
	_b.Core.navState.translateStart = "translate" + (use3dTransform ? "3d(" : "(");
	_b.Core.navState.translateEnd = use3dTransform ? ",0)" : ")";
	_b.Core.navState.browserTag = "-webkit-"; // use3dTransform ? "-webkit-" : "-moz-";

	// touch css
	if (BrowserDetect.useTouch)
		$("html").addClass("useTouch");
};

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.Initializer.loginOnReady = function() {

};


/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.getThumbImg$ = function (pageName) {
	return $("#pft" + pageName);
};

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.navigateToLink = function (link) {
	/*link is typically of the format /20030201/#!/123 but we should be forgiving with the number of /'s and whether or not ! is present, etc. 
	acceptable input - let's try to match as many of these as is practical for now, add rest to TODO:
	issue and page
	/20060501?foo=bar&yadi=yada/#!/C1 - works, but does not keep query string - no use case?
	/20060501/#!/C1 - works
	/20060501#!/C1 - works
	/20060501#!C1 - works
	issue only
	/20060501/#!/ - Works
	/20060501/# - Works
	/20060501/ - Works
	/20060501 - Works
	20060501 - Works
	page only - MUST be prefixed with #
	#!/C1 - Works
	#!C1 - Works
	#/C1 - Works
	#C1 - Works
	*/
	//split the link at #
	//anything to the right is a page
	//trim ! if needed
	//look for last /fragment/ - this is the issue
	var linkItems = parseUri(link), page = "", paths = [], issue = "";
	//issue: get the last element of the path
	issue = _(linkItems["path"]).trim("/").split("/").pop();
	issue = issue || _b.Core.navState.issueKey;
	page = _(linkItems["anchor"]).trim("!/ ");
	_b.Core.navigateTo(issue, page);
};

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.navigateTo = function (issue, page) {
	//debugger;
	page = page ? "#!/" + page : "";
	//TODO: Add page tracking/analytics/etc here as needed
	if (page && issue == _b.Core.navState.issueKey) {
		//exit zoom
		_b.Events.zoomOut();
		//then navigate
		window.location.hash = page;
	} else if (_.isInteger(issue)) {
		window.location.href = _b.Uri.virtualRoot + issue + "/" + page;
	} else {
		//TODO: do nothing? go to home page? 
	}
	return;
};

/*______________________________________________________________________________________________________________________________________________________________________________________*/
_b.Core.navigateToPage = function (event) {
	event.stopPropagation();
	_b.Core.navigateTo(_b.Core.navState.issueKey, this.title);
};
