/* FW100.a1 */
/* (c)2009 SugarHill Works LLC - http://www.sugarhillworks.com */

if (randomizeOrder === true) {
	slideshow.sort(randOrd);
}

var imgs = [], titles = [], captions = [], nav_nums = [], current_fr = 0, current = 0, nIndex = 1, ssRunning, nPause, lastRun, ssPauseTO, resumeToggle = '', thm_scroll_TO, ssPoll, loaded_imgs = [], loader_INT, titles2 = [];

// THIS IS TEMPORARILY SET TO true TO POSITION ALT CNTRS ALTWIDE LAYOUT CONFLICTS WITH imgFit AND imgGrow
var useWideLayout = true;

if (ss_resume_interval) {
	ssPoll = setInterval("ssPoller()",ss_resume_interval);
}



			function buildForm(num) {
				var fm_opts0='', fm_inpts0='', fm_ps0='';
				for (i = 0; i < slideshow[num].option_list_0.length; i++) {
					if(slideshow[num].option_list_0[i].match(/WHITE/)) {
						var swt = 'WHITE';
					}
					if(slideshow[num].option_list_0[i].match(/YELLOW/)) {
						var swt = 'YELLOW';
					}
					if(slideshow[num].option_list_0[i].match(/ROSE/)) {
						var swt = 'ROSE';
					}
					if(slideshow[num].option_list_0[i].match(/SILVER/)) {
						var swt = 'SILVER';
					}
					fm_opts0 += '<option></option>';
					fm_inpts0 += '<input type="hidden" name="option_select'+i+'" id="os0_option_select'+i+'" value="" /> ' + 
								'<input type="hidden" name="option_amount'+i+'" id="os0_option_amount'+i+'" value="" /> ';
					fm_ps0 += '<p><span class="optbg"></span><span class="swatch" id="'+swt+'swt"></span><a class="fadeCol">OPTION 1</a></p>' + 
							 '<div class="clear"></div>';
				}
				$('#opt0_cntr').html('<input type="hidden" name="on0" id="on0" value="METAL" />' + 
				'<select name="os0" id="os0" size="4">' + 
				fm_opts0 + 
				'</select>' + 
				fm_inpts0 + 
				'<div class="label">SELECT A METAL</div>' + 
				fm_ps0 + 
				'<div class="clear"></div>');
				
				var fm_opts1='', fm_ps1='';
				
				slideshow[num].option_list_1.length;
				
				if(slideshow[num].option_list_1.length > 0) {
					for (i = 0; i < slideshow[num].option_list_1.length; i++) {
						fm_opts1 += '<option></option>';
						fm_ps1 += '<p><span class="optbg"></span><a class="fadeCol">1</a></p>';
					}
					$('#opt1_cntr').html('<input type="hidden" name="on1" id="on1" value="SIZE" />' + 
					'<select name="os1" id="os1" size="4">' + 
					fm_opts1 + 
					'</select>' + 
					'<div class="label">SELECT A SIZE</div>' + 
					fm_ps1 + 
					'<div class="clear"></div>');

					$('#os1 > option').each(function(i){
						$('#os1 > option:eq('+i+')').val(slideshow[num].option_list_1[i]).html(slideshow[num].option_list_1[i]);
						$('#opt1_cntr > p:eq('+i+') > a').html(slideshow[num].option_list_1[i]);
					});
			
					$('#opt1_cntr > p').each(function(){
						$(this).click(function(){
							$('#opt1_cntr > .label').css(defaultCss);
							var i = $('#opt1_cntr > p').index(this);
							$('#opt1_cntr > p:eq('+i+')').removeClass().addClass('slctd');
							$('#opt1_cntr > p:not(:eq('+i+'))').removeClass().addClass('deslctd');
							$('#opt1_cntr > p > .optbg').each(function(){
								if ($(this).parent().hasClass('slctd')) {
									$('#os1 > option:eq('+i+')').attr('selected','selected');
									$(this).stop(true, true).fadeTo(750, 1);
								}
								if ($(this).parent().hasClass('deslctd')) {
									$(this).stop(true, true).fadeTo(750, 0);
								}
							});
							$('#opt1_cntr > p > a').each(function(){
								if ($(this).parent().hasClass('slctd')) {
									$(this).stop(true, true).animate({color:"#000"}, 500);
								}
								else if ($(this).parent().hasClass('deslctd')) {
									$(this).stop(true, true).animate({color:"#D4D4D4"}, 500);
								}
							});
						});
					}).hover(
						function() {
							$(this).children('a').stop(true, true).animate({color:"#000"}, 500);
							$(this).children('.optbg').stop(true, true).fadeTo(750, 1);
						}, 
						function() {
							$(this).children('a').stop(true, true).animate({color:"#D4D4D4"}, 500);
							$(this).children('.optbg').stop(true, true).fadeTo(750, 0);
						}
					);
				} else {
					$('#opt1_cntr').html('');
				}
				
				$('#quantity').val(1);
				if (retain_choices === false) {
					//$('opti').attr('selected','selected');
					$(".slctd").each(function(){// alert($(this).html());
						$(this).removeClass().addClass('deslctd');
						//$(this).stop(true,true).fadeTo(0, 750);
					});
				}
				
				$('#item_number').val(slideshow[num].item_num);
				if ((slideshow[num].on_sale === 'checked') && (slideshow[num].discount > 0)) {
					$('#item_name').val(slideshow[num].title + ' (ON SALE)');
				} else {
					$('#item_name').val(slideshow[num].title);					
				}
				$('#os0 > option').each(function(i){
					if ((slideshow[num].on_sale === 'checked') && (slideshow[num].discount > 0)) {
						var saleAmt = Math.round(slideshow[num].option_amts_0[i] - (slideshow[num].option_amts_0[i] * (slideshow[num].discount/100)));
						$('#opt0_cntr > p:eq('+i+') > a').html(slideshow[num].option_list_0[i] + 
															' <span class="regPrice">$' + slideshow[num].option_amts_0[i] + '</span>' +
															' <span class="salePrice">$' + saleAmt + '</span>'
															);
						$('#os0 > option:eq('+i+')').val(slideshow[num].option_list_0[i]).html(slideshow[num].option_list_0[i] + " $"+ saleAmt);
						$('#os0_option_select'+i).val(slideshow[num].option_list_0[i]);
						$('#os0_option_amount'+i).val(saleAmt);
					} else {
						$('#opt0_cntr > p:eq('+i+') > a').html(slideshow[num].option_list_0[i] + " $"+ slideshow[num].option_amts_0[i]);
						$('#os0 > option:eq('+i+')').val(slideshow[num].option_list_0[i]).html(slideshow[num].option_list_0[i] + " $"+ slideshow[num].option_amts_0[i]);
						$('#os0_option_select'+i).val(slideshow[num].option_list_0[i]);
						$('#os0_option_amount'+i).val(slideshow[num].option_amts_0[i]);
					}
				});
				

				$('#opt0_cntr > p').each(function(){
					$(this).click(function(){
						$('#opt0_cntr > .label').css(defaultCss);
						var i = $('#opt0_cntr > p').index(this);
						var saleAmt = Math.round(slideshow[num].option_amts_0[i] - (slideshow[num].option_amts_0[i] * (slideshow[num].discount/100)));
						$('#opt0_cntr > p:eq('+i+')').removeClass().addClass('slctd');
						$('#opt0_cntr > p:not(:eq('+i+'))').removeClass().addClass('deslctd');
						$('#opt0_cntr > p > .optbg').each(function(){
							if ($(this).parent().hasClass('slctd')) {
								$('#os0 > option:eq('+i+')').attr('selected','selected');
								$('#os0_option_select'+i).val(slideshow[current].option_list_0[i]);
								$('#os0_option_amount'+i).val(((slideshow[num].on_sale === 'checked') && (slideshow[num].discount > 0)) ? saleAmt : slideshow[current].option_amts_0[i]);
								$(this).stop(true, true).fadeTo(750, 1);
							}
							if ($(this).parent().hasClass('deslctd')) {
								$(this).stop(true, true).fadeTo(750, 0);
							}
						});
						$('#opt0_cntr > p > a').each(function(){
							if ($(this).parent().hasClass('slctd')) {
								$(this).stop(true, true).animate({color:"#000"}, 500);
							}
							else if ($(this).parent().hasClass('deslctd')) {
								$(this).stop(true, true).animate({color:"#D4D4D4"}, 500);
							}
						});
					});
				}).hover(
					function() {
						$(this).children('a').stop(true, true).animate({color:"#000"}, 500);
						$(this).children('.optbg').stop(true, true).fadeTo(750, 1);
					}, 
					function() {
						$(this).children('a').stop(true, true).animate({color:"#D4D4D4"}, 500);
						$(this).children('.optbg').stop(true, true).fadeTo(750, 0);
					}
				);
				
				$("#qty_cntr").click(function(){
					$("#qty_txt").css(defaultCss);
				});
			
				$(".fadeCol").hover( 
					function() {
						if ($(this).parent().hasClass('slctd')) { return false; }
						else { $(this).stop(true, true).animate({color:"#FFF"}, 750); }
					}, 
					function() {
						if ($(this).parent().hasClass('slctd')) { return false; }
						else { $(this).stop(true, true).animate({color:"#D4D4D4"}, 750); }
					}
				);
			
/* DEBUG $("#logo_cntr").html(slideshow[num].option_list_0[0] + " $"+ slideshow[num].option_amts_0[0]); */
				if ($(window).height() < 725) {
					$('#shopping_url_add').val(window.location.toString().replace(/\/$|\/index.*|\/#.*$|\/\?.*$/i, '?pp_resume='+num+'#menu_cntr'));
					$('#shopping_url_view').val(window.location.toString().replace(/\/$|\/index.*|\/#.*$|\/\?.*$/i, '?pp_resume='+num+'#menu_cntr'));
				} else {
					$('#shopping_url_add').val(window.location.toString().replace(/\/$|\/index.*|\/#.*$|\/\?.*$/i, '?pp_resume='+num));
					$('#shopping_url_view').val(window.location.toString().replace(/\/$|\/index.*|\/#.*$|\/\?.*$/i, '?pp_resume='+num));
				}
			}



function ss_init(ss_cntr_id) {
	var i;
	if (!document.getElementById || !document.createElement) {
		return;
	}

	if (q.pp_resume) {
		current = parseInt(q.pp_resume);
		if ((current < 0) || (current >= ssn) || (isNaN(current) === true)) { current = 0; }
	}
	
	if(glass_nav === true) {
		$('#glass_cntr').click(function () { ssPause(1); });
		$('#glass_cntr').addClass('glass_nav');
		if (showImgsTitleAtt === true) {
			$('#glass_cntr').attr('title', (current+1) + ' of ' + ssn);
		}
	}
	$('#'+ss_image_cntr_id).html('');


	loadThisImg(true, current);


	if ($('#'+ss_thumb_cntr_id).length > 0) { 
		var thms_scr_cntrW = 0, thmsHTML = [], j = 0, thms_folder = slideshow.sPicsFolder.replace('images', 'thumbs');
		for (var i = 0; i < ssn; i++) {
			thmsHTML[j++] = '<div class="thm_cntr">\n' +
								'<div></div><a id="'+i+'_thm_a">' +
									'<img src="'+thms_folder + slideshow[i].thumb +'" id="ssThm_'+i+'" />' +
								'</a></div>\n';
		}

		thmsHTML[j++] = '<div class="clear"></div>';
		
		if ((isitIE === false) || ((isitIE === true) && (IEv > 6))) {
			$('#thms_scr_cntr').append(thmsHTML.join(''));
		} else {//IE6 FIX
			document.getElementById('thms_scr_cntr').innerHTML = thmsHTML.join('');
		}
		
		for (i = 0; i < ssn; i++) {
			var thm = $("#ssThm_"+i);
			if (showThmsTitleAtt === true) {
				thm.attr('title', (i + 1) + ' of ' + ssn);
			}
			thm.click( function() {
				getImgsToFade(current, current_fr, nPause, Number($(this).attr('id').match(/[0-9]+$/)));
			});
			if (i === current) {
				thm.css({'opacity':'1','MozOpacity':'1', 'filter':'alpha(opacity=100)'});
				thm.hover(
					function() {
						$(this).stop(true, true).fadeTo(500, 1);
					},
					function () {
						$(this).stop(true, true).fadeTo(500, 1);
					}
				);
			}
			else {
				thm.css({'opacity':thms_minOpacity.toString(),'MozOpacity':thms_minOpacity.toString(), 'filter':'alpha(opacity='+(thms_minOpacity*100)+')'});
				thm.hover(
					function() {
						$(this).stop(true, true).fadeTo(500, 1);
					},
					function () {
						$(this).stop(true, true).fadeTo(500, thms_minOpacity);
					}
				);
			}
		}
		if (thms_behavior === 'scrolly') { 
			var thmsScrCntrW =0;
			$('#ssThm_'+(ssn - 1)).load(function(){
				$("#thms_scr_cntr").css({display:'block',visibility:'visible'});
				$(".thm_cntr").each(function(i){
					thmsScrCntrW += $(this).outerWidth();
					
					$(this).children("div").width($(this).children("a").children("img").width());
					$(this).children("div").height($(this).children("a").children("img").height());
				});
//				thmsScrCntrW += $(".firstThm").outerWidth();
//				thmsScrCntrW += $(".lastThm").outerWidth();
//				$(".firstThm").children("div").width($(".firstThm").children("a").children("img").width());
//				$(".firstThm").children("div").height($(".firstThm").children("a").children("img").height());
//				$(".lastThm").children("div").width($(".lastThm").children("a").children("img").width());
//				$(".lastThm").children("div").height($(".lastThm").children("a").children("img").height());

				$("#thms_scr_cntr").width(thmsScrCntrW + 'px');
				$('#main_thms_cntr').height($('#main_thms_cntr').height() +'px');
				scrollyThumbsInit();
				thm_scroll_TO = window.setTimeout("scrollThms("+current+");", ss_fade_duration);
			});
			if ((isitIE === true) && (IEv <= 6)) {//IE6 FIX
				thmsScrCntrW = 0;
				window.setTimeout(function(){
					$(".thm_cntr").each(function(i){
						thmsScrCntrW += (parseInt($(this).outerWidth()) - parseInt($(this).offset().left));
					});
					$("#thms_scr_cntr").width(thmsScrCntrW + 'px');
					$('#main_thms_cntr').height($('#main_thms_cntr').height() +'px');
					scrollyThumbsInit();			
				}, 1000);
				thm_scroll_TO = window.setTimeout("scrollThms("+current+");", ss_fade_duration+1000);
			}
		}
	}

	if ($('#'+ss_nav_nums_cntr_id).length > 0) {
		$('#'+ss_nav_nums_cntr_id).html('')
		nav_nums[0] = $(document.createElement('div'));
		nav_nums[0].attr('id','nav_nums_txt_0');
		nav_nums[1] = $(document.createElement('div'));
		nav_nums[1].attr('id','nav_nums_txt_1');
		nav_nums[0].html((current+1));
		nav_nums_info_cntr = $(document.createElement('div'));
		nav_nums_info_cntr.attr('id','nav_nums_txt');
		nav_nums_info_cntr.html(nav_nums_separator + ssn);
		$('#'+ss_nav_nums_cntr_id).append(nav_nums[1]);
		$('#'+ss_nav_nums_cntr_id).append(nav_nums[0]);
		$('#'+ss_nav_nums_cntr_id).append(nav_nums_info_cntr);
	}
	if ($('#'+ss_title_cntr_id).length > 0) {
		$('#'+ss_title_cntr_id).html('');
		titles[0] = $(document.createElement('div'));
		titles[0].attr('id', 'title_txt_0');
		titles[1] = $(document.createElement('div'));
		titles[1].attr('id', 'title_txt_1');
		titles[0].html(slideshow[current].title.replace(/ /g, '&nbsp;'));
		titles2[0] = $(document.createElement('div'));
		titles2[0].attr('id', 'title_txt2_0');
		titles2[1] = $(document.createElement('div'));
		titles2[1].attr('id', 'title_txt2_1');
		titles2[0].html(slideshow[current].title);//.replace(/ /g, '&nbsp;'));
		$('#'+ss_title_cntr_id).append(titles[1]);
		$('#'+ss_title_cntr_id).append(titles[0]);
		$('#title_cntr2').append(titles2[1]);
		$('#title_cntr2').append(titles2[0]);
	}
		if ($('#'+ss_captions_cntr_id).length > 0) {
			$('#'+ss_captions_cntr_id).html('');
			captions[0] = $(document.createElement('p'));
			captions[0].attr('id','caption_txt_0');
			captions[1] = $(document.createElement('p'));
			captions[1].attr('id','caption_txt_1');
			var c1p='', c1a='', c2p='', c2a='';
			var ac1p='', ac1a='', ac2p='', ac2a='';
			if (slideshow[current].caption1) { c1p = caption1Pre; c1a = caption1App; }
			if (slideshow[current].caption2) { c2p = caption2Pre; c2a = caption2App; }
			function doAltCaptions (useWideLayout) {
				if (useWideLayout !== true) {
					$('#'+ss_captions_cntr_id).append(captions[0]);
					$("#caption_txt_0").html(c1p + slideshow[current].caption1 + c1a  + c2p + slideshow[current].caption2 + c2a);
					$("#caption_txt_0").fadeIn(ss_fade_duration);
					$("#alt_caption_txt_0").fadeIn(ss_fade_duration);
				} else {
					$('#'+alt_captions_cntr_id).append(captions[0]);
					$("#caption_txt_0").html(c2p + slideshow[current].caption2 + c2a + c1p + slideshow[current].caption1 + c1a);
					$("#caption_txt_0").fadeIn(ss_fade_duration);
					$("#alt_caption_txt_0").fadeIn(ss_fade_duration);
				}	
			}
			var func = function()
			{
				doAltCaptions(useWideLayout);
			}
			window.setTimeout(func, 100);
		}

	if ($('#paypal_fm').length > 0) {
		buildForm(current);
		if ((show_form_on_return === true) && q.pp_resume) {
			$("#purch_cntr_bg,#purch_cntr").show(0);
		}
	}
	if ((slideshow[current].on_sale === 'checked') && (slideshow[current].discount > 0)) {
		$('#sale_txt_cntr').fadeIn(750);
	} else {
		$('#sale_txt_cntr').fadeOut(500);
	}

	
	lastRun = new Date();
	
	if (ss_autorun === true) {
		window.setTimeout("ssResume();", ss_interval+ss_fade_duration);
	}
	return ssRunning;
}

function imgLoader(num) { //TO BE CALLED FROM SET INTERVAL SO WE CAN 'PAUSE' IT // OR WITH AN ARG SO WE CAN SHOW JUST ONE
	if (loaded_imgs.length < ssn) {//alert(loaded_imgs.length);
		if (!num) {
			if (imgs.length > loaded_imgs.length) { //alert(imgs.length - loaded_imgs.length);//we skipped some...
				num = loaded_imgs.length-1;
			} else {
				num = imgs.length;
			}
		}
		if(!slideshow[num]){return;}
		if (imgs[num]) { 
			posImgs(num, img_h_position, img_v_position);
			if (loader_INT) { clearTimeout(loader_INT); }
			loader_INT = setTimeout("imgLoader("+(num+1)+")",0);
			return;
		}

		imgs[num] = new Image();
		$(imgs[num]).css('display', 'none')
		.load(function () {
			posImgs(num, img_h_position, img_v_position);	
			loaded_imgs.push(loaded_imgs.length);
			//DEGUG:$("#logo_cntr").html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imgs: "+imgs.length+"<br>loaded_imgs: "+loaded_imgs.length);
			if (loader_INT) { clearTimeout(loader_INT); }
			loader_INT = setTimeout("imgLoader()",0);
		})
		.error(function () {
			//alert('image load error');
		})
		.attr({
			alt: num,
			src: slideshow.sPicsFolder + slideshow[num].image,
			id: 'ssImg_'+num
		});
	}
}
function loadThisImg(init, num, fadingOut,fadingIn,fadingOut_fr,fadingIn_fr,nPause) { //alert(num);
	if (loader_INT) { clearTimeout(loader_INT); }
	if (loaded_imgs.length < ssn) {
		if (imgs[num]) { 
			posImgs(num, img_h_position, img_v_position);
			return;
		}
		imgs[num] = new Image();
		$(imgs[num]).css('display', 'none')
		.load(function () {
			posImgs(num, img_h_position, img_v_position);	
			loaded_imgs.push(loaded_imgs.length+"!");
			$('#'+ss_image_cntr_id).append(imgs[num]);
			if (init == false) { //alert(fadingOut);
				$("#ssImg_"+fadingOut).fadeOut(ss_fade_duration);
				$("#ssImg_"+num).fadeIn(ss_fade_duration, function(){shw_fade(fadingOut,fadingIn,fadingOut_fr,fadingIn_fr,nPause);});
			} else {
				$("#ssImg_"+num).fadeIn(ss_fade_duration, function(){shw_fade(fadingOut,fadingIn,fadingOut_fr,fadingIn_fr,nPause);});
			}
			loader_INT = setTimeout("imgLoader()",0);
		})
		.error(function () {
			//alert('image load error');
		})
		.attr({
			src: slideshow.sPicsFolder + slideshow[num].image,
			alt: '',
			id: 'ssImg_'+num
		});
	}
}

function ssPoller(arg) {
	var rnow = new Date();
	if ((rnow - lastRun) > ss_resume_interval) {
	if (ssRunning) { window.clearTimeout(ssRunning); }
		ssResume();
	}
}

function ssPause(dir) { 
	if (ssPauseTO) { window.clearTimeout(ssPauseTO); }
	if (ssRunning) { window.clearTimeout(ssRunning); }
	if (thm_scroll_TO) { window.clearTimeout(thm_scroll_TO); }
	nPause = 'paused';
	if (dir === 0) {
		if ($('#ss_play_cntr').length > 0) {
			$('#ss_play_cntr').css('display','block');
			$('#ss_pause_cntr').css('display','none');
		}
		return;
	} else {
		getImgsToFade(current, current_fr, nPause, -1, dir);
		return nPause, ssRunning, dir;
	}
}

function ssResume() {
	if (ssRunning) { window.clearTimeout(ssRunning); }
	if (ssPauseTO) { window.clearTimeout(ssPauseTO); }
	if (thm_scroll_TO) { window.clearTimeout(thm_scroll_TO); }
	if ((!nPause) || (nPause == 'paused')) {
		nPause = 'resume';
		resumeToggle = 'run';
	} else if (nPause == 'run') { 
		nPause = 'resume';
		resumeToggle = 'paused';
	} else if (nPause == 'resume') {
		if (resumeToggle == 'run') {
			resumeToggle = 'paused';
		} else {
			resumeToggle = 'run';
		}
	}
	getImgsToFade(current, current_fr, nPause);
	return nPause, resumeToggle;//, ssPoll;
}


//posImgs args: num = index of this img in imgs[] array, hpos = 'left'|'center'|'right', vpos = 'top'|'middle'|'bottom'
var stopPos = 0;
function posImgs(num,hpos, vpos) {
	if (stopPos > 20) {
		stopPos = 0;
		return;
	}
	if(!$(imgs[num])) { return;}
	
	if ((imgs[num].width < 50) || (imgs[num].height < 50)) {
		stopPos +=1;
		window.setTimeout(function(){posImgs(num,hpos,vpos);}, 50);
		return;
	}
	if (layout_sm === true) { 
		var ss_cntrW = $('#'+ss_image_cntr_id).innerWidth();
		var ss_cntrH = $('#'+ss_image_cntr_id).innerHeight();
		var imgW = imgs[num].width;
		var imgH = imgs[num].height;
	} else {
		var ss_cntrW = $('#'+ss_image_cntr_id).innerWidth();
		var ss_cntrH = $('#'+ss_image_cntr_id).innerHeight();
		var imgW = imgs[num].width;
		var imgH = imgs[num].height;
	}

	function chkFit () {
		
		if (imgs[num].width > ss_cntrW) {
			var rsz = (ss_cntrW / imgW);
			imgW = Math.floor(imgW * rsz);
			imgH = Math.floor(imgH * rsz);
			imgs[num].style.width = imgW + 'px';
			imgs[num].style.height = imgH + 'px';
		}
		if (imgs[num].height > $('#'+ss_image_cntr_id).innerHeight()) {
			var rsz = (ss_cntrH / imgH);
			imgW = Math.floor(imgW * rsz);
			imgH = Math.floor(imgH * rsz);
			imgs[num].style.width = imgW + 'px';
			imgs[num].style.height = imgH + 'px';
		}
	}
//alert('ss_cntrW = '+$('#'+ss_image_cntr_id).offsetWidth+'\n'+
//		'ss_cntrH = '+$('#'+ss_image_cntr_id).offsetHeight+'\n'+
//		'imgW = '+imgs[num].offsetWidth+'\n'+
//		'imgH = '+imgs[num].offsetHeight);

	function chkGrow () {
		if (imgs[num].width < $('#'+ss_image_cntr_id).innerWidth()) {
			var rsz = (ss_cntrW / imgW);
			imgW = Math.floor(imgW * rsz);
			imgH = Math.floor(imgH * rsz);
			imgs[num].style.width = imgW + 'px';
			imgs[num].style.height = imgH + 'px';
			chkFit();
		}
		if (imgs[num].height < $('#'+ss_image_cntr_id).innerHeight()) {
			var rsz = (ss_cntrH / imgH);
			imgW = Math.floor(imgW * rsz);
			imgH = Math.floor(imgH * rsz);
			imgs[num].style.width = imgW + 'px';
			imgs[num].style.height = imgH + 'px';
			chkFit();
		}
	}

	if ((imgFit === true) && (imgGrow === true)) {
		chkGrow();
	}
	if (imgFit === true) {
		chkFit();
	}

	
	var img_h_position_setBack = hpos, capH, capP; 
	if (altWideLayout === true) {
		if (layout_sm === true) { 
			imgWmax = 475;
			ss_cntrWmax = 720;
			capH = '189px';
			capP = '241px 0 0 0';
		} else {
			imgWmax = 600;
			ss_cntrWmax = 900;
			capH = '239px';
			capP = '351px 0 0 0';
		}
		if (((altWideLayout === true) && (imgW > imgWmax)) || ((altWideLayout === true) && (slideshow[num].caption1 == '') && (slideshow[num].caption2 == ''))) {//ss_cntrW)) {
			$('#'+ss_image_cntr_id).css('width', ss_cntrWmax + 'px');
			ss_cntrW = ss_cntrWmax;
			$('#'+ss_captions_cntr_id).css('height', '1px');
			$('#'+ss_captions_cntr_id).css('padding', '0');
			useWideLayout = true;
			hpos = 'center';
		} else {
			$('#'+ss_image_cntr_id).css('width', imgWmax + 'px');
			ss_cntrW = imgWmax;
			$('#'+ss_captions_cntr_id).css('height', capH);
			$('#'+ss_captions_cntr_id).css('padding', capP);
			useWideLayout = false;
			hpos = img_h_position_setBack;
		}
	}
	
	
	if (hpos === 'left' || !hpos) {
		imgs[num].style.left = 0 + 'px';
	}
	if (hpos === 'center') {
		imgs[num].style.left = Math.round(parseInt((ss_cntrW - imgW) / 2)) + 'px';
	}
	if (hpos === 'right') {
		imgs[num].style.left = Math.round(parseInt(ss_cntrW - imgW)) + 'px';
	}
	if (vpos === 'top' || !vpos) {
		imgs[num].style.top = 0 + 'px';
	}
	if (vpos === 'middle') {
		imgs[num].style.top = Math.round(parseInt((ss_cntrH - imgH) / 2)) + 'px';
	}
	if (vpos === 'bottom') {
		imgs[num].style.top = Math.round(parseInt(ss_cntrH - imgH)) + 'px';
	}
}

function scrollThms(fadingIn) {
	if ($('.jScrollPaneTrack').length < 1) {
		return;
	}
	var tImgOffL = $('#ssThm_'+fadingIn).position().left;
	var thmCntrW = $('.thm_cntr:eq('+fadingIn+')').outerWidth();
	var thmNxImgW = $('#ssThm_'+(fadingIn+1>ssn-1?ssn-1:fadingIn+1)).width();
	var thmScrW = $("#main_thms_cntr").parent().width();
	var thmScrL = $("#main_thms_cntr").position().left;
	var thmLmin = $('#ssThm_'+(fadingIn-1<0?0:fadingIn-1)).width() + tImgOffL;
	var thmLmax = thmScrW - thmCntrW - thmNxImgW - tImgOffL;
	
	var thmLpos = ($(".thm_cntr:eq("+fadingIn+")").position().left) + (thmScrL);
	if (thmLpos < thmLmin) {
		$("#main_thms_cntr")[0].scrollBy(thmLpos - thmLmin);
	}
	if (thmLpos > thmLmax) {
		$("#main_thms_cntr")[0].scrollBy(thmLpos - thmLmax);
	}
}

function getImgsToFade(current, current_fr, nPause, nIndex, dir) {
	if (current == nIndex) {
		if ($('#ss_play_cntr').length > 0) {
			$('#ss_play_cntr').css('display','block');
			$('#ss_pause_cntr').css('display','none');
		}
		ssPause(0);
		return;
	}
	if (ssRunning) { window.clearTimeout(ssRunning); }
	if (thm_scroll_TO) { window.clearTimeout(thm_scroll_TO); }
	
	if($('#purch_cntr_bg').length > 0) {
		if (hide_form_on_nav && ($('#purch_cntr_bg').css('display') !== 'none')) {
			$("#purch_cntr_bg,#purch_cntr").toggle("slide", {direction: "right"}, 500);
		}
	}
	
	var fadingOut = current, fadingOut_fr = current_fr, fadingIn, fadingIn_fr, ss_TO=(ss_fade_duration+150);
	fadingIn_fr = ((current_fr === 0) ? 1 : 0);
	if (nIndex > -1) {  // direct call from thumbnails, we know what imgs to fade...
		fadingIn = nIndex;
		nPause = 'paused';
		if ($('#ss_play_cntr').length > 0) {
			$('#ss_play_cntr').css('display','block');
			$('#ss_pause_cntr').css('display','none');
		}
	} else {
		if (nPause == 'paused') {// make clicked thumb's image the next image to fade in
			if (current+dir < 0) {
				fadingIn = ssn - 1;
			} else {
				fadingIn = (current + dir < ssn) ? current+dir : 0;
			}
			if ($('#ss_play_cntr').length > 0) {
				$('#ss_play_cntr').css('display','block');
				$('#ss_pause_cntr').css('display','none');
			}
		} else if (nPause == 'resume') { //alert('resume')
			fadingIn = (current+1 < ssn) ? current+1 : 0;
			nPause = resumeToggle;
			if (resumeToggle == 'paused') {
				if ($('#ss_play_cntr').length > 0) {
					$('#ss_play_cntr').css('display','block');
					$('#ss_pause_cntr').css('display','none');
				}
			} else {
				ss_TO = (ss_interval+150);
				if ($('#ss_play_cntr').length > 0) {
					$('#ss_play_cntr').css('display','none');
					$('#ss_pause_cntr').css('display','block');
				}
			}
		} else if (nPause == 'run') { //alert('run')
			if ($('#ss_play_cntr').length > 0) {
				$('#ss_play_cntr').css('display','none');
				$('#ss_pause_cntr').css('display','block');
			}
			fadingIn = (current+1 < ssn) ? current+1 : 0;
			ss_TO = (ss_interval+150);
		}
	}

	if ($('#'+ss_thumb_cntr_id).length > 0) {
		if (thms_behavior === 'scrolly') {
			thm_scroll_TO = window.setTimeout("scrollThms("+fadingIn+");", ss_fade_duration);
		}
		if (thms_linkFading === true) {
			$("#ssThm_"+fadingOut).fadeTo(ss_fade_duration,thms_minOpacity);
			$("#ssThm_"+fadingIn).fadeTo(ss_fade_duration,1);
		}
	}
	if ($('#'+ss_nav_nums_cntr_id).length > 0) {
		nav_nums[fadingIn_fr].html((fadingIn+1));
		if (navNums_linkFading === true) {
			$("#nav_nums_txt_"+fadingOut_fr).fadeOut(ss_fade_duration);
			$("#nav_nums_txt_"+fadingIn_fr).fadeIn(ss_fade_duration);
		}
	}
	if ($('#'+ss_title_cntr_id).length > 0) {
		titles[fadingIn_fr].html(slideshow[fadingIn].title.replace(/ /g, '&nbsp;'));
		titles2[fadingIn_fr].html(slideshow[fadingIn].title);//.replace(/ /g, '&nbsp;'));
		if (titles_linkFading === true) {
			$("#title_txt_"+fadingOut_fr).fadeOut(ss_fade_duration);
			$("#title_txt_"+fadingIn_fr).fadeIn(ss_fade_duration);
			if ($("#purch_cntr").is(":visible")) {
				$("#title_txt2_"+fadingOut_fr).fadeOut(ss_fade_duration);
				$("#title_txt2_"+fadingIn_fr).fadeIn(ss_fade_duration);
			} else {
				//$("#buy_btn_cntr").click(function(){
					$("#title_txt2_"+fadingOut_fr).css({display:'none'});
					$("#title_txt2_"+fadingIn_fr).css({display:'block'});
				//);
			}
		}
	}

	if ((slideshow[fadingIn].on_sale === 'checked') && (slideshow[fadingIn].discount > 0)) {
		$('#sale_txt_cntr').fadeIn(750);
	} else {
		$('#sale_txt_cntr').fadeOut(500);
	}

	if (imgs[fadingIn]) {
		posImgs(fadingIn, img_h_position, img_v_position);	
		$('#'+ss_image_cntr_id).append(imgs[fadingIn]);
		$("#ssImg_"+fadingOut).fadeOut(ss_fade_duration);
		$("#ssImg_"+fadingIn).fadeIn(ss_fade_duration, function(){shw_fade(fadingOut,fadingIn,fadingOut_fr,fadingIn_fr,nPause);});
	} else { 
		//DEBUG:titles[fadingIn_fr].html('img_'+fadingIn+' NOT preloaded');
		if (loader_INT) { clearTimeout(loader_INT);}
		loadThisImg(false, fadingIn, fadingOut,fadingIn,fadingOut_fr,fadingIn_fr,nPause);
	}

	
	if ($('#'+ss_captions_cntr_id).length > 0) {
		var c1p='', c1a='', c2p='', c2a='';
		if (slideshow[fadingIn].caption1) { c1p = caption1Pre; c1a = caption1App; }
		if (slideshow[fadingIn].caption2) { c2p = caption2Pre; c2a = caption2App; }
		function doAltCaptions (useWideLayout) {//alert(useWideLayout);
			if (useWideLayout !== true) {
				$('#'+ss_captions_cntr_id).append(captions[fadingIn_fr]);
				$("#caption_txt_"+fadingIn_fr).html(c1p + slideshow[fadingIn].caption1 + c1a  + c2p + slideshow[fadingIn].caption2 + c2a);
				$("#caption_txt_"+fadingOut_fr).fadeOut(ss_fade_duration);
				$("#caption_txt_"+fadingIn_fr).fadeIn(ss_fade_duration);
				$("#alt_caption_txt_"+fadingOut_fr).fadeOut(ss_fade_duration);
				$("#alt_caption_txt_"+fadingIn_fr).fadeIn(ss_fade_duration);
			} else {
				$('#'+alt_captions_cntr_id).append(captions[fadingIn_fr]);
				$("#caption_txt_"+fadingIn_fr).html(c2p + slideshow[fadingIn].caption2 + c2a + c1p + slideshow[fadingIn].caption1 + c1a);
				$("#caption_txt_"+fadingOut_fr).fadeOut(ss_fade_duration);
				$("#caption_txt_"+fadingIn_fr).fadeIn(ss_fade_duration);
				$("#alt_caption_txt_"+fadingOut_fr).fadeOut(ss_fade_duration);
				$("#alt_caption_txt_"+fadingIn_fr).fadeIn(ss_fade_duration);
			}	
		}
		var func = function()
        {
            doAltCaptions(useWideLayout);
        }
        window.setTimeout(func, 100);
	}

	nIndex = fadingIn;
	
	return current, current_fr, nIndex, nPause, ssRunning;
}
	



function shw_fade(fOut,fIn,frfOut,frfIn,nPause) {
	if (fOut == fIn) { return; }
	$("#ss_cntr > img:not(#ssImg_"+fIn+")").remove();
	
	if (($('#'+ss_thumb_cntr_id)) && (thms_linkFading === true)) {
		$("#ssThm_"+fIn).css({'opacity':'1','MozOpacity':'1', 'filter':'alpha(opacity=100)'});
		$("#ssThm_"+fIn).hover(
			function() {
				$(this).stop(true, true).fadeTo(500, 1);
			},
			function () {
				$(this).stop(true, true).fadeTo(500, 1);
			}
		);
		$("#ssThm_"+fOut).css({'opacity':thms_minOpacity.toString(),'MozOpacity':thms_minOpacity.toString(), 'filter':'alpha(opacity='+(thms_minOpacity*100)+')'});
		$("#ssThm_"+fOut).hover(
			function() {
				$(this).stop(true, true).fadeTo(500, 1);
			},
			function () {
				$(this).stop(true, true).fadeTo(500, thms_minOpacity);
			}
		);
	}
	if (showImgsTitleAtt === true) {
		$('#glass_cntr').title = (fIn + 1) + " of " + ssn;
	}

	if ($('#paypal_fm').length > 0) {
		buildForm(fIn);
	}

	//------- reset --------
	fOut = fIn;
	frfOut = frfIn;
	lastRun = new Date();
	if (nPause == 'run') {
		ssRunning = window.setTimeout(function(){getImgsToFade(fOut,frfOut,nPause);}, ss_interval)
	} else {
		window.clearTimeout(ssRunning);
	}
	current = fOut;
	current_fr = frfOut;
}


