//v 1.10 updated June 19, 2009
var bn_location_href = window.location.href;
// Helper function to get the value of a parameter from URL. 
function baynote_getUrlParam(name) {
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS, "i" );
  var pageUrl = baynote_getPageUrl();
  var results = regex.exec( pageUrl );
  if( results == null )
    return "";
  else
    return results[1];
}
function baynote_getPageUrl() {
   var pageUrl = bn_location_href;
   if ( (typeof(pageUrl) == "undefined") || (pageUrl == null) || (pageUrl == ""))
     pageUrl = window.location.href;
   return pageUrl;
}
function bn_get_attributes() {
	var results = new Array(1);
	var patt = new RegExp ("^http://(stg.)?wedding.theknot.com/([^/]*)/([^/]*)/([^/]*).*", "i");	
	results = patt.exec(bn_location_href);
	if (results && results.length > 2) {
		baynote_tag.docAttrs.channel = results[1];	
		baynote_tag.docAttrs.subchannel = results[2];			
		baynote_tag.docAttrs.category = "article";			
		baynote_tag.docAttrs.pageType = "article";		
	} else {
	patt = RegExp ("^http://(stg.)?wedding.theknot.com/([^/]*)/([^\.]*).*", "i");
	results = patt.exec(bn_location_href);
	if (results && results.length > 2) {
		baynote_tag.docAttrs.channel = results[1];	
		baynote_tag.docAttrs.subchannel = results[2];			
		baynote_tag.docAttrs.category = "channel";			
		baynote_tag.docAttrs.pageType = "subchannel";		
		} else {
	patt = RegExp ("^http://(stg.)?wedding.theknot.com/([^./]*)\..*", "i");
	results = patt.exec(bn_location_href);
	if (results && results.length > 1) {
		baynote_tag.docAttrs.channel = results[1];			
		baynote_tag.docAttrs.category = "channel";			
		baynote_tag.docAttrs.pageType = "channel";		
			}	
		}
	}
	if (bn_location_href.search("^http://(stg.)?(www|weddings).theknot.com/(checklist|budgeter|guestlistintro|mynotebook|pl_webpage|pwp|correspondence/savethedate|pl_shoppinglist).*", "i") >= 0) {
		baynote_tag.docAttrs.category = "tool";
	}
	else if (bn_location_href.search("^http://(stg.)?(www|weddings).theknot.com/(real-weddings|gallery|bs).*", "i") >= 0) {
		baynote_tag.docAttrs.category = "slideshow";
	}
	else if (bn_location_href.search("^http://(stg.)?weddings.theknot.com/weddingdress.*GownType=[0-9].*", "i") >= 0) {
		baynote_tag.docAttrs.category = "slideshow";
	}
	else if (bn_location_href.search("^http://(stg.)?weddings.theknot.com/quiz.*", "i") >= 0) {
		baynote_tag.docAttrs.category = "quiz";
	}
	else if (bn_location_href.search("^http://(stg.)?weddings.theknot.com/theknottv.*", "i") >= 0) {
		baynote_tag.docAttrs.category = "video";
	}
	else if (bn_location_href.search("^http://(stg|www).theknot.com/(bs|vs|kl|wp).*", "i") >= 0) {
		baynote_tag.docAttrs.category = "local";
	}
	else if (bn_location_href.search("^http://(stg.)?(www|community|talk).theknot.com/(pl|cs|boards|co).*", "i") >= 0) {
		baynote_tag.docAttrs.category = "community";
	}
	else if (bn_location_href.search("^http://lifestyleregistry.theknot.com/.*", "i") >= 0) {
		baynote_tag.docAttrs.category = "registry";
	}
	else if (bn_location_href.search("^http://weddingshop.theknot.com.*", "i") >= 0) {
		baynote_tag.docAttrs.category = "TheKnotShop";
	}
}
// take everything before the dash in the title
function truncate_title(title_pos) {	
	result = document.title.split(" - ");
	if(title_pos == "afterDash") {
		if (result.length >1)
			baynote_tag.title =result[1];
	}
	else {
		baynote_tag.title =result[0];
	}		
}

