/*
	Site:		Bruntwood: Website
	File:		/assets/scripts/global.js
	Version:	2009-05-28
	Author: 	
----------------------------------------------- */

/*	Global Configuration
----------------------------------------------- */
jQuery.noConflict();
var $j = jQuery;
var isIE6 = false;
var isSafari = false;

/*	Global Literal Object - Site-wide functions
----------------------------------------------- */
var Global = {

	/* Configuration */

	/* jQuery Object References to the Elements this speeds up the DOM */
	jOENavigationPrimary: null,

	/* Utility: Browser Tests / Specific Fixes/Hooks */
	utilBrowserTests : function() {

		/* Detect if User Agent is IE6 using object detection, apply fix for background flicker bug */
		if (typeof document.body.style.maxHeight == 'undefined') { isIE6 = true; try { document.execCommand('BackgroundImageCache', false, true); } catch(e) { } }

		/* Detect if User Agent is Safari, add class '.safari' to div#Container */
		if ($j.browser.safari) { isSafari = true; $j('div#Container').addClass('safari'); }

	},

	/* Utility: External Links */
	utilExternalLinks : function() {
		$j('a.external').each(function() {
			var jOEThis = $j(this);
			jOEThis.click(function() {
				return !window.open(jOEThis.attr('href'));
			});
		});
	},

	/* Utility: Navigation Primary li:hover */
	eventsNavigationPrimary : function() {

		/* Class Context */
		var cc = this;

		$j('ul li', cc.jOENavigationPrimary).hover(function() {
			$j('ul', this).css('left', '0');
		}, function() {
			$j('ul', this).css('left', '-9999px');
		});

		/* If User Agent is IE6 apply bgiframe for z-index bug */
		if (isIE6) {
			$j('ul li', cc.jOENavigationPrimary).bgiframe();
		}

	},

	/* Initialise */ 
	init : function() {

		/* Class Context */
		var cc = this;

		/* Configuration */

		/* jQuery Object References to the Elements this speeds up the DOM */
		cc.jOENavigationPrimary = $j('div#NavigationPrimary');

		cc.utilBrowserTests();
		cc.eventsNavigationPrimary();

	}

};

/*	Plugins Literal Object: jQuery Plugins
----------------------------------------------- */
var Plugins = {

	jDelay : function() {

		/*	jQuery Plugin: delay
			http://james.padolsey.com/javascript/jquery-delay-plugin/
		----------------------------------------------- */
		jQuery.fn.delay = function(time, callback){
			jQuery.fx.step.delay = function(){};
			return this.animate({delay:1}, time, callback);
		};

	},

	jCarouselLite : function() {

		/*	jQuery Plugin: jCarouselLite
			Copyright (c) 2007 Ganeshji Marwaha (gmarwaha.com)
			Dual licensed under the MIT and GPL licenses:
			http://www.opensource.org/licenses/mit-license.php
			http://www.gnu.org/licenses/gpl.html
			http://gmarwaha.com/jquery/jcarousellite/
		----------------------------------------------- */
		(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v}var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");f.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var g=o.vertical?height(f):width(f);var h=g*itemLength;var j=g*v;f.css({width:f.width(),height:f.height()});ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");if(o.btnPrev)$(o.btnPrev).click(function(){return go(curr-o.scroll)});if(o.btnNext)$(o.btnNext).click(function(){return go(curr+o.scroll)});if(o.btnGo)$.each(o.btnGo,function(i,a){$(a).click(function(){return go(o.circular?o.visible+i:i)})});if(o.mouseWheel&&c.mousewheel)c.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll)});if(o.auto)setInterval(function(){go(curr+o.scroll)},o.auto+o.speed);function vis(){return f.slice(curr).slice(0,v)};function go(a){if(!b){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(a<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll}else if(a>=itemLength-v+1){ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll}else curr=a}else{if(a<0||a>itemLength-v)return;else curr=a}b=true;ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")}}return false}})};function css(a,b){return parseInt($.css(a[0],b))||0};function width(a){return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')};function height(a){return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')}})(jQuery);

	},

	init : function(){

		/* Class Context */
		var cc = this;

		cc.jDelay();
		cc.jCarouselLite();

	}

};

