$(document).ready(function() {

	jQuery.preloadImages = function()
	{
		for(var i = 0; i<arguments.length; i++)
		{
			jQuery("<img>").attr("src", arguments[i]);
		}
	}

	$.preloadImages("images/uploads/secondary-link_02.jpg");
	//$.preloadImages("images/uploads/highlight.png");
	
	if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
		$('img').each(function() {
			if ($(this).attr('src').match("png$"))
			{
				$(this).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).attr('src') + "', sizingMethod='scale')");
				$(this).attr('src','images/blank.gif');
			};
		});
	};

	if ($('#imageSwitcher').length > 0) {
		indexImageSwitcher = Math.floor(Math.random()*12);

		nextImage = function() {
			indexImageSwitcher++;
			if (indexImageSwitcher > 13) {
				indexImageSwitcher = 1;
			}
			if (indexImageSwitcher < 10) {
				indexImageSwitcher = "0" + indexImageSwitcher;
			}
			$('#imageSwitcher').attr('src', "http://www.hytechspring.com/themes/site_themes/images/home/home_random_" + indexImageSwitcher + ".jpg");
		}

		nextImage();
		setInterval('nextImage()', 5000);	
	}

	if ($(".fancybox").length > 0) {
		$(".fancybox").fancybox({
			'titlePosition'		: 'inside',
			'overlayColor'		: '#000',
			'overlayOpacity'	: 0.9
		});
	}

	$("img.rollover").each(function() {$.preloadImages(this.src.replace("_01","_02"));});

	$("img.rollover").hover(
		function() {
			this.src = this.src.replace("_01","_02");
		},
		function() {
			this.src = this.src.replace("_02","_01");
		}
	);

	$("#contactLink").click(function(){
		if ($("#contactForm").is(":hidden")){
			$("#contactForm").show();
			$("#contactForm").animate({width: "135px"}, 1000);
		} else {
			$("#contactForm").animate({width: "0px"}, 1000, function() {$("#contactForm").hide();});
		}
	});

	$("#contentArea1 a p").css('opacity','.8');

	$("#contentArea1 a").hover(function() {$(this).find('p').css('background-color','#1E6DBC');}, function() {$(this).find('p').css('background-color','#000');});

	$("fieldset a").click(function(e) {
		e.preventDefault();
		$("#contactForm").animate({width: "0px"}, 1000, function() {$("#contactForm").hide();});
	});
	
	
	/*if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 9) {
		$('#scroller').css('position', 'relative');
	} else {
		$(".floatL a").hover(
			function() {
				$(this).append('<img src="http://www.hytechspring.com/images/uploads/highlight.png" class="highlighter" />');
				$("#description p").html($(this).attr("rel"));
			},
			function() {
				$(".highlighter").remove();
				$("#description p").html("");
			}
		);
	}*/
    
    $(".floatL a").hover(
        function() {
            $(this).find('img').css('border', '2px solid #1E6DBC');
            $(this).find('img').css('margin', '-1px');
			$("#description p").html($(this).attr("rel"));
        },
        function() {
            $(this).find('img').css('border', '1px solid #333333');
            $(this).find('img').css('margin', '0');
			$("#description p").html("");
        }
    );

	if ($('#scroller').length > 0) {
    
    	var updateUrl = function() {
        		var industries = '';
				$('#industries input').each(function() {
					if ($(this).is(':checked')) {
						if (industries == '') {
							industries = $(this).val();
						} else {
							industries += '|' + $(this).val();
						}
					}
				});
				var classes = '';
				$('#classes input').each(function() {
					if ($(this).is(':checked')) {
						if (classes == '') {
							classes = $(this).val();
						} else {
							classes += '|' + $(this).val();
						}
					}
				});
				var materials = '';
				$('#materials input').each(function() {
					if ($(this).is(':checked')) {
						if (materials == '') {
							materials = $(this).val();
						} else {
							materials += '|' + $(this).val();
						}
					}
				});
				if (industries == '') {
					industries = 'void';
				}
				if (classes == '') {
					classes = 'void';
				}
				if (materials == '') {
					materials = 'void';
				}
                
				//materials = materials.replace(/Precious Metals/gi, "Gold|Platinum|Silver|Titanium|Precious%20Metals");
                
                		$('#description p').html('Updating&hellip;');
				document.location = 'http://www.hytechspring.com/' + 'index.php/gallery/' + industries + '/' + classes + '/' + materials + '/';
			};
    
		var $panels = $('#scroller > .scrollContainer > div');
		if ($panels.length > 0) {
			var $container = $('#scroller .scrollContainer');
			var horizontal = true;
			if (horizontal) {
			  $panels.css({
				'float' : 'left',
				'position' : 'relative' // IE fix to ensure overflow is hidden
			  });
			  $container.css('width', $panels[0].offsetWidth * $panels.length);
			}
			var $scroll = $('#scroller').css('overflow', 'hidden');
			
			function selectNav() {
			  $(this)
				.parents('ul:first')
				  .find('a')
					.removeClass('selected')
				  .end()
				.end()
				.addClass('selected');
			}
			
			$('#scrollNav').find('a').click(selectNav);
			
			function trigger(data) {
			  var el = $('#scrollNav').find('a[href$="' + data.id + '"]').get(0);
			  selectNav.call(el);
			}
			if (window.location.hash) {
			  trigger({ id : window.location.hash.substr(1) });
			} else {
			  $('ul#scrollNav a:first').click();
			}
			var offset = parseInt((horizontal ? 
			  $container.css('paddingTop') : 
			  $container.css('paddingLeft')) 
			  || 0) * -1;
			var scrollOptions = {
			  target: $scroll,
			  items: $panels,
			  navigation: '#scrollNav a',
			  prev: 'img.scrollLeft', 
			  next: 'img.scrollRight',
			  axis: 'xy',
			  onAfter: trigger,
			  offset: offset,
			  duration: 500,
			  easing: 'swing'
			};
			$('#galleryContent').serialScroll(scrollOptions);
			$.localScroll(scrollOptions);
			scrollOptions.duration = 1;
			$.localScroll.hash(scrollOptions);
		}
		if ($panels.length < 2) {
			$('img.scrollLeft').css('opacity', 0);
			$('img.scrollRight').css('opacity', 0);
		}
		
		var siteUrl = 'http://www.hytechspring.com/';
		
		$('input').attr('checked', false);
		
		//get url and highlight correct options
		var adjustedUrl = unescape(document.URL.substring(siteUrl.length + 18));
		var urlArray = adjustedUrl.split('/');

		var collapseClasses = true;
		var collapseMaterials = true;

		for(var i in urlArray)
		{
			var split = urlArray[i].split('|');
			for(var j in split)
			{
            			split[j] = split[j].charAt(0).toUpperCase() + split[j].slice(1);
				if (split[j] != "" && $('#' + split[j].replace(/ /gi, "_")).length > 0) {
					$('#' + split[j].replace(/ /gi, "_")).attr('checked', 'checked');
					if (split[j].replace(/ /gi, "_") == "Springs") {
						$('#Coil_Springs').attr('checked', 'checked');
						$('#Compression_Springs').attr('checked', 'checked');
						$('#Extension_Springs').attr('checked', 'checked');
						$('#Garter_Springs').attr('checked', 'checked');
						$('#Torsion_Springs').attr('checked', 'checked');
					} else if (split[j].replace(/ /gi, "_") == "Precious_Metals") {
						$('#Gold').attr('checked', 'checked');
						$('#Platinum').attr('checked', 'checked');
						$('#Silver').attr('checked', 'checked');
						$('#Titanium').attr('checked', 'checked');
					} else if (split[j].replace(/ /gi, "_") == "Machining") {
						$('#Precision_Machining').attr('checked', 'checked');
						$('#Prototype_Machining').attr('checked', 'checked');
						$('#Specialty_Machining').attr('checked', 'checked');
						$('#Swiss_Machining').attr('checked', 'checked');
					}
					if (i == 1 && $('#' + split[j].replace(/ /gi, "_")).parent('dd').length > 0) {
						collapseClasses = false;
					} else if (i == 2 && $('#' + split[j].replace(/ /gi, "_")).parent('dd').length > 0) {
						collapseMaterials = false;
					}
				}
			}
		}

		/*$("dl").collapsorz({
			toggle: "> dd",
			minimum: 0,
    			showText: "&darr; More",
    			hideText: "&uarr; Less"
		});*/

		if (!collapseClasses) {
			$('#classes a').click();
		}
		if (!collapseMaterials) {
			$('#materials a').click();
		}
		
		$('#secondaryImageLeft').css('margin-bottom', '5px');
		
		$('input').click(function() {
			
			if ($(this).attr('id') == "Springs") {
				$('#Coil_Springs').attr('checked', 'checked');
				$('#Compression_Springs').attr('checked', 'checked');
				$('#Extension_Springs').attr('checked', 'checked');
				$('#Garter_Springs').attr('checked', 'checked');
				$('#Torsion_Springs').attr('checked', 'checked');
			} else if ($(this).attr('id') == "Precious_Metals") {
				$('#Gold').attr('checked', 'checked');
				$('#Platinum').attr('checked', 'checked');
				$('#Silver').attr('checked', 'checked');
				$('#Titanium').attr('checked', 'checked');
			} else if ($(this).attr('id') == "Machining") {
				$('#Precision_Machining').attr('checked', 'checked');
				$('#Prototype_Machining').attr('checked', 'checked');
				$('#Specialty_Machining').attr('checked', 'checked');
				$('#Swiss_Machining').attr('checked', 'checked');
			} else if ($(this).attr('id') == "Coil_Springs" || $(this).attr('id') == "Compression_Springs" || $(this).attr('id') == "Extension_Springs" || $(this).attr('id') == "Garter_Springs" || $(this).attr('id') == "Torsion_Springs") {
				$('#Springs').attr('checked', false);
			} else if ($(this).attr('id') == "Gold" || $(this).attr('id') == "Platinum" || $(this).attr('id') == "Silver" || $(this).attr('id') == "Titanium") {
				$('#Precious_Metals').attr('checked', false);
			} else if ($(this).attr('id') == "Precision_Machining" || $(this).attr('id') == "Prototype_Machining" || $(this).attr('id') == "Specialty_Machining" || $(this).attr('id') == "Swiss_Machining") {
				$('#Machining').attr('checked', false);
			}
			if ($(this).attr('id') != 'search') {updateUrl();}
		});
		
		$('#search').keypress(function(e) {
			if(e.which == 13) {updateUrl();}
		});
		
		$('#magnifyingGlass').click(function() {updateUrl();});
		
	} else {$.localScroll();}

});

function closeForm(){
	$("#messageSent").show("slow");
	setTimeout('$("#messageSent").hide();$("#contactForm").animate({width: "0px"}, 1000, function() {$("#contactForm").hide();});');
}

