﻿/// <reference path="./jquery.min.1.6.2.js" />
/* Iron.Routes.js
/********************************/
var curRoute;
var servicesHandler;
var caseStudiesHandler;
var newsHandler;
var contactsHandler;

function OnRouteChanged() {
	// create route, parse path
	if (curRoute === undefined) {
		curRoute = new Route();
	}

	// handler : what-we-do
	if (servicesHandler === undefined) {
		servicesHandler = new ServicesHandler("what-we-do");
		curRoute.addRouteHandler(servicesHandler);
	}

	// create case studies handler
	if (caseStudiesHandler === undefined) {
		caseStudiesHandler = new CaseStudiesHandler("case-studies");
		curRoute.addRouteHandler(caseStudiesHandler);
	}

	// create news handler
	if (newsHandler === undefined) {
		newsHandler = new NewsHandler("latest-news");
		curRoute.addRouteHandler(newsHandler);
	}

	// handler : contacts
	if (contactsHandler === undefined) {
		contactsHandler = new ContactsHandler("contact");
		curRoute.addRouteHandler(contactsHandler);
	}

	// xss prevention
	var raw_routeStr = window.location.href.replace(/</g, "&lt;").replace(/>/g, "&gt;");

	// parse route
	var routeStr = raw_routeStr.match('#') ? raw_routeStr.split('#').pop() : '';
	curRoute.parseRoute(routeStr);

	UpdateNavigation(curRoute.handler);

	if (curRoute.isNewHandler) {
		curRoute.resetActiveHandler();
	} else {
		curRoute.runRouteHandler(curRoute.view, curRoute.position, curRoute.command);
	}
	
}

// Clears 'active' class from all navigation items.
// Finds element with href matching formation activeItem value and
// adds 'active' class.
function UpdateNavigation(activeItem) {
	$('.navigation-buttons a').each(function () {
		$(this).removeClass('active');
	});

	var activeItemLink = "#/" + activeItem + "/";
	$('.navigation-buttons a[href="' + activeItemLink + '"]').addClass('active');
}

function UpdatePosition(view, duration, callback) {
	var offsetHeight = $('#navigation').height() - 5,
		top = $(view).offset().top - offsetHeight,
		$bodyOrHtml = $($.browser.webkit ? 'body' : 'html');
	
	// console.log('UpdatePosition', top, $bodyOrHtml.scrollTop(), top === $bodyOrHtml.scrollTop() ? 'immediately' : duration, callback ? callback.name : callback);
	// console.markTimeline('UpdatePosition ' + (callback && callback.name));
	
	// skip animation if there is no difference
	if (top === $bodyOrHtml.scrollTop()) {
		if (typeof callback === 'function') {
			callback.call(this);
		}
		return;
	}
	
	// otherwise animate

	$bodyOrHtml.animate({ scrollTop: top }, {
		duration: duration,
		easing: "easeInOutExpo",
		complete: function () {
			// console.log('UpdatePosition: complete', callback && callback.name);
			// console.markTimeline('UpdatePosition: complete ' + (callback && callback.name));
			if (typeof callback == 'function') {
				callback.call(this);
			}
		}
	});

}

function UpdateInnerPosition(view, duration, callback) {
	var offsetHeight = $('#navigation').height() + 65;

	if ($.browser.webkit) {
		$('body').animate({ scrollTop: $(view).offset().top - offsetHeight },
		{
			duration: duration,
			easing: "easeInOutExpo",
			complete: function () {
				if (typeof callback == 'function') {
					callback.call(this);
				}
			}
		});
	} else {
		$('html').animate({ scrollTop: $(view).offset().top - offsetHeight },
		{
			duration: duration,
			easing: "easeInOutExpo",
			complete: function () {
				if (typeof callback == 'function') {
					callback.call(this);
				}
			}
		});
	}
}

function UpdateCustomPosition(view, duration, offset, callback) {
	var offsetHeight = offset;

	if ($.browser.webkit) {
		$('body').animate({ scrollTop: $(view).offset().top - offsetHeight },
		{
			duration: duration,
			easing: "easeInOutExpo",
			complete: function () {
				if (typeof callback == 'function') {
					callback.call(this);
				}
			}
		});
	} else {
		$('html').animate({ scrollTop: $(view).offset().top - offsetHeight },
		{
			duration: duration,
			easing: "easeInOutExpo",
			complete: function () {
				if (typeof callback == 'function') {
					callback.call(this);
				}
			}
		});
	}
}


/* Route									*/
/********************************************/
function Route() {
	this.handler = '';
	this.view = '';
	this.position = '';
	this.command = '';
	this.isNewHandler = false;
	this.routeHandlers = [];
}

Route.prototype = {
	parseRoute: function (routeStr) {
		if (routeStr != '') {
			var paths = routeStr.split('/');

			if (paths[1] == '') paths[1] = 'what-we-do';

			this.isNewHandler = false;
			if (this.handler != paths[1] && this.handler != '') this.isNewHandler = true;

			this.handler = paths[1];
			this.view = paths[2] ? paths[2] : '';
			this.position = paths[3] ? paths[3] : '';
			this.command = paths[4] ? paths[4] : '';
		}
	},
	resetActiveHandler: function () {
		if (this.routeHandlers.length > 0) {
			for (var index = 0; index < this.routeHandlers.length; index++) {
				if (this.routeHandlers[index].isActive()) {
					this.routeHandlers[index].reset();
					break;
				}
			}
		}
	},
	addRouteHandler: function (handler) {
		if (handler instanceof RouteHandler) {
			this.routeHandlers.push(handler);
		}
	},
	runRouteHandler: function () {
		if (this.routeHandlers.length > 0) {
			for (var index = 0; index < this.routeHandlers.length; index++) {
				if (this.routeHandlers[index].route == this.handler) {
					this.routeHandlers[index].runRoute.apply(this, arguments);
				}
			}
		}
	}
}

