/*
NGW general JS - permanent functionality
Dependencies: prototype 1.6.0, script.aculo.us effects 1.8.1, swfObject 2.0

*/

// page initialisation

jQuery(document).ready(function init() {
    carousel.init();
});

var carousel = function() {
	var config = {
		fadeIn: 250,
		fadeOut: 150,
		opacity: 0.75,
		jcarousel: {
			start: 1,
			buttonNextHTML: '<a href="#" onclick="return false;"></a>',
			buttonPrevHTML: '<a href="#" onclick="return false;"></a>',
			animation: 600,
			visible: 4,
			scroll: 1
		}
	};
	var imageGallery = {
		launchCarousel: function() {
			if (!$('#jcarousel').length && typeof(jcarousel) !== undefined) return false;
			var that = this;
			config.jcarousel.start = $('#jcarousel li').index($('li.selected'));
			$('#jcarousel').show().jcarousel(config.jcarousel);
			that.hoverCarousel();
		},
		hoverCarousel: function() {
			$('#jcarousel li').not('.selected').find('img').css('opacity', config.opacity).hover(function() {
				$(this).stop().fadeTo(config.fadeIn, 1);
			}, function() {
				$(this).stop().fadeTo(config.fadeOut, config.opacity);
			});
		}
	};
	return {
		init: function() {
			imageGallery.launchCarousel();
		}
	};
} ();

var hero = {
            flashElId: 'expandDemo',
            containerElId: 'expanding-hero',
            defaultExpandDuration: 300,
            defaultCollapseDuration: 300,
            elementProperties: null,
            
            // duration is in ms
            expand: function(duration) {
                        var ep = this.getElementProperties();
                        if (ep === null) return false;
                        ep.container.animate({
                                    height: ep.expandHeight + 'px'
                        }, duration || this.defaultExpandDuration);
                        return true;
            },
            collapse: function (duration) {
                        var ep = this.getElementProperties();
                        if (ep === null) return false;
                        ep.container.animate({
                                    height: ep.collapseHeight + 'px'
                        }, duration || this.defaultExpandDuration);
                        return true;
            },
            getElementProperties: function () {
                        if (this.elementProperties) return this.elementProperties;
                        
                        var flashEl = jQuery('#' + this.flashElId);
                        var container = jQuery('#' + this.containerElId);
                        if (!container || !flashEl) return null;
                        
                        var expandHeight = flashEl.height();
                        var collapseHeight = container.height();
                        if (expandHeight > 0 && collapseHeight > 0) {
                                    return this.elementProperties = {
                                                container: container,
                                                expandHeight: expandHeight,
                                                collapseHeight: collapseHeight
                                    };
                        }
                        return null;
            }
};

