// JavaScript document for Dorte Hyldsrup website

// function to append album cover image to music player
function appendMusicPlayerImg () {
	// get the mp3 player interface
	MPInterface = jQuery('#sideRight #playerMultipleList');
	MPInterfaceExists = MPInterface.length;	
	if (MPInterfaceExists) {
		// create image
		var albumImg = jQuery('<img />');
		albumImg.attr('src', _themeBaseURL + '/images/album_cover.jpg');
		albumImg.css('margin', '0 29px 0 29px');
		albumImg.css('position', 'relative');
		albumImg.css('top', '-175px');
		MPInterface.append(albumImg);
		// adjust margin of container
		MPInterface.css('margin-bottom', '-140px');
	} 	
}

function validateiiiNewsLetterForm () {
	// get newsletter form
	iiiNewsLetterFormE = jQuery('#frm_subscription');
	iiiNewsLetterFormEExists = iiiNewsLetterFormE.length;
	if (iiiNewsLetterFormEExists) {
		textboxE = iiiNewsLetterFormE.find('INPUT[type="text"]');		
		if (textboxE.val() == textboxE.attr('title') || textboxE.val() == '') {
			return false;
		}
	}	
	// disable form elements
	iiiNewsLetterFormE.find('BUTTON[type="submit"]').attr('disabled', 'disabled');
	iiiNewsLetterFormE.find('INPUT[type="text"]').attr('readonly', 'readonly');
	jQuery('BODY').css('cursor', 'progress');
	
	// send form via ajax
	postData = { 
		'txt_email' : textboxE.val()
		};
	jQuery.post(iiiNewsLetterFormE.attr('action'), postData, function (data) {		
		jQuery('BODY').css('cursor', 'default');
		// display confirmation message
		switch (icl_lang) {
			case 'en': {
				var msg = 'Thank you';
				break;
			}	
			case 'da': {
				var msg = 'Tak';
				break;
			}
		}
		
		iiiNewsLetterFormE.find('INPUT[type="text"]').val(msg);
		
	}, 'html');
	
	
	return false;
}

// function to adjust height of mp3 player interface
function adjustMP3Player () {
	mp3ObjE = jQuery('#playerMultipleList > OBJECT');
	mp3ObjEExists = mp3ObjE.length;
	mp3EmbedE = jQuery('#playerMultipleList EMBED');
	mp3EmbedEExists = mp3EmbedE.length;	
	
	// get number of mp3 files
	//		then adjust player height depending on number of mp3 files
	
	jQuery.get(_themeBaseURL + '/../../plugins/mp3-player-plugin-for-wordpress/file_count.php?rand=' + Math.random(), function(data) {
		mp3Count = data;		
		mp3Count = (mp3Count <= 9) ? (Number(mp3Count) + 1) : mp3Count;
		
		heightSingleAdj = (mp3Count > 9) ? 17 + ((mp3Count - 9) * 0.25) : 17;
		heightSingleAdj = (mp3Count > 13) ? 17 + ((mp3Count - 13) * 0.25) : heightSingleAdj;
		heightSingleAdj = (mp3Count > 15) ? 17 + ((mp3Count - 15) * 0.25) : heightSingleAdj;
		heightSingleAdj = (mp3Count > 20) ? 17 + ((mp3Count - 20) * 0.05) : heightSingleAdj;
		heightAdj = mp3Count * heightSingleAdj;	
		
		if (mp3ObjEExists) {
			mp3ObjE.height(Number(mp3ObjE.height()) + heightAdj);
		}
		if (mp3EmbedEExists) {
			mp3EmbedE.height(Number(mp3EmbedE.height()) + heightAdj);
		}
	});
	
	
}


// fix layout on the links box
function adjustLinks () {
	linksContainerE = jQuery('#sideRight > .sideBox > UL.xoxo');
	linksContainerEExists = linksContainerE.length;
	
	if (linksContainerEExists) {
		// find all li element
		linkItemEs = linksContainerE.find('LI');
		// move anchor tags to the end of the LI element
		linkItemEs.each(function () {
			anchorE = jQuery(this).find('A');	
			anchorEExists = anchorE.length;
			if (anchorEExists) {
				anchorE.remove();
				jQuery(this).append('<br />');
				jQuery(this).append('<br />');
				jQuery(this).append(anchorE);
			}
			
			jQuery(this).append('<br />');
			jQuery(this).append('<br />');
		});
	}	
	
}

jQuery(function() {	
	appendMusicPlayerImg();
	adjustMP3Player();
	adjustLinks();
});