/*	Component: Slideshow
----------------------------------------------- */
var ComponentSlideshow = {

	/* Configuration */
	cSlideshowRotate: null,
	cSlideshowImageCount: null,
	cSlideshowImageCurrent: null,
	cSlideshowThumbCount: null,
	cSlideshowThumbCurrent: null,

	/* jQuery Object References to the Elements this speeds up the DOM */
	jOESlideshow: null,
	jOESlideshowThumbs: null,
	jOESlideshowThumbsNav: null,

	/* clickSlideshow - non-carousel version < 5 images */
	clickSlideshow : function() {

		/* Class Context */
		var cc = this;

		$j('li', cc.jOESlideshowThumbs).click(function() {
			var jOEThis = $j(this);

			/* If current li is active return */
			if (jOEThis.hasClass('active')) {
				return;
			}

			/* Clear current Slideshow rotation */
			clearInterval(cc.cSlideshowRotate);

			/* Remove current active classes */
			$j('li.active', cc.jOESlideshow).removeClass('active');
			$j('p.active', cc.jOESlideshow).removeClass('active');

			var cssClassesStr = jOEThis.attr('class').toString();
			var cssClasses = cssClassesStr.split('slideshow-image-');

			/* Set cc.cSlideshowImageCurrent to new image */ 
			cc.cSlideshowImageCurrent = cssClasses[1];

			var activeImageClass = 'p.slideshow-image-' + cc.cSlideshowImageCurrent;
			var activeThumbClass = 'li.slideshow-image-' + cc.cSlideshowImageCurrent;

			$j(activeThumbClass, cc.jOESlideshowThumbs).addClass('active');
			$j(activeImageClass, cc.jOESlideshow).css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
				$j('p:not(.active)', cc.jOESlideshow).css({opacity: 0.0});
			});

			/* Define startSlideshow to rotate every 4 seconds */
			cc.cSlideshowRotate = setInterval(function () { ComponentSlideshow.rotateSlideshow(); }, 4000);

		});

	},

	/* rotateSlideshow - non-carousel version < 5 images */
	rotateSlideshow : function() {

		/* Class Context */
		var cc = this;

		cc.cSlideshowImageCurrent++;

		var activeImageClass = 'p.slideshow-image-' + cc.cSlideshowImageCurrent;
		var activeThumbClass = 'li.slideshow-image-' + cc.cSlideshowImageCurrent;

		/* Current active image and thumbnail */
		var $activeImage = $j('p.active', cc.jOESlideshow);
		/* If Slideshow has more than 1 image, enable image thumbnails functionality */
		var $activeThumb = $j('li.active', cc.jOESlideshow);

		/* If activeImage empty set to last image */
    	if ( $activeImage.length === 0 ) {
			$activeImage = $j('p:last', cc.jOESlideshow);
		}

		/* If activeImage empty set to last thumb */
    	if ( $activeThumb.length === 0 ) {
			$activeThumb = $j('li:last', cc.jOESlideshow);
		}

	    /* Images are displayed in order of Markup */
		var $nextImage = $activeImage.next().length ? $activeImage.next() : $j('p:first', cc.jOESlideshow);
		var $nextThumb = $activeThumb.next().length ? $activeThumb.next() : $j('li:first', cc.jOESlideshow);

		/* Add classes to show which was last active */
		$activeImage.addClass('last-active');
		$activeThumb.addClass('last-active');

		/* Asign new class to next thumb and image */
		$activeThumb.removeClass('active last-active');
		$nextThumb.addClass('active');
		$nextImage.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
			$activeImage.removeClass('active last-active').css({opacity: 0.0});
		});

	},

	/* changeSlideshowCarousel - carousel version > 5 images */
	changeSlideshowCarousel : function(curr) {

		/* Class Context */
		var cc = this;

		var jThis = $j(curr);
		var jThisClass = jThis.attr('class').toString();
		$j('.' + jThisClass).addClass('active');

		var jThisClassSplit = jThisClass.split('slideshow-image-');

		/* Set cc.cSlideshowImageCurrent to new image */
		cc.cSlideshowImageCurrent = jThisClassSplit[1];

		/* Remove current active class */
		$j('.c-slideshow p.slideshow-image-' + cc.cSlideshowImageCurrent).css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
			$j('.c-slideshow p:not(.slideshow-image-' + cc.cSlideshowImageCurrent + ')').css({opacity: 0.0}).removeClass('active');
		});

	},

	/* clickSlideshowCarousel - non-carousel version > 5 images */
	clickSlideshowCarousel : function() {

		/* Class Context */
		var cc = this;

		$j('.c-slideshow-carousel-nav-prev-2').click(function() {
			/* Clear current Slideshow rotation */
			$j().stop({clearQueue:true,gotoEnd:false});
			clearInterval(cc.cSlideshowRotate);
			/* Simular double click on prev */
			$j('.c-slideshow-carousel-nav-prev').click().delay(200, function() {
				$j('.c-slideshow-carousel-nav-prev').click();
			});
		});

		$j('.c-slideshow-carousel-nav-next-2').click(function() {
			/* Clear current Slideshow rotation */
			$j().stop({clearQueue:true,gotoEnd:false});
			clearInterval(cc.cSlideshowRotate);
			/* Simular double click on next */
			$j('.c-slideshow-carousel-nav-next').click().delay(200, function() {
				$j('.c-slideshow-carousel-nav-next').click();
			});
		});

	},

	/* rotateSlideshowCarousel - carousel version > 5 images */
	rotateSlideshowCarousel : function() {

		/* Class Context */
		var cc = this;

		$j('.c-slideshow-carousel').jCarouselLite({
			btnNext: '.c-slideshow-carousel-nav-next',
			btnPrev: '.c-slideshow-carousel-nav-prev',
			visible: 5,
			start: cc.cSlideshowImageCurrent,
			vertical: true,
			beforeStart : function(curr) {
				/* Remove current active classes and apply last-active */
				$j('.active', cc.jOESlideshow).removeClass('active');
				/* Clear current Slideshow rotation */
				$j().stop({clearQueue:true,gotoEnd:false});
				clearInterval(cc.cSlideshowRotate);
			},
			afterEnd : function(curr) {
				cc.changeSlideshowCarousel(curr);
				cc.cSlideshowRotate = setInterval(function () { $j('.c-slideshow-carousel-nav-next').click(); }, 4000);
			}
		}).height('140px');

		/* Bind clicks */
		cc.clickSlideshowCarousel();

		/* Define cSlideshowRotate to rotate every 4 seconds by simulating a click */
		cc.cSlideshowRotate = setInterval(function () { $j('.c-slideshow-carousel-nav-next').click(); }, 4000);

	},

	/* buildSlideshow */
	buildSlideshow : function() {

		/* Class Context */
		var cc = this;

		/* Only run slideshow if more than 1 image, enable image thumbnails */
		if (cc.cSlideshowImageCount > 1) {

			/* Build Thumbs UL by itterating each .c-h p */
			var aSlideshowThumbs = [];
			cc.cSlideshowThumbCount = 0;

			/* For each image add item to aSlideshowThumbs array */
			$j('p img', cc.jOESlideshow).each(function(i) {
				cc.cSlideshowThumbCount++;
				var jOEThis = $j(this);
				aSlideshowThumbs[i] = '<li class="slideshow-image-' + cc.cSlideshowThumbCount + '"><img alt="" height="40" src="' + jOEThis.attr('src') + '" width="55" /><span class="thumb-cover"></span></li>';
			});

			/* If more than 5 images, enable thumbnail scoll / clicks */
			if (cc.cSlideshowImageCount > 5) {

				/* Add aSlideshowThumbs array to UL */
				cc.jOESlideshowThumbsNav = '<a class="c-slideshow-carousel-nav c-slideshow-carousel-nav-prev" href="#">Previous</a><a class="c-slideshow-carousel-nav c-slideshow-carousel-nav-next" href="#">Next</a><a class="c-slideshow-carousel-nav c-slideshow-carousel-nav-prev-2" href="#">Previous</a><a class="c-slideshow-carousel-nav c-slideshow-carousel-nav-next-2" href="#">Next</a>';

				/* Add aSlideshowThumbs array to UL */
				cc.jOESlideshowThumbs = $j('<div class="c-b"><div class="c-slideshow-carousel"><ul>' + aSlideshowThumbs.join('') + '</ul></div>' + cc.jOESlideshowThumbsNav + '</div>');

			} else {

				/* Add aSlideshowThumbs array to UL */
				cc.jOESlideshowThumbs = $j('<div class="c-b"><ul>' + aSlideshowThumbs.join('') + '</ul></div>');

			}

			/* Append Thumbs to Slideshow */
			cc.jOESlideshow.append(cc.jOESlideshowThumbs);

			/* Apply active classes to first image / thumbnail */
			$j('p:first, li:first', cc.jOESlideshow).addClass('active').css({opacity: 1.0});


			/* Initialise roation / carousel */
			if (cc.cSlideshowImageCount > 5) {

				/* Initialise rotateSlideshowCarousel */
				cc.rotateSlideshowCarousel();

			} else {

				/* Define cSlideshowRotate to rotate every 4 seconds */
				cc.cSlideshowRotate = setInterval(function () { ComponentSlideshow.rotateSlideshow(); }, 4000);

				/* Initialise clickSlideshow */
				cc.clickSlideshow();

			}

		}

	},

	/* Initialise */ 
	init : function() {

		/* Class Context */
		var cc = this;

		/* jQuery Object References to the Elements this speeds up the DOM */
		cc.jOESlideshow = $j('div.c-slideshow');

		/* Configuration */
		cc.cSlideshowImageCount = 0;
		cc.cSlideshowImageCurrent = 0;

		/* Only run slideshow if more than 1 image, enable image thumbnails */
		if ($j('p', cc.jOESlideshow).length > 1) {

			$j('p', cc.jOESlideshow).each(function() {
				var jOEThis = $j(this);
				cc.cSlideshowImageCount++;
				jOEThis.addClass('slideshow-image-' + cc.cSlideshowImageCount + '').css('opacity', '0.0');
			});

		}

		cc.buildSlideshow();

	}

};

