$extend(Browser,{fixPNG:function(d){try{if(Browser.Engine.trident){d=$(d);if(!d){return d}if(d.get("tag")=="img"&&d.get("src").test(".png")){var h=d.isVisible();try{dim=d.getSize()}catch(i){}if(!h){var g={};["visibility","display","position"].each(function(e){g[e]=this.style[e]||""},this);this.setStyles({visibility:"hidden",display:"block",position:"absolute"});dim=d.getSize();this.setStyles(g);d.hide()}var c=new Element("span",{id:(d.id)?d.id:"","class":(d.className)?d.className:"",title:(d.title)?d.title:(d.alt)?d.alt:"",styles:{display:h?"inline-block":"none",width:dim.x,height:dim.y,filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader (src='"+d.src+"', sizingMethod='scale');"},src:d.src});if(d.style.cssText){try{var f={};var b=d.style.cssText.split(";");b.each(function(e){var j=e.split(":");f[j[0]]=j[1]});c.setStyle(f)}catch(i){}}if(c.cloneEvents){c.cloneEvents(d)}c.replaces(d)}else{if(d.get("tag")!="img"){var a=d.getStyle("background-image");if(a.test(/\((.+)\)/)){d.setStyles({background:"",filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src="+a.match(/\((.+)\)/)[1]+")"})
}}}}}catch(i){}},pngTest:/\.png$/,scanForPngs:function(c,b){b=b||"fixPNG";if(document.getElements){c=$(c||document.body);c.getElements("img[src$=.png]").addClass(b)}else{var a=$$("img").each(function(d){if(Browser.pngTest(d.src)){d.addClass(b)}})}}});Element.implement({isVisible:function(){return this.getStyle("display")!="none"},toggle:function(){return this[this.isVisible()?"hide":"show"]()},hide:function(){var b;try{if("none"!=this.getStyle("display")){b=this.getStyle("display")}}catch(a){}this.store("originalDisplay",b||"block");this.setStyle("display","none");return this},show:function(a){original=this.retrieve("originalDisplay")?this.retrieve("originalDisplay"):this.get("originalDisplay");this.setStyle("display",(a||original||"block"));return this},swapClass:function(a,b){return this.removeClass(a).addClass(b)},fxOpacityOk:function(){return !Browser.Engine.trident}});

var slides = [];
var fx = [];
var index = 0;
var timer = null;
var dtimer = null;

var fadeInterval = 2500;
var showInterval = 7500;
var delayInterval = 400;

var animateInterval = 0;
var replaseInterval = 0;

