﻿//body Id defines the folder from which subMenu texts are loaded
//for subMenu loading: file_name = a.href = div.id
var ctner = { 
	subMenuObj: '#subMenu',
	subMenuId:'subMenu',
	textCtnrObj: '#contLowerMid',
	textCtnrId:'contLowerMid',
	vendorsObj: '#vendors',
	faqObj: '#faqList'
	}
var g = {
	displayed: '',
	classOfInitiallyLoaded: 'htmlLoad', // hard coded in this js
	current: 'current', // hard coded in this js
	textFolderPath: 'texts/',
	vendorFolderPath: 'texts/vendors/',
	faqFolderPath: 'texts/faq/'
	//faqArticleOnLoad: 'Weddings'
	}
$(document).ready(init);
function init() {

//$(document.body).removeClass('jsOff').addClass("jsOn");
setWidthClass();

//IE6 for a:hover with png 1px 5% opacity
/*@cc_on
     @if   (@_jscript_build < 8832) 
$('a.am-hoverShw').append('<span></span>');
   /*@end
@*/
//end IE6 for a:hover

if  ($(ctner.subMenuObj)[0]) {
	
	g.textFolderPath = g.textFolderPath+getId()+"/"; //for submenu texts

	setTextFileLinks(ctner.subMenuObj); //change links into hash values (from href)
	var h = hashValue(); // returns hash without #
	
	//first div is always included in html and made invisible in main css file
	var fDiv = $(ctner.textCtnrObj+' div.htmlLoad');
	g.displayed = fDiv.attr('id');
	if (h == '' || h == g.displayed) {
		 fDiv.removeClass('htmlLoad'); // remove class, because class makes it display:none	  
	} else {
		showPart(h,false,true,false,false); // on load 2:checkIfDivExists() will always be false (no other divs loaded yet)
	}
	
	$(ctner.subMenuObj+' a').click(function() {
		
		var a = this.href.split('#')[1];
		if (a == g.displayed) { 
			return false;
		};
		
		// show/hide sub-sub menus
		var curLI = $(this).parent('li');
		if (curLI.hasClass('prnt')) { 
			var l = curLI.next();
			if(l.is(':not(visible)')) {
				$(ctner.subMenuObj+' li.chld').hide();
				while (l.hasClass('chld')) {
					l.css('display','list-item');
					l = l.next();
				}
			}
		} else if (!curLI.hasClass('chld')) {
				$(ctner.subMenuObj+' li.chld:visible').hide();
		};
		// end show/hide sub-sub menus
		
		//e.preventDefault( ); do not prevent, to have hash for bookmarks
		showPart(a,checkIfDivExists(a),true,false,false); 
	});
}
if($(ctner.vendorsObj)[0]) {
	$(ctner.vendorsObj+' a').click(function(e) {
	
		var hrefFileName = this.pathname; 
		hrefFileName = hrefFileName.substring(hrefFileName.lastIndexOf('/')+1,hrefFileName.lastIndexOf('.html'));
		
		if (hrefFileName == g.displayed) { 
			return false;
		};
		
		e.preventDefault( );
		showPart(hrefFileName,checkIfDivExists(hrefFileName),false,true,false); 
	});
}

if($(document.body).hasClass('faq'))
 {
	 var h = hashValue();
	 var fDiv = $(ctner.textCtnrObj+' div.htmlLoad');
	g.displayed = fDiv.attr('id');
	 if (h == '' || h == g.displayed) { 
		fDiv.removeClass('htmlLoad'); // remove class, because class makes it display:none	  
	} else {
		showPart(h,false,false,false,true); // on load checkIfDivExists() will always be false (no other divs loaded yet)
	}
	
	setTextFileLinks(ctner.faqObj);
	
	$(ctner.faqObj+' a').click(function() {
	
		var a = this.href.split('#')[1];
		if (a == g.displayed) { 
			return false;
		};
		
		showPart(a,checkIfDivExists(a),false,false,true); 
	})
		
}
 
 //change links for faq into hash ones
 $('#faqLink a, #footer li.faq a').each(function() {
 
	var hrefFileName = this.pathname; 
	hrefFileName = hrefFileName.substring(hrefFileName.lastIndexOf('/')+1,hrefFileName.lastIndexOf('.html'));
	if(hrefFileName!='faq') { //if it IS faq, then it's already set with hash, like when loading Bar Mitzvah from bookmark
		this.href = 'faq.html#'+ hrefFileName;
	}
 })
 
 if (getId()=='index')
 {
 swfobject.embedSWF("SaintC_Home.swf", "flashShow", "538", "240", "8.0.0","expressInstall.swf",{},{ wmode: "transparent"},{});
 }
 if (getId()=='about')
 {
 swfobject.embedSWF("SaintC_about.swf", "flashShow", "538", "240", "8.0.0","expressInstall.swf",{},{ wmode: "transparent"},{});
 }

//  if (getId()=='contact')
//  {
// 	 
// 	$(document.body).addClass("jsOn"); //add class, if not added to every page
// 	$('#content').attr('class','am_cfwrap');//'content' should be the title also in html and css
// 	$('noscript').empty();	 
// 	prepareContactForm();
//  }
$('.popUp').click(function(e) {
	e.preventDefault();
	var hrefFileName = this.pathname; 
		hrefFileName = hrefFileName.substring(hrefFileName.lastIndexOf('/')+1);
	popUpShow(hrefFileName); //pass file name (xxx.html)
	this.blur();
 })
 fixBottom();
$('#contLowerMid h3:first').css({marginTop:0});
if (getId()=='news') {
	$('#wpContent h2:first').css({marginTop:0});
	$('#wpSidebar #onlineResources img:odd').css({paddingLeft:6});
	}
$('#promotions').click(function(e) {
	e.preventDefault();
	if ($('#promo').is(':visible')) return false;
	$("#initial").hide();
	$('#promo').show('fast', function() {
		fade(document.getElementById('contLowerMid'),'#edebd3');
	});
});
//end init
};
var fade = function (node,nodeBgrdColor) {
	if (nodeBgrdColor.charAt(0) == '#') {
        nodeBgrdColor = nodeBgrdColor.substr(1,6);
    }
	var rgb_strs = 
        {
            re: /^(\w{2})(\w{2})(\w{2})$/,
            example: ['#00ff00', '336699'],
            process: function (bits){
                return [
                    parseInt(bits[1], 16),
                    parseInt(bits[2], 16),
                    parseInt(bits[3], 16)
                ];
            }
        };
	var rgb_str = rgb_strs.process(rgb_strs.re.exec(nodeBgrdColor));
	var r = rgb_str[0];
	var g = rgb_str[1];
	var b = rgb_str[2];
	
	var fix_rgb = function() {
		r = r > 255 ? 255 : r; 
		g = g > 255 ? 255 : g;
		b = b > 255 ? 255 : b
	};
    
    var step = function (  ) {
        fix_rgb();
        node.style.backgroundColor = 'rgb('+r+','+g+','+b+')';
        if (r < 255 || g < 255 || b < 255) {
            r += 5; g += 5; b+=5;
            setTimeout(step, 100);
        }
    };
    node.style.backgroundColor = 'rgb('+r+','+g+','+b+')';
    setTimeout(step, 100);
};

