/**
 * clearFusionCMS script to manage sidebar position.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2015 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearFusionCMS.com
 */

$fl(document).ready(function() {
	var sidebar = $fl('#sidebar'),
		sidebarContent = $fl('#sidebar .content'),
		sidebarTree = $fl('#sidebar .treeviewwidgetContainer'),
		sidebarHeader = $fl('#sidebar .header'),
		sideContainer = $fl('#sideContainer'),
		sidebarCard = $fl('#sidebar .card'),
		appbar = $fl('.appbar'),
		submitList = $fl('#submit-list');
		win = $fl(window);

	function updateSidebar() {
		try {
			if(win.width() > 767) {

				var appbarHgt = appbar.height() +
						parseInt(appbar.css('padding-top')) +
						parseInt(appbar.css('padding-bottom')),
					toolbarHgt,
					submitListHgt,
					offset = Math.max(appbarHgt - sidebarCard[0].offsetTop, sideContainer[0].offsetTop - win.scrollTop());

				if(sidebarHeader.length) {
					toolbarHgt = sidebarHeader.height() +
							parseInt(sidebarHeader.css('padding-top')) +
							parseInt(sidebarHeader.css('padding-bottom'));
				}
				else
					toolbarHgt = 0;

				if(submitList.length && submitList.css('position') == 'fixed') {
					submitListHgt = submitList.height() +
						parseInt(submitList.css('padding-top')) +
						parseInt(submitList.css('padding-bottom'));
				}
				else
					submitListHgt = 0;

				// Set position of sidebar
				sidebar
					.addClass('sidebarFixed')
					.css('left', sideContainer[0].offsetLeft + parseInt(sideContainer.css('padding-left')) + 'px')
					.css('width', sideContainer.width() + 'px')
					.css('top', offset + 'px');

				sidebarContent.css('height', (win.height() - toolbarHgt - submitListHgt - offset - sidebarCard[0].offsetTop) + 'px');
				sidebarTree.css('height', (win.height() - toolbarHgt - submitListHgt - offset - sidebarCard[0].offsetTop) + 'px');
			}
			else {
				sidebar
					.removeClass('sidebarFixed')
					.css('top', 'inherit')
					.css('left', 'auto')
					.css('width', 'auto');
				sidebarContent.css('height', 'auto');
				sidebarTree.css('height', 'auto');
			}
		} catch(e) {
		}
	}

	win.bind('scroll', updateSidebar)
		.bind('resize', updateSidebar)
		.bind('load', updateSidebar);
	updateSidebar();

	// Main menu
	$fl('#mainMenuLevel2 ul').attr('aria-hidden', 'true');

	$fl('#mainMenu li').bind('click', function(e) {
		var mnuName = $fl(this).attr('data-mainmenu');

		if(!mnuName) {
			$fl('#mainMenuLevel2').removeClass('active');
			$fl('#mainMenuOverlay').removeClass('active');
			$fl('#mainMenu li').removeClass('active');
		}
		else {
			var mnu = $fl('#' + mnuName);

			if($fl('#mainMenuLevel2').hasClass('active') && mnu.hasClass('active')) {
				$fl('#mainMenuLevel2').removeClass('active');
				$fl('#mainMenuOverlay').removeClass('active');
				$fl(this).removeClass('active');
			}
			else {
				$fl('#mainMenuOverlay').addClass('active');
				$fl('#mainMenuLevel2').addClass('active');
				$fl('#mainMenuLevel2 ul').removeClass('active').attr('aria-hidden', 'true');
				mnu.addClass('active').attr('aria-hidden', 'false');
				$fl('#mainMenu li').removeClass('active');
				$fl(this).addClass('active');
			}
		}
	});

	$fl('#mainMenuOverlay').bind('click', function() {
		$fl('#mainMenuLevel2').removeClass('active');
		$fl('#mainMenu li').removeClass('active');
		$fl('#mainMenuOverlay').removeClass('active');

		$fl('#mainMenu').removeClass('exposed');
		$fl('.page-wrapper').removeClass('exposed');
		$fl('#mainMenuExpose').attr('data-menustate', false);
	});

	// Expose button
	$fl('#mainMenuExpose').bind('click', function() {
		if($fl(this).attr('data-menustate')) {
			$fl(this).attr('data-menustate', false);
			$fl('#mainMenu').removeClass('exposed');
			$fl('.page-wrapper').removeClass('exposed');
		}
		else {
			$fl(this).attr('data-menustate', true);
			$fl('#mainMenu').addClass('exposed');
			$fl('.page-wrapper').addClass('exposed');
			$fl('#mainMenuOverlay').addClass('active');
		}

		setTimeout(updateSidebar, 180);
	});

	// Tooltips
	$fl('#mainMenu li').each(function() {
		$fl(this).attr('data-menutitle', $fl(this).attr('title'));
		$fl(this).attr('title', '');
	});

	$fl('#mainMenu li').bind('mouseenter', function() {
		if($fl(window).width() > 767) {
			$fl('#mainMenuTip')
					.css('display', 'block')
					.css('top', $fl(this).offset().top + 'px')
					.html($fl(this).attr('data-menutitle'));
		}
	}).bind('mouseleave', function() {
		$fl('#mainMenuTip').css('display', 'none');
	});

	// Add working indicator to form submit
	$fl('form').bind('submit', function(e) {
		$fl('#submit-list input').addClass('disabled');
		$fl('#submit-list button').addClass('disabled');
		$fl.toastHide();
		$fl('.alertfade').addClass('hide');
	}).bind('formValidationFailed', function(e) {
		$fl('#submit-list input').removeClass('disabled');
		$fl('#submit-list button').removeClass('disabled');
	});

	// Update the notices areas
	var msgCount = 0;
	if(typeof managerPath !== 'undefined') {
		var source = null;

		function updateNotices(d) {
			if (d.hasNotices) {
				msgCount = d.noticeCount;
				$fl('#notifyCount').removeClass('hide').html(d.noticeCount);

				var html = '';
				for(var i=0;i< d.notices.length;i++) {
					html += '<li><div class="msg">';

					if(d.notices[i].type == 1) {
						html += '<i class="material-icons info">info_outline</i>';
					}
					else if(d.notices[i].type == 2) {
						html += '<i class="material-icons warning">warning</i>';
					}
					else if(d.notices[i].type == 3) {
						html += '<i class="material-icons error">error_outline</i>';
					}

					if(d.notices[i].count !== null)
						html += '<b>' + d.notices[i].count + '</b>';

					html += d.notices[i].text;

					if(d.notices[i].link !== null) {
						html = '<a href="' + d.notices[i].link + '">' + html + '</a>';
					}

					html += '</div></li>';
				}
				$fl('#cfNotifications').html(html);
			}
			else {
				msgCount = 0;
				$fl('#notifyCount').addClass('hide');
				$fl('#cfNotifications').html('');
			}
		}

		function getNotices() {
			// If SSE object then close it as it's not responded
			if(source) {
				source.close();
				source = null;
			}

			$fl.get(managerPath + 'getnotices/', updateNotices);
		}

		// Setup interval for notice updates and force a load
		var noticeTimer = setInterval(getNotices, 5000);
		getNotices();

		// Open Server-Sent Event connection
		if(window.EventSource) {
			source = new EventSource(managerPath + 'getnotices/');
			source.addEventListener('message', function(e) {
				// Kill the notice update timer as SSE working
				if(noticeTimer) {
					clearInterval(noticeTimer);
					noticeTimer = false;
				}

				// Update the user
				updateNotices(JSON.parse(e.data));
			});
		}
	}

	// Alert menu display handler
	$fl('.notify-icon .fa-bell').bind('click', function(e) {
		if($fl('#cfNotifications').hasClass('closed') && msgCount) {
			$fl('#cfNotifications').removeClass('closed').attr('aria-hidden', 'false');
		}
		else {
			$fl('#cfNotifications').addClass('closed').attr('aria-hidden', 'true');
		}
		e.preventDefault();
		return false;
	});

	$fl('body').bind('click', function(e) {
		if(!$fl(e.target).parent().hasClass('notify-icon')) {
			$fl('#cfNotifications').addClass('closed').attr('aria-hidden', 'true');
		}
	});
});
