﻿/* Eksakte Frontend - javascript functions */
/* RDC 08.04.2010 */

/*Checker om fonten er loadet.
  Viser først indholdet efter serverfonten er loadet - medmindre det tager over 3 sekunder.*/
(function(){
  // if firefox 3.5+, hide content till load (or 3 seconds) to prevent FOUT
  var d = document, e = d.documentElement, s = d.createElement('style');
  if (e.style.MozTransform === ''){ // gecko 1.9.1 inference
    s.textContent = 'body{visibility:hidden}';
    e.firstChild.appendChild(s);
    function f(){ s.parentNode && s.parentNode.removeChild(s); }
    addEventListener('load',f,false);
    setTimeout(f,3000); 
  }
})();

$(document).ready(function() {

	StartMenuBoxes();

	StartScrollPages();

	StartFrontPageAnimation();
	
	StartAutoLabel();

});


//##########################################################################################
//	Scroll pages
//##########################################################################################


var bScrollRunning = false;


function StartScrollPages() {

	if ($('.slidingImages').size() == 0) {
		return false;
	}

	var intFirstPosition = 0;
	var intWidth = 0;
	var intIndexSelected = 0;
	var intScrollTime = 1000;
	var intImageResizeTime = 1000;
	
	var intLiCount = $('.slidingImages li').size();
	var intLiSize = $('.slidingImages li:eq(0)').width();
	var intFirstGroupOffset = intLiCount * intLiSize;
	
	var intDescriptionSize = $('.descriptionsInner li:eq(0)').width() + 47;
	var intFirstDescriptionGroupOffset = intLiCount * intDescriptionSize;
	
	var intHeadlineSize = $('.headlines li:eq(0)').width() + 32;
	var intFirstHeadlinesGroupOffset = intLiCount * intHeadlineSize;
	
	// Set default Scroll settings
	var objDefaultScrollSettings = {
		duration: intScrollTime,
		axis: 'x'
	};

	CopyElements();
	
	// Calculate and set width of the ul containing the scrollable li's
	$('.slidingImages li').each(function() {
		intWidth += $(this).width();
	});
	intWidth += 2000;
	$('.slidingImages ul').css({width: intWidth + 'px'});

	intFirstPosition = $('.clickPosition0:eq(1)').position().left;
	$('.slidingImages ul').css({left: '-' + (intFirstPosition - 52) + 'px'});
	$('.clickPosition0:eq(1)').addClass('activeImage').removeClass('iconImage');
	
	intFirstPosition = intLiCount * intDescriptionSize;
	$('.descriptionsInner').css({
		left: '-' + intFirstPosition + 'px'
	});

	intFirstPosition = intLiCount * intHeadlineSize;
	$('.headlines ul').css({
		left: '-' + intFirstPosition + 'px'
	});


	// Calculate and set width of the ul containing the scrollable descriptions
	intWidth = 0;
	$('.descriptionsInner li').each(function() {
		intWidth += $(this).width() + 47;
	});
	$('ul.descriptionsInner').css({width:intWidth + 'px'});
	
	$('.descriptions').css({
		height: ($('.descriptionsInner li[id|=descriptionGroup1]:eq(0)').height() + 20) + 'px'
	});
	
	
	// Calculate and set width of the ul containing the headlines
	intWidth = 0;
	$('.headlines li').each(function() {
		intWidth += $(this).width() + 47;
	});
	$('.headlines ul').css({width:intWidth + 'px'});
	
	
	// Calculate and set width of the ul containing the small navigation buttons
	intWidth = 0;
	$('.navigation li').each(function() {
		intWidth += $(this).width();
	});
	$('.navigation ul').css({width:intWidth + 'px'});


	$('.navigation a').click(function() {
		var $this = $(this);
		intIndexSelected = $this.parent().index();
		
		$('.slidingImages li[id|=liGroup1]:eq(' + intIndexSelected + ') a').click();
		return false;
	});




	var objDescriptionScrollSettings = objDefaultScrollSettings;
	
	$('.slidingImages a').click(function() {
		
		if (bScrollRunning) {
			return false;
		}
		
		var $this = $(this);
		
		if ($this.is('.activeImage a')) {
			return false;
		}
		
		bScrollRunning = true;
		
		intIndexSelected = $this.parent().attr('class').match('clickPosition(\\d+)')[1];

		// Hvis der er klikket på et element i group 0 eller 2 skal systemet "snydes" lidt.
		if ($this.parents('li').attr('id').indexOf('liGroup0') > -1 || $this.parents('li').attr('id').indexOf('liGroup2') > -1) {
			var intCurrentActiveIndex = $('.activeImage').attr('class').match('clickPosition(\\d+)')[1];

			var intCheatPosition = 0;
			var intDescriptionCheatPosition = 0;
			var intHeadlinesCheatPosition = 0;
			var intFalseGroupToUse;
			
			if ($this.parents('li').attr('id').indexOf('liGroup0') > -1) {
				intFalseGroupToUse = 2;
				intCheatPosition = ((intLiSize * intCurrentActiveIndex) + intFirstGroupOffset + intFirstGroupOffset) - 52;
				intDescriptionCheatPosition = ((intDescriptionSize * intCurrentActiveIndex) + intFirstDescriptionGroupOffset + intFirstDescriptionGroupOffset);
				intHeadlinesCheatPosition = ((intHeadlineSize * intCurrentActiveIndex) + intFirstHeadlinesGroupOffset + intFirstHeadlinesGroupOffset);
			} else {
				intFalseGroupToUse = 0;
				intCheatPosition = (intLiSize * intCurrentActiveIndex) - 52;
				intDescriptionCheatPosition = (intDescriptionSize * intCurrentActiveIndex);
				intHeadlinesCheatPosition = (intHeadlineSize * intCurrentActiveIndex);
			}

			$('.slidingImages li[id|=liGroup' + intFalseGroupToUse + ']:eq(' + intCurrentActiveIndex + ') img').css({
				width: '185px',
				height: '175px',
				marginTop: '0'
			});
		
			$('.slidingImages li[id|=liGroup' + intFalseGroupToUse + ']:eq(' + intCurrentActiveIndex + ')').removeClass('iconImage').addClass('activeImage');

			$('.slidingImages ul').css({
				left: '-' + intCheatPosition + 'px'
			});
			
			$('.descriptionsInner').css({
				left: '-' + intDescriptionCheatPosition + 'px'
			});
			
			$('.headlines ul').css({
				left: '-' + intHeadlinesCheatPosition + 'px'
			});

			$('.slidingImages li[id|=liGroup1].activeImage img').css({
				width: '58px',
				height: '55px',
				marginTop: '88px'
			});
			
			$('.slidingImages li[id|=liGroup1].activeImage').removeClass('activeImage').addClass('iconImage');
			
		}
		
		$('.navigation li.active').removeClass('active');
		$('.navigation li:eq(' + intIndexSelected + ')').addClass('active');
		
		$('.activeImage img').animate({
			width: '58px',
			height: '55px',
			marginTop: '88px',
			borderWidth: '1px'
		}, {
			duration: intImageResizeTime,
			complete: function() {
				$(this).parents('li').addClass('iconImage').removeClass('activeImage');
				CleanUpOnDone();
			}
		});
		
		var objSelectedItem = $('.slidingImages li[id|=liGroup1]:eq(' + intIndexSelected + ')');
		
		objSelectedItem.find('img').animate({
			width: '185px',
			height: '175px',
			marginTop: '0',
			borderWidth: '2px'
		}, {
			duration: intImageResizeTime,
			complete: function() {
				objSelectedItem.addClass('activeImage');
				CleanUpOnDone();
			},
			queue: false
		});
		objSelectedItem.removeClass('iconImage');
		
		var intScrollTarget = ((intIndexSelected * intLiSize) + intFirstGroupOffset) - 52;
		var intPixelsToScrollDescriptions = ((intIndexSelected * intDescriptionSize) + intFirstDescriptionGroupOffset);
		var intDescriptionContainerHeight = $('.descriptions li[id|=descriptionGroup1]:eq(' + intIndexSelected + ')').height() + 20;
		var intPixelsToScrollNames = (intIndexSelected * intHeadlineSize) + intFirstHeadlinesGroupOffset;

		$('.headlines ul').animate({
			left: '-' + intPixelsToScrollNames + 'px'
		}, {
			duration: intScrollTime,
			queue: false
		});

		$('.slidingImages ul').animate({
			left: '-' + intScrollTarget + 'px'
		}, {
			duration: intScrollTime,
			complete: function() {
				$('.activeImage').not(objSelectedItem).removeClass('activeImage').addClass('iconImage');
				bScrollRunning = false;
			}
		})
		
		$('.descriptions').animate({
			height: intDescriptionContainerHeight + 'px'
		}, {
			duration: intScrollTime,
			queue: false
		});
		
		$('.descriptionsInner').animate({
			left: '-' + intPixelsToScrollDescriptions + 'px'
		}, {
			duration: intScrollTime,
			queue: false
		});

		return false;
	});

	var strHash = location.hash;
	strHash = strHash.replace('#','');
	if (strHash != '') {
		$('.slidingImages li#liGroup1-' + strHash + ' a').click();
	}
	if ($('.descriptionsInner h5 a').size() > 0) {
		$('.descriptionsInner h5 a').text($('.descriptionsInner h5 span').attr('title'));
		$('.descriptionsInner h5 a').addClass('arrowAppended');
	}
	
	
}