// fade(document.body);

function setTextFileLinks(ctner) {
	//use this if you need to update location string with hash (for bookmarks)
	$(ctner+' a').each(function() {
	if(this.href.slice(-1)=='#') { // if link is # then take the name from the text
		var n = replaceInStr("&","and",$(this).text());
		n = replaceInStr(" ","-",n);
		n = replaceInStr("’","",n);
		this.href = '#'+ n;
	} else { //here take the name from the href
		var hrefFileName = this.pathname; 
		hrefFileName = hrefFileName.substring(hrefFileName.lastIndexOf('/')+1,hrefFileName.lastIndexOf('.html'));
		this.href = '#'+ hrefFileName;
	}
	})

}

function replaceInStr(a,b,c) {
	var re = new RegExp(a , "g");
	return c.replace(re, b);
}

function hashValue() {
	return (location.hash.substring(1));
}

function checkIfDivExists(divId) {
	return $('#'+divId)[0];
	}
function setSubMenuMarker(subMenu) {
	$(ctner.subMenuObj+' li.current').removeClass('current');
	if(subMenu) {
		$(ctner.subMenuObj+' a').each( function() {
			if(this.href.split('#')[1]==g.displayed) {
				$(this).parent('li').addClass('current');
			}
		});
	}
}

function showPart(divId,divExist,subMenu,vendorsList,faq) {
// 	subMenu,vendorsList,faq : Boolean
	
	if (divExist) {
		if (jQuery.browser.msie) {
			$('#'+g.displayed).hide();
			$('#'+divId).show();
			g.displayed = divId; 
			setSubMenuMarker(subMenu);
		} else {
			$('#'+g.displayed).fadeOut('600', function() {
				$('#'+divId).fadeIn('1000');
				g.displayed = divId; 
				setSubMenuMarker(subMenu);
			});
		}
		
	} else {
		//define text folder first
		var textFolder;
		if(subMenu) {
			textFolder = g.textFolderPath;
		} else if (vendorsList) {
			textFolder = g.vendorFolderPath;
		} else if (faq) {
			textFolder = g.faqFolderPath;
		}
		
		$.get(textFolder+divId+'.html', function(t){											
			$(ctner.textCtnrObj).append(t);
			$('#backToHome').remove();		
			if (jQuery.browser.msie) {
				$('#'+g.displayed).hide();		
				$('#'+divId).show().removeClass('htmlLoad');
				g.displayed = divId; 
				setSubMenuMarker(subMenu);
			} else { 
				$('#'+g.displayed).fadeOut('600', function() { 
					$('#'+divId).fadeIn('1000').removeClass('htmlLoad');
					g.displayed = divId; 
					setSubMenuMarker(subMenu);
				});
			} // do not move outside the block!
		});
	}
	//show/hide vendors for Bar Mitzvah
	if(getId()=='invitations') {
		if(divId=='Bar-and-Bat-Mitzvah') {
			$('#vendors').addClass('bar');
			$('#faqLink a, #footer li.faq a').each(function() {
				this.href = 'faq.html#Bar-and-Bat-Mitzvah';
			 })
		} else {
			$('#vendors').removeClass('bar');
			$('#faqLink a, #footer li.faq a').each(function() {
				this.href = 'faq.html#invitations';
			 })
		}
	}
	
}
/*---STRETCH FOOTER MODULE-----------------------------*/
function fixBottom() {	
	var height = 0;
	var plusMinus = -20;
	if (document.documentElement) {
		height = document.documentElement.offsetHeight ;
	} else if (window.innerHeight) {
		height = window.innerHeight;
	}		
	var dHeight = ($('#wrapper').height() + $('#footer').height()) || 0; //alert('d:'+dHeight); 
	if(dHeight>0 && dHeight<height) {
		$('#footer').css('paddingBottom',(plusMinus+height-dHeight)+'px');
	}
}
/*---CONTACT FORM JAVASCRIPT PART-----------------------------*/

