//<![CDATA[
		   
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('#wrapper') != -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('#wrapper') != -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);
                }
        }
}

$(document).ready(function(){
	//Adds class to body to confirm JS is enabled (used in CSS)
	$('body').addClass("jsenabled");
	
  	//Adds Faceboxes
	var page = document.location.href;
	$('.whyintercanvas').attr('href', page+'#why');
	$('a[rel*=facebox]').facebox();

	// Use AJAX to load "why intercanvas" text to product pages
	$('#gallery').append('<div id="why" style="display:none"></div>');
	$('#gallery #why').load('/why-intercanvas.html .article .outer');
	  	  
	//Clears inital form field values on focus	
	$(".clearonfocus").focus(function() {
		if(this.value == this.defaultValue) {
			this.value = "";
		}
	}).blur(function() {
		if(!this.value.length) {
			this.value = this.defaultValue;
		}
	});
	
	//Launch links in new window where rel="popup"
	$('a[rel~=popup]').click(function(){
		window.open(this.href);
		return false;
	});
	
	//Eased back to top link
	fdScroller.init();
	
	//Stripe tables
	//$('tr:nth-child(odd)').addClass('odd');
	
	//Footer Product Thumbnails
	$('#all-products h3').after('<div id="product-photo-wrapper"><img src="assets/templates/intercanvas/images/photos/footer-products/canvas.jpg" alt="Product Thumbnail" width="106" height="106" id="product-photo" /></div>');
	$('#product-photo').hide();
	$('#product-photo').addClass('hidden');
	//Hover events for all products links in footer
	$('#all-products a').hover(function() {
		try{clearTimeout(hoverIntent);}catch(e){};
		$('#product-photo').fadeIn('slow');
		$('#product-photo').removeClass('hidden');
		$('#product-photo').attr("src","assets/templates/intercanvas/images/photos/footer-products/"+$(this).attr('id')+".jpg");
	}, function() {
		$('#product-photo').addClass('hidden');
		hoverIntent = setTimeout(function() { $('#product-photo.hidden').fadeOut('slow'); }, 1000);
	});
	
	//Hover background image animations for primary navigation
	$('#primary > ul > li:not(.current)').hover(function() {
		$(this).children('a').css({backgroundPosition:'0% -180px'}).stop(true, false).animate({backgroundPosition: '(0% -204px)'}, 300, 'easeOutCubic');
		$(this).css({backgroundPosition:'100% -180px'}).stop(true, false).animate({backgroundPosition: '(100% -204px)'}, 300, 'easeOutCubic');
	}, function() {
		//When hovering off disable is the menu has children so is Products menu
		if ($(this).attr('class') != 'haschildren') {
			$(this).children('a').stop(true, false).animate({backgroundPosition: '(0% -160px)'}, 300, 'easeInCubic');
			$(this).stop(true, false).animate({backgroundPosition: '(100% -160px)'}, 300, 'easeInCubic');
		} else {
			//Set a timeout of 300 milliseconds to complete the background hover off event
			hoverIntentMenu = setTimeout(function() {backgroundAnimateOff(); }, 300);
		}
	});
	
	//This event is called to complete the background hover off event
	function backgroundAnimateOff() {
		$('#primary ul li:not(.current).haschildren a').filter(':not(:animated)').animate({backgroundPosition: '(0% -160px)'}, 300, 'easeInCubic');
		$('#primary ul li:not(.current).haschildren').filter(':not(:animated)').animate({backgroundPosition: '(100% -160px)'}, 300, 'easeInCubic');
	}
	
	//This adds a hover class to the li elements when the sub-menu is hovered over
	$('#primary li ul').hover(function() {
		$(this).parent().addClass('hover');
		//This cancels the background hover off timeout event
		try{clearTimeout(hoverIntentMenu);}catch(e){};
	}, function() {
		$(this).parent().removeClass('hover');
	});
	
	//Hover padding animations for secondary navigation
	if ($('#secondary').length > 0) {
		$('#secondary li:not(.current) a').hover(function() {
			$(this).stop(true, false).animate({paddingLeft: 20}, 200);
		}, function() {
			$(this).stop(true, false).animate({paddingLeft: 15}, 200);
		});
	}
	
	//Accordion initialisation
	if ($('#accordion').length > 0) {
		
		var	title,
			num = 0, 
			hash = document.URL.replace(/(.+)#/, "");
		
		$('#accordion dt').each(function(i) {
			title = $(this).text().toLowerCase().replace(/ /g, "-");
			if(title == hash) {
				num = i;
			}				
		});
		
		$('#accordion').accordion({ 
			header: 'dt',
			autoHeight: false,
			active : num
		});
	}
	
	//Disable right click
	$(document).bind("contextmenu",function(e){
        return false;
    });
	
});
//]]>
