﻿// Javascript for Menu Navigation and Other Page Functionality - Guida Doors
// requires jquery (jquery.com)

//Utility function to preload images
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

jQuery.preloadImageArr = function(imgArr)
{
	for(i = 0; i<imgArr.length; i++)
	{
		jQuery("<img>").attr("src", imgArr[i]);
	}
}

function preloadNavImages() {
	//gather images in to a string
	var imgStr = "";
	$('img', '#leftNav').add($('img', '#topNav')).each( function() {
		imgStr += ',' + $(this).attr('src').replace('.gif', '-s.gif');
		if($(this).parents('#topNav').length > 0) {
			imgStr += ',' + $(this).attr('src').replace('.gif', '-o.gif');
		}
	 }
	);
	
	if(imgStr.length > 0) {
		//split images string into array
		imgArr = imgStr.substr(1).split(',');
		
		//run preload function on array
		$.preloadImageArr(imgArr);
	}
}

//Utility function for css class replacement on hover
$.fn.hoverClass = function(c) {
	return $(this).each(function() {
		$(this).hover(
			function() { $(this).addClass(c); },
			function() { $(this).removeClass(c); }
		);
	});
};

//Utility function - generic image swap on hover.
//imgDiff: the difference in the hover image's name (i.e. '-s.gif' or '-hover.jpg')
$.fn.swapImg = function(imgDiff) { 
	return $(this).each(function() {
		$(this).hover(
			function() { arrSplit = imgDiff.split('.'); imgExt = '.' + arrSplit[arrSplit.length - 1]; $(this).attr('src', $(this).attr('src').replace(imgExt, imgDiff)); },
			function() { arrSplit = imgDiff.split('.'); imgExt = '.' + arrSplit[arrSplit.length - 1]; $(this).attr('src', $(this).attr('src').replace(imgDiff, imgExt)); }
		);
	});
};

//Top Menu
function topInit() {
	$('.topNode').hover( function() {
		$('img',this).attr('src', $('img',this).attr('src').replace('.gif','-o.gif'));
	},
	function() {
		$('img',this).attr('src', $('img',this).attr('src').replace('-o.gif','.gif'));
	} );
	
	//if($('img','.topNode-selected')[0]) {
	//	$('img','.topNode-selected').attr('src', $('img','.topNode-selected').attr('src').replace('.gif','-s.gif'));
	//}

}

//Left Menu
var sepHover = { background : '#3082A1' };
var sepOut = { background : '#83b4c7' };
function leftInit() {
	$('.baseNode').hover(
		function() {
			$('img',this).attr('src', $('img',this).attr('src').replace('.gif','-s.gif'));
			$(this).prev().children('.seperator').css(sepHover);
		},
		function() {
			$('img',this).attr('src', $('img',this).attr('src').replace('-s.gif','.gif'));
			$(this).prev().children('.seperator').css(sepOut);
		}
	);
	
	$('.subNode').hover(
		function() {
			$('img',this).attr('src', $('img',this).attr('src').replace('.gif','-s.gif'));
		},
		function() {
			$('img',this).attr('src', $('img',this).attr('src').replace('-s.gif','.gif'));
		}
	);
	
	$('.baseNode').hoverClass('baseNode-hover');
	$('.subNode').hoverClass('subNode-hover');
	
	if($('.baseNode-selected/a/img')[0]) {
		$('.baseNode-selected/a/img').attr('src', $('.baseNode-selected/a/img').attr('src').replace('.gif','-s.gif'));
		doBorders($('.baseNode-selected'));		
	}
	if($('.subNode-selected/a/img')[0]) {
		$('.subNode-selected/a/img').attr('src', $('.subNode-selected/a/img').attr('src').replace('.gif','-s.gif'));
	}
	
	$('.baseNode').each(
		function() {
			if( $('#topNavList').length > 0 ) {
				if( $(this).attr('class').indexOf('firstNode') > 0 ) {
					$(this).css('border-top','solid 1px #b5d2dd');
				}
			}
			
			if( $(this).attr('class').indexOf('lastNode') > 0 ) {
				$(this).css('border-bottom','solid 1px #b5d2dd');
			}
		}
	);
	
	linkRel();
}

function doBorders(selectedElem) {
	if( $(selectedElem).prev().length > 0 ) {
		$('.seperator', $(selectedElem).prev()).css( { 'display' : 'none' } );
	}
}


/*** Misc Functions ***/

//sliding show/hide for faq
function doSlide(elem) {
	var hidden = false;
	if ($(elem).siblings('.showhide').css('display').toLowerCase() == 'none') {
		hidden = true;
	}
	
	if(hidden) {
		$(elem).siblings('.showhide').show("slow", function() {
				if($(elem).attr('class') == 'arrow')
					$(elem).show();
				else
					$(elem).siblings('.arrow').show();
			}
		);
	}
	else {
		if($(elem).attr('class') == 'arrow')
			$(elem).hide();
		else
			$(elem).siblings('.arrow').hide();
			
		$(elem).siblings('.showhide').hide("slow");
	}
	
	//this worked ok, but the arrow looked buggy on slideUp because it's hidden in the callback function after the animation
//	$(elem).siblings('.showhide').slideToggle("normal", function() {
//		if($(elem).attr('class') == 'arrow')
//			$(elem).toggle();
//		else
//			$(elem).siblings('.arrow').toggle();
//	} );
}

//make rel='external' links open in new window/tab
function linkRel() {
	$('a[@rel=external]').attr('target','_blank');
}

