var $jQuery = jQuery.noConflict();	/* Remove Javascript libary conflicts between Prototype and $jQuery */


$jQuery(function() {

	populateElement('#search', 'Search BMJ Masterclasses');
	
	populateElement('#groupSearchGadget', 'Search all BMJ Products');

	popUp(); /* All A tags with a reference 'rel="external"' will be opened in a pop-up window */ 



	/* The $jQuery script below inserts sections from the smart folder generated collection pages */


	if ($jQuery(".tophome").length >= 1)  /* The $jQuery script below inserts sections from the smart folder generated collection pages if home page identified by class tophome */
	{
		$jQuery("#region-content div.details").each(function (index) {

		/* create a div for the section to be inserted */
		$jQuery('#region-content div.details .tileBody').eq(index).after('<div class="hsection"></div>');

		/* the URL of the smart folder collection page */
		url = $jQuery("#region-content div.details a.summary").eq(index).attr("href");

		/* A section of the smart folder collection page which will be inserted on the home page */
		page_section = url + ' #region-content #up-and-coming-events .details:lt(2) > *:not(".tileBody"):not(".links")';

		/* Insert the collection section HTML content at the home page */
		$jQuery("#region-content div.details div.hsection").eq(index).load(page_section);

	      })
	}



	/* Ajax page section downloads for event page tabs */
	/* move the event nav outside the event body to be replaced with body content with one of the tabs */



	if ($jQuery("#region-content .vevent").length >= 1)  /* specific for event page types only */
	{

			$jQuery('ul.event-nav').insertBefore(".eventbody");

			$jQuery(".eventDetails ~ ul.event-nav li a").each(function (index) {

			/* Replace body section - specific for event content type pages */
			$jQuery('ul.event-nav li a').eq(index).click(function () { 

				if ($jQuery('ul.event-nav li a').eq(index).text()=="Overview") {
			                /* This is specific for the Overiew tab - the URL of the tab page to be inserted */
					purl = $jQuery("ul.event-nav li.event-nav a").eq(index).attr("href");	
				
					/* section of the programme page to be inserted */
					page_section = purl + ' #region-content .eventbody > :not("ul.event-nav") ';	
			
					/* create a new DOM to insert the Overview section page */
					$jQuery('<div class="event3body"></div>').insertBefore(".eventbody");
					/* remove the redundant eventbody class to avoid conflict */
					$jQuery('.event3body ~ .eventbody').remove();
						
					/* Insert the collection section HTML content at the default page */
					$jQuery('.event3body').load(page_section);
					$jQuery('div.event3body').addClass('eventbody');
					return false;				
				}			

				/* this condition has been added to sort out IE 6 & 7 the Register now tab link issue */
				else if (($jQuery('ul.event-nav li a').eq(index).text()=="Register now") && ($jQuery('ul.event-nav a[@href^="https://"]').length >=1))  {
			                /* the URL of the tab page to be inserted */
					purl = $jQuery("ul.event-nav li.event-nav a").eq(index).attr("href");	
		
					/* section of the programme page to be inserted */
					page_section = purl + ' #region-content > :not("#portal-breadcrumbs") ~ :not("div.documentActions") ';		

					/* Insert the collection section HTML content at the default page */
					$jQuery('.eventbody').load(page_section);
					//return false;
				}
				else {
			                /* the URL of the tab page to be inserted */
					purl = $jQuery("ul.event-nav li.event-nav a").eq(index).attr("href");	
		
					/* section of the programme page to be inserted */
					page_section = purl + ' #region-content > :not("#portal-breadcrumbs") ~ :not("div.documentActions") ';		

					/* Insert the collection section HTML content at the default page */
					$jQuery('.eventbody').load(page_section);
					return false;		
				}
		       });

		});
	}
	else {  /* specific for non event type pages */
	
		/* Ajax page section downloads for temporary event page (content type page) tabs */
		/* move the event nav outside the event body to be replaced with body content with one of the tabs */
		$jQuery('<div class="event2body"></div>').insertBefore(".plain ul.event-nav"); 
		$jQuery('.event2body').prepend( $jQuery('ul.event-nav ~ *'));
		$jQuery('ul.event-nav').insertBefore(".event2body"); 

		$jQuery(".plain ul.event-nav li a").each(function (index) {

			/* Replace body section - specific for event content type pages */
			$jQuery('ul.event-nav li a').eq(index).click(function () { 

				if ($jQuery('ul.event-nav li a').eq(index).text()=="Overview") {
				
					purl = $jQuery("ul.event-nav li.event-nav a").eq(index).attr("href");

					page_section = purl + ' div.plain > :not("ul.event-nav"):not("div.hsection")';
					
					$jQuery('.event2body').load(page_section);				
					return false;				
				
				}

				else if ($jQuery('ul.event-nav li a').eq(index).text()=="Register now") {
	
			                /* the URL of the tab page to be inserted */
					purl = $jQuery("ul.event-nav li.event-nav a").eq(index).attr("href");
		
					/* section the programme page to be inserted */
					page_section = purl + ' #region-content > :not("#portal-breadcrumbs") ~ :not("div.documentActions") ~ :not("h1.documentFirstHeading")';		
			
					/* Insert the collection section HTML content at the default page */
					$jQuery('.event2body').load(page_section);
					return false;
				}
				else {
				
			                /* the URL of the tab page to be inserted */
					purl = $jQuery("ul.event-nav li.event-nav a").eq(index).attr("href");
		
					/* section the programme page to be inserted */
					page_section = purl + ' #region-content > :not("#portal-breadcrumbs") ~ :not("div.documentActions") ~ :not("h1.documentFirstHeading")';		
			
					/* Insert the collection section HTML content at the default page */
					$jQuery('.event2body').load(page_section);
					return false;
				
				}

		       });

		});
	}
})