function prepareContactForm() {
	if (jQuery.browser.msie) {
		$('#amcf_submitBtn').hover(function() { 
		$(this).toggleClass("over"); },function() {
		$(this).toggleClass("over"); });
		$('input').focus(function() {$(this).addClass('ieFocus')});
		$('input').blur(function() {$(this).removeClass('ieFocus')});
		$('textarea').focus(function() {$(this).addClass('ieFocus')});
		$('textarea').blur(function() {$(this).removeClass('ieFocus')})
	};
	if($.browser.safari) {
		$("input, textarea").addClass('safari');
	}
		$("#amcf_name, #amcf_note, #amcf_email").focus( function() { 
			if($(this).next().is(':visible')) { $(this).next().hide('slow')}
		});
		var options = { 
		beforeSubmit: validate, 
		success: showResponse 
		};
		$('#am_cf').ajaxForm(options);
};

function validate(formData, jqForm, options) { 
	var form = jqForm[0];
	var err= false;
	if (!form.amcf_name.value) { $("#amcf_nameErr").show(); var err = true;}
	if (form.amcf_note.value.length<6) { $("#amcf_noteErr").show(); var err = true;}
	if (emailCheck(form.amcf_email.value)) { $("#amcf_emailErr").show(); var err = true; }
	if (err) return false;
};