/*	Component: Filter
----------------------------------------------- */
var ComponentFilter = {

	/* Configuration */
	cFilterInactiveText: null,
	
	/* jQuery Object References to the Elements this speeds up the DOM */
	jOEFilter: null,

	/* buildFilter */ 
	buildFilter : function(obj) {

		/* Class Context */
		var cc = this;

		var $jOEFilterContent = obj;
		var $jOEFilterContentUL = $j('ul', $jOEFilterContent);

		var $jOEFilterContentActive = $j('li.active', $jOEFilterContentUL);

		/* Add width to filter content */
		$j($jOEFilterContent).width($j($jOEFilterContent).width());

		/* If Filter has an active LI display that text else display cc.cFilterInactiveText */
		if ($jOEFilterContentActive.length) {
			$j($jOEFilterContent).prepend('<p>' + $jOEFilterContentActive.text() + '</p>');
		} else {
			$j($jOEFilterContent).prepend('<p>' + cc.cFilterInactiveText + '</p>');
		}

		/* Filter hover */
		$j($jOEFilterContent).hover(function() {
			$jOEFilterContentUL.show();
		}, function() {
			$jOEFilterContentUL.hide();
		});

	},

	/* Initialise */ 
	init : function() {

		/* Class Context */
		var cc = this;

		/* jQuery Object References to the Elements this speeds up the DOM */
		cc.jOEFilter = $j('.c-filter');

		/* Configuration */
		cc.cFilterInactiveText = 'Choose';

		$j('.c-b', cc.jOEFilter).each(function() {
			cc.buildFilter(this);
		});

		/* Add jQuery class to c-filter */
		cc.jOEFilter.addClass('c-filter-jquery');

	}

};

