var orbis=function(){};

orbis.change_tab = function(anchor){
	var tabclass=$(anchor).closest('ul').attr('class').replace('-nav','-tab');
	$('div.'+tabclass).hide();
	var targetTabId=$(anchor).closest('li').attr('id').replace('li-','');
	$('div#'+targetTabId).show();
	$(anchor).closest('ul').find('li').removeClass('active');
	$(anchor).closest('li').addClass('active');
	return false;
}

jQuery.fn.extend({
	filterSelect: function(){
		return this.each(function(){
			var filterVals=[];
			var filterOn = function(){
				var tbody=$(this).closest('table').find('tbody');
				var selects=$(this).closest('tr').find('select');
				tbody.find('tr').each(function(){
					$(this).show();
				});
				selects.each(function(){
					var val=$(this).val();
					var col=$(this).closest('td').prevAll('td').length;
					if(val){
						tbody.find('tr').each(function(){
							if($.trim($(this).find('td').eq(col).text())!=val){
								$(this).hide();
							}
						});
					}
				});
			};
			$(this).find('tbody').find('tr').each(function(){
				var cols=jQuery(this).find('td');
				for (var i=0;i<cols.length;i++){
					var val=jQuery.trim(cols[i].innerHTML.replace(/(<([^>]+)>)/ig,''));
					if(val!=''){
						if(typeof(filterVals[i])=='undefined'){
							filterVals[i]={};
						}
						if(typeof(filterVals[i][val])=='undefined'){
							filterVals[i][val] = 1;
						}else{
							filterVals[i][val]++;
						}
					}
				}
			});
			sortRow = $('<tr>').addClass('filter').appendTo($(this).find('thead'));
			for(var i=0; i<filterVals.length; i++){
				var optionsGood=false;
				for(var j in filterVals[i]){
					if(filterVals[i][j]>1){
						optionsGood=true;
						break;
					}
				}
					sortCol=$('<td>').css('background-color','#CCCCCC').appendTo(sortRow);
					if(!optionsGood || typeof(filterVals[i])=='undefined' || filterVals[i].length<=1){
						sortCol.html('&nbsp;');
					}else{
						sortSelect=$('<select>').appendTo(sortCol).bind('change',filterOn);
						var options=[];
						for(var j in filterVals[i]){
							//commented line below adds a count after each option
	//						options.push($('<option>').attr('value',j).text(j+"("+filterVals[i][j]+")"));
							options.push($('<option>').attr('value',j).text(j));
						
						}
						options.sort(function(a,b){
							ca=a.attr('value').toUpperCase();
							cb=b.attr('value').toUpperCase();
							if(ca.match(/^[\$,\.\d]+$/) && cb.match(/^[\$,\d\.]+$/)){
								ca=ca.replace(/[^\d\.]/ig,'');
								cb=cb.replace(/[^\d\.]/ig,'');
								return ca-cb;
							}
							return (ca < cb) ? -1 : (ca > cb) ? 1 : 0;
						});
						$('<option>').attr({'value':'','selected':'selected'}).text('-all-').appendTo(sortSelect);
						for(var j=0;j<options.length;j++){
							sortSelect.append(options[j]);
						}
					}
			}
			
		})
	}	,
		firework:function(bounds,duration,avg_interval){
			var killswitch=false;
			var shutdown=function(){
				killswitch=true;
			}
			setTimeout(shutdown,duration)
			var nextStart=0;
			return this.each(function(){
				var img=this;
				var doFirework=function(){
					var w2=$(img).width();
					var h2=$(img).height();
					var t2=bounds.top + Math.floor(Math.random()*(bounds.height-h2+1));
					var l2=bounds.left + Math.floor(Math.random()*(bounds.width-w2+1));
					var w1=Math.round(w2/4);
					var h1=Math.round(h2/4);
					var t1=t2-(h1-h2)/2;
					var l1=l2-(w1-w2)/2;

					$(img).css({
						opacity:0.2,
						width:w1+'px',
						height:h1+'px',
						top:t1+'px',
						left:l1+'px',
						zIndex:11
					}).show().animate({
						width:w2+'px',
						height:h2+'px',
						top:t2+'px',
						left:l2+'px',
						opacity:0.6
					},500,'swing',function(){
						$(this).delay(200).animate({
								opacity:0
						},1000,'linear',function(){
							$(this).hide().css({
								width:w2+'px',
								height:h2+'px',
								top:t2+'px',
								left:l2+'px',
								zIndex:10
							});
							if(!killswitch){
								setTimeout(doFirework,avg_interval*1.5-Math.floor(Math.random()*(avg_interval/4+1)));
							}
						});
					});
				};
				$(this).hide();
				nextStart += avg_interval*1.5-Math.floor(Math.random()*(avg_interval/4+1));
				if(!killswitch){
					setTimeout(doFirework,nextStart);
				}
				
			});
			
		}
});
$(function(){
	$('table.product-item-list').filterSelect();
});
$(function(){
	orbis.stageNum=Math.floor(Math.random()*($('div.stage li').length))
	var nextNum=(orbis.stageNum>=$('div.stage li').length)?0:(orbis.stageNum+1);
	$('div.stage li').hide().eq(nextNum).show();
});

$(function(){
	$('div#main-nav>ul>li').hover(function(){
		clearTimeout(orbis.delayclose);
		if($(this).children('ul').length && $(this).children('ul').css('display')=='none'){
			$('div#main-nav>ul>li>a').css('background-image','none').next('ul').stop().css('height','auto').hide();
			$(this).children('a').css('background-image','url(/images/bkg_main_nav_li_hover.png)').next('ul').slideDown(200);
		}
	},
	function(){
		orbis.closeme=$(this);
		orbis.delayclose=setTimeout("orbis.closeme.children('a').css('background-image','none').next('ul').slideUp(200); orbis.closeme=null;",400)
	});
});

$(window).load(function(){
	orbis.stageNext();
});
orbis.stageNext=function(){
	var nextNum=orbis.stageNum+1;
	if(nextNum>$('div.stage li').length-1){
		nextNum=0;
	}
	var next=$('div.stage li').eq(nextNum).css('z-index',8).show();
	next.find('img.product').hide();
	var cur=$('div.stage li').eq(orbis.stageNum).animate({opacity:0},600,'swing',function(){
		cur.css({zIndex:0,opacity:1});
		var prod=next.find('img.product').show()
		onstage=prod.css('left');
		offstage=prod.width()*-1-parseInt(onstage);
		next.css({zIndex:10});
		prod.css('left',offstage+'px').delay(1600).animate({left:onstage},800,'linear',function(){
			next.find('div.fireworks img').firework({left:0,top:0,width:175,height:279},3400,1000);
		});
		orbis.stageNum=orbis.stageNum+1;
		if(orbis.stageNum>$('div.stage li').length-1){
			orbis.stageNum=0;
		}
		setTimeout(orbis.stageNext,9000);
	});
}
try{Typekit.load();}catch(e){}