$(document).ready(function() {
	
var breakercount = $('img[alt="nodisplay"]').length;
	
		$('img[alt="nodisplay"]').parent().remove();


// Append Profile navigation items----------------------------
	$('.section-title').each(function(index) {
		$(this).nextAll('li').wrapAll('<ul class="sublist"></ul>');
	});
	$('.sublist').hide().appendTo('#submenuwrap');

	if ($('.profile *').length > 0) {
		$('<div class="profile button active">Profile</div>').appendTo('.profilenav');
	}

	if ($('.news *').length > 0) {
		$('<div class="news button">News</div>').appendTo('.profilenav');
	} else {
		$('.news.conbox').remove();
	}
	
	if ($('.pictures img').length > 0 && breakercount === 0) {
		$('<div class="picture button">Pictures</div>').appendTo('.profilenav');
	}
	
	if ($('.cv td').length > 0) {
		$('<div class="cv button">Cv</div>').appendTo('.profilenav');

	}
//-------------------------------------------------------------------------------------	
	
	
// All news handling_________________________	
if ($('.allnews').length > 0) {
	$('.allnews').each(function(index) {
		$(this).insertAfter('#profile');
		$(this).contents().wrapAll('<div class="newsdesc"></div>');
		if ($('.newsdesc img').length > 0) {
			$('.newsdesc img').parent().addClass('newsimg').prependTo($(this));
		} else {
			$('<div class="placeholder">').prependTo($(this)); // make empty placeholder if no image exists
		}
	});
	$('#profile').remove();
	}
	
	$('.allnews .newsdesc h3').each(function(index) {
		if (index === 0) {
			$($(this).contents().clone()).appendTo('.profilenav').wrap('<div class="miscnews button active"></div>');
		} else {
			$($(this).contents().clone()).appendTo('.profilenav').wrap('<div class="miscnews button"></div>');	
			}
		});

		$('.allnews:not(:first)').hide();
		$('.allnews.conbox:first').addClass('active');
//----------------------------------------	

	
// Main category handling	
	$('.section-title').parent().click(function() {
		$('.sublist.active').hide().removeClass('active');
		$('.section-title.active').removeClass('active');
		$(this).children().addClass('active');
		var menupos = $(this).prevAll('ul').length;
		$('.sublist:eq('+menupos+')').fadeIn(200).addClass('active');
	});
	
	$('.sublist li.active').parent().addClass('active').show();
//____________________________________________________

// Wrap news and desc images and content	
	$('.profile img').parent('p').addClass('profileimg');
	if ($('.profileimg').length === 0) {
		$('.profile.conbox img').wrapAll('<p class="profileimg"></p>');
	}
	$('.news img').parent('p').addClass('newsimg');
	if ($('.newsimg').length === 0) {
		$('.news.conbox img').wrapAll('<p class="newsimg"></p>');
	}
	$('.profile.conbox').contents().wrapAll('<div class="profiledesc"></div>').filter('.profileimg, .profileimg img').insertBefore('.profiledesc');	
	$('.news.conbox').contents().wrapAll('<div class="newsdesc"></div>').filter('.newsimg, .newsimg img').insertBefore('.newsdesc');		
//____________________________________________________	

// Uniform thumbpage span click function
	var spanclick = function(item) {
		$('span.active').removeClass('active');
		var activeclass = $(item).attr('class');
		$('.pictures.conbox.active').removeClass('active').fadeOut('fast', function() {
			$('.conbox[id^="'+activeclass+'"]').addClass('active').fadeIn('fast');
		});
		$(item).addClass('active');
	}
//-----------------------------------


// In case of image categories_______________________________________
	
	if (breakercount > 0) {
		
	var arra = ['imgcat_Portrait', 'imgcat_Commercial', 'imgcat_Project']; // category array
	var portraitcount = $('img[alt$="imgcat_Portrait"]').length; // determine individual cat img counts
	var commercialcount = $('img[alt$="imgcat_Commercial"]').length;
	var projectcount = $('img[alt$="imgcat_Project"]').length;
	var portraitcon = Math.ceil(portraitcount/12); // Determine number of conbox to make
	var commercialcon = Math.ceil(commercialcount/12);
	var projectcon = Math.ceil(projectcount/12);
	var totalcount = portraitcon+commercialcon+projectcon; // total conbox count
	var allimg = portraitcount + commercialcount + projectcount; // total img coun

	$('.pictures.conbox').removeAttr('id');

	$('.pictures.conbox img').each(function(index) { // stupid solution. Loop through array instead?
		if (index+1 <= totalcount) {
			index = index+1;
			$('<div class="pictures conbox a'+index+'" id="thpagea'+index+'imgcat">').hide().insertBefore('.cv.conbox:first'); // make new conbox'es
		}
	});

	var getimages = function(kind, boxcount, catcount, allimg) { // function to extract images by category and insert in created conbox'es
		var pagenumber = 1;
		$('<div id="thpagea'+boxcount+'" class="pictures button">'+kind.replace("imgcat_","")+'</div>').appendTo('.profilenav');
	// number version	$('<span class="thpagea'+boxcount+'outer catspan"><span class="thpagea'+(boxcount)+'">'+pagenumber+'</span></span>').hide().appendTo('.thumbpage'); // first thumbnav for new category
	$('<span class="thpagea'+boxcount+'outer catspan"><span class="thpagea'+(boxcount)+'"></span></span>').hide().appendTo('.thumbpage');

	$('img[alt$="'+kind+'"]').each(function(index) { // loop through category
		
		if (allimg > 0) { // only run function while unprocessed images are still left
			allimg--;
		
			if (index == 12) { // spill into new box if one gets filled
				boxcount++;
				index = 0;
			// number version	$('<span class="thpagea'+(boxcount)+'">'+(pagenumber+1)+'</span>').appendTo('.thpagea'+(boxcount-1)+'outer');
			$('<span class="thpagea'+(boxcount)+'"></span>').appendTo('.thpagea'+(boxcount-1)+'outer');
			}
			
			$(this).parent().appendTo('#thpagea'+(boxcount)+'imgcat');
		}
	});
		
	if (arra[catcount] != null && allimg !== 0) { // when done if there are still images left, return this function with new category
		return getimages(''+arra[catcount+1]+'', (boxcount+1), (catcount+1), allimg);
	} else {
		$('.pictures.conbox:not(.conbox[id^="thpage"])').remove(); // otherwise remove old conboxes and assign clickhandlers to new buttons
		
		$('.pictures.button').click(function() {
			$('.photocredit').hide();
			var clickedclass = $(this).attr('id');
			$('.conbox.active').hide().removeClass('active');
			$('.pictures.conbox[id='+clickedclass+'imgcat]').addClass('active').fadeIn('slow', function() {
				$('.thumbpage span.active:first').removeClass('active').hide();
				$('span.'+clickedclass+'outer').addClass('active').show();
				$('span.'+clickedclass+'outer span:first').addClass('active');
				$('span.'+clickedclass+'outer span').fadeIn('fast', function() {
					$('.thumbpage span span').click(function() {
						spanclick(this);
					});
				});
				});
			});
	}
	};
	
	getimages('imgcat_Portrait', 1, 0, allimg); // call geimages on first load
	}
// ----------------------------------	


// Attach thumbpage buttons
if ($('.pictures.conbox').length > 1 && $('li.active a.Morten.Lundrup').length === 0) {
	$('.pictures').each(function(index) {
	// numbers	$('<span class="thpagea'+(index+1)+'">'+(index+1)+'</span>').appendTo('.thumbpage');
	$('<span class="thpagea'+(index+1)+'"></span>').hide().appendTo('.thumbpage');
	});
	}
	
$('.button').click(function() {
	$('.thumbpage span.active').removeClass('active');
});	
// ----------------------------------	
	
	
// profile navigation: make uniform function to handle all

$('.button').click(function() {
	$('.photocredit').hide();
	$('.cvlink').hide();

	
});
	
	$('.news.button').click(function() {
		$('.conbox.active').hide(0, function() {
			$('.conbox.active').removeClass('active');
			if ($('.news.conbox .newsimg img').attr('src') == $('.profile.conbox .profileimg img').attr('src') && $('.profile.button.active').length > 0) {
				$('.news.conbox .newsdesc').hide();
				$('.news.conbox').addClass('active').show(0, function() {
					$('.news.conbox .newsdesc').fadeIn('slow');
					$('.photocredit.new').css('display', 'block');
				});
			
				
			} else {
			$('.news.conbox').addClass('active').fadeIn('slow');
			$('.photocredit.new').css('display', 'block');	
			}
			
		});
	});
	

		$('.profile.button').click(function() {
		$('.conbox.active').hide(0, function() {
			$('.conbox.active').removeClass('active');
			if ($('.news.conbox .newsimg img').attr('src') == $('.profile.conbox .profileimg img').attr('src') && $('.news.button.active').length > 0) {
						$('.profile.conbox .profiledesc').hide();
						$('.profile.conbox').addClass('active').show(0, function() {
							$('.profile.conbox .profiledesc').fadeIn('slow');

							$('.photocredit.prof').css('display', 'block');
						});
			
			
			} else {
			$('.profile.conbox').addClass('active').fadeIn('slow');
			$('.photocredit.prof').css('display', 'block');
			}
		});
	});
	
	$('.button').click(function() {

		$('.button.active').removeClass('active');
		$(this).addClass('active');

	});
	
	
	$('.cv.button').click(function() {
		$('.conbox.active').hide();
		$('.conbox.active').removeClass('active');
		$('.cv.conbox').addClass('active').fadeIn('slow', function() {
			$('.cvlink').fadeIn('slow');
		});
		});
	
	
	$('.picture.button').click(function() {
		if (breakercount === 0) {
			$('.conbox.active').hide();
			$('.conbox.active').removeClass('active');
			$('.pictures.conbox.a1').addClass('active').fadeIn('slow', function() {
				$('.thumbpage span:first').addClass('active');
				$('.thumbpage span').css('display','inline-block').fadeIn('fast');
			});
		}
	});
	
	
	$('.miscnews.button').click(function() {
		var newsitem = $(this).prevAll().length;
		$('.allnews.conbox.active').hide(0, function() {
			$('.conbox.active').removeClass('active');
			$('.allnew').hide();
			$('.allnews.conbox:eq('+newsitem+')').addClass('active').fadeIn('slow').next('.allnew').css('display', 'block');
		});
	});
	

// ----------------------------------	

// Grab cv link and attach to cv table
$('a[href$=cv.pdf]').html('download full cv').wrap('<div class="cvlink"></div>');
$('.cvlink').hide().appendTo('.thumbpage');	
// ----------------------------------	
	
	$('.button:not(.picture.button)').click(function() {
		$('.thumbpage span').hide();
	});
	
// Picture navigation --------------------	
if (breakercount === 0) {
	$('.thumbpage span').click(function() {
		spanclick(this);
	});}
// ----------------------------------	

// Grab profile/news image credit and attach under image
if ($('.profileimg img').length > 0){
$('<span class="photocredit prof">'+($('.profileimg img').attr('title'))+'</span>').insertAfter('.profile.conbox');
}
if ($('.newsimg img').length > 0){
$('<span class="photocredit new">'+($('.newsimg img').attr('title'))+'</span>').hide().insertAfter('.news.conbox');
}
$('.allnews.conbox .newsimg img').each(function(index) {
	var imgparent = $(this).parent().parent();
	$('<span class="photocredit allnew">'+($(this).attr('title'))+'</span>').hide().insertAfter(imgparent);
});
// ----------------------------------	



// Extract secondary menu	
$('ul:has(a.news)').addClass('secmenu').insertBefore('p:has(.copy)');
$('ul.secmenu li').each(function(index) {
	if (index < 2) { 
	$('<li>::</li>').insertAfter(this);}
});
// ----------------------------------

// Determine active section on load
var activesec = $('.sublist.active').prevAll().length;
$('.section-title:eq('+activesec+')').addClass('active');

// ----------------------------------

// Frontpage handling
if ($('.frontpage').length > 0) {
	$('.frontpage').parent().css('background-image', 'none');
	$('li.section-title, .sublist li a').css({'color': '#777'});
	$('li.section-title.active').removeClass('active');
	
	$('.section-title').click(function() {
		$('.section-title').css({'color': '#777'});
		$(this).css({'color': '#000'});
	});
}
//-----------------------------------

// Fade to front illustration
	$('.section-title').click(function() {
		var count = $(this).parent().prevAll('ul').length+2;
		$('.illu').fadeOut('slow');
		$('.illu.'+count+'').fadeIn('slow');
	});

		var isloaded = $('.illu').attr('complete'); // In case of IE check if image is already cached...

		if (isloaded === true) { // ...if it is skip the preload function and ...
			$('.illu:first').fadeIn('slow');
		} else {
			$('.illu').load(function() {
				$('.illu:first').fadeIn('slow');
			});
		}
//----------------------------------	
	
// Secondary menu and allnews menu handling	
	if ($('.secmenu li.active').length > 0) {
		$('.profilenav *:not(.miscnews)').remove();
		$('.section-title.active').removeClass('active');
	}
	
		if ($('.miscnews').length > 0) {
			$('.section-title.active').removeClass('active');
		}
//---------------------------


// Active menu reversion function
	function junk() {	
		if ($('.sublist li.active').parent().hasClass('active')) {
			return false;
		} else {
			$('.sublist.active').removeClass('active').hide();
			$('.sublist li.active').parent().addClass('active').fadeIn('fast');
			var blah = $('.sublist li.active').parent().prevAll().length;
			$('.section-title.active').removeClass('active');
			$('.section-title:eq('+blah+')').addClass('active');
			}
		}
//---------------------------------


// Revert to active menu point if user does not click client name	

	$('#submenuwrap').mouseout(function() {
		if ($('.sublist li.active').parent().hasClass('active')) {
			return false;
		}
		if ($('.frontpage').length === 0 && ($('.secmenu li.active').length === 0) ) {	
			var timer = setTimeout(junk, 5000);
		}
	});

	$('#menu, #submenuwrap').mouseover(function() {
		clearTimeout(junk);
	});
// ----------------------------------	
		
// Set cv colors as active menu point color

var color = $('.section-title.active').css('border-top-color');
$('.headrow td').css('border-bottom', 'thin solid '+color+'');

// ----------------------------------		
	

// Delete undefined img titles
	$('img').each(function(index) {
		if ($(this).attr('title') == 'undefined') {
			$(this).attr('title', '');
		}
	});
//-------------------------------

// Showreel handling
	if ($('a[href$=.mp4]').length > 0) {
	var mp4url = ($('a[href$=.mp4]').attr('href'));
	var oggurl = mp4url.replace('.mp4','.ogv');
	var settingsattrW = $('a[href$=.mp4]').attr('alt');
	var settingsattrH = $('a[href$=.mp4]').attr('title');
	var ratio = (500/settingsattrW);
	var videolength = $('a[href$=.mp4]').attr('length');
	settingsattrHnew = Math.round(settingsattrH * ratio);
	settingsattrW = 500;
	$('<div id="showreel" class="showreel conbox"><div class="videowrapper"><video id="vidd" width="500" height="'+settingsattrHnew+'"><source src="'+oggurl+'" type="video/ogg"><source src="'+mp4url+'"><applet code="com.fluendo.player.Cortado.class" archive="http://www.finemanagement.dk/files/cortado_latest.jar" width="'+settingsattrW+'" height="'+settingsattrHnew+'"><param name="autoPlay" value="false"/><param name="statusHeight" value="15"/><param name="seekable" value="false"/><param name="duration" value="'+videolength+'"/><param name="url" value="'+oggurl+'"/></applet></video></div></div>').hide().insertBefore('.clear:last');
	$('<div class="showreel button">Showreel</div>').appendTo('.profilenav');
	$('.videowrapper').css('margin-top', ((450-settingsattrHnew)/2)).width(500);
	}
	
	
	$('.showreel.button').click(function() {
		$('.cvlink').hide();
		var v = document.getElementsByTagName("video")[0]; 
		$('.photocredit').hide();
		$('.button.active').removeClass('active');
		$(this).addClass('active');
		$('.conbox.active').hide();
			$('.conbox.active').removeClass('active');
			$('.showreel.conbox').addClass('active').fadeIn('slow', function() {
			
				try { v.play(); }
				catch (err)	{
					document.applets[0].doPlay();
				}
				$(v).attr('controls', 'controls');
			});
			
				$('.button:not(.showreel)').click(function() {
					try { v.pause(); }
					catch (err)	{
						document.applets[0].doStop();
					}
				});
			});

// Enable proper line breaks	
$('p').each(function(index) {
	if ($(this).html() == " ") {
		$(this).html('<br />');
	}
});
//------------------

// Assign specific classes to thumbpage nav
$('.thumbpage').addClass($('.section-title.active').html());
//------------------------------------------


// Add logo
$('<div class="ullogo"><p class="section-titlelogo"><a href="http://www.finemanagement.dk">Fine Management</a></p></div>').insertBefore($('.section-title:first').parent());
//------------------------------------------	
	
	
// Adjust profile menu position
	$('.profilenav .button:last').css('margin-bottom', '0');
		var navpos = $('.profilenav').innerHeight();
		$('.profilenav .button').each(function(index) {
			navpos -= $(this).outerHeight();
			navpos -= parseFloat($(this).css('margin-bottom'));
		});	
		$('.profilenav .button:first').css('margin-top', navpos);
// ----------------------------------

$('.allnews.conbox:first, .profile.conbox:first').show();

// Table styling --------------------
	$('table').attr('cellspacing', '0');
	$('.headrow').next('tr').children().css("padding-top", "5px");
	$('.headrow:not(:first)').children().css("padding-top", "15px");
	$('.cv table').css('cursor', 'text');
// ----------------------------------

// Get rid of top margin on first p element 
if ($('.profiledesc h3').length === 0) {
	$('.profiledesc p:first').css('margin-top','0px');
}
if ($('.newsdesc h3').length === 0) {
	$('.newsdesc p:first').css('margin-top','0px');
}	
//--------------------------------
});