// take the title from the first non-number value in url
function slideshow_title_from_url() {	
	details = baynote_getUrlParam("wedding_details");
	colors = baynote_getUrlParam("colors");	
	if (  typeof(colors) != "undefined" && colors!= "" && typeof(details) != "undefined" && details!="") {			
		baynote_tag.title ="Photo Galleries - "+ unescape(colors) + " - " + unescape(details);
	} else if (typeof(colors) != "undefined" && colors!= ""){		
		baynote_tag.title ="Photo Galleries - "+ unescape(colors);	
	} else if (typeof(details) != "undefined" && details!= ""){		
		baynote_tag.title ="Photo Galleries - "+ unescape(details);
	}	
}

   
unction bn_getImageSrc() {
	
	var childEleArr = document.getElementsByTagName("*");
	var foundEleArr = [];
	for (var i=0; i<childEleArr.length; i++) {
		if(childEleArr[i].className == "mainimage" ) {
			foundEleArr.push(childEleArr[i]);
		}
	}
	return foundEleArr;
}
function bn_get_img_src(){
	var imgSrc = '';
	if (bn_location_href.search("^http://(stg.)?weddings.theknot.com/(Real-Weddings|Wedding-Tuxedos).*/detailview.aspx.*", "i") >= 0){
		//Extracts the picture from a detailview page for Tuxes and Realwedding content	
		imgSrc = document.getElementById('ImageLarge').src;
	}else if(bn_location_href.search("^http://(stg.)?weddings.theknot.com/(Wedding-Rings|Wedding-Invitations|Bridal-Accessories|Bridal-Registry).*/detailview.aspx.*", "i") >= 0){
		//Extracts the picture from a detailview of rings, accessories, and invitiations
		var nodes [];
		nodes = bn_getImageSrc();
		if(nodes.length > 0){
			if(bnIsIE){
				imgSrc = nodes[0].childNodes[0].src;
			}else{
				imgSrc = nodes[0].childNodes[1].src;
			}
		}
		
	}else if (bn_location_href.search("^http://(stg.)?weddings.theknot.com/Real-Weddings.*/view.aspx.*", "i") >= 0){
		//Extracts the picture from a view page for Real Weddings
		imgSrc =document.getElementById('images').firstChild.firstChild.src;
	}else if(bn_location_href.search("^http://(stg.)?weddings.theknot.com/(Wedding-Rings|Wedding-Invitations|Wedding-Tuxedos).*/View.aspx.*", "i") >= 0){
		//Extracts the picture for a Brand page for Rings, Tuxes, and invites
		imgSrc =document.getElementById('main_image').childNodes[1].src;
	}else if(bn_location_href.search("^https?://(stg.|www.)theknot.com/kl_vendordetail.html?.*", "i") >= 0){
		if(bnIsIE){
			imgSrc =document.getElementById('images').firstChild.firstChild.firstChild.firstChild.firstChild.firstChild.src;
		}else{
			imgSrc =document.getElementById('images').childNodes[1].childNodes[1].childNodes[1].firstChild.childNodes[1].firstChild.src;
		}
		var vcategory = baynote_getUrlParam("category");
		var vcate = vcategory + "";
		baynote_tag.docAttrs.vendCategory = vcate;
		var region = baynote_getUrlParam("keyword");
		var reg = region + "";
		baynote_tag.docAttrs.vendRegion = reg;
	}
	var imgSrcStr = imgSrc + "";
	if(imgSrcStr.length > 1){
		imgSrcStr = imgSrcStr.replace(/(large|intro)_image/gi,'thumbnail');
		if(imgSrcStr.search("^https?://.*","i")>= 0){
			baynote_tag.docAttrs.picture = imgSrcStr;
			baynote_tag.docAttrs.hasPicture = "true";
		}else{
			baynote_tag.docAttrs.picture ='none';
			baynote_tag.docAttrs.hasPicture = "false";
		}
	}else{
		baynote_tag.docAttrs.picture ='none';
		baynote_tag.docAttrs.hasPicture = "false";
	}
	
}

function bn_get_titles() {
	//create array
	var urlregexes = new Array();
	var urltitles = new Array();

	urlregexes[0] = /^http:\/\/(stg.)?wedding.theknot.com\/.*/i;
	urltitles[0] = 'truncate_title' ;
	urlregexes[1] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/checklist.*/i;
	urltitles[1] = 'Wedding Checklist' ;
	urlregexes[2] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/budgeter.*/i;
	urltitles[2] = 'Wedding Budgeter' ;
	urlregexes[3] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/guestlistintro.*/i;
	urltitles[3] = 'Wedding Guest List' ;
	urlregexes[4] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/mynotebook.*/i;
	urltitles[4] = 'The Knot Notebook' ;
	urlregexes[5] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/(pl_webpage|pwp).*/i;
	urltitles[5] = 'Wedding Web Page' ;
	urlregexes[6] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/correspondence\/savethedate.*/i;
	urltitles[6] = 'Save the Dates' ;
	urlregexes[7] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/pl_shoppinglist.*/i;
	urltitles[7] = 'Wedding Shopping List' ;
	urlregexes[8] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/real-weddings\/results.*/i;
	urltitles[8] = 'slideshow_title_from_url' ;
	urlregexes[9] = /^http:\/\/(stg|www).theknot.com\/ch_.*/i;
	urltitles[9] =  'truncate_title_after_dash' ;
	urlregexes[10] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/bs.*/i;
	urltitles[10] = 'truncate_title' ;
	urlregexes[11] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/wp.*/i;
	urltitles[11] = 'Local Wedding Resources' ;
	urlregexes[12] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/pl_main.*/i;
	urltitles[12] = 'My Knot' ;
	urlregexes[13] = /^http:\/\/(stg.)?(www|weddings).theknot.com\/co.*/i;
	urltitles[13] = 'Local Message Boards' ;
	urlregexes[14] = /^http:\/\/lifestyleregistry.theknot.com.*/i;
	urltitles[14] = 'truncate_title' ;
	urlregexes[15] = /^http:\/\/weddingshop.theknot.com.*/i;
	urltitles[15] = 'truncate_title' ;

	//loop through array to find if url matches regex
	for ( i=0; i<urlregexes.length; i++) {
		if (bn_location_href.search(urlregexes[i]) > -1 ) {
			if (urltitles[i] == 'truncate_title' ){		
				truncate_title("beforeDash");
				break;
			}else if (urltitles[i] == 'truncate_title_after_dash' ) {
				truncate_title("afterDash");	
				break;
			}else if (urltitles[i] == 'slideshow_title_from_url' ) {
				slideshow_title_from_url();	
				break;
			}else if (urltitles[i] == 'slideshow_title_from_element' ) {
				slideshow_title_from_el();	
				break;
			}else {
				baynote_tag.title = urltitles[i];
				break;			
			}
		}
	}
}

if (bn_location_href.indexOf("https://") == 0)
{
   baynote_tag.server = "https://theknot-www.baynote.net";
} else {
   baynote_tag.server = "http://theknot-www.baynote.net";
}
baynote_tag.customerId="theknot";
baynote_tag.code="www";
baynote_tag.type="baynoteObserver";
baynote_globals.cookieDomain="theknot.com";
bn_get_attributes();
bn_get_titles();
bn_get_img_src();
baynote_tag.show();


