// -- PAGE INIT
//Functions to be performed after the page has loaded
$(document).ready(function(){
	//init
	//init();
	//start event checker
	//startEventChecker();
	
	//Enable top bar navigation
	enableTopbarNavigation();
	//Enable search field
	enableSearchField();
	//Enable more functions
	enableMoreFunctions();
	//Enable left menu
	enableLeftMenu();
	//Enable fancybox
	enableFancybox();
	//Enable calendar image slider
	enableCalendarSlider();

	if (isOnline)
	{
		init_check_events();
		//Set new messages etc
//		setNewMessages();
	}
	

	//IE fixes
});
// -- /PAGE INIT


//Enable top bar navigation
function enableTopbarNavigation() {
	$('#topbarNavigation li').each(function(index) {

		//Create captions and links
		var sUrl = $(this).find('a').attr('href');
		$(this).append('<span class="topbar-caption" style="display: none;"><a href="' + sUrl + '">' + $(this).text() + '</a></span>');
		
		//Show active caption
		if ($(this).hasClass('on')) {
			$(this).find('.topbar-caption').show();
		}
		
		//Enable mouseover and mouseout for inactive captions
		if ($(this).hasClass('on') == false) {
			$(this).find('a').bind('mouseover focus', function() {
				$(this).parent().find('.topbar-caption').slideDown('fast');
			});
			$(this).find('a').bind('mouseout blur', function() {
				$(this).parent().find('.topbar-caption').slideUp('fast');
			});
		}
		
	});
}

//Set new messages etc
function setNewMessages() {
	for (var i=0;i<aNewItems_identifier.length;i++) {
		if (aNewItems_counter[i] > 0) {
			$('#' + aNewItems_identifier[i]).html(aNewItems_counter[i]);
			$('#' + aNewItems_identifier[i]).show();
		}
		else {
			$('#' + aNewItems_identifier[i]).hide();
		}
	}
}

//Enable search field
function enableSearchField() {
	//Get defaults
	var iAddLength = 184;
	var iDefWidth = $('#topbarSearch').width();
	var iFieldDefWidth = $('.topbar-search-input').width();
	var iDefText = $('.topbar-search-input').val();
	
	//Bind functions for focus and blur
	$('.topbar-search-input').bind('focus', function() {
		$('#topbarSearch').addClass('on');
		$(this).css('font-size','160%');
		$(this).val('');
		$('.topbar-search-input').width(iFieldDefWidth + iAddLength);
		$('.topbar-search-inner').width(iDefWidth + iAddLength);
		$('#topbarSearch').stop(true, true).animate({ width: iDefWidth + iAddLength }, 500 );
	});
	$('.topbar-search-input').bind('blur', function() {
		$('.topbar-search-input').width(iFieldDefWidth);
		$('.topbar-search-inner').width(iDefWidth);
		$('#topbarSearch').stop(true, true).animate({ width: iDefWidth }, 500 );
		if ($(this).val() == '') {
			$(this).val(iDefText);
		}
		$('#topbarSearch').removeClass('on');
		$(this).css('font-size','');
	});
}

//Enable more functions
var showDelay = 1500;
var showTimer = null;
function enableMoreFunctions() {
	//Bind click functions
	$('#topbarMore_Trigger').bind('click', function() {
		if ($('#topbarMore_Items').css('display') == 'none') {
			$('#topbarMore_Items').slideDown();
		}
		else {
			$('#topbarMore_Items').slideUp('fast');
		}
	});
	
	//Enable close on document click
	$(document).bind('click', function(e) {
		if ($('#topbarMore_Items').css('display') == 'block') {
			var sParentsClasses = $(e.target).parents().map(function () { 
				return this.className; 
			}).get().join(", ");
			if (sParentsClasses.indexOf('topbar-more') == '-1') {
				$('#topbarMore_Items').slideUp('fast');
			}
		}
	});
}

//Enable left menu
function enableLeftMenu() {
	$('.menu li').each(function(index) {
		if ($(this).find('ul').length > 0) {
			$(this).find('a').bind('click', function() {
				$(this).parent().find('ul').slideToggle('fast');
			});	
		}
	});
}

