$(function(){

	jQuery.extend(
		jQuery.expr[':'], {
			regex: function(a, i, m, r) {
				var r = new RegExp(m[3], 'i');
				return r.test(jQuery(a).text());
			}
		}
	);
	
	jQuery.fn.specsCell = function() {
		/*var w = 0;
		$(this).each(function(){
			$(this).wrapInner('<div class="specs-right" />');
			thisW = $(this).children("div.specs-right").width()
			if (thisW > w) w = thisW;
		});
		$(this).children("div.specs-right").css('margin-right', ($(this).width() - w + 10) / 2 + "px");*/
		$(this).css("text-align","left");
	}

	function collapseTop(){ $("#brand-page #top-wrapper").animate({ marginTop: "-86px" }, "normal"); }
	function expandTop(){ $("#brand-page #top-wrapper").animate({ marginTop: "0" }, "normal"); }
	collapseTop();
	$("#brand-page #top-wrapper").hoverIntent(expandTop, collapseTop);
	$("#brand-page-content .product-specs td").each(function(){
		if($(this).attr("rowspan") > 1) {
			$(this).css("background","#fff");
		}
	});
	
	$("#brand-page table.product-specs td:regex('^[A-Za-z]+|^[0-9.]+\"|[0-9] Portions')").specsCell();
	// $("#brand-page table.product-specs td:regex('[0-9]+g|[0-9.]+kg$')").specsCell();
	// 	$("#brand-page table.product-specs td:regex('[0-9.]+kg$')").specsCell();
	// 	$("#brand-page table.product-specs td:regex('[0-9 ]+pcs$|x [0-9]+ kg$')").specsCell();
	// 	$("#brand-page table.product-specs td:regex('[0-9]+ shts$')").specsCell();
	// 	$("#brand-page table.product-specs td:regex('^[0-9.]+$')").specsCell();
	
	// Search
	var searchForm = $('#search form');
	var searchInput = $('#search input[name="q"]');
	var searchDefaultText = 'Type your search here...';
	searchInput.attr("value", searchDefaultText);
	searchInput.focus(function(e){
		if($(this).attr("value") == searchDefaultText) $(this).attr("value", "");
	})
	searchInput.blur(function(e){
		if($(this).attr("value") == "") $(this).attr("value", searchDefaultText);
	})
	searchForm.submit(function(e){
		if(searchInput.attr("value") == "" || searchInput.attr("value") == searchDefaultText) return false;
	})
	
	// Updating in Progress -- Disable Links
	$(".updating a").each(function(){
		$(this).attr("href", "#");
		var initText = $(this).text();
		// console.log(initText);
		$(this).hover(
			function(){
				$(this).text("Updating in Progress");
			},
			function(){
				$(this).text(initText);
			}
		);
	});
	$(".updating a").click(function(){
		return false;
	});
	
	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	myModalOpen = function(hash){
		$(hash.w).css("top", $(window).scrollTop() + 150);
		$('.scroll-pane', hash.w).jScrollPane({scrollbarWidth:3,scrollbarMargin:17});
		$('.jqmPrint a').attr('target','_blank');
	}
	
	$('#dialog').jqm({ajax:'@href', target:'.jqmContent', trigger: 'a.showDialog', overlay:0, onLoad:myModalOpen});
	$('#video1').jqm({trigger: 'a[href="#video1"]', overlay:0, onLoad:myModalOpen});
	$('#video2').jqm({trigger: 'a[href="#video2"]', overlay:0, onLoad:myModalOpen});
	$('#video3').jqm({trigger: 'a[href="#video3"]', overlay:0, onLoad:myModalOpen});
	$('#video1').jqmAddClose($('#video1 .jqmVideoThumbs a'));
	$('#video2').jqmAddClose($('#video2 .jqmVideoThumbs a'));
	$('#video3').jqmAddClose($('#video3 .jqmVideoThumbs a'));
})
