//<![CDATA[
		   
var d = new Date();
var year = d.getFullYear();

function clearOnFocus(type)
{
  if (!document.getElementsByTagName) return false;
  var fields = document.getElementsByTagName(type);
  for (var i=0; i < fields.length; i++) {
    if (fields[i].className.match("clearonfocus")) {
      fields[i].onfocus = function() {
        if (this.value == this.defaultValue) {
          this.value = "";
          return false;
		}
      }
	  fields[i].onblur = function() {
        if (this.value == "") {
          this.value = this.defaultValue;
          return false;
		}
      }
    }
  }
}

var fdScroller = {
        scrollLoop:false,
        scrollInterval:null,
        currentBlock:null,
        lastY:0,
        requestedY:0,
        init: function() {
                var lnks = document.getElementsByTagName('a');
                var removeLoc = window.location + ' ';

                // strip any #target, force a String cast
                if(removeLoc.indexOf('#') != -1) {
                        removeLoc = removeLoc.substr(0, removeLoc.indexOf('#'));
                } else {
                        removeLoc = removeLoc.substr(0, removeLoc.length - 1);
                }

                for(var i = 0, lnk; lnk = lnks[i]; i++) {
                        tmploc = lnk.href.replace(removeLoc,'');
                        if(tmploc.charAt(0) == '#' && !lnk.onclick) lnk.onclick = fdScroller.initScroll;
                }
        },
        getWindowHeight: function(){
                var myHeight = 0;
                if( typeof( window.innerWidth ) == 'number' ) {
                        //Non-IE
                        myHeight = window.innerHeight;
                } else if( document.documentElement &&
                                ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                        //IE 6+ in 'standards compliant mode'
                        myHeight = document.documentElement.clientHeight;
                } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                        //IE 4 compatible
                        myHeight = document.body.clientHeight;
                }
                return myHeight;
        },
        getScrollLeft: function(){
                if(document.all) return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
                else return window.pageXOffset;
        },
        getScrollTop: function(){
                if(document.all) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
                else return window.pageYOffset;
        },
        getElementYpos: function(el){
                var y = 0;
                while(el.offsetParent){
                        y += el.offsetTop
                        el = el.offsetParent;
                }
                return y;
        },
        initScroll: function(e){
                var targ;

                if (!e) var e = window.event;
                if (e.target) targ = e.target;
                else if (e.srcElement) targ = e.srcElement;
                if (targ.nodeType == 3) targ = targ.parentNode;

                if(targ.href && targ.href.indexOf('#') != -1) {
                        var where = targ.href.substr(targ.href.indexOf('#')+1,targ.href.length);
                        fdScroller.currentBlock = document.getElementById(where);
                        if(!fdScroller.currentBlock) return;

                        var documentHeight = document.getElementsByTagName('body').offsetHeight;
                        var windowHeight = fdScroller.getWindowHeight();
                        var ypos = fdScroller.getElementYpos(fdScroller.currentBlock);
                        if(ypos > documentHeight - windowHeight) ypos = documentHeight - windowHeight;
                        fdScroller.requestedY = ypos;
                        fdScroller.scroll();
                }

                return false;
        },
        scroll: function(){
                var left = fdScroller.getScrollLeft();
                var top  = fdScroller.getScrollTop();

                if(Math.abs(top-fdScroller.requestedY) <= 1){
                        window.scrollTo(0,fdScroller.requestedY);
                        fdScroller.lastY = -1;
                } else {
                        fdScroller.lastY = top;
                        window.scrollTo(0, Math.round(top+((fdScroller.requestedY-top)/2)));
                        setTimeout(fdScroller.scroll,50);
                }
        }
}

function doPopups()
{
if (!document.getElementsByTagName) return false;
	var anchors = document.getElementsByTagName('a');
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		var relAttribute = String(anchor.getAttribute('rel'));
		if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('popup'))){
			anchor.onclick = function () {window.open(this.href); return false;}
		}
	}
}

$(document).ready(function(){
	//Clears inital form field values on focus
	clearOnFocus('input');
	clearOnFocus('textarea');
	//Launch links in new window where rel="popup"
	doPopups();
	//Eased back to top link
	fdScroller.init();
	//Stripe tables
	$("tr:nth-child(odd)").addClass("odd");
	//Initialise Menu
	$("ul.sf-menu").superfish({ 
		pathClass:'current',
		autoArrows:false
	});
	//Adds unique styling to range back button on product pages
	if ($('#navigation').length > 0) {
		$('#navigation ul li:first').addClass('range-back');
	}
	// Removes border and padding on last item in quick links list
	if ($('#quick-links').length > 0) {
		$('#quick-links ul li:last a').css({'border':'0','padding-right':'0'});
	}
	if ($('.product-listing-home').length > 0) {
		//Load colour plug-in for highlight fade
		$.getScript("js/jquery.color.js");
		$('.product-listing-home .product-thumb').css({opacity: 0.5});
		$('.product-listing-home a').hover(
			function () {
				$(this).next('div').fadeTo("normal", 1);
				$(this).children('strong').animate({color:'#eee'}, 600);
			},
			function () {
		  		$(this).next('div').fadeTo("normal", 0.5);
				$(this).children('strong').animate({color:'#ddd'}, 600);
		});
	}
	if ($('.product-listing').length > 0) {
		//Load colour plug-in for highlight fade
		$.getScript("js/jquery.color.js");
		$('.product-listing .product-thumb').css({opacity: 0.5});
		$('.product-listing a').hover(
			function () {
				$(this).next('div').fadeTo("normal", 1);
				$(this).children('strong').animate({color:'#000'}, 600);
			},
			function () {
		  		$(this).next('div').fadeTo("normal", 0.5);
				$(this).children('strong').animate({color:'#666'}, 600);
		});
	}
	//Initialise Fancy Zoom
	if ($('#image-container').length > 0) {
		//$("a[rel*=lightbox]").lightBox();
    	$('a[rel*=lightbox]').fancyZoom({directory: 'images/fancyzoom'}); // no need for trailing slash
	}
	//Initialise Homepage News Accordian
	if ($('#news-items').length > 0) {
		$('#news-items').accordion({ 
			header: '.news-headline'
		});
	}
	
	$('.link-news').hover(
      function () {
        $('#tt-news').fadeIn('fast');  
      }, 
      function () {
         $('#tt-news').fadeOut('fast');  
      }
    );

    $('.link-rss').hover(
      function () {
        $('#tt-rss').fadeIn('fast');  
      }, 
      function () {
         $('#tt-rss').fadeOut('fast');  
      }
    );
   

	$('.link-twitter').hover(
      function () {
        $('#tt-twitter').fadeIn('fast');  
      }, 
      function () {
         $('#tt-twitter').fadeOut('fast');  
      }
    );
	//Initialise Dealers UK Seach Results Accordian
	if ($('#dealerresults').length > 0) {
		$('#dealerresults').accordion({ 
			header: '.org'
		});
	}
	$('#accordion').accordion({ 
		header: '.attribute',
		autoheight: false
	});
	$('#glossary').accordion({ 
		header: '.attribute',
		autoheight: false
	});
	
});	   

//]]>