function CleanUpOnDone() {
	$('.iconImage img').css({
		width: '58px',
		height: '55px',
		marginTop: '88px'
	});
}

function CopyElements() {
	var strElementsHtml;
	
	// Images
	strElementsHtml = $('.slidingImages ul').html();
	strElementsHtml = strElementsHtml.replace(/liGroup1/gi, 'liGroup0');
	$('.slidingImages ul').prepend(strElementsHtml);
	strElementsHtml = strElementsHtml.replace(/liGroup0/gi, 'liGroup2');
	$('.slidingImages ul').append(strElementsHtml);
	
	// Descriptions
	strElementsHtml = $('ul.descriptionsInner').html();
	strElementsHtml = strElementsHtml.replace(/descriptionGroup1/gi, 'descriptionGroup0');
	$('ul.descriptionsInner').prepend(strElementsHtml);
	strElementsHtml = strElementsHtml.replace(/descriptionGroup0/gi, 'descriptionGroup2');
	$('ul.descriptionsInner').append(strElementsHtml);
	
	// Headlines
	strElementsHtml = $('.headlines ul').html();
	strElementsHtml = strElementsHtml.replace(/headlineGroup1/gi, 'headlineGroup0');
	$('.headlines ul').prepend(strElementsHtml);
	strElementsHtml = strElementsHtml.replace(/headlineGroup0/gi, 'headlineGroup2');
	$('.headlines ul').append(strElementsHtml);
	
}



//##########################################################################################
//	Menu boxes
//##########################################################################################

var intBoxHoverWidth;
var intBoxHoverLeftPosition;
var intBoxHoverIndex;
var objTimeOut;
var objSelectedItem;
var bMenuSelected;

function StartMenuBoxes() {

	$('ul.nav').append('<li class="graphics"><div class="graphicHover"></div></li>');

	objSelectedItem = $('.nav .selected');
	
	var intSelectedWidth = 0;
	var intSelectedLeftPosition = 0;
	
	if (objSelectedItem.size() > 0) {
		bMenuSelected = true;
		intSelectedWidth = objSelectedItem.width();
		intSelectedLeftPosition = objSelectedItem.position().left;
		
		$('.graphicHover').css({width: intSelectedWidth + 'px', left: intSelectedLeftPosition + 'px'})
		
	} else {
		bMenuSelected = false;
		$('.graphicHover').hide();
	}
	
	$('.nav a').mouseover(function() {
		var $this = $(this);
		
		clearTimeout(objTimeOut);
		
		if ($this.parent().index() != intBoxHoverIndex) {
			intBoxHoverWidth = $this.parent().width();
			intBoxHoverLeftPosition = $this.parent().position().left;
			intBoxHoverIndex = $this.parent().index();
		
			$('.graphicHover').stop();
			$('.graphicHover').animate({
				width: intBoxHoverWidth,
				left: intBoxHoverLeftPosition
			}, {
				duration: 600
			});
			
		}
				
	});
	
	$('.nav a').mouseout(function() {
		objTimeOut = setTimeout(function () {
			$('.graphicHover').stop();
			if (bMenuSelected) {
				$('.graphicHover').animate({
					width: intSelectedWidth,
					left: intSelectedLeftPosition
				}, {
					duration: 600
				});
			} else {
				$('.graphicHover').animate({
					width: 0
				}, {
					duration: 600
				});
			}
			
			intBoxHoverIndex = -1;
		}, 80);
	});

}