var flashOverlay = {
    init: function() {
    	// switch off overlay if url has query string '?overlay=viewed'
    	if (gup('alloverlay') == 'viewed') simpleCookies.set('flashOverlay', 'viewed');
        // test for takeover config object and player version:
        if (!window.flash_takeover || typeof swfobject !== 'object') return false;
        if (!swfobject.hasFlashPlayerVersion(flash_takeover.flash.version)) return;
		if (flash_takeover.play == 'once' && simpleCookies.get('flashOverlay') == 'viewed') {
			return false;
		}
        /* expose click functionality for flash CTA */
		window.clickThru = flashOverlay.clickThru;
		this.create();
    },
    clickThru: function() {
		if (arguments.length !== 0) {
				return; //place to extend the function
			}
		if (flash_takeover.click.length !== 0) {
			magicTracking(flash_takeover.click);
		}    
	},
	setDims: function(el) {
		el.css({
			width: jQuery(document).width() + 'px',
			height: jQuery(document).height() + 'px'
		});
	},
	create: function() {
		simpleCookies.set('flashOverlay', 'viewed');
	
		if (typeof flash_takeover.overlay == 'object') {
			jQuery('body').append('<div id="flash_shade"></div>');
			//$('footer').insert({ after: '<div id="flash_shade"></div>' });
			var shade = jQuery('#flash_shade');
			shade.css({ 
				backgroundColor: flash_takeover.overlay.bgcolor,
				top: 0,
				position: 'absolute',
				left: 0,
				zIndex: 999,
				opacity: 0.7
			});
			/* Disable click anywhere to close overlay function */
			/*shade.click(function() {
				flashOverlay.close();
			});*/
			jQuery(window).resize(function(){
				flashOverlay.setDims(shade);
			}).trigger('resize');
		}
	
		jQuery('body').append('<div id="overlayFlash"><span id="overlayFlashStub"></span></div>');
		
		jQuery('#overlayFlash').css({
			top: flash_takeover.position.top + 'px',
			left: flash_takeover.position.left + 'px',
			position: 'absolute',
			zIndex: 1000000
		});
	
		flashOverlay.set('overlayFlashStub');
		
		$('#contentBox').children('.heroBox').css('visibility', 'hidden');
		$('#modelrange').css('visibility', 'hidden');
		$('#expanding-hero').css('visibility', 'hidden');
		return true;
	},
	close: function() {
		jQuery('#overlayFlash').remove();
		jQuery('#flash_shade').fadeOut(300);
		
		/*if (createFlashOverlay.swfsInPage) {
			createFlashOverlay.swfsInPage.show();
		}*/

		$('#contentBox').children('.heroBox').css('visibility', 'visible');
		$('#modelrange').css('visibility', 'visible');
		$('#expanding-hero').css('visibility', 'visible');
	},
	set: function (containerId) {
		if (!window.flash_takeover) return false;
		var f = flash_takeover;
		swfobject.embedSWF(f.flash.src, containerId, f.flash.width, f.flash.height, f.flash.version, false, f.variables,
				$.extend({
					quality: "high",
					bgcolor: f.flash.bgcolor,
					allowScriptAccess: "sameDomain",
					wmode: "transparent",
					menu: "false"
				}, f.params),
				{ id: f.flash.id }
			);
		window.focus();
		return true;
	}
};

/* Same functionality as flashOverlay.close() */
function removeFlashOverlay() {
	flashOverlay.close();
};

/**
expanding hero area for flash content
called from flash movie with hero.expand(duration) / hero.collapse(duration)
where duration is in msec
*/
var heroExpandVD = {
    flashElId: 'expandDemo',
    containerElId: 'expanding-hero',
    defaultExpandDuration: 300,
    defaultCollapseDuration: 300,
    elementProperties: null,

    // duration is in ms
    expand: function(duration) {
        var ep = this.getElementProperties();
        if (ep === null) return false;
        ep.container.animate({
            height: ep.expandHeight + 'px'
        }, duration || this.defaultExpandDuration);
        return true;
    },
    collapse: function(duration) {
        var ep = this.getElementProperties();
        if (ep === null) return false;
        ep.container.animate({
            height: ep.collapseHeight + 'px'
        }, duration || this.defaultExpandDuration);
        return true;
    },
    getElementProperties: function() {
        if (this.elementProperties) return this.elementProperties;

        var flashEl = jQuery('#' + this.flashElId);
        var container = jQuery('#' + this.containerElId);
        if (!container || !flashEl) return null;

        var expandHeight = flashEl.height();
        var collapseHeight = container.height();
        if (expandHeight > 0 && collapseHeight > 0) {
            return this.elementProperties = {
                container: container,
                expandHeight: expandHeight,
                collapseHeight: collapseHeight
            };
        }
        return null;
    }
};

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapFaq(id) {
	var Span = document.getElementById ('faq' + id);
	Span.style.display = Span.style.display == 'block' ? 'none' : 'block';
}

// Top navigation navigation
sfHover = function() {
	if (document.getElementById("nav") !== null) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i = 0; i < sfEls.length; i++) {
			sfEls[i].onmouseover = function() {
				this.className += " sfhover";
			}
			sfEls[i].onmouseout = function() {
				this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

