$(document).ready(function() {

	// feature scroller
	if ($('ul.featuredProducts li').length>1) {
		features.init();
	}
	
	// promo scroller
	if ($('ul.homeBanner li').length>1) {
		promos.init();
	}
	
	// add site selector to all links [temp]
	/*
	href = window.location.href;
	if (href.indexOf('magnum')!=-1) { site = 'magnum'; othersite = 'hi-tec'; }
	else { site = 'hi-tec'; othersite = 'magnum'; };
	$.each($('a'), function() { 
		if ($(this).attr('href') && $(this).attr('href').indexOf('javascript:')==-1) {
			href = $(this).attr('href');
			if (href.indexOf('?')==-1) { $(this).attr('href', href+'?'+site); }
			else if (href.indexOf('?'+othersite)==-1) { $(this).attr('href', href+'&'+site); }
		}
	});
	*/
	
	// add captionator into form fields
	var field_captions = Array();
	$.each($('input.titled_field'), function() { 
		$(this).click(function() {
			id = $(this).attr('id');
			if (!field_captions[id]) { field_captions[id] = $(this).val(); }	
			if ($(this).val()==field_captions[id]) { 
				$(this).val(''); 
				$(this).attr('class', 'titled_field selected'); 
			}
		});
		$(this).blur(function() {
			if ($(this).val()=='') { 
				$(this).val(field_captions[id]); 
				$(this).attr('class', 'titled_field'); 
			}		   
		});		
	});
	
	// peel position validator
	position_peel();
	$(window).resize(function() {
		position_peel();
	});
	
});

var peel_mode = 'large';
function position_peel() {
	var min_w = 1240;

	// move to small peel
	if ($(window).width()<min_w && peel_mode=='large') {
		$('#pagePeelWrap').attr('class', 'small');
		peel_mode = 'small';
	}
	
	// move to large peel
	if ($(window).width()>=min_w && peel_mode=='small') {
		$('#pagePeelWrap').attr('class', 'large');
		peel_mode = 'large';
	}	
}

var features = {
	frame:4,
	bootwidth:194,
	speed:500,
	products:null,
	product_group:null,
	nav_ul:null,
	num_products:0,
	
	init:function() {	
		$.each($('.featuredHolder'), function() { 
			features.products = $(this).find('ul.featuredProducts li');
			features.product_group = $(this).find('ul.featuredProducts');	
			features.num_products = features.products.length;
			features.nav_ul = $(this).find('.featuredStyles ul');
			
			// scroller required
			if (features.num_products > features.frame) {
				
				// scroll left
				$(this).find('.featuredStyles ul li.leftArrow').click(function () {
					x = parseInt(features.product_group.css('marginLeft'));
					if (x >= 0 ) { 
						features.product_group.css('marginLeft', '0px');
						return; 
					}
					features.product_group.animate({marginLeft:'+='+(features.bootwidth*features.frame)}, features.speed);
				});
				
				// scroll right
				$(this).find('.featuredStyles ul li.rightArrow').click(function () {
					x = parseInt(features.product_group.css('marginLeft'));
					w = features.num_products * features.bootwidth;
					if (x < (w*-1) + (features.frame*features.bootwidth) ) { 
						return; 
					}
					features.product_group.animate({marginLeft:'-='+(features.bootwidth*features.frame)}, features.speed);
				});	
				
				// cat selector
				var cats = Array();
				cats['Mens'] = false;
				cats['Womens'] = false;
				cats['Kids'] = false;
				$.each(features.products, function() { 	
					if (cats[$(this).attr('class')] == false) { cats[$(this).attr('class')] = true; }
				});
				
				html = '';
				count = 0;
				for (i in cats) {
					if (cats[i]==true) {
						count++;
						html += '<li><a href="javascript:features.select_cat(\''+i+'\', '+count+')">'+i+'</a></li>';
					}
				}
				count++;
				if (html!='') { html += '<li><a href="javascript:features.select_cat(\'all\', '+count+')">All</a></li>'; }
				$(this).find('.featuredStyles ul').prepend(html);
			}
			
			// scroller not required
			else {
				$(this).find('.featuredStyles ul li.leftArrow').css('display', 'none');
				$(this).find('.featuredStyles ul li.rightArrow').css('display', 'none');
			};
		});
	},
	
	select_cat:function(cat, count) {		
		// mark category button as selected		
		$(features.nav_ul).find('li a').css('textDecoration', 'underline');
		$(features.nav_ul).find('li:nth-child('+count+') a').css('textDecoration', 'none');
		
		// hide all
		features.products.hide();
		
		// reset x position
		features.product_group.css('marginLeft', '0px');		
	
		// fade in any in this category
		features.num_products = 0;
		$.each(features.products, function() { 
			if (cat=='all' || $(this).attr('class') == cat) { 
				features.num_products++;
				$(this).fadeIn(); 
			}
		});		
		
		// show left/right nav if there are enough products
		if (features.num_products > features.frame) {
			$(features.nav_ul).find('li.leftArrow').attr('class', 'leftArrow');
			$(features.nav_ul).find('li.rightArrow').attr('class', 'rightArrow');
		}
		else {
			$(features.nav_ul).find('li.leftArrow').attr('class', 'leftArrow hide');
			$(features.nav_ul).find('li.rightArrow').attr('class', 'rightArrow hide');
		};		
	}
}

var promos = {
	interval: 6000,	// ms
	fadeTime: 500, //ms
	scrolling: true,
	current: 1,
	total: 0,
	t:0,
	busy:false,
	
	init: function() {
		var html = '<ul class="homeBannerNav">';
		
		promos.total = $('ul.homeBanner li').length;
		
		for (i=0; i<promos.total; i++) {
			className = i==0?'class="selected"':'';
			html += '<li><a '+className+' href="javascript:promos.jumpto('+(i+1)+')">'+(i+1)+'</a></li>';
		}
		
		html += '</ul>';
		$('ul.homeBanner').after(html);
		
		promos.t = setTimeout('promos.next();', promos.interval);
	},
	
	jumpto:function(i) {
		if (promos.busy==true) { return; }
		if (i==promos.current) { return; }
		clearTimeout(promos.t);
		
		$('ul.homeBannerNav li a').attr('class', '');
		promos.busy = true;
		$('ul.homeBanner li:nth-child('+promos.current+')').fadeOut(promos.fadeTime, function() {
				$('ul.homeBanner li:nth-child('+i+')').fadeIn(promos.fadeTime);
				$('ul.homeBannerNav li:nth-child('+i+') a').attr('class', 'selected');
				promos.current = i;
				promos.busy = false;
				if (promos.scrolling) { promos.t = setTimeout('promos.next();', promos.interval); }
			});
		
	},
	
	next:function() {
		n = promos.current+1;
		if (n>promos.total) { n=1; }
		
		promos.jumpto(n);
	}
}
