$(document).ready(function() {
	
	/* NavBar */
	// Open External Links in New Windows
	$('a:not([href^=http://www.siouxsteel.com],[href^=/index.php/])').attr('target','_blank');
	//if ($('div.new-products').length) $('div#header > ul.nav > li.prod > ul').hide().insertAfter('div#header h1').addClass('staticMenu').slideDown(500);
	$('div#header ul#prod-line > li').each(function(i) {
		$('ul',this).appendTo($('div#header ul.nav li.prod-line:eq('+i+')'));
	});
	var navOptions = (jQuery.browser.msie)? 
		{
			autoArrows:		false,
			dropShadows:	false,
			onInit:			generateBreadcrumbs
		}:
		{
			animation:		{opacity:'show',height:'show'},
			autoArrows:		false,
			dropShadows:	false,
			onInit:			generateBreadcrumbs
		};
	$('div#header ul.nav')
		//.supersubs(
		//	{
		//		minWidth:		12,
		//		maxWidth:		20,
		//		extraWidth:		1
		//	})
		.superfish(navOptions);
	$('ul#prod-line > li,div#header ul.nav > li:not(.ss,.prod)').addClass('prod-line');
	$('ul.nav li.prod-line:eq(2) ul').append('<li><a href="/index.php/dl/">Dealer Locator</a></li>');
	$('div#header ul.nav li:first-child,div#header ul.staticMenu li:first-child').addClass('first');
	$('div#header ul.nav li:last-child,div#header ul.staticMenu li:last-child').addClass('last');
	$('div#header ul.nav li.prod-line:first').hover(
		function() {
			$('div#header ul.nav li.prod').css({backgroundPosition:'0 -42px'});
		},
		function() {
			$('div#header ul.nav li.prod').css({backgroundPosition:'0 0'});
		});
	$('div#header ul.nav > li').hover(
		function() {
			$(this).css({backgroundPosition:'0 -21px'});
		},
		function() {
			$(this).is('.last') ? $(this).css({backgroundPosition:'top left'}): $(this).css({backgroundPosition:'top right'});
		});
	
	
	
	/* New Products */
	$('div.new-products ul#new li div.icon img').each(function() {
		$(this)
			.css({
				width:'67px',
				height:'67px',
				background:'transparent url('+$(this).attr('src')+') no-repeat top center'
				})
			.attr({
				width:67,
				height:67,
				src:'/media/images/structure/holder.gif'
				})
			.hover(
				function() {
					$('div.new-products div.info p').text($(this).attr('alt'));
				},
				function() {
					$('div.new-products div.info p').text('Click image to view video');
				}
			)
			.click(function() {
				var url = $(this).parents('a').attr('href');
				window.location=url;
			});
	});
	
	/* Featured Products */
	$('div#sidebar ul.products li div.icon img').each(function() {
		$(this)
			.css({
				width:'62px',
				height:'62px',
				background:'transparent url('+$(this).attr('src')+') no-repeat top center'
				})
			.attr({
				width:62,
				height:62,
				src:'/media/images/structure/holder.gif'
				});
	});
	/*
	$('div#new.products ul li').each(function() {
		var item = $(this).attr('className').replace(/^item-/,'');
		var name = $(this).text();
		var apos = $(this).is(':first-child')? '50px 0px': '160px 0px';
		$(this).html('<img src="/media/images/products/'+item+'.jpg" alt="'+name+'">');
		$(this).hover(
			function() {
				$('div#new.products div.info').stop().css({backgroundPosition:apos}).slideDown(200,function() {
					$('div#new.products div.info p').text(name);
				});
			},
			function() {
				$('div#new.products div.info').stop().slideUp(200,function() {
					$('div#new.products div.info p').text('');
					$(this).css({backgroundPosition:'-11px 0px'});
				});
			});
		});*/
		
		
		
		/* Slideshow */
		$('div#slides img:first-child').fadeIn('slow',function() {
			$('div#slides.show')
				.cycle({
					timeout:8000,
					speed:1000
				});
		});
		/*
		$('div#slides img:first-child').load(function() {
			console.log("we're loaded.");
			$(this).fadeIn('slow',function() {
				console.log("we've faded.");
				$('div#slides.show')
					.cycle({
						timeout:8000,
						speed:1000
					});
			});
		});
		*/
		/*
		$('div#slides.show img#slide-01').load(function() {
			$('div#slides.show img#slide-01').show('slow');
		});
		$('div#slides.show')
			.cycle({
				timeout:8000,
				speed:1000
			});
			*/
		/*
		$('div#slides.show')
			.cycle({
				timeout:8000,
				speed:1000,
				before:makeCallout
			});
		(jQuery.browser.msie)? $('div#callouts p:first-child').show(): $('div#callouts p:first-child').fadeIn(500);
		*/
		
		
		
		/* Bump Footer Up on Prod / Cat Pages */
		if ($('div#page div#prod-info').length || $('div#page div#cat-info').length) {
			$('div#footer').css({marginTop:'-37px'});
		}
		
		/* Baggin' some Els on the Product Detail pages */
		if ($('div#prod-info').length || $('div#cat-info').length) {
			var ei = 1;
			$('div#prod-info > :parent,div#cat-info > :parent').each(function(i) {
				if ($(this).is('p,ul')) {
					$(this).addClass('e'+ei);
					ei++;
				}
			});
			if ($('div#sidebar div.image-gallery img').length) {
				$('div#sidebar div.image-gallery a:not(.primary)').each(function() {
					var thisalign = $('img',this).is('.align-Left')?'left':'right';
					var thisname = $('img',this).attr('name');
					if ($('div#prod-info').length) {
						$(this).insertBefore('div#prod-info > .'+thisname);
					} else {
						$(this).insertBefore('div#cat-info > .'+thisname);
					}
					$('img',this).attr({align:thisalign});
					/*
					$(this).hide();
					*/
				});
			}
		}
		
		/* Assigning TH/TD Classes Per Col */
		if ($('div#prod-info table:has(col)').length) {
			$('div#prod-info table:has(col)').each(function() {
				var tab = $(this);
				$('col',this).each(function(i) {
					var cls = $(this).attr('class');
					$('tr',tab).each(function() {
						$(':eq('+i+')',this).addClass(cls);
					});
				});
				$('tr:even',this).addClass('odd');
			});
		}
		
		/* Contact Form Validation */
		if ($('form#contact').length) {
			$('form#contact').validate();
		}
		
		/* Catalog Request Form Validation */
		if ($('form#catalog-request').length) {
			$('form#catalog-request').validate();
		}
		
		/* Commercial Bin Sliders */
		if ($('div#comm-bin-slider').length) {
			// $('div#comm-bin-slider div.y-slider').slider({ steps: 10, range: true, change: function(e,ui) { console.log(ui.range); } });
			// $('div#comm-bin-slider div.x-slider').slider({ steps: 10, range: true, change: function(e,ui) { console.log(ui.range); } });
			$('div#comm-bin-slider').show();
		}
		
		/* Product List Display on Cat Pages */
		if ($('div.product-list ul li').length)
		{
			$('div.product-list,div.view-products').show();
		}
		
		/* Subcategory Display on Cat Pages */
		if ($('div.sub-categories ul li').length)
		{
			var subsstr = [];
			$('div.sub-categories ul').hide();
			$('div.sub-categories ul li').each(function() {
				subsstr.push($(this).html());
				// $('a',this).appendTo($('div.sub-categories h4'));
			});
			$('div.sub-categories h4').append(' '+subsstr.join(', '));
			$('div.sub-categories').show();
		}
		
		/* Return Goods Form New Line */
		if ($('form#return').length)
		{
			$('form#return').validate();
			$('form#return span#new-line').click(function() {
				var fh = $('form#return').innerHeight() + 30;
				var newline = $('form#return fieldset.item-list tr.invisible-line').clone();
				var linecount = $('form#return fieldset.item-list tr.line').length;
				$('td:first',newline).text(++linecount);
				$(newline)
					.addClass('line')
					.removeClass('invisible-line')
					.insertBefore($('form#return fieldset.item-list  tr.new-line'));
				// if (jQuery.browser.msie) $('form#return').css({overflow:'hidden'});
				// $(window).resize();
			});
		}
		
		/* Dealer File List Header */
		if($('div.dealer-info div.file-list ul li').length) {
			$('div.dealer-info div.file-list').show();
		}

		/* Adjust Content Height */
		if ($('div#sidebar').length && $('div#prod-info').length) {
			var sh = $('div#sidebar').innerHeight() + 40;
			var ch = $('div#prod-info').innerHeight();
			if (sh > ch) $('div#prod-info').css({height:sh+'px'});
		}
		
		/* Commercial Bin Table Mod */
		if ($('table.comm-bin').length) {
			var to = [];
			$('table.comm-bin td.size').each(function() {
				to.push('<a href="#'+$(this).attr('id')+'">'+$(this).text()+'</a>');
			});
			$('table.comm-bin').before('<p>Choose a Bin Diameter: '+to.join(', ')+'</p>');
		}
		
		/* Dealer File List Stuff */
		$('ul.new_dealer_files li h4.engineering-manuals').siblings('ul').eq(0).remove();
		$('ul.new_dealer_files li h4').click(function() {
			// if ($(this).siblings('ul').html()!='') {
			$(this).siblings('ul').slideToggle('fast',function() {
			$('div#prod-info').css({height:'1200px'});
			$('div#footer').css({marginTop:'1200px'});
			});
			// }
		});

})

