/*
 colorfactory.co.nz js functions
 requires Mootools v1.2.1 (www.mootools.net) and cnet libraries build 640 or later (http://www.clientcide.com/js)
 (c) 2008 Gavin Botica - [stompbox.net.nz]
 */
var Site = {

    init: function(){
		
        // browser detection shortcuts
        isIE6 = Browser.Engine.trident4;
        isIE = Browser.Engine.trident;
		isOpera9 = Browser.Engine.presto;
        
        // collapsable panels
        Site.initCollapsables();
		
        // if 'banner' is present init the tabbed carousel UI
        if ($('banner')) {	
			// load the images
			var bannerImages = new Asset.images('/images/banner-dyeing-organic.jpg', 
												'/images/banner-curtain-dyeing.jpg', 
												'/images/banner-pressing.jpg', 
												'/images/banner-washing.jpg'
												);
            Site.initTabs();
			$('banner').setStyle('display', 'block');
		}
		
		// external link handlers
       	Site.extLinks();
        
		// smooth internal anchors
       	if (!isIE6) var smoothScroll = new SmoothScroll();
        
        // if 'technology' page, insert the swf for the dye animation movie
        if ($('technology')) Site.dyeAnimSwf();
		
		// if 'faq' page, init FAQ accordian
		if ($('faq')) Site.faqAccordian();
		
		// gallery lightbox
		if ($('gallery')) Site.initGallery();
		
    }, // init
	
    // collapsable panels
    initCollapsables: function(){
    
        // get togglers
        var togglers = $$('#content .cp-toggle');
        
        // define settings and events for togglers
        togglers.set({
        
            'title': 'Click to show or hide more content',
            'events': {
                'mouseenter': function(){
                    this.toggleClass('over');
                },
                'mouseleave': function(){
                    this.toggleClass('over');
                },
				'click': function(e){
					e.preventDefault();	
				}
            }
        });
        
        // init the collapsable panels
        togglers.each(function(el){
            var cpnl = el.getNext();
            // if it has class "open" then don't hide on initial load
            if (!el.hasClass('open')) 
                cpnl.setStyle('display', 'none');
            var cp = new Collapsable(el, cpnl, {
				'transition': 'quad:out',
				'duration': 550,
                onComplete: function(){
					if((isIE || isOpera9) && cpnl.hasClass('box-1')) cpnl.setStyle('padding', '2em 1em 2em 1em');
                    el.toggleClass('open');
                    var tmp = el.getElement('a');
                    if (tmp && tmp.get('text') == 'Read more') {
                        tmp.set('text', 'Close');
                    }
                    else 
                        if (tmp && tmp.get('text') == 'Close') {
                            tmp.set('text', 'Read more');
                        }
                }
            });
        });
    },
    
    initTabs: function(){
		var clicked = 0, currentTab, nextTab, tabTimer;
        // homepage tabs
        var homeTabs = $$('#banner-menu li'); // tabs
		if (!isIE) homeTabs.addEvent('click', function() {clicked = 1;}); // trigger "clicked" flag when human click happens
        var homeTabsContent = $$('#banner .banner-inner'); // tab content panels
        var showFirst = (isIE6) ? 0 : $random('0', (homeTabs.length - 1)); // select initial tab at random (except IE6)
        var useTransition = (isIE6) ? false : true; // opacity fx is ugly in IE6
        var homeTabsObj = new TabSwapper({
            selectedClass: 'sel',
            deselectedClass: 'off',
            mouseoverClass: 'over',
            mouseoutClass: 'out',
            smooth: useTransition,
            tabs: homeTabs,
            sections: homeTabsContent,
            initPanel: showFirst,
			onActive: function(i){
				if (!isIE) {
					$clear(tabTimer);
					if (!clicked) {
						nextTab = (i == '2') ? 0 : i + 1;
						tabTimer = tabTo.delay(5250);
					}
				}
			}
        });
		
		function tabTo(){
			homeTabsObj.show(nextTab);
			}
		
    }, // initTabs
	
    // hyperlink handlers
    extLinks: function(){
    
       	$$('#content a', '#footer a').each(function(el, i){
        
            var elHref = el.get('href');
            if (elHref.contains('http:') || elHref.contains('.pdf')) {
                tmpTitle = el.get('title') ? el.get('title') + ' [in new window]' : tmpTitle = el.get('href') + ' [link will open in new window]';
				el.set('target', '_blank');
                el.set('title', tmpTitle);
            }
        });
    },
	
	dyeAnimSwf: function() {
		
		// create the swfObject
		var swfObj = new Swiff('/images/dye-anim.swf', {
			width: 342,
			height: 200,
			params: {
				swLiveConnect: 'false',
				allowScriptAccess: 'false',
				menu: 'false'
			}
		});
		// fix for ie six margin diff
		var rMargin = (isIE6) ? '-85px' : '-75px';
		// create an new wrapper div and style it		
		var swfWrapper = new Element('div', {
			'id': 'swfWrapper',
			'styles': {
				'float': 'right',
				'margin-right': rMargin
				}
		});
		// inject the wrapper and the swfObject
		swfWrapper.inject($('content').getElement('h3'), 'after');
		swfObj.inject('swfWrapper');
	
	},
	
	faqAccordian: function() {
	// accordian UI for the FAQ page
	var toggles = $$('dt');
	var content = $$('dd');

	// define settings and events for toggles
	toggles.set({
	
		'title': 'Click to show answer',
		'events': {
			'mouseenter': function(){
				this.toggleClass('over');
			},
			'mouseleave': function(){
				this.toggleClass('over');
			}
		}
	});

	var faqAccordion = new Accordion(toggles, content,{
									 'show': 999,
									 'transition': 'quad:out',
									 'opacity': 0
									 });	
	},
	
	initGallery: function() {

		$$('p.gallery-thumbs a').set('rel', 'milkbox:gallery');
		var startOpacity = 0.7;
		$$('p.gallery-thumbs a img').set({
					   'opacity': startOpacity,
					   'tween': {
						   'duration': 'short'
						   },
					   'events': {
							'mouseenter': function() {this.fade('in')},
							'mouseleave': function() {this.fade(startOpacity)}
						   }
					   });	
	}
    
}; // Site
// run initialise method on DOM load
window.addEvent('domready', function(){Site.init()});