
//  run jQuery in noConflict mode to avoid conflict with scriptalicious
	jQuery.noConflict();
	var $j=jQuery.noConflict();
	
//  menu functions
	function MH_ShowMenu(pThis) {
		$j(pThis)
			.next().css( {'visibility': 'visible'} )
			.css( {'left': $j(pThis).position().left + 164 + 'px' } )
			.css( {'top': $j(pThis).position().top + 'px' });
	}
	function MH_HideMenu (pThis, pEvent) {
		xSubMenu = $j(pThis).next();
		if ((pEvent.pageX < xSubMenu.offset().left) || (xSubMenu.children().length == 0)) {
			$j(pThis)
				.next().css( {'visibility': 'hidden'} );
		}
	}
	function MH_HideSubMenu(pThis) {
		$j(pThis)
			.css( {'visibility': 'hidden'} );
	}
	
//	video functions

	function mhRight(str, n){
		if (n <= 0)
		   return "";
		else if (n > String(str).length)
		   return str;
		else {
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
	}

	function getDocHeight() {
		var D = document;
		return Math.max(
			Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
			Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
			Math.max(D.body.clientHeight, D.documentElement.clientHeight)
		);
	}

	// functions to play and stop video
	function mhShowVideo(pEmbed, pTitle, pType) {
		document.getElementById('mhVideoTitle').innerHTML = pTitle;
		if (pType == 'yt') {
			xEmbed = '<object width="445" height="364" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="movie" value="http://www.youtube.com/v/' + pEmbed 
				+ '&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>'
				+ '<embed src="http://www.youtube.com/v/' + pEmbed + '&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" '
				+ 'allowfullscreen="true" width="445" height="364"></embed></object>';
		}
		if (pType == 'wmv') {
			xEmbed = '<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" width="445" height="364">'
				+ '<param name="Filename" value="' + pEmbed + '">'
				+ '<embed id="m1" name="m1" src="' + pEmbed + '" type="application/x-mplayer2" width="445" height="364"></embed></object>';
		}
		if (pType == 'fp') {
			// xEmbed = '<a href="/media/upload/media/CP_Intro.mov" style="display:block;width:445px;height:334px;" id="fpplayer"><img src="/media/upload/image/CP_VideoLarge.jpg" alt="" style="width: 445px; height: 334px" /></a>';
			xEmbed = '<a href="' + pEmbed + '" style="display:block;width:445px;height:334px;" id="fpplayer"><img src="/media/upload/image/CP_VideoLarge.jpg" alt="" style="width: 445px; height: 334px" /></a>';
		}
		document.getElementById('mhVideoPlayer').innerHTML = xEmbed;
		if (pType == 'fp') {
			flowplayer("fpplayer", "/skin/frontend/maupinhouse/default/flowplayer-3.2.4.swf", {
				plugins: {
					controls: {
						stop: true
					}
				}

			});
		};
	
		// Set up the blue background
		var xVideoBlue = document.getElementById('mhVideoBlue');
		xVideoBlue.style.height = getDocHeight().toString() + 'px';
		xVideoBlue.style.visibility = 'visible';
		
		
		// Center the video player window
		var xVideoWindow = document.getElementById('mhVideoWindow');
		xVideoWindow.style.left = ((document.documentElement.clientWidth - 485) / 2).toString() + 'px';
		xVideoWindow.style.top = (document.documentElement.scrollTop + 20).toString() + 'px';
		
		// Show the video player window
		xVideoWindow.style.visibility = 'visible';
	}
			
	function mhCloseVideo() {
		document.getElementById('mhVideoPlayer').innerHTML = ''
		document.getElementById('mhVideoWindow').style.visibility = 'hidden';
		document.getElementById('mhVideoBlue').style.visibility = 'hidden';
	}

	// Attach the video player to the av links in the specified ID
	function mhVideoSetup(pDivID, pImages) {
		if (document.getElementById(pDivID)) {
			var xDiv = document.getElementById(pDivID);
			xNodes = xDiv.getElementsByTagName('A');
			for (var i = 0;  i < xNodes.length; i++) {
				xNode = xNodes[i];
				if (xNode.href.indexOf('youtube') > 0) {
					xYouTubeID = mhRight(xNode.href, 11);
					xTitle = xNode.innerHTML.replace("'", "&acute;");
					xNode.href = "javascript:mhShowVideo('" + xYouTubeID + "','" + xTitle + "','yt')";
					if (pImages) {
						newImg = document.createElement('img');
						newImg.setAttribute('src', 'http://i.ytimg.com/vi/' + xYouTubeID + '/2.jpg');
						xNode.insertBefore(newImg, xNode.firstChild);
					}
				};
				if (xNode.href.indexOf('.wmv') > 0) {
					xTitle = xNode.innerHTML.replace("'", "&acute;");
					xNode.href = "javascript:mhShowVideo('" + xNode.href + "','" + xTitle + "','wmv')";
					if (pImages) {
						newImg = document.createElement('img');
						newImg.setAttribute('src', '/skin/frontend/default/maupinhouse/images/Listen.gif');
						xNode.insertBefore(newImg, xNode.firstChild);
					}
				}
				if (xNode.href.indexOf('.mp3') > 0) {
					xTitle = xNode.innerHTML.replace("'", "&acute;");
					xNode.href = "javascript:mhShowVideo('" + xNode.href + "','" + xTitle + "','wmv')";
					if (pImages) {
						newImg = document.createElement('img');
						newImg.setAttribute('src', '/skin/frontend/default/maupinhouse/images/Listen.gif');
						xNode.insertBefore(newImg, xNode.firstChild);
					}
				}
				if (xNode.href.indexOf('.mov') > 0) {
					xTitle = xNode.innerHTML.replace("'", "&acute;");
					if (xTitle.substr(0,4) == '<img') {
						xTitle = 'Video';
					};
					xNode.href = "javascript:mhShowVideo('" + xNode.href + "','" + xTitle + "','fp')";
				}
			};
		};
	}
			
//	download functions

	function mhCloseDNL() {
		document.getElementById('mhEmailDownload').style.visibility = 'hidden';
	};
	
	function mhCloseDGN() {
		document.getElementById('mhGraphicDownload').style.visibility = 'hidden';
	};
	
	function mhOpenDNL(pIndex) {
		
		// put the filename into the form
		document.getElementById('DNL_FileName').value = gURLs[pIndex];
		
		// Center the window
		var xEmailWindow = document.getElementById('mhEmailDownload');
		xEmailWindow.style.left = ((document.documentElement.clientWidth - 495) / 2).toString() + 'px';
		xEmailWindow.style.top = (document.documentElement.scrollTop + 60).toString() + 'px';
		
		// And show it
		xEmailWindow.style.visibility = 'visible';
	}
			
	function mhOpenDGN(pIndex) {
		
		// put the filename into the form
		document.getElementById('DGN_FileName').value = gURLs[pIndex];
		
		// Center the window
		var xEmailWindow = document.getElementById('mhGraphicDownload');
		xEmailWindow.style.left = ((document.documentElement.clientWidth - 485) / 2).toString() + 'px';
		xEmailWindow.style.top = (document.documentElement.scrollTop + 60).toString() + 'px';
		
		// And show it
		xEmailWindow.style.visibility = 'visible';
	}
			
	// attach the download form to the pdf links
	
	var gURLs = new Array();
	
	function mhDownloadSetup() {
	
		gBaseURL1 = 'maupinhouse.com/media/upload/file/CraftPlus';
		gBaseURL2 = 'maupinhouse.com/media/upload/file/Graphic_Novels';	

		xNodes = document.getElementsByTagName('A');
		for (var i = 0;  i < xNodes.length; i++) {
			xNode = xNodes[i];
			if (xNode.href.substring(0, gBaseURL1.length + 7) == 'http://' + gBaseURL1) {
				gURLs[i] = xNode.href;
				xNode.href = 'javascript:mhOpenDNL(' + i + ')';
			}
			if (xNode.href.substring(0, gBaseURL1.length + 11) == 'http://www.' + gBaseURL1) {
				gURLs[i] = xNode.href;
				xNode.href = 'javascript:mhOpenDNL(' + i + ')';
			}
			if (xNode.href.substring(0, gBaseURL1.length + 8) == 'https://' + gBaseURL1) {
				gURLs[i] = xNode.href;
				xNode.href = 'javascript:mhOpenDNL(' + i + ')';
			}
			if (xNode.href.substring(0, gBaseURL1.length + 12) == 'https://www.' + gBaseURL1) {
				gURLs[i] = xNode.href;
				xNode.href = 'javascript:mhOpenDNL(' + i + ')';
			}
			if (xNode.href.substring(0, gBaseURL2.length + 7) == 'http://' + gBaseURL2) {
				gURLs[i] = xNode.href;
				xNode.href = 'javascript:mhOpenDGN(' + i + ')';
			}
			if (xNode.href.substring(0, gBaseURL2.length + 11) == 'http://www.' + gBaseURL2) {
				gURLs[i] = xNode.href;
				xNode.href = 'javascript:mhOpenDGN(' + i + ')';
			}
			if (xNode.href.substring(0, gBaseURL2.length + 8) == 'https://' + gBaseURL2) {
				gURLs[i] = xNode.href;
				xNode.href = 'javascript:mhOpenDGN(' + i + ')';
			}
			if (xNode.href.substring(0, gBaseURL2.length + 12) == 'https://www.' + gBaseURL2) {
				gURLs[i] = xNode.href;
				xNode.href = 'javascript:mhOpenDGN(' + i + ')';
			}
		};
	
	}
	
//  embedded video on the CraftPlus page

	function mhPlayCPVideo(pAutoStart) {
		flowplayer("cpvideoPlayer", "/skin/frontend/maupinhouse/default/flowplayer-3.2.4.swf", {	
			plugins: {
				controls: {
					stop: pAutoStart
				}
			}
		}) 
	}
	
//  page initialization
	$j(window).load(function() {
		
		// attach handles to menus
		$j('#mhMenu div').each(function() {
			$j(this)
				.mouseleave(function() { MH_HideSubMenu(this) })
				.prev()
				.mouseenter(function() { MH_ShowMenu(this) })
				.mouseleave(function(event) { MH_HideMenu(this,event) });
		});

		// attach video players to the contents of the Videos & Podcasts page
		mhVideoSetup('mhVideoPage', false);
		// attach video players to the contents of the left-hand media box
		mhVideoSetup('mhVideos', true);
		// attach video players to the contents of the CraftPlus home page
		mhVideoSetup('mhCraftPlusHome', false);
		
		// attach the download form to pdf links
		mhDownloadSetup();
		
		// set up the embedded video on the CraftPlus page
		$j('#cpvideoSelect').change(function () {
			xValue = this.value;
			if (xValue.length != 0) {
				//document.getElementById('cpvideoPlayer').href='/media/upload/media/' + xValue;
				document.getElementById('cpvideoBox').innerHTML = '<a id="cpvideoPlayer" style="display: block; width: 480px; height: 360px;" href="/media/upload/media/' + xValue + '"></a>';
				document.getElementById('cpvideoCaption').innerHTML = 'Watch these brief videos<br />about the CraftPlus curriculum.';
				mhPlayCPVideo(false);
			}
		});
		mhPlayCPVideo(true);
		
	});