//Enable fancybox
function enableFancybox() {
	//For basic dialogs
	$(".iframe").fancybox({
		'width'				: '70%',
		'height'			: '70%',
		'autoScale'			: true,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe'
	});
	$(".inline").fancybox({
		'width'				: '70%',
		'height'			: '70%',
		'autoDimensions'	: false,
		'autoScale'			: true,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'inline'
	});
	$(".inline-auto").fancybox({
		'width'				: 'auto',
		'height'			: 'auto',
		'autoDimensions'	: false,
		'autoScale'			: true,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'inline'
	});
	$(".mappicker").fancybox({
		'width'				: 'auto',
		'height'			: 'auto',
		'autoDimensions'	: false,
		'autoScale'			: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'inline'
	});
	//For large dialogs
	$(".iframe-large").fancybox({
		'width'				: '90%',
		'height'			: '90%',
		'autoScale'			: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe'
	});
	//For image gallery
	$("a[rel=example_group]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Bild ' +  (currentIndex + 1) + ' av ' + currentArray.length + ' - '  + title + '</span>';
		}
	});
	$("a[rel=example_group_edit]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Bild ' +  (currentIndex + 1) + ' av ' + currentArray.length + ' - '  + title + ' [<a href="#" onclick="return generate_img_descr_edit_form('+currentIndex+');">Redigera</a> | <a href="#" onclick="return delete_gallery_img('+currentIndex+');">Ta bort</a> }</span>';
		}
	});
	// For stand-alone images
	$("a[rel=single_image]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic'
	});
	// For cartoon archives
	$("a[rel=cartoon_group]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return (currentIndex + 1) + '. '  + title;
		}
	});


}

//Enable calendar image slider
var iCalendarListElements = 0;
var iCalendarListRollThis = 0;
var aCalendarListElement = new Array();
var aCalendarListElementImageLength = new Array();
var bCalendarItemVisible = false;
function enableCalendarSlider() {
	//check if calendar exists
	if ($('#calendar').length > 0) {
		//create calendar item foldout
		$('body').append('<div id="calendarFoldOut" class="calendar-foldout" style="display: none;"><div class="calender-foldout-inner"></div><span id="calendarFoldOut_hide" title="Dölj rubriker" class="item-fold"></span></div>');
		$('#calendarFoldOut').bind('mouseleave', function(event) {
			$('#calendarFoldOut').delay(200).fadeOut('slow');
		});
		$('#calendarFoldOut_hide').bind('click', function(event) {
			$('#calendarFoldOut').slideUp('slow');
		});
		//iterate through calendar items
		$('#calendar').find('li').each(function(index) {
			//create item foldouts for large items
			if ($(this).find('.item-text').outerHeight() > $(this).height()) {
				//add clickable item for showing all
				$(this).append('<span class="item-fold" title="Visa alla rubriker"></span>');
				$(this).find('.item-fold').bind('click', function(event) {
					createCalendarItem($(this).parent());
				});
			}
			//get list elements with multiple images and set image ids
			if($(this).find('img').length > 1) {
				var sListItemId = $(this).attr('id');
				aCalendarListElement[iCalendarListElements] = sListItemId;
				aCalendarListElementImageLength[iCalendarListElements] = $(this).find('img').length;
				iCalendarListElements++;
				$(this).find('img').each(function(index) {
					$(this).attr('id',sListItemId + '_' + index);
				});
			}
		});
		//begin rolling images
		rollCalendarImages();
	}
}

//Create calendar foldout item
function createCalendarItem(oLi) {
	//bCalendarItemVisible = true;
	var sText = '<span class="date">' + oLi.find('.item-date').html() + '</span>' + oLi.find('.item-text').html();
	$('#calendarFoldOut').find('.calender-foldout-inner').bind('click', function(event) {
		location.href = oLi.find('a').attr('href');
	});
	$('#calendarFoldOut').find('.calender-foldout-inner').html(sText);
	$('#calendarFoldOut').css('top',oLi.position().top);
	$('#calendarFoldOut').css('left',oLi.position().left);
	$('#calendarFoldOut').css('width',oLi.find('.item-text').width());
	$('#calendarFoldOut').slideDown('slow');
}

//Roll calendar images
var oActiveImage = null;
var oNextImage = null;
var iNextImage = 0;
function rollCalendarImages() {
	//randomize element to swap images in
	if (iCalendarListRollThis == iCalendarListElements) {
		iCalendarListRollThis = 0;
	}
	//find current and next image for this list item
	oActiveImage = $('#' + aCalendarListElement[iCalendarListRollThis]).find('.show');
	iNextImage = parseInt(oActiveImage.attr('id').split('_')[1]) + 1;
	if (iNextImage == aCalendarListElementImageLength[iCalendarListRollThis]) {
		iNextImage = 0;
	}
	oNextImage = $('#' + aCalendarListElement[iCalendarListRollThis] + '_' + iNextImage);
	oActiveImage.removeClass('show');
	oNextImage.addClass('show');
	//fade out current, fade in new
	oActiveImage.stop().delay(2000).fadeOut(1000);
	oNextImage.stop().delay(3000).fadeIn(1000);
	oActiveImage = null;
	oNextImage = null;
	iCalendarListRollThis++;
	//timeout
	setTimeout(rollCalendarImages, 4000);
}

