// Utility function for swapping element (ie. a to span) while maintaining class and id.
jQuery.fn.replaceWith = function(replacement) {
	return this.each(function(){
	element = jQuery(this);
	jQuery(this)
		.after(replacement).next()
		.attr('class', element.attr('class')).attr('id', element.attr('id'))
		.html(element.html())
		.prev().remove();
	});
};

jQuery(document).ready(function(){ 

	// Initialize superfish.
	jQuery("ul#nav").superfish(); 
	jQuery(".user-nav-items").superfish();

	// Disable click events on main nav items.
	jQuery("#brand").find("ul#nav > li > a").each(function(i) {
		jQuery(this).click(function () {
			return false;
		});
	});

	// Disable click for main products
	jQuery("li.page-item-4").find("ul.children > li").each(function(i) {
		if(!jQuery(this).hasClass('page-item-2285')) {
			if(jQuery(this).parent('ul').hasClass('children')) {
				jQuery(this).find("a").each(function(i) {
					if(jQuery(this).parent('li').parent('ul:first').parent('li').hasClass('page-item-4')) {
						jQuery(this).click(function () {
							return false;
						});
					}
				});
			}
		}
        });

	// get cart from kommerce server
        if(jQuery('.cart-replace').length) {
		var url = jQuery('#span-store-url').html()+"/microairecart.aspx";
		jQuery.getJSON(url+"?callback=?", function(data) {
			jQuery('.cart-replace').hide();
			jQuery('div.breadcrumb-wrapper').before(htmlDecode(data.cart));
		}); 
        }

	// add Site as selection to search
	if(jQuery('form:first').attr('id') == 'cse-search-box') {
		jQuery('#search_type').append('<option selected="selected">Site</option>');
	}

	// set google search and change method for advanced search
	jQuery('#cse-search-box-not-logged-in').unbind('submit').bind('submit', function() {
		if(jQuery('#search_type').val() == 'Site') {
			jQuery(this).attr('method','get');
			jQuery(this).attr('action',jQuery('#span-url').html()+'/search/');
			jQuery(this).append('<input type="hidden" name="q" value="' + jQuery("#terms").val() + '" />');
		}
		return true;
	});
        jQuery('#cse-search-box').unbind('submit').bind('submit', function() {
                if(jQuery('#search_type').val() == 'Site') {
                        jQuery(this).attr('method','get');
                        jQuery(this).attr('action',jQuery('#span-url').html()+'/search/');
                        jQuery(this).append('<input type="hidden" name="q" value="' + jQuery("#terms").val() + '" />');
                }
                return true;
        });

	// do cufon for any elements added by javascript
        Cufon.replace(".section", { fontFamily: "Frutiger LT Std" });
        Cufon.replace("a.home", { fontFamily: "Frutiger LT Std" });

}); 

function htmlDecode(value){ 
	return jQuery('<div/>').html(value).text(); 
}