function RouteHandler(route) {
	this.route = route;
	return this;
}

RouteHandler.prototype = {
	isActive: function () { throw new Error(this.route + ":isActive function must be implemented."); },
	runRoute: function (a, b, c) { throw new Error(this.route + ":runRoute function must be implemented."); },
	reset: function () { throw new Error(this.route + ":reset function must be implemented."); }
}

/* ServicesHandler */
function ServicesHandler(route) {
	RouteHandler.apply(this, arguments);
	return this;
}

ServicesHandler.prototype = new RouteHandler();
ServicesHandler.prototype.isActive = function () {
	return $('.services-contents').hasClass('active');
}
ServicesHandler.prototype.runRoute = function (view, position, command) {
	switch (view) {
		case "new-brand":
			serviceInfoToLoad = view;
			serviceInfoData = newBrandServiceInfo;
			break;
		case "extend-brand":
			serviceInfoToLoad = view;
			serviceInfoData = extendBrandServiceInfo;
			break;
		case "non-profit-brand":
			serviceInfoToLoad = view;
			serviceInfoData = nonProfitBrandServiceInfo;
			break;
		default:
			serviceInfoToLoad = 'services';
			serviceInfoData = '';
			break;
	}

	closeServiceInfo();
}
ServicesHandler.prototype.reset = function () {
	$('.services-contents').animate({ 'height': 0 },
					{
						duration: 1000,
						easing: 'easeInOutExpo',
						complete: function () {
							$(this).removeClass('active');
							$(this).empty();
							UpdateServiceOverlay('');
							curRoute.runRouteHandler(curRoute.view, curRoute.position, curRoute.command);
						}
					});

}


/* CaseStudiesHandler */
function CaseStudiesHandler(route) {
	RouteHandler.apply(this, arguments);
	return this;
}

CaseStudiesHandler.prototype = new RouteHandler();
CaseStudiesHandler.prototype.isActive = function () {
	return $('#case-studies').hasClass('active');
}
CaseStudiesHandler.prototype.runRoute = function (view, position, command) {
	caseStudyToLoad = view + '-case-study';
	switch (view) {
		case "patelco":
			caseStudyUrl = 'Patelco.html';
			break;
		case 'purcell-murray':
			caseStudyUrl = 'PurcellMurray.html';
			break;
		case 'dockers':
			caseStudyUrl = 'Dockers.html';
			break;
		case 'levis':
			caseStudyUrl = 'Levis.html';
			break;
		case 'deyoung':
			caseStudyUrl = 'deYoung.html';
			break;
		case 'springboard-forward':
			caseStudyUrl = 'SpringboardForward.html';
			break;
		default:
			caseStudyToLoad = 'categories';
			caseStudyUrl = '';
			break;
	}

	closeCaseStudy();
}

CaseStudiesHandler.prototype.reset = function () {
	resetCaseStudy();
}

/* NewsHandler */
function NewsHandler(route) {
	RouteHandler.apply(this, arguments);
	return this;
}

NewsHandler.prototype = new RouteHandler();
NewsHandler.prototype.isActive = function () {
	return $('#latest-news').hasClass('active');
}
NewsHandler.prototype.runRoute = function (view, position, command) {
	switch (view) {
		case "list":
			newsContentToLoad = 'list';
			newsContentData = '';
			break;
		case "category":
			newsContentToLoad = 'category-list';
			newsContentData = position;
			break;
		case "post":
			newsContentToLoad = 'news-article';
			newsContentData = position;
			break;
		default:
			newsContentToLoad = 'featured';
			newsContentData = '';
			break;
	}

	if (!loadingNews) {
		if (newsContentToLoad == 'featured' && currentNewsContent == 'featured' && !$('#latest-news').hasClass('active')) {
			$('#latest-news').addClass('active');
			UpdatePosition('#latest-news', 1000);
		} else {
			closeLatestNews();
		}
	}
}
NewsHandler.prototype.reset = function () {
	if (currentNewsContent != 'featured') {
		newsContentToLoad = 'reset';
		closeLatestNews();
	} else {
		$('#latest-news').removeClass('active');
		curRoute.runRouteHandler(curRoute.view, curRoute.position, curRoute.command);
	}
}

/* ContactsHandler */
function ContactsHandler(route) {
	RouteHandler.apply(this, arguments);
	return this;
}

ContactsHandler.prototype = new RouteHandler();
ContactsHandler.prototype.isActive = function () {
	return $('#contacts').hasClass('active');
}
ContactsHandler.prototype.runRoute = function (view, position, command) {
	if (view == '') {
		UpdatePosition('#contacts', 1000);
		$('#contacts').addClass('active');
	}
}
ContactsHandler.prototype.reset = function () {
	$('#contacts').removeClass('active');
	curRoute.runRouteHandler(curRoute.view, curRoute.position, curRoute.command);
}