function showResponse()  {
	$('#am_cf').unbind('submit'); $('#amcf_submitBtn').unbind('click');
	$('#am_cf').hide('slow',function() {
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) != 6) {
		$('div.amcf_response').show();
	} else {
		$('div.amcf_response').fadeIn('slow');
	}
	});
};

function emailCheck(value) {
	return !value.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
};

function getId() {
return document.body.getAttribute('id');
};

function setWidthClass() {
	if (screen.availHeight > 768) $(document.body).removeClass('w1024').addClass("w1280");
};

/*---LIGHTBOX MODULE-----------------------------*/
function popUpShow(page) {
	try {
		if (document.getElementById("popUpHideSelect") == null) {
		
			var popUpHeight = 340; var popUpWidth = 550;
		
		// winStyle = "style='min-height:"+popUpHeight+"px;'"; 
// 		if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
// 			winStyle = "style='height:"+popUpHeight+"px;'"; 
// 		}

			$("body").append("<iframe id='popUpHideSelect' style='opacity:0'></iframe><div id='popUpOverlay'></div><div id='popUpWindow' style='display:none'><div id='popUpClose'></div></div>"); 
			$("#popUpOverlay").click(popUpRemove);
			setOpacity(document.getElementById("popUpHideSelect"), 0);
			setOpacity(document.getElementById("popUpOverlay"), 60);
		}
	popUpPosition(popUpHeight,popUpWidth);
	var popUpContents = 'texts/misc/'+page;

    $.get('texts/misc/'+page, function(t){											
		$("#popUpWindow").append(t).css({display:"block"});
		bindCloseButton();
	});
	  
    document.onkeyup = function(e){   
      if (e == null) { // ie
        keycode = event.keyCode;
      } else { // mozilla
        keycode = e.which;
      }
      if(keycode == 27){ // close
        popUpRemove();
      }  
    };
    
  } catch(e) {
    alert( e );
  }
};
function bindCloseButton() {
	$('#popUpClose').hover(function() {
			$(this).addClass("hover");
	}, function() {
			$(this).removeClass("hover");
	});
	$("#popUpClose").click(popUpRemove);
}
function popUpRemove() {
	$("#popUpOverlay").unbind();
	$("#popUpClose").unbind('hover');
	$("#popUpClose").unbind('click');
	// $('#form1').unbind('submit'); $('button').unbind('click'); 
	$("#popUpWindow").fadeOut("fast",function(){$('#popUpWindow, #popUpOverlay, #popUpHideSelect').remove()});
	return false;
};
function popUpPosition(h,w) {
	$("#popUpWindow").css({marginLeft:'-'+w/2+"px"});
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) { 
		var pageScroll = this.getPageScroll();
		h = Number(pageScroll.yScroll-h/2);
		$("#popUpWindow").css({marginTop: h+"px"});
	} else {
       $("#popUpWindow").css({marginTop:'-'+h/2+"px"});
    }
};
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  obj.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="
+ opacity + ");";
  obj.style.KHTMLOpacity = opacity/100;
  obj.style.MozOpacity = opacity/100;
  obj.style.opacity = opacity/100;
};
function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {// Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
	return arrayPageScroll;
};

// function sizeShopFrame() {
// 	var shopFrame = $('#shopFrame');
// 	var innerDoc = (shopFrame.get(0).contentDocument) ? shopFrame.get(0).contentDocument : shopFrame.get(0).contentWindow.document;
// 	//shopFrame.height(innerDoc.body.scrollHeight + 35);
// 	console.log(innerDoc.body.getAttribute('marginwidth'));
// };
// 
// jQuery(function() {
// 	sizeShopFrame();
// 	jQuery("#shopFrame").load(sizeShopFrame);
// })
//end lightbox module