function makeCallout() {
	var cls = $(this).attr('id');
	(jQuery.browser.msie)? $('div#callouts p').hide(): $('div#callouts p').fadeOut(1000);
	(jQuery.browser.msie)? $('div#callouts p.'+cls).show(): $('div#callouts p.'+cls).fadeIn(1000);
}

function generateBreadcrumbs() {
	$('ul.nav_categories').remove();
	if ($('div#bc').length) {
		var c = $('div#bc').attr('class');
		var o = [];
		$('ul.nav li a.'+c).parents('li:not(.prod)').each(function() {
			o.push('<a href="'+$('>a',this).attr('href')+'">'+$('>a',this).text()+'</a>');
		});
		$('div#bc').append(o.reverse().join(' &#187; '));
		// $('title').text('Sioux Steel :: '+$('div#bc').text());
		if ($('div.back-button').length) {
			if ($('div.back-button.prod-info').length)
			{
				// console.log($('div#bc a:last'));
				var lasta = $('div#bc a:last').clone();
				$('div.back-button').html($(lasta));
				$('div.back-button a').text('Return to '+$('div.back-button a').text());
				$('div.back-button').show();
			} else if ($('div#prod-info.gallery div.back-button a').length) {
				$('div.back-button').show();
			} else {
				if (o.length > 1) {
					var thisn = o.pop();
					var thatn = o.pop();
					$('div.back-button').html(thatn);
					$('div.back-button a').text('Return to '+$('div.back-button a').text());
					$('div.back-button').show();
				} else {
					$('div.back-button').remove();
				}
			}
		}
	}
}