//dropdownlist client-side validation function
function validateSelection(source, args) {
	if(args.Value < 1) {
		args.IsValid = false;
	}
	else {
		args.IsValid = true;
	}
}

/*** Functions for Door-Style Pages ***/
function initStyleViwer() {
	//$('.styleImg').wrap('<div class="ieImgWrap" style=""></div>');
	
	$('li.style-item').click(
		function() {
			//clear last selection
			//clearSelection($(this).parent('ul').children('.style-item-selected'));
			clearSelection($('.style-item-selected', $(this).parents('.styles').children('.stylelist')));
			
			//add selected class to this
			$(this).addClass('style-item-selected');
			
			//get selection-image relative to this
		    selDiv = $(this).parents('.styles').siblings('.selection');
		    selImg = $('.styleImg', selDiv); //$(selDiv).children('.styleImg');
		    selTxt = $(selDiv).children('.styleNo');
			
			//get image type by extension
			imgSplit = $('img', this).attr('src').split('.');
			imgExt = '.' + imgSplit[imgSplit.length - 1];
			
			//set selection image and text
			$(selImg).attr('src', $('img', this).attr('src').replace('-sm'+imgExt, imgExt));
			$(selTxt).html($('img', this).attr('alt'));
		}
	);
	
	//$('li:first-child', $('.stylelist')).trigger("click");
	$('li:first-child', $('.stylelist').not('[@id^=pg]').add('[@id=pg1]')).trigger("click");
}

function clearSelection(elem) {
	if( $(elem).length > 0 ) {
		$(elem).removeClass('style-item-selected');
	}
}

//for paging through styles
function initStylePager() {
	
	currPgCss = {  };
	
	$('.pager//a').click(
		function() {
			pgID = $(this).attr('href');
			var currentList = "";
			$(this).parents('.styles').children('.stylelist').each(
				function() {
					if( $(this).css('display') != 'none' ) {
						currentList = $(this);
						return;
					}
				}
			);
			prevId = '#' + $(currentList).attr('id');
			$('a[@href='+prevId+']').siblings('span').hide();
			$('a[@href='+prevId+']').show();
				
			if( pgID != "#prev" && pgID != "#next" ) {
				$(this).parents('.styles').children('.stylelist').hide();
				$(this).parents('.styles').children(pgID).show(); 
				
				
				$(this).hide();
				$(this).siblings('span').show();
			}
			else {
			
				if( pgID == '#prev' && $(currentList).attr('id') != 'pg1' ) { 
					$(currentList).hide();
					$(currentList).prev().show(); 
					
					$('a', $('a[@href='+prevId+']').parents('li').prev()).hide();
					$('span', $('a[@href='+prevId+']').parents('li').prev()).show();
					
					pgID = $('a', $('a[@href='+prevId+']').parents('li').prev()).attr('href');
				}
				else if ( pgID == '#next' && $(currentList).attr('id') != 'pgLast' ) { 
					$(currentList).hide();
					$(currentList).next().show(); 
					
					$('a', $('a[@href='+prevId+']').parents('li').next()).hide();
					$('span', $('a[@href='+prevId+']').parents('li').next()).show();
					
					pgID = $('a', $('a[@href='+prevId+']').parents('li').next()).attr('href');
				}
			}
			
			prevLnk = $('a[@href=#prev]', $(this).parents('.pager'));
			nextLnk = $('a[@href=#next]', $(this).parents('.pager'));
			
			$(prevLnk).siblings('span').hide(); $(prevLnk).show();
			$(nextLnk).siblings('span').hide(); $(nextLnk).show();
			
			if( pgID == '#pg1' || pgID == '#pgLast' ) {
				if ( pgID == '#pg1' ) { $(prevLnk).hide(); $(prevLnk).siblings('span').show(); }
				else if ( pgID == '#pgLast' ) { $(nextLnk).hide(); $(nextLnk).siblings('span').show(); }
			}
			
			return false; //don't navigate / propagate event
		}
	);
}

//  features pages
function preloadFeaturesImages() {
	strFeatImgs = ""
	$('.fl-item a').add('.hax-item a').each( function() {
		strFeatImgs += ',' + $(this).attr('href');
	 }
	);
	
	if(strFeatImgs.length > 0) {
		arrFeatImgs = strFeatImgs.substr(1).split(',');
		$.preloadImageArr(arrFeatImgs);
	}
}
function initFeaturesViewer() {
	var fadeOpacity = 0.3;
	$('.fl-item a').click(
		function() {
			door = $('.doorImg', $(this).parents('.door'));
			if( $(door).css('opacity') != fadeOpacity ) {
			$(door).fadeTo("slow", fadeOpacity); }

			oldA = $('.selected', $(this).parents('.features'));
			
			$(oldA).removeClass('selected');
			
			featImg = $('.featImg img', $(this).parents('.door'));
			imgUrl = $(this).attr('href');
			
			$(featImg).fadeOut("slow", function() { 
				if( imgUrl.length > 0 ) {
					$(this).attr('src', imgUrl);
				}
			} );
			
			
			if( $(oldA)[0] != this ) {
			
			
				if( imgUrl.length > 0 ) {
					//featImg.attr('src', imgUrl);
					featImg.attr('alt', $(this).text());
					$(this).addClass('selected');
				}
				
				$('.featImg img', $(this).parents('.door')).fadeIn("slow");
			
			}
			else {
				$(door).fadeTo("slow", 1.0);
			}
			
			return false;
		}
	);
}