//##########################################################################################
//	Frontpage text changer
//##########################################################################################

var intSelectedTextIndex = 0;
var arrTexts;
function StartFrontPageAnimation() {

	if ($('.frontpage').size() > 0) {
		
		var strTexts = $.trim($('.wideImageText').html());
		strTexts = strTexts.replace(/<br \/>/gi,'<br>').replace(/<br\/>/gi,'<br>').replace(/\n/gi,'');
		strTexts = strTexts.replace(/>\s+</gi, '><');
		strTexts = strTexts.replace(/\s+</gi, '<');
		strTexts = strTexts.replace(/>\s+/gi, '>');
		strTexts = strTexts.replace(/<br>{2,}/gi, '<br>');
		strTexts = strTexts.replace(/<BR>/gi, '<br>');
		if (strTexts.length > 4) {
			if (strTexts.substring(0,4) == '<br>') {
				strTexts = strTexts.substring(4);
			}
		}
		if (strTexts.length > 4) {
			if (strTexts.substring(strTexts.length - 4) == '<br>') {
				strTexts = strTexts.substring(0, strTexts.length - 4);
			}
		}
		arrTexts = strTexts.split('<br>');

		if (arrTexts.length > 0) {
			$('.frontpage .wideImage h1 span').text(arrTexts[0]);
			setTimeout('ShowNextFrontPageText()', 2000);
		}
		
	}

}

function ShowNextFrontPageText() {
	intSelectedTextIndex++; 
	
	if (intSelectedTextIndex >= arrTexts.length) {
		intSelectedTextIndex = 0;
	}
	
	var strNextText = arrTexts[intSelectedTextIndex];
		
	$('.frontpage .wideImage h1 span').animate({
		left: '-500px'
	}, {
		duration: 500,
		easing: 'easeInBack',
		complete: function() {
			$('.frontpage .wideImage h1 span').css({left:'500px'});
			
			$('.frontpage .wideImage h1 span').text(strNextText).animate({
				left: 0
			}, {
				duration: 500,
				easing: 'easeOutBack',
				complete: function() {
					setTimeout('ShowNextFrontPageText()', 5000);
				}
			});
		}
	});

}



//##########################################################################################
//	Start auto label
//##########################################################################################

function StartAutoLabel() {

	$('.autoLabel').each(function() {
	
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	
		if ($(this).val() != $(this).attr('title')) {
			$(this).addClass('autoLabelActive');
		}
	});
	
	$('.autoLabel').focus(function() {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
			$(this).addClass('autoLabelActive');
		}
	});
	
	$('.autoLabel').blur(function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
			$(this).removeClass('autoLabelActive');
		}
	});

}