/**
 * Compact labels plugin
 */
(function($){$.fn.compactize=function(){return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){label.hide();}).blur(function(){if(input.val()===''){label.show();}});window.setTimeout(function(){if(input.val()!==''){label.hide();}},50);});};})(jQuery);

/*
 * hrefID jQuery extention - returns a valid #hash string from link href attribute in Internet Explorer
 */
(function($){$.fn.extend({hrefId:function(){return $(this).attr('href').substr($(this).attr('href').indexOf('#'));}});})(jQuery);

/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			}
		},
		forms : {
			labels : function() {
				$('div.cta-a div.email form p label').compactize();
			},
			quantity : function() {
				$('table.cart-a td.quantity span.value a, div.cartbox-a div.item p.quantity a').click(function() {
					var $input = $(this).parent().find('input[type=text]');
					var value = parseInt($input.val());
					if((typeof value !== typeof 1) || (null == value) || !isFinite(value)) value = 0;
					if($(this).is('.more')) value++;
					if($(this).is('.less') && value > 0) value--;
					$input.val(value);
					return false;
				});
			}
		},
		ui : {
			zoom : function() {
				$('img.zoom-a').each(function() {
					$(this).data({owidth: $(this).width(), oheight: $(this).height()});
				}).bind('mouseenter',function() {				
					if($(this).is(':animated')) return false;
					$(this).parent().andSelf().addClass('zoomed');
					
					var full = new Image();
					full.src = $(this).attr('src');
					
					if (full.complete) {
						var left = Math.round(($(this).data('owidth') -full.width)/2);
						var top = Math.round(($(this).data('oheight') - full.height)/2);
						//$(this).animate({width: full.width, height: full.height, left: left, top: top},300);
						$(this).css({width: full.width, height: full.height, left: left, top: top});
					} else {
						$(this).removeClass('thumb');
					}
				}).bind('mouseleave',function() {
					//$(this).animate({width: $(this).data('owidth'), height: $(this).data('oheight'), left: 0, top: 0},300).parent().andSelf().removeClass('zoomed');
					$(this).css({width: $(this).data('owidth'), height: $(this).data('oheight'), left: 0, top: 0}).parent().andSelf().removeClass('zoomed');
				});
			},
			bargains : function() {
				$('table.cart-a td.pricing a.bargains').mouseenter(function() {
					$(this).parent().find('ul.bargains').show();
				}).mouseleave(function() {
					$(this).parent().find('ul.bargains').hide();
				}).click(function() {
					return false;
				});
			},
			tabs : function() {
				$('div.tabs-a').each(function() {
					$(this).find('div.tabs ul li a').click(function() {
						if($(this).is('.active')) return false;
						
						$(this).addClass('active').parent().siblings().find('a.active').removeClass('active');
						$($(this).hrefId()).show().siblings().filter('.content').hide();
						Cufon.refresh();
						
						return false;
					});
				});
			},
			showcase : function() {
				$showcase 	= $('div.showcase-a');
				var time	= 750;		// transition time
				var cycle	= 5000;		// break time
				
				// mark activity
				$showcase.find('div.item').mouseenter(function() {
					$showcase.data('activity',1);
					if(!$(this).is('.active')) {
						$(this).addClass('active').siblings().removeClass('active').find('p.photo').hide();
						$(this).find('p.photo').show();
					}
				}).mouseleave(function() {
					$showcase.removeData('activity');
				});
				
				// link whole box
				$showcase.find('div.item').each(function() {
					var $link = $(this).find('p.photo a');
					if($link.size() > 0){
						$(this).addClass('linked');
						$(this).find('div.side').click(function() {
							window.location = $link.attr('href');
						});
					}
				});

				// switch
				showcaseChange = function($next) {	
					if($showcase.find('div.item p.photo:animated').size() > 0) return false;
					var $current = $showcase.find('div.item.active');
					if(typeof arguments[0] == 'undefined'){
						if($showcase.data('activity') == 1) return false;
						var $next = ($current.next('div.item').size() < 1) ? $showcase.find('div.item:first') : $current.next('div.item');
					}
					$current.find('p.photo').fadeOut(time,function(){
						$current.removeClass('active');
						$next.addClass('active');
						$next.find('p.photo').fadeIn(time);
					});
				};
				
				// cycle
				var cycle = setInterval('showcaseChange()',(cycle+2*time));

			},
			sidenav : function() {
				var $root = $('ul.nav-a');
				var $drops = $root.find('div.drop');
				
				$root.find('> li').has('div.drop').mouseenter(function() {
					$drops.hide();
					$(this).find('div.drop').show();
				}).mouseleave(function() {
					$(this).find('div.drop').animate({opacity: 1},200,function() {
						$(this).hide();
					});
				});
			}
		},
		design : {
			equalize : function() {
				var tallest = 0
				/*$('ul.products-a li').each(function(i) {
					if(i%4 == 0){
						$(this).nextAll().andSelf().slice(0,4).each(function() {
							var current = $(this).find('strong.name').height();
							if(current > tallest) tallest = current;
						})
						$(this).nextAll().andSelf().slice(0,4).find('strong.name').height(tallest);
						tallest = 0;
						if(i != 0) $(this).before('<li class="separate"><a href="#top">Back to top ^</a></li>')
					}
				});
				
				
				tallest = 0;*/					  
				$('.bt-show').click(function(e){
					e.preventDefault();
					$('div.hide').css('display', 'inline');
					$('p.bt-show').hide();
				});
				$('div.account-a > div').each(function() {
					if($(this).height() > tallest) tallest = $(this).height();
				}).height(tallest);
			}		
		},
		fixes : {
			go : function() {
				// internet explorer 6
				if($.browser.msie && parseInt($.browser.version) == 6){
					$('div.combos-a div.row ul li a').mouseenter(function() {
						$(this).css('z-index','10').find('span').show();
					}).mouseleave(function() {
						$(this).css('z-index','5').find('span').hide();
					});
					
					$('div.overlay-a').width($('body').width()).height($('body').height())
				}
				
				// internet explorer
				if($.browser.msie){
					$('ul.nav-a div.drop').parent().addClass('dropparent');
				}
				
				// firefox < 3.6
				if($.browser.mozilla){
					var version = $.browser.version;
					var segment = version.split('.');
					if(parseInt(segment[2]) < 2) $('html').addClass('ff191');
				}

			}
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.forms.labels();
	Engine.forms.quantity();
	Engine.ui.zoom();
	Engine.ui.bargains();
	Engine.ui.tabs();
	Engine.ui.showcase();
	Engine.ui.sidenav();
	Engine.design.equalize();
	Engine.fixes.go();
	
});
