/*
	Copyright Code Computerlove Ltd 2009-2010
	Build: 1.0.0.107
	Date: 07/01/2010 09:46:12
*/

/*
	Copyright Code Computerlove Ltd 2009
	Build: 1.0.0.5
	Date: 19/05/2009 12:22:56
*/

(function($j) {

	Code.registerNamespace('Website');

	/*
	* Class: Website.Core
	*/
	Website.Core = {

		content: "content-container",
		navigation: "Branding",
		navigationSecondary: "NavigationSecondary",
		carouselContainer: "carousel-container",
		contentSlideContainer: "content-slide-container",
		contentSource: null,
		preloading: false,

		/*
		* Function: onReady
		*/
		onReady: function() {
			var self = this;
			self.initNavigation();
			self.initContent();
		},

		initContent: function() {
			var self = this;

			var content = self.content;

			//put a wrapper around content						
			$j('.' + content).wrap('<div class="' + self.contentSlideContainer + '"></div>');
			$j('.' + content).css({ 'margin-top': '0px', 'height': 'auto' });


			self._onContentClick();
		},

		_onContentClick: function() {

			var self = this;
			var content = self.contentSlideContainer;


			$j('#Container').click(function(evt) {

				//If its an internal link, stop the click and load in through an AJAX call
				var myTarget = $j(evt.target);
				if (myTarget.is('a')) {

					var href = myTarget.attr('href');
					var hrefStart = href.substring(0, 7);
					var hrefEnd = href.substring(href.length - 4);

					//If its a link that doesnt want to preload, leave
					if (myTarget.hasClass('noload') || myTarget.parent().hasClass('noload'))
						return true;

					if (hrefStart == "http://" || hrefStart == "mailto:" || hrefEnd == ".pdf") {
						return true;
					} else {
						self._checkSection(href);
						self.contentSource = "getpagecontentfromurl&url=" + escape(href); //note the admin editor takes the '/' out of the link so added here
						self.preloadContent(); // Perform an AJAX request to load in the content
						return false;
					}
				}
			});
		},

		_loadInternalContent: function(targetEl, event) {

			var self = this;

			//If its an internal link, stop the click and load in through an AJAX call
			var myTarget = $j(targetEl);
			if (myTarget.is('a')) {

				var href = myTarget.attr('href');
				var hrefStart = href.substring(0, 7);
				var hrefEnd = href.substring(href.length - 4);

				if (hrefStart == "http://" || hrefStart == "mailto:" || hrefEnd == ".pdf") {
					return true;
				} else {
					self._checkSection(href);
					self.contentSource = "getpagecontentfromurl&url=" + escape(href); //note the admin editor takes the '/' out of the link so added here
					self.preloadContent(); // Perform an AJAX request to load in the content
					return false;
				}
			}
		},

		_checkSection: function(sectionUrl) {
			var self = this;

			var nav = self.navigation;



			$j('a', '#' + nav).each(function() {
				$j(this).parent().removeClass('active');
				if ($j(this).attr('href') == '/' + sectionUrl) {
					// add active class on main navigation and re-write secondary navigation
					if ($j(this).parent().parent().parent().is('li')) {
						$j(this).parent().parent().parent().addClass('active');
					} else {
						$j(this).parent().addClass('active');
					}
					self.createSectionNav($j(this));
				}
			});

		},

		initMenuClick: function(nav) {
			var self = this;
			//var nav = self.navigation;

			$j('#' + nav).click(function(evt) {
				var myTarget = $j(evt.target);

				//if its an noload link, just carry on
				if (myTarget.parent().hasClass('noload')) {
					return true;
				}

				//if a link, preload content in
				if (myTarget.is('a')) {

					var contentSource = myTarget.attr('href');
					self.contentSource = "getpagecontentfromurl&url=" + contentSource;
					self.preloadContent();

					$j('li', '#' + nav).removeClass('active');

					if (myTarget.parent().parent().parent().is('li')) {
						myTarget.parent().parent().parent().addClass('active');
					} else {
						myTarget.parent().addClass('active');
					}

					/* FLASH CALL */
					//set the video to its relevant section
					var flashSubSection = myTarget.attr('rel');
					if (flashSubSection == "1") {
						window.document.FlashHolder.changeToSubSection1(flashSubSection);
					}
					else if (flashSubSection == "2") {
						window.document.FlashHolder.changeToSubSection2(flashSubSection);
					}
					else if (flashSubSection == "3") {
						window.document.FlashHolder.changeToSubSection3(flashSubSection);
					}
					else if (flashSubSection == "4") {
						window.document.FlashHolder.changeToSubSection4(flashSubSection);
					}
					else if (flashSubSection == "5") {
						window.document.FlashHolder.changeToSubSection5(flashSubSection);
					}
					else if (flashSubSection == "6") {
						window.document.FlashHolder.changeToSubSection6(flashSubSection);
					}
					else if (flashSubSection == "7") {
						window.document.FlashHolder.changeToSubSection7(flashSubSection);
					}
					/* END FLASH CALL*/
				}

				if (nav != self.navigationSecondary) {
					self.createSectionNav(myTarget);
				}
				return false;

			});

		},

		/* FLASH TO JS */
		toSubSection: function(subSection) {
			var self = this;

			//get the 'rel' related to the subsection
			$j('a', '#' + self.navigation).each(function() {
				var linkRel = $j(this).attr('rel');

				if (linkRel == toSubSection) {
					//load the content
					var contentSource = $j(this).attr('href');
					self.contentSource = "getpagecontentfromurl&url=" + contentSource;
					self.preloadContent();

					$j('li', '#' + nav).removeClass('active');

					if ($j(this).parent().parent().parent().is('li')) {
						$j(this).parent().parent().parent().addClass('active');
					} else {
						$j(this).parent().addClass('active');
					}
				}

			});

		},

		toUrl: function(url) {
			var self = this;

			//load the content for this url
			self.contentSource = "getpagecontentfromurl&url=" + url;
			self.preloadContent();

		},
		/* END FLASH TO JS */

		preloadContent: function() {
			var self = this;
			var contentSource = self.contentSource;
			var content = self.content;
			var contentWidth = $j('.' + content).width();
			var carouselWidth = parseInt(contentWidth) * 2;

			if (!self.preloading) {
				self.preloading = true;

				$j('.' + self.contentSlideContainer).wrapInner('<div class="' + self.carouselContainer + '" style="width: ' + carouselWidth + 'px; position: absolute; left: 0; top: 0;"></div>');

				//add a loader
				$j('.' + self.contentSlideContainer).prepend('<div class="loader" style="background: #363428; opacity: 0.8;"></div>');
				$j('.' + self.contentSlideContainer).prepend('<div class="image-loader"><img src="/_Client/Images/Global/AjaxLoader.gif" alt="loading" /><p>Loading...</p></div>');

				//load in source to a temporary container off stage
				$j.ajax({
					type: "GET",
					url: "/Handlers/GenericHandler.ashx?action=" + contentSource,
					dataType: "html",
					success: function(data) {
						var contentToLoad = $j(data);
						$j('.' + self.carouselContainer).append('<div class="' + content + ' ' + content + '-active" style="margin-top: 0; height: auto;">' + data + '</div>');

						//slide content in
						$j('.' + self.carouselContainer).animate({
							left: "-=964px"
						},
						1000, "", function() {

							self.resetToOriginal();

						}
						);

					}
				});

			}

		},

		//do fade out of loader and remove all wrappers
		resetToOriginal: function() {
			var self = this;
			self.preloading = false;
			var content = self.content;

			$j('.loader').fadeOut('fast');
			$j('.image-loader').fadeOut('fast');
			$j('.loader').remove();
			$j('.image-loader').remove();

			var currentContent = $j('.' + content + '-active').html();
			var relayContent = ('<div class="' + content + '" style="margin-top: 0; height: auto;" >' + currentContent + '</div>');

			$j('.' + self.carouselContainer).remove();
			$j('.' + self.contentSlideContainer).append(relayContent);

		},

		initNavigation: function() {
			var self = this;

			// activate the content loading functions
			var nav = self.navigation;

			self.initMenuClick(nav);

			var activeClass = false;
			var currentClasses = "";

			function getClass(str) {
				var currentClasses = str.split(" ");

				for (var i = 0; i < currentClasses.length; i++) {
					if (currentClasses[i] == "active") {
						activeClass = true;
					} else {
						activeClass = false;
					}
				}

			}

			$j('li', '#' + nav).hover(function(evt) {

				getClass($j(this).attr('class'));


				if (activeClass) {
					$j(this).addClass('active-hover');
				} else {
					$j(this).addClass('hover');
				}

			}, function(evt) {

				//if(activeClass){
				$j(this).removeClass('active-hover');
				//}else{
				$j(this).removeClass('hover');
				//}

			});

		},

		createSectionNav: function(clicked) {
			var self = this;
			var nav = self.navigation;
			var activeNav = $j('li.active', '#' + nav);
			var sectionNavContainer = ('<div id="' + self.navigationSecondary + '"></div>');
			var sectionNav = ('<h2>Navigate to:</h2><ul>');
			var activeClass = ('');
			var navItems = false;

			if (!$j('#' + self.navigationSecondary).size()) {
				$j('#ContentPrimary').append(sectionNavContainer);
			}

			$j('a', activeNav).each(function(index) {

				//Set the active class if this section and retain noload class
				if ($j(this).attr('href') == clicked.attr('href')) {
					if ($j(this).parent().hasClass('noload')) {
						activeClass = ('class="active noload"');
					}
					else {
						activeClass = ('class="active"');
					}
				} else {
					if ($j(this).parent().hasClass('noload')) {
						activeClass = ('class="noload"');
					}
					else {
						activeClass = ('');
					}
				}

				sectionNav += ('<li ' + activeClass + '><a href="' + $j(this).attr('href') + '">' + $j(this).text() + '</a></li>');

				if (index > 0) {
					navItems = true;
				}

			});

			sectionNav += ('</ul>');

			if (navItems) {

				$j('#' + self.navigationSecondary).html(sectionNav);
				$j('ul', '#' + self.navigationSecondary).fadeIn('fast');
				$j('li:first', '#' + self.navigationSecondary).addClass('first');
				$j('li:last', '#' + self.navigationSecondary).addClass('last');

				// init secondary nav click
				self.initMenuClick(self.navigationSecondary);
			} else {
				$j('#' + self.navigationSecondary).html('');
			}
		}

	};

	$j().ready(function() {
		Website.Core.onReady();
	});


})(jQuery);	

