/*
 * Ryobi javascript functions
 
 * Compnany:		BMF
 * Author:        	Kevin Su
 * Born:          	January 2010
 * Last modified: 
 * 
 * recommand compress code use http://javascriptcompressor.com/ for deployment
 */
var Ryobi = {

	init: function() {
		
		//accordion specs
		$('#accordion').accordion({autoHeight: false});
		
		//tabs
		$('.tabs').tabs();
		
		// Set Active Tab on tabbed pages with pagination
		var hash = window.location.hash;
		if (hash.indexOf("#activetab")>-1) {
			$('.tabs').tabs('select', hash.substr(hash.indexOf("#activetab")+10));
		}
 
		// form
		$('input.keyword').focus(function(){
			if ($(this).val() == $(this).prev('label').text()) {
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val()===''){
				$(this).val($(this).prev('label').text());
			}
		});

		/* Mini product finder */
		$(".form-mini-product-finder input.bt").click(function(event){
			
			event = event || window.event;
			event.preventDefault();

			//get the value of the field, urlencode it and send the user to the search page
			
			theVal = escape($("#prod-search").val());
			l = window.location;
			theURL = l.protocol + "//"+ l.host + "/";
			theURL += "toolfinder.aspx?name="+theVal;
			window.location = theURL;
			
			return false;
		});

		$(".form").delegate('input, select','keypress',function(event) {
			event = event || window.event;
			if (event.keyCode == 13) {
				event.preventDefault();
				// enter key is pressed, find the submit button that belongs
				// with this form and trigger a click.
				$(this).closest(".form").find("input[type=submit]").trigger('click');
				return false;
			}
			return true;					
		});
		
		/* Store locator searching */
		$(".form-search-store input.bt").click(function(event) {
			event = event || window.event;
			event.preventDefault();
			Ryobi.findNearestBunnings($(this).parent().find('.keyword').val());
			
			return false;
		});
		
		/* Service center locator searching */		
		$(".form-search-services input.bt").click(function(event) {
			event = event || window.event;
			event.preventDefault();
			thePostcode = $(this).closest(".form").find("input[type=text]").val();
			Ryobi.findServiceCenterLocator(thePostcode);
			return false;
		});

		
		/* Fix focus/nav issues with search fields in top nav */
		$('#header .nav input').focus(function(){
			$(this).closest(".nav li").addClass("activeInput");
		}).blur(function(){
				$(this).closest("li.activeInput").removeClass("activeInput");
		});
		
		$("#header .nav > li > a").hover(function() {$("#header .nav .activeInput input").blur(); }, function() {});

	
		/* FAQ List */
		$fl = $("#faq-list");
		$fl.find("div").slideUp(0);
		$fl.find("h3").addClass('clickable').click(function() { $(this).toggleClass('open').next('div').slideToggle(500); });

		/* Service centre locator table */
		$(".sclTable tr:odd").addClass('odd');
		//$(".sclTable").delegate('tr','hover',function(){ $(this).toggleClass('hover'); });
		
		

	},
	
	findNearestBunnings: function (postcode) {
		if (Ryobi.isNumber(postcode)) {
				
				if (postcode.length > 2 && postcode.length < 5) {
					if (!isNaN(postcode)) {
						Ryobi.redirectToBunnings("Normal", postcode);
					}
				}
				else {
					alert("Postcode must be 3 or 4 numbers.");
				}
		}
		else {
		  alert("Postcode must be numbers only.");
		}
			
	},

	findServiceCenterLocator: function (postcode) {
		if (Ryobi.isNumber(postcode)) {
				if (postcode.length > 2 && postcode.length < 5) {
					if (!isNaN(postcode)) {
						window.location = "/Service-Centre-Locator.aspx?postcode="+postcode;
					}
				}
				else {
					alert("Postcode must be 3 or 4 numbers.");
				}
		}
		else {
		  alert("Postcode must be numbers only.");
		}
		
	},

	isNumber: function(oNum) {
		if(!oNum) {
			return false;
		}
		var strP=/^\d+(\.\d+)?$/;
		if(!strP.test(oNum)) {
			return false;
		}
		try	{
			if(parseFloat(oNum)!=oNum) return false;
		} catch(ex) {
		 return false;
		}
		return true;
	},

	redirectToBunnings: function(type, key) {
	  var strDomain = "www.bunnings.com.au";
	  if(location.href.indexOf("com.au") == -1) {
	  	strDomain = "www.bunnings.co.nz";
	  }
	  if(key!="") {
			if(type=="Normal") {
				window.open("http://" + strDomain + "/discover-our-stores_find-your-nearest-store.aspx?location=" + key, "", "", "");
			}
			if(type=="State") {
				window.open("http://" + strDomain + "/stores_"+ key +".aspx", "", "", "");
			}
		}
	},

	SIFR: function() {

		//sIFR Flash font-face
		//var HelveticaNeueLTStd_thin = { src: '/webdev/bmf/TTI0008 - Ryobi Website Overhaul [Lester]/html/flash/sifr/HelveticaNeueLTStd_thin.swf' };//for local testing
		var HelveticaNeueLTStd_thin = { src: '/App_Themes/Ryobi/flash/HelveticaNeueLTStd_thin.swf' };//for stage
		//var HelveticaNeueLTStd_thin = { src: '/App_Themes/Ryobi/flash/HelveticaNeueLTStd_thin.swf' };//for live
		//sIFR.prefetch(HelveticaNeueLTStd_thin);
		sIFR.activate(HelveticaNeueLTStd_thin);
		sIFR.replace(HelveticaNeueLTStd_thin, {
			selector: 'h1,.updates-register .col-2 h2',
			css: '.sIFR-root {color: #eeeeee;text-transform:uppercase }',
			wmode:'transparent',
			repaintOnResize:true,
			offsetTop:3
		});


	},
	
	ProductCategoryNavigation: function(){
		var path = location.pathname.substring(0);
		
        if (path) {            
			if ($('.tabs-nav li a[href$="' + path + '"]').length ) {
				$('.tabs-nav li').removeClass('ui-tabs-selected');
				$('.tabs-nav li a[href$="' + path + '"]').parent('li').attr('class', 'ui-tabs-selected');
			}			           
        }
	},
	
	productListing: function() {
		
		$("ul.items li:nth-child(3n)").addClass("last");

		$("ul.items").each(function(){
			$this = $(this);
			if ($this.find("li.last:last").is(":last-child")) {
				$children = $this.find("li"); //for some reason the LIs are sometimes wrapped in divs...
				
				$last3 = $children.slice($children.length-3, $children.length);
				$last3.each(function(){
					$(this).addClass("last-row");
				});
			}
			else {
				$this.find("li.last:last").nextAll().addClass("last-row");
			}
		});		
	}
}

var addthis_config = {
    data_track_clickback: true
}


$(document).ready(function() {
	Ryobi.init();
	Ryobi.productListing();
	Ryobi.ProductCategoryNavigation();
});
	Ryobi.SIFR();