window.addEvent('domready', function() {

  /* iphone / ipad display hack */
  var deviceAgent = navigator.userAgent.toLowerCase();
  if (deviceAgent.match(/(iphone|ipod|ipad)/)){
   $('wrapper').addClass('ios');
  }

  /* external links */
  var objs = $$('a');
  objs.each(function(obj, i) {
    if (obj.get('href') && (obj.get('rel') == 'external')){
      obj.set('target', '_blank');
    }
  });
  
  if (Browser.Engine.trident4){
    $$('img.fixPNG').each(Browser.fixPNG);
  }

  /* IE hovers */
  if (Browser.Engine.trident){
    var objs = $$('button');
    objs.each(function(obj, i) {
      obj.addEvent("mouseover", function(event) {
        this.addClass('hover');
      });
    	obj.addEvent("mouseout", function(event) {
        this.removeClass('hover');
      });
    });
    var objs = $$('input').extend($$('textarea'));
    objs.each(function(obj, i) {
      obj.addEvent("focus", function(event) {
        this.addClass('focus');
    	});
    	obj.addEvent("blur", function(event) {
        this.removeClass('focus');
    	});
    });
  }
  
  if ($('showcase')) {
    fobj = new Swiff('swf/showcase_030810.swf', {
      id: 'fObject',
      width: 393,
      height: 409,
      version: 9,
      container: 'showcase',
      params: {
      	wmode: 'opaque',
      	bgcolor: '#ffffff'
      },
      vars: {
        img: 'swf/imports/11.jpg,swf/imports/13.jpg,swf/imports/12.jpg,swf/imports/4.jpg,swf/imports/5.jpg,swf/imports/6.jpg,swf/imports/7.jpg,swf/imports/9.jpg',
        urls: 'rooftops,rooftops,ponds,about-us,balconies,ponds,industrial-coatings,about-us',
        txt: 'St Kilda Pier Kiosk Rooftop.  200m out to sea in the full sun with foot traffic and outdoor furniture.  Doesn\'t get harsher than that;'+
          'The guys waterproofing the two Heritage listed (Circa 1905) Myer facades, Lonsdale and Lt Bourke Sts;'+
          'Royal Exhibition Fountain – Resealed for another 20 years;'+
          /*'Waterproofing Melbourne, part of the Australian team at <br>the Chelsea 2008 Flower show - London;'+*/
          /*'Preparing the water feature for spraying ensuring the <br>$200k sandstone wall is well protected;'+*/
          /*'We also waterproof bathroom floors, walls and <br>bath surrounds;'+*/
          'Here\'s Ron applying primer to the pond at the Chelsea Flower Show - London;'+
          'We also waterproof decks/balconies and planter boxes as shown in this Toorak Penthouse;'+
          'The finished Vortex Pond at the Chelsea Flower Show - London. 2008 Gold Medal awarded;'+
          'Waterproofing Melbourne, proud to be a part of a $4m <br>rebuild of Paddle Steamer \'Hero\';'+
          'The crew about to torch-on the membrane to the Crown Valet Parking Planters;'
          /*'The team working on rectifying a leaking Toorak home;'*/
      }
    });
  }
  
  $$('.email').each(function(el) {
    var anchor = new Element('a', {
      href: 'mailto:' + el.get('rel').replace('|','@'),
      'class': el.get('class'),
      'text': el.get('text')
    }).replaces(el);
  });
  
  if ($('gallery')){
    milkbox.addEvent('xmlGalleries',function(){
    	$$('a.gallery').addEvent('click', function(e){
    		e.preventDefault();
    		var id = $(this).get('id');
    		milkbox.showGallery({ gallery:id });
    		return false;
    	});
    });
    milkbox.addGalleries('/gallery.xml');
  }
  
  if ($('testimonial')){
  
    var imagesDir = '/images/testimonials/';
    var imageWidth = 381;
    var imageHeight = 165;
    var imageTop = 0;
    var imageLeft = 0;
    var holder = $('tslide');
  
  	holder.empty();
  	new Asset.image('/images/ajax-loader.gif', {
      id: 'tloader'
    }).inject(holder).show();
    new Asset.image('/images/pause.gif', {
      id: 'tpause',
      styles: {
        'z-index': 100,
        'position': 'absolute',
        'bottom': 15,
        'right': 20
      }
    }).inject(holder).hide();
  	testimonials.each(function(img,i){ testimonials[i] = imagesDir + '' + img; }); //add dir to images
  	var loader = new Asset.images(testimonials, {
  		onComplete: function() {
  			testimonials.each(function(im) {
  				slides.push(new Element('img',{
  					src:im,
  					width: imageWidth,
  					height: imageHeight,
  					styles: {
  						opacity:0,
  						top:imageTop,
  						left:imageLeft,
  						position:'absolute',
  						'z-index': 10
  					}
  				}).inject(holder));
  			});
  			
  			var start = function() {
  			
  			  var d = new Date();
  			  animateInterval = d.getTime();
    		  timer = animate.periodical(showInterval);
  			  
  			  holder.setStyle('background','');
  				$('tslide').addEvent('mouseover', function(e){
  				  clearInterval(timer);
  				  clearInterval(dtimer);
  				  $('tpause').show();
  				  slides.each(function(sl,i){
  				    fx[i].pause();
  				  });
  				  var d = new Date();
  				  var t = d.getTime();
  				  replaseInterval = showInterval+(animateInterval-t);
  				});
  				$('tslide').addEvent('mouseout', function(e){
  				  $('tpause').hide();
  				  slides.each(function(sl,i){
  				    fx[i].resume();
  				  });
  				  clearInterval(timer);
  				  clearInterval(dtimer);
  				  
  				  var d = new Date();
  				  animateInterval = d.getTime()-(showInterval-replaseInterval);
  				  
  				  if (replaseInterval > 0){
    				  dtimer = (function(){
    				    animate();
      				  timer = animate.periodical(showInterval);
    				  }).delay(replaseInterval);
  				  } else {
    				  animate();
      				timer = animate.periodical(showInterval);
  				  }
  				});
  			};
  			
  			(function() {
  			  slides.each(function(sl,i){
  			    fx[i] = new Fx.Tween(slides[i], {
    			    duration: fadeInterval,
    			    property: 'opacity'
  			    });
  			  });
  			  fx[index].start(0, 1);
          $('tloader').hide();
          start();
  		  }).delay(delayInterval);
  		  
  		}
  	});
    /*$('testimonial').addEvent('click', function(event){
      var url = $(this).get('src').split('/');
      var cimg = url.pop();
      var dir = url.join('/');      
      cindex = 0;
      testimonials.each(function(el, i){
        if (cimg == el){ cindex = i; }
      });
      cindex++;
      if (cindex >= (testimonials.length)){
        cindex = 0;
      }
      $(this).set('src', dir+'/'+testimonials[cindex]);
    });*/
    
  }

});

function animate(){

  var d = new Date();
  animateInterval = d.getTime();

  fx[index].start(1, 0);
	++index;
	index = (slides[index] ? index : 0);
	fx[index].start(0, 1);
	
}

Cufon.replace('h2', { fontFamily: 'Helvetica' });
Cufon.replace('h3', { fontFamily: 'Helvetica Neue' });
Cufon.replace('h4', { fontFamily: 'Helvetica' });
