$(function(){
	var theurlargsarr = geturlargs();
	
	// .offsite links (and store locator search) open in new window
	$('a.offsite, #header li.store_locator form').attr('target', '_blank');

	var sl_default = 'POSTAL CODE OR CITY, PROVINCE';
	// for any form inputs with the class 'phtext',
	// clear/restore the placeholder text when focussed/blurred
	// this could be done with feature detection and the html5 placeholder attribute
	$('input.phtext').each(function(){
		var thise = $(this);
		if (thise.parents('.store_locator') && thise.val() == ''){
			thise.attr('value', sl_default).val(sl_default);
		}
		var placeholder = thise.attr('value');
		thise.focus(function(e){
			//console.log(e.target);
			if (thise.val() == placeholder) thise.val('');
			thise.closest('.hoverwrap').addClass('active');
		});
		thise.blur(function(e){
			if ($(this).val() == '') $(this).val(placeholder);
			$(e.target).closest('.hoverwrap').removeClass('active');
		});
	});
	// store locator search shouldn't submit default or empty form
	$('#header li.store_locator form').submit(function(e){
		var thisv = $(this).find('input:text');
		if (thisv.val() == sl_default){
			e.preventDefault();
			window.open($(this).attr('action'), '_blank');
		}
	});
	
	// append #header facebook like button (facebook like button code is not valid xhtml 1.0 strict, 
	//	therefore we append it like so, and our served html continues to be w3c valid). it still causes a console error:
	//  uncaught exception: Error: Permission denied for <http://www.facebook.com> to get property Proxy.InstallTrigger
	// but at least our served html is valid.
	if (theurlargsarr['fpo'] != 'true'){
		$('#header .social').append('<iframe class="fbiframe" src="//www.facebook.com/plugins/like.php?href=www.facebook.com%2Farbyscanada&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe>');
	}
	
	
	// this would be better done with feature detection instead of user agent detection
	if (isltIE9()){
		// fix ie <9's lack of nth-child support
		$('ul.menu li li:nth-child(3n+1)').addClass('nth3n1');
		// ..and lack of first-child support
		$('ul#header_nav > li:first-child').addClass('first');
		$('ul#footer_nav > li:first-child').addClass('first');
	}
	// fix ie7's floats not clearing.. this is less than ideal, and won't work if ie7 user has js disabled,
	// but if that's the case, why bother? why is the world so tollerant of incompetent IT depts. and pirates?
	if (islteIE7()){
		$('ul.menu li li.nth3n1').before('<li class="ie7floatclearfix"><!-- --></li>');
	}
	
	
});// end document ready function


function geturlargs(){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++){
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}


function isltIE9(){
    var IEv = false;
    try {
        if(navigator.appName=='Microsoft Internet Explorer'){
            var version = parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]);
            if(version < 9){
                IEv = true;
            }
        }
    }
    catch(e){}
    return IEv;
}
function islteIE7(){
    var IEv = false;
    try {
        if(navigator.appName=='Microsoft Internet Explorer'){
            var version = parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]);
            if(version <= 7){
                IEv = true;
            }
        }
    }
    catch(e){}
    return IEv;
}

