/* Author: 

*/

jQuery(document).ready(function($) {

	$('footer.Single a').attr('target', '_blank');
	$('header.Single a').attr('target', '_top');	
	
	if ( $('#info').length > 0 ) {
		//$.scrollTo($('#info'), 600, {offset: {top:-65, left: 0} });
		$('html,body').animate({scrollTop: $('#info').offset().top-65}, 600, 'easeOutCubic');		
	}	
	
	
		
	if($('h2[data-event-poster]').attr('data-event-poster')) {
		$('section.col2 img').attr('src', $('h2[data-event-poster]').attr('data-event-poster')).fadeIn(300);
		$('section.col2 div').fadeIn();
	} else {
		$('section.col2 img').removeClass('hidden', 300);
	}

	$('.Social li').hover(function() {
		$(this).animate({top: '-=5', marginRight: '+=5', marginLeft: '+=5'}, 200);
	}, function() {
		$(this).animate({top: '+=5', marginRight: '-=5', marginLeft: '-=5'}, 200);
	});
	
	$('.DNBLogo').hover(function() {
		$(this).stop().animate({opacity: 0.80}, 200);
	}, function() {
		$(this).stop().animate({opacity: 0.50}, 200);
	});	
	
	$('a:not(.HeaderImageExpand):has(section), ul.ImageList li a:has(img)').css({'position': 'relative', 'display': 'block'}).hover(function(){
		$('<div />').text(' ').addClass('ColorOverlay').css({
			'height': $(this).height(),
			'width': $(this).width(),
			'background-color': '#00a8ff',
			'position': 'absolute',
			'top': 0,
			'left': 0,
			'opacity': 0.0
		}).appendTo(this);
		$('div.ColorOverlay').animate({
			'opacity': 0.2
		}, 'fast');
	}, function() {
		$('div.ColorOverlay').fadeOut('fast', function(){
			$(this).remove();
		});
	});
	
	$('.MediaBox a[data-expand=true], .MediaBox a[data-expand=false]').live({
	'mouseenter':function() {
		$(this).find('img').fadeTo(1, 1);
		},
	'mouseleave': function() {
		$(this).find('img').fadeTo(1, 0.8);		
		}
	});

	
	//------------ Login form focus effect --------------//
	
	var defaultValue;
	$('input.Text').focus(function() {
		defaultValue = $(this).attr('data-default');
		if($(this).attr('value') == defaultValue){
			$(this).attr('value', '');
		}
	});
	$('input.Text').blur(function() {
		defaultValue = $(this).attr('data-default');
		if($(this).attr('value') == ''){
			$(this).attr('value', defaultValue);
		}
	});	
	
	//------------ Bilde/Video Effects --------------//	
	//------- Open/Close media box -------//
	/*$('section.MediaBox a[data-expand=true]').live('click', function() {
		$(this).parent().attr('data-expand', 'false');
		$('section.Expanded').removeClass('Expanded', 300, function() {
			$('section[data-expand=false]').attr('data-expand', 'true');			
			$(this).empty();
			$(this).prepend('<a href="#" data-expand="true">Open</a>'); /* <------------- Populate the (NOT expanded) MediaBox here (closed by opening a different box) --- 			
		});
		$('section[data-expand=false]').find('*').fadeTo(300, 0.01, function() {
			$(this).parents('section').addClass('Expanded', 300, function() {
				$(this).empty();
				$(this).prepend('<a href="#" class="Close">Close</a>'); /* <------------- Populate the (expanded) MediaBox here --- 
			});
		});
		return false;
	});
	
	$('a.Close').live('click', function() {
		$('section.Expanded *').fadeTo(300, 0.01, function() {
			$('section.Expanded').removeClass('Expanded', 300, function() {
				$('section[data-expand=false]').attr('data-expand', 'true');
				$(this).empty();
				$(this).prepend('<a href="#" data-expand="true">Open</a>');	/* <------------- Populate the (NOT expanded) MediaBox here (closed by using the 'X' button) --- 		
			});
		});
		return false;
	}); */
	
	//------- Expand filter menu -------//	
	$('.YaddaYaddaFilterYaddaNav > li').live('click', function() {
		if($('.FilterNav ul[data-current=true]') == $(this)) {
			$('.FilterNav ul').fadeOut(300);			
			$('ul.FilterNav').removeClass('Expanded', 300);			
		} else {
			if($('.FilterNav ul[data-current=true]')) {
				$('.FilterNav > li').removeClass('Expanded');				
				$('.FilterNav ul[data-current=true]').attr('data-current', 'false').fadeOut(300);			
				$('ul.FilterNav').addClass('Expanded', 300);
				$(this).find('ul').attr('data-current', 'true').fadeIn(300);
				$(this).addClass('Expanded', 300);
			}		
		}
	});	
	
	//------- Expand Link -------//

	var j = 1;
	var defaultClass;
	var currentValue;
	
	$('a.ExpandLink').click(function() {
		currentValue = $(this).html();
		$(this).html($(this).attr('data-alt-value'));
		$(this).attr('data-alt-value', currentValue);
		if(j==1) {
			if($(this).parents('section, div').attr('class') == 'TinyBox' || $(this).parent('section, div').attr('class') == 'LargeBox') {
				defaultClass = $(this).parent('section').attr('class');
			} else {
				defaultClass = ' ';
			}
		}
		j++;
		$(this).siblings('.expandable.hidden').removeClass('hidden');
		$(this).siblings('img.small').addClass('large', 300, 'swing').removeClass('small', 300, 'swing');		
		$(this).parent('section').toggleClass('expanded static '+defaultClass, 300, 'swing', function() {
			if ($(this).attr('data-expanded') == 'true') {
				$(this).find('img.large').addClass('small', 300, 'swing').removeClass('large', 300, 'swing');			
				$(this).find('.expandable').addClass('hidden');
				$(this).attr('data-expanded', 'false');
			} else {
			$(this).attr('data-expanded', 'true');
			}
		});
		return false;
	});

	$('a.HeaderImageExpand').click(function() {
		currentValue = $(this).find('.ExpandLink').html();
		$(this).find('.ExpandLink').html($(this).find('.ExpandLink').attr('data-alt-value'));
		$(this).find('.ExpandLink').attr('data-alt-value', currentValue);

		$(this).find('img.small').addClass('large', 300, 'swing').removeClass('small', 300, 'swing');		
		$(this).find('.HeaderImage').toggleClass('expanded', 300, 'swing', function() {
			if ($(this).attr('data-expanded') == 'true') {
				$(this).find('img.large').addClass('small', 300, 'swing').removeClass('large', 300, 'swing');			
				$(this).attr('data-expanded', 'false');
			} else {
			$(this).attr('data-expanded', 'true');
			}
		});
		return false;
	});	
	
	$('.TinyBox.Club .SectionFooter .ShowMore').click(function(e) {

		if( $(this).parents('.Club').attr('data-expanded') != 'true' ) {			
			currentValue = $('.TinyBox.Club.Expanded .ShowMore').html();
			$('.Club.Expanded .ShowMore').html($('.Club.Expanded .ShowMore').attr('data-alt-value'));
			$('.Club.Expanded .ShowMore').attr('data-alt-value', currentValue);
			
			currentValue = $(this).html();
			$(this).html($(this).attr('data-alt-value'));
			$(this).attr('data-alt-value', currentValue);
			
			$('.TinyBox.Club[data-expanded=true]').removeClass('Expanded', 300, 'swing').attr('data-expanded', 'false').find('.Overflow').removeClass('Expanded', 300, 'swing');
			$(this).parents('.Club').addClass('Expanded', 300, 'swing', function() {
			//	$.scrollTo($(this), 600, {offset: {top:-65} });
				$('html,body').animate({scrollTop: $(this).offset().top-65}, 600, 'easeOutCubic');
			}).attr('data-expanded', 'true').find('.Overflow').addClass('Left').addClass('Expanded', 300, 'swing');

			
			
			
		} else {
			currentValue = $(this).html();
			$(this).html($(this).attr('data-alt-value'));
			$(this).attr('data-alt-value', currentValue);
			$(this).parents('.Club').removeClass('Expanded', 300, 'swing').attr('data-expanded', 'false').find('.Overflow').removeClass('Expanded', 300, 'swing').removeClass('Left');	
		}
		e.preventDefault();
	});	
	
	var innerHeight;
	
	$('.FindClub a.ReadMore').click(function(e) {
		if($(this).parents('.FindClub').hasClass('Expanded')) {
			currentValue = $(this).html();
			$(this).html($(this).attr('data-alt-value'));
			$(this).attr('data-alt-value', currentValue);
			$(this).parents('.FindClub').removeClass('Expanded', 300).find('.hidden').animate({'height': '0px'});
		} else {
			currentValue = $(this).html();
			$(this).html($(this).attr('data-alt-value'));
			$(this).attr('data-alt-value', currentValue);
			innerHeight = $(this).parents('.FindClub').find('.hidden div:first').height();
			$(this).parents('.FindClub').addClass('Expanded', 300, 'swing').find('.hidden').animate({'height': innerHeight+15+'px'});
		}
		e.preventDefault();
	});
	
	

// ======= Klub Filter ======= //

	function sortObj(arr){
		// Setup Arrays
		var sortedKeys = new Array();
		var sortedObj = {};
	
		// Separate keys and sort them
		for (var i in arr){
			sortedKeys.push(i);
		}
		sortedKeys.sort();
	
		// Reconstruct sorted obj based on keys
		for (var i in sortedKeys){
			sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
		}
		return sortedObj;
	}
	
	var features = new Array();
	
	$('.TinyBox.Club').each(function() {
		
		features = $(this).attr('data-tilbud').split(',');
		for(var i = 0; i < features.length; i++) {
			$('.Klubb .FeatureFilter').data(features[i], features[i]);
		}	
		
		$('.FindClub .filter select').data($(this).attr('data-fylke'), $(this).attr('data-fylke'));	
	});
	
	var filterList = $('.Klubb .FeatureFilter').data();
	
	for(var i in filterList) {
		if(filterList[i] != '') {
			$('.Klubb .FeatureFilter').append('<li><a data-tilbud="'+filterList[i]+'" href="#">'+filterList[i]+'</a></li>');
		}
	}
	
	var countyList = $('.FindClub .filter select').data();
	for(var i in sortObj(countyList)) {
		if(countyList[i] != '') {
			$('.FindClub .filter select').append('<option value="'+countyList[i]+'">'+countyList[i]+'</option>');
		}
	}

	var active = 0;	
	var activeAll;
	
	$('.Klubb .FeatureFilter a').live('click', function() {
		
		if($(this).attr('data-active') == 'true') {
			$(this).attr('data-active', false).removeClass('active');
			activeAll = 1;
		} else {
			$('.Klubb .FeatureFilter li a').attr('data-active', false).removeClass('active');
			$(this).attr('data-active', true).addClass('active');
			activeAll = 0;
			
		}
		
		
		var feature = $(this).attr('data-tilbud');
		$('section.Club:visible').each(function() {

			thisFeatures = $(this).attr('data-tilbud').split(',');
			for(var i = 0; i < thisFeatures.length; i++) {
				if(thisFeatures[i] != feature) {
					if(activeAll == 1) {
						active = 1;
						break;
					} else {
						active = 0;
					}
				} else {
					active = 1;
					break;
				}
			}

			if(active == 0) {
				$(this).fadeTo(300, 0.2);
			} else {
				$(this).fadeTo(300, 1);
			}
			features = $(this).attr('data-tilbud').split(',');
			for(var i = 0; i < features.length; i++) {
				$('.Klubb .FeatureFilter').data(features[i], features[i]);
			}
		});

		return false;
	});
	
	$('.FindClub .filter select').live('change', function() {

		if($(this).find('option:selected').attr('value') == 'Vis alle' || $(this).find('option:selected').attr('value') == 'Velg fylke') {
			$('section.Club').fadeIn();
		} else {	
			var selectedOption = $(this).find('option:selected').attr('value');
			$('section.Club:not([data-fylke="'+selectedOption+'"])').fadeOut();
			$('section.Club[data-fylke="'+selectedOption+'"]').fadeIn();		
		}
	});

// ======= Landslag box height ======= //

	var heightArr = new Array();
	var n = 0;

	function sortNumber(a,b) {
		return b - a;
	}

	$('.Landslag .Single .LargeBox, .Landslag .Single .RiderBox').each(function() {	
		heightArr[n] = $(this).height();
		n++;	
	});
	
	heightArr = heightArr.sort(sortNumber);
	if(heightArr[0] > 450) {
		$('.Landslag .Single .LargeBox, .Landslag .Single .RiderBox').animate({height: 470, marginBottom: 30}).removeClass('Dynamic');
		$('section.Team.Single').append('<a href="#" class="ExpandLink col4" data-alt-value="Lukk" data-expanded="false" style="bottom:  -7px;">Vis mer</a>');
		$('.Team.Single .ExpandLink').live('click', function(event) {
			event.preventDefault();
			if($(this).attr('data-expanded') == 'false') {
				currentValue = $(this).html();
				$(this).html($(this).attr('data-alt-value'));
				$(this).attr('data-alt-value', currentValue).attr('data-expanded', true);
				$('.Landslag .Single .LargeBox, .Landslag .Single .RiderBox').animate({height: heightArr[0]+56}, 200);	

			} else {
				currentValue = $(this).html();
				$(this).html($(this).attr('data-alt-value'));
				$(this).attr('data-alt-value', currentValue).attr('data-expanded', false);
				$('.Landslag .Single .LargeBox, .Landslag .Single .RiderBox').animate({height: 470}, 200);	
			}
				
		});
		
	}
		
// ======= Search results expand ======= //	

	$('.Sok a.ExpandLink').click(function() {
		
		var wrapper = $(this).parents('.ExpandWrapper')[0];
		var contentHeight = wrapper.scrollHeight;
//		alert(wrapper.clientHeight+' .. '+contentHeight)
		if(wrapper.clientHeight < contentHeight) {
			$(wrapper).animate({height: contentHeight}, function() {
				$(this).css({height: 'auto'}).removeClass('More');
			});
			$(this).remove();
		}
		
	});
	
	var searchFootageCount = $('.Sok .ExpandWrapper .BildeVideo section.MediaBox').length;
	if(searchFootageCount <= 8) {
		$('.Sok .ExpandWrapper div.BildeVideo').siblings('a.ExpandLink').remove();
		$('.Sok .ExpandWrapper div.BildeVideo').parent().removeClass('More');
	}
	$('#search-bilde-video h2 span').html('('+searchFootageCount+' treff)');
	
	$('a.AdvantagesBadge').click(function(e) {
		$('.AdvantagesOverlay').parents('.TopImage').stop().addClass('Dynamic', 300);
		e.preventDefault();
	});
	$('a.AdvantagesExpand').click(function(e) {
		$('.AdvantagesOverlay').parents('.TopImage').stop().addClass('Dynamic', 300);
		$.scrollTo('.TopImage', 600);
		e.preventDefault();
	});	
	$('a.AdvantagesClose').click(function() {
		$('.AdvantagesOverlay').parents('.TopImage').stop().removeClass('Dynamic', 300);
		return false;
	});	

// ======= Bilde/Video Counter ======= //	

	$('.BildeVideo span.Counter').text($('.BildeVideo div.BildeVideo').attr('data-count'));

// ======= Remove duplicate search results ======= //	

	var seen = {};
	$('.Sok .BildeVideo .MediaBox a').each(function() {
	    var txt = $(this).attr('data-link');
	    if (seen[txt])
	       $(this).parent('.MediaBox').remove();
	    else
	        seen[txt] = true;
	});
	
	var footageCount = $('.Sok .BildeVideo .MediaBox').length;
	$('#search-bilde-video h2 span').text('('+footageCount+' treff)');

});


