﻿function setupControlLinks(activePanel) {
	if (activePanel == 'map') {
		jQuery('.ContentToolBar .mapLink').hide();
		jQuery('.ContentToolBar .mapLinkSelected').show();
		jQuery('.ContentToolBar .slideShowLink').show();
		jQuery('.ContentToolBar .slideShowLinkSelected').hide();
		jQuery('#PageContent .ContentToolBar .hideButton').show();
		jQuery('#PageContent .ContentToolBar .showButton').hide();
	} else if (activePanel == 'images') {
		jQuery('.ContentToolBar .mapLink').show();
		jQuery('.ContentToolBar .mapLinkSelected').hide();
		jQuery('.ContentToolBar .slideShowLink').hide();
		jQuery('.ContentToolBar .slideShowLinkSelected').show();
		jQuery('#PageContent .ContentToolBar .hideButton').show();
		jQuery('#PageContent .ContentToolBar .showButton').hide();
	} else {
		jQuery('#PageContent .ContentToolBar .mapLinkSelected').hide();
		jQuery('#PageContent .ContentToolBar .mapLink').show();
		jQuery('#PageContent .ContentToolBar .slideShowLink').show();
		jQuery('#PageContent .ContentToolBar .slideShowLinkSelected').hide();
		jQuery('#PageContent .ContentToolBar .hideButton').hide();
		jQuery('#PageContent .ContentToolBar .showButton').show();
	}
}

function showToolbar(activePanel, scrollToView) {
	if (activePanel == null) {
		activePanel = getFazerToolbarActivePanel();
	}

	if (jQuery('#PageContent .Slideshow').is(':hidden') || jQuery('#PageContent .Slideshow').css('position') == 'absolute') {
		jQuery("#PageContent .Slideshow").css({ "position": "static" });
		jQuery('#PageContent .Slideshow').hide();
		jQuery('#PageContent .Slideshow').slideDown();
	} else {
		jQuery('#PageContent .Slideshow').show();
	}

	setupControlLinks(activePanel);

	if (activePanel == 'map') {
		jQuery('#ImageSlideshow').hide();
		jQuery('#ProjectMap').show();
		jQuery('#ImageSlideshow .controls .stopButton').click();

		Fazer.Base.MapInstance.loadMap();
		Fazer.Base.MapInstance.checkResize();
	} else if(activePanel == 'images') {
		jQuery('#ImageSlideshow').show();
		jQuery('#ProjectMap').hide();

		jQuery('#ImageSlideshow .controls .startButton').click();
		jQuery('#ImageSlideshow .controls').show(400);
		jQuery('#ImageSlideshow').css({ "position": "relative" });
		jQuery('#ImageSlideshow').css({ "top": "0" });
	}	else {
		jQuery('#ImageSlideshow').hide();
		jQuery('#ProjectMap').hide();
	}

	if (scrollToView) {
		jQuery(window).scrollTop(155);
	}

	setFazerToolbarActivePanel(activePanel);
	setFazerToolbarVisible(true);
}

function hideToolbar(instant) {
	jQuery('#ImageSlideshow .controls .stopButton').click();

	if (instant) {
		jQuery('#PageContent .Slideshow').hide();
	} else {
		jQuery('#PageContent .Slideshow').slideUp();
	}

	setupControlLinks();

	setFazerToolbarVisible(false);
}

var _contentToolBar_initialView = '';

function setInitialView(initialView) {
	_contentToolBar_initialView = initialView;
}

function setupToolbar() {
	var activePanel = getFazerToolbarActivePanel();
	if (getFazerToolbarSelected() == false) {
		activePanel = _contentToolBar_initialView;
	}
	if (!getFazerToolbarVisible() || !isStartPage()) {
		hideToolbar(true);
	} else {
		showToolbar(activePanel);
	}
}

function setFazerToolbarActivePanel(panel) {
	setFazerCookieValue('__fazer_activePanel', panel);
	_contentToolBar_initialView = panel;
}

function getFazerToolbarActivePanel() {
	if (isStartPage()) {
		var panel = getFazerCookieValue('__fazer_activePanel');
		if (panel != null) {
			return panel;
		}
		return 'images';
	}
	else {
		return _contentToolBar_initialView;
	}
}

function getFazerToolbarSelected() {
	var panel = getFazerCookieValue('__fazer_activePanel');
	if (panel != null) {
		return true;
	}
	return false;
}

function setFazerToolbarVisible(show) {
	setFazerCookieValue('__fazer_toolbarVisible', show);
}

function getFazerToolbarVisible() {
	var visible = getFazerCookieValue('__fazer_toolbarVisible');
	if (visible == 'false') {
		return false;
	}
	return true;
}

function setFazerCookieValue(name, value) {
	if (isStartPage()) {
		jQuery.cookie(name, value, { expires: 7, path: '/', domain: location.host, secure: false });
	}
}

function getFazerCookieValue(name) {
	if (isStartPage()) {
		return jQuery.cookie(name);
	}
}

function isStartPage() {
	return ContentToolBarData[0].useCookie;
}