/*	Component: Expandable
----------------------------------------------- */
var ComponentExpandable = {

	/* Initialise */ 
	init : function() {

		$j('.c-expandable .c-h').click(function() {

			var jExpandableToShow = $j(this);
			var jExpandableToShowContent = $j(this).next();

			/* Check if clicked .c-expandable is already visable */
			if (jExpandableToShowContent.is(':visible')) {

				/* If it is visable remove .s-i-arrow and .c-h-active then slideUp it */
				$j('.s-i-arrow', jExpandableToShow).removeClass('s-i-arrow-down').addClass('s-i-arrow-right');
				jExpandableToShow.removeClass('c-h-active');
				jExpandableToShowContent.slideUp('fast');

			} else {

				/* If it is not visable remove .s-i-arrow and .c-h-active then slideUp it */
				$j('.c-expandable .c-b').slideUp('fast');
				$j('.c-expandable .c-h .s-i-arrow').removeClass('s-i-arrow-down').addClass('s-i-arrow-right');
				$j('.c-expandable .c-h').removeClass('c-h-active');

				/* slideDown jExpandableToShow  */
				jExpandableToShowContent.slideDown('fast', function() {
					jExpandableToShow.addClass('c-h-active');
					$j('.s-i-arrow', jExpandableToShow).removeClass('s-i-arrow-right').addClass('s-i-arrow-down');
				});

			}

			return false;

		});

	}

};

