window.addEvent('domready', function() {

/********************************************************

        C C C C         X     X   S S S  V           V
      C         C        X   X   S     S  V         V
     C    CCC    C        X X    S         V       V
    C    C        C        X      S S S     V     V
     C    CCC    C        X X          S     V   V
      C         C        X   X   S     S      V V
        C C C C         X     X   S S S        V


 Created by Josh Reid for InventNZ Ltd Copyright 2008 

*********************************************************/

	// IE6 Backgrounds
	var bks = $$('.bk');
	bks.each(function(bk, i) {
		bk.setStyle('height',bk.getParent().getSize()['scrollSize']['y']+400);
		bk.setStyle('width',bk.getParent().getSize()['scrollSize']['x']);
	});
	
	// If Index (and cookie?) allows... 
	// GO Flash Intro
	if(!window.ie6) {
		if($('index')) {
			openIntro();
		}
		else
		{
			closeIntro();
		}
	} else {
		closeIntro();
	}
	
	// Previews Sliders
	var szNormal = 187, szSmall = 155, szFull = 251;
	var otherSzNormal = 272, otherSzSmall = 95, otherSzFull = 272;
	 
	var kwicks = $$("a.big");
	var fx = new Fx.Elements(kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Cubic.easeOut});
	kwicks.each(function(kwick, i) {
		kwick.addEvent("mouseenter", function(event) {
			var o = {};
			if(kwick.id != "other")
			{
				o[i] = {width: [kwick.getStyle("width").toInt(), szFull]};
			} 
			else 
			{
				o[i] = {width: [kwick.getStyle("width").toInt(), otherSzFull]};
			}
			kwicks.each(function(other, j) {
				if(i != j) {
					var w = other.getStyle("width").toInt();
					if(other.id != "other")
					{
						if(w != szSmall) o[j] = {width: [w, szSmall]};
					}
					else
					{
						if(w != otherSzSmall) o[j] = {width: [w, otherSzSmall]};
					}
				}
			});
			fx.start(o);
		});
	});
	
	$$("a.big").addEvent("mouseleave", function(event) {
		var o = {};
		kwicks.each(function(kwick, i) {
			if(kwick.id != "other")
			{
				o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
			}
			else
			{
				o[i] = {width: [kwick.getStyle("width").toInt(), otherSzNormal]}				
			}
		});
		fx.start(o);
	});
	
	// External Links: Target New Window (_blank)
	var extLinks = $$('a[rel="extLink"]','.extLink');
	extLinks.each(function(extLink, i) {
		
		extLink.target = '_blank';
		
	});
	
	var smoothlyScroll = new SmoothScroll({
		links: 'a.smoothly'
	});
	
	// TOOL TIPS - minimum a's to get: '#hlink a','#menu a'
	var tipMe = new Tips($$('a','abbr'), {
		showDelay: 1500,
		
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 300, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
	
	
});
/* END DOM READY */

/********** HOME PANEL ***********/

	function goHomePanel() {
		
		var hPimgs = $$('a.hP');
		var countImgs = hPimgs.length;
		var groupImgs = countImgs/5;
		
		var hPwidth = countImgs * 180;
		
		var myTime = 6000;
		
		var hP = $('homePanelInner');
		var hPfx = new Fx.Styles(hP,{duration: 1500, transition: Fx.Transitions.Cubic.easeInOut});
		
		hPfx.start({
			// Start
			'left':hP.getStyle('left').toInt()
		
		}).chain(function(){
			if ((hPwidth + hP.getStyle('left').toInt()) > 900) {
				// Continue
				this.start.delay(myTime,this,{
					'left': (hP.getStyle('left').toInt()-900)
				});	
			}
			else
			{
				// Back to start
				hPfx.start.delay(myTime,this,{
					'left':0
				});
			}
		}).chain(function(){
			// Repeat
			goHomePanel();
		
		});
		
	}

function goSize() {
	// IE6 flashBk size
	var flashBk = $('flashBk');
	var entireY = $('entire').getSize()['scrollSize']['y'];
	
	if (entireY > window.getHeight()) {
		flashBk.setStyle('height',entireY+25);
	}
	else if (entireY > window.getHeight()-25) {
		flashBk.setStyle('height',entireY+45);
		flashBk.setStyle('margin-bottom','-3px');
	}
	else
	{
		flashBk.setStyle('height',window.getHeight());
	}
}

function openIntro() {
	// Remove entire from picture
	$('entire').setStyle('height', window.getHeight());
	// Setup markup for Intro
	var fOverlay = new Element('div', {'id': 'fOverlay', 'styles': {'top': window.getScrollTop(), 'height': window.getHeight()/*$('flashBk').getSize()['scrollSize']['y']*/ }}).injectInside(document.body);
	var fSkip = new Element('div', {'id': 'fSkip'}).injectInside(fOverlay);
	var fSkipH3 = new Element('h3', {'id': 'fSkipH3'}).injectInside(fSkip);
	var fSkipLink = new Element('a', { 'href':'javascript:closeIntro();', 'title':'Skip Intro'}).setHTML('Skip Intro').injectInside(fSkipH3);
	var fCenter = new Element('div', {'id': 'fCenter'}).injectInside(fOverlay);
	var fIntro = new Element('div', {'id': 'fIntro'}).injectInside(fCenter);
	// Include Intro
	var flashvars = {};
	var params = {};
		params.allowFullScreen="false";
		params.quality="best";
		params.wmode="transparent";
		params.align="top";
		params.salign="LT";
		params.scale="noScale";
		params.devicefont="false";
		params.id="fIntro";
		params.bgcolor="#000000";
		params.name="fIntro";
		params.menu="true";
		params.allowScriptAccess="sameDomain";		
	
	swfobject.embedSWF("/flash/intro/intro1.swf", "fIntro", "567", "567", "9.0.0", "/flash/expressInstall.swf",flashvars,params);
	
}

function closeIntro() {
	// Return entire
	$('entire').setStyle('height','auto');		
	//$('entire').setStyle('height',$('entire').getSize()['scrollSize']['y']);
	// Remove flash overlay and all in it!
	if ($('fOverlay')) {
		$('fOverlay').remove();
	}
	// Stop INTRO if it exists
	if ($('fIntro')) {
		// Stop it!
		$('fIntro').stop();
	}
	
	// GO Home Panel!
	if ($('homePanelInner')) {
		goHomePanel();
	}
	// Include Flash Logo
	var flashvars = {};
		flashvars.linkURL = "http://www.inventnz.com";
	var params = {};
		params.allowFullScreen="false";
		params.quality="best";
		params.wmode="transparent";
		params.align="top";
		params.salign="LT";
		params.scale="noScale";
		params.devicefont="false";
		params.id="flashLogo";
		params.bgcolor="#000000";
		params.name="flashLogo";
		params.menu="true";
		params.allowScriptAccess="sameDomain";		
	
	swfobject.embedSWF("/flash/logo/logo.swf", "logoReplace", "240", "150", "6.0.0", "/flash/expressInstall.swf",flashvars,params);
	
	// GO flashBk size
	goSize();
	// Include Flash Background	
	var flashvars = {};
	var params = {};
		params.allowFullScreen="false";
		params.quality="best";
		params.wmode="transparent";
		params.align="top";
		params.salign="LT";
		params.scale="noScale";
		params.devicefont="false";
		params.id="imgBk";
		params.bgcolor="#000000";
		params.name="imgBk";
		params.menu="true";
		params.allowScriptAccess="sameDomain";		
	
	swfobject.embedSWF("/flash/imgBk.swf", "replace", "100%", "100%", "9.0.0", "/flash/expressInstall.swf",flashvars,params);
	
	
	// Include Flash HomePanel	
	/*
	var fHome = new Element('div', {'id': 'fHome'}).injectInside($('homePanel'));
	
	var flashvars = {};
	var params = {};
		params.allowFullScreen="false";
		params.quality="best";
		params.wmode="transparent";
		params.align="top";
		params.salign="LT";
		params.scale="noScale";
		params.devicefont="false";
		params.id="fHome";
		params.bgcolor="#000000";
		params.name="fHome";
		params.menu="true";
		params.allowScriptAccess="sameDomain";		
	
	swfobject.embedSWF("/flash/home/home.swf", "fHome", "896", "180", "9.0.0", "/flash/expressInstall.swf",flashvars,params);
	*/
	
}

function flashOverlay(hex) {
	if ($('fOverlay')) {
		$('fOverlay').setStyle('background-color','#'+hex);
	}
}

function changeImg(imgNum) {
	if ($('sspInsert')) {
		$('sspInsert').changeImg(imgNum);
	}
}