// JavaScript Document

YAHOO.namespace('mix');

YAHOO.mix.site = function() {

	var _public = {};
	_public.ABOUT = 'about';
	_public.SERVICES = 'services';
	_public.SERVICES_COMMUNICATIONS = 'services-communications';
	_public.SERVICES_TECHNOLOGY = 'services-technology';
	_public.SERVICES_EVENTS = 'services-events';
	_public.NEWS = 'news';
	_public.BLOG = 'blog';
	_public.TEAM = 'team';
	_public.CONTACT = 'contact';
	_public.TOOLS = 'tools';
	_public.CLIENT = 'client';
	_public.MORE_NEWS = 'more-news';
	
	var IFRAME_ID = 'myFrame';
	var SWF_ID = 'flash';
	var D = YAHOO.util.Dom;
	var E = YAHOO.util.Event;
	var _myFrame = null;
	var _mySwf = null;
	var _currentPage = null;
	var _pagePoll = null;

	var _init = function() {

		_myFrame = D.get(IFRAME_ID);

		E.on(window, 'resize', _arrange);
		_arrange();

		// check for page request
		var currentLocation = document.location.hash.substr(1).toLowerCase();

		// is valid page?
		if (currentLocation === _public.ABOUT ||
			currentLocation === _public.CLIENT ||
			currentLocation === _public.MORE_NEWS ||
			currentLocation === _public.TEAM ||
			currentLocation === _public.TOOLS ||
			currentLocation === _public.CONTACT ||
			currentLocation === _public.NEWS ||
			currentLocation === _public.SERVICES ||
			currentLocation === _public.SERVICES_COMMUNICATIONS ||
			currentLocation === _public.SERVICES_TECHNOLOGY ||
			currentLocation === _public.SERVICES_EVENTS ||
			currentLocation === _public.BLOG) {

			// show page
			_setPage(currentLocation);

		} else {

			// show default page
			_setPage(_public.ABOUT);

		}
		
	};

	var _arrange = function(e) {

		// update iframe height (required for ie)
		if (YAHOO.env.ua.ie > 0) {
			var iframeHeight = D.getViewportHeight() - (100 + 120); // window height - (top border + nav + bot border)
			D.setStyle(_myFrame, 'height', iframeHeight);
		}

	};

	var _startPagePoll = function() {
		//console.log('_startPagePoll');
		_pagePoll = setInterval(_checkPageLocation, 800);
	};

	var _stopPagePoll = function() {
		//console.log('_stopPagePoll');
		clearTimeout(_pagePoll);
		_pagePoll = null;
	};

	var _checkPageLocation = function() {

		var currentLocation = document.location.hash.substr(1).toLowerCase();
		//console.log('_checkPageLocation', currentLocation, _currentPage);
		
		if (currentLocation !== _currentPage) {
			_setPage(currentLocation);
		}

	};

	var _setPage = function(page) {
		//console.log('_setPage');
	
		// if no page, exit
		if (page === undefined) return;

		_stopPagePoll();
		_currentPage = page;

		// update browser address and title
		document.location.hash = _currentPage;
		document.title = _getPageTitle(_currentPage);
		
		// update iframe page
		if (_myFrame !== null) {
			_myFrame.src = _getPageUrl(_currentPage);
		}

		_startPagePoll();
		
		// update swf
		_mySwf = D.get(SWF_ID);

		if (_mySwf) {
			_mySwf.setCurrentPage(_currentPage);
		}

	};

	var _getPageTitle = function(page) {

		var pageTitle = 'theMIX agency';

		switch (page) {

			case _public.ABOUT:
				pageTitle += ' - About us';
				break;

			case _public.TEAM:
				pageTitle += ' - Team';
				break;
				
			case _public.TOOLS:
				pageTitle += ' - Tools';
				break;

			case _public.CONTACT:
				pageTitle += ' - Contact us';
				break;

			case _public.NEWS:
				pageTitle += ' - Client news';
				break;

			case _public.BLOG:
				pageTitle += ' - Our blog';
				break;

			case _public.SERVICES:
			case _public.SERVICES_COMMUNICATIONS:
			case _public.SERVICES_TECHNOLOGY:
			case _public.SERVICES_EVENTS:
				pageTitle += ' - Services';
				break;
				
			case _public.CLIENT:
				pageTitle += ' - Client';
				break;
				
			case _public.MORE_NEWS:
				pageTitle += ' - More news';
				break;

		}

		return pageTitle;

	};

	var _getPageUrl = function(page) {

		var pageUrl = '';

		switch (page) {

			case _public.ABOUT:
				pageUrl = 'aboutus.php';
				break;

			case _public.CONTACT:
				pageUrl = 'contact.php';
				break;
				
			case _public.TOOLS:
				pageUrl = 'tools.php';
				break;

			case _public.TEAM:
				pageUrl = 'team.php';
				break;

			case _public.NEWS:
				pageUrl = 'tma/client-news';
				break;

			case _public.BLOG:
				pageUrl = 'tma/blink';
				break;

			case _public.SERVICES:
				pageUrl = 'services.php';
				break;
				
			case _public.SERVICES_COMMUNICATIONS:
				pageUrl = 'services.php?countrytabs=0';
				break;
				
			case _public.SERVICES_TECHNOLOGY:
				pageUrl = 'services.php?countrytabs=1';
				break;
				
			case _public.SERVICES_EVENTS:
				pageUrl = 'services.php?countrytabs=2';
				break;
				
			case _public.CLIENT:
				pageUrl = 'client.php';
				break;
				
			case _public.MORE_NEWS:
				pageUrl = 'morenews.php';
				break;

		}

		return pageUrl;

	};

	YAHOO.util.Event.onDOMReady(_init); 
	_public.setPage = _setPage;
	return _public;

}();