/*	Component: Sidebar CTA
----------------------------------------------- */
var ComponentSidebarCTA = {

	/* jQuery Object References to the Elements this speeds up the DOM */
	jOESidebar: null,
	jOESidebarPrint: null,
	jOESidebarPrintCTA: null,
	jOESidebarPrintForm: null,

	/* toggleSidebarPrint */
	toggleSidebarPrint : function() {

		/* Class Context */
		var cc = this;

		cc.jOESidebarPrintCTA.click(function() {
			cc.jOESidebarPrintForm.toggle();
		});

	},

	/* Initialise */ 
	init : function() {

		/* Class Context */
		var cc = this;

		cc.jOESidebar = $j('div.c-sidebar-cta');
		cc.jOESidebarPrint = $j('.c-sidebar-cta-print', cc.jOESidebar);
		cc.jOESidebarPrintCTA = $j('a', cc.jOESidebarPrint);
		cc.jOESidebarPrintForm = $j('.form', cc.jOESidebarPrint);

		/* If sidebar-cta print exisits initialise functions */
		if (cc.jOESidebarPrint) {
			cc.toggleSidebarPrint();
		}

	}

};

/*	DOM Ready events
----------------------------------------------- */
$j(function() {

	/* Fire global functions */
	Global.init();

	/* Initialise Plugins */
	Plugins.init();

	/* If component div.c-slideshow exisits initialise functions */
	if ($j('div.c-slideshow').length) {
		ComponentSlideshow.init();
	}

	/* If component div.c-filter exisits initialise functions */
	if ($j('div.c-filter').length) {
		ComponentFilter.init();
	}

	/* If component div.c-expandable exisits initialise functions */
	if ($j('div.c-expandable').length) {
		ComponentExpandable.init();
	}

	/* If component div.c-sidebar-cta exisits initialise functions */
	if ($j('div.c-sidebar-cta').length) {
		ComponentSidebarCTA.init();
	}

	/* Fire Global.utilExternalLinks() last so any external links added via DOM during local page initialization will be activated */
	Global.utilExternalLinks();

});