
var MiscEffects = {
	
	ToggleSlider: function(slider) {
		slider = $(slider);
		var sliderContent = $(slider).select('div.content').first();
	    if (!sliderContent.visible()) {
	        new Effect.Parallel([new Effect.BlindDown(sliderContent, {sync:true}), new Effect.Appear(sliderContent, {sync:true})], {duration:0.3});
	    	slider.select('h3').first().toggleClassName('active');
		} else {
	        new Effect.Parallel([new Effect.BlindUp(sliderContent, {sync:true}), new Effect.Fade(sliderContent, {sync:true})], {duration:0.3});
			slider.select('h3').first().toggleClassName('active');
	    }
	},
	
	ToggleTab: function(tab_section_id, active_tab_id, active_tab_content_id) {
		active_tab_id = $(active_tab_id);
		active_tab_content_id = $(active_tab_content_id);
		if(!active_tab_id.hasClassName('active'))
		{
			$(tab_section_id).select("div.tabs li a.active").first().removeClassName('active');
			$(tab_section_id).select("div.tab_bottom div.active").first().removeClassName('active');
			active_tab_id.addClassName('active');
			active_tab_content_id.addClassName('active');
		}
	}
	
 }

var Products = {
	
	SelectPic: function(link, imageId) {
		link = $(link);
		if(!link.hasClassName('active'))
		{
			$("picture_frame").select("img.active").first().removeClassName('active');
			$("pic_list").select("li a.active").first().removeClassName('active');
			
			link.addClassName('active');
			$(imageId).addClassName('active');
		}
	}
}

function fittext(divid, spanid, divHoriz, divVert)
{
	if (document.getElementById(divid)) {
		var textSpan = document.getElementById(spanid);
		var textDiv = document.getElementById(divid);
		var fontSize = 100;
		textSpan.style.fontSize = fontSize + '%';
		
		var textDivWidth = textDiv.offsetWidth - divHoriz;
		var textDivHeight = textDiv.offsetHeight - divVert;

		while(textSpan.offsetHeight > textDivHeight)
		{
			fontSize--;
			textSpan.style.fontSize = fontSize + '%';
		}

		while(textSpan.offsetWidth > textDivWidth)
		{
			fontSize--;
			textSpan.style.fontSize = fontSize + '%';
		}
		
		if(textSpan.offsetHeight < textDivHeight) {
			var textDifferencePadding = ((textDivHeight - textSpan.offsetHeight)/2);
			if(textDifferencePadding%1 != 0) {textDifferencePadding -= .5};
			textSpan.style.padding = textDifferencePadding + 'px 0 0';
			textSpan.style.display = 'block';
		}
	}

}

// login widget
function toggleLoginWidget() {
	Effect.toggle('loginWidgetLink','blind', {duration: .1});
	Effect.toggle('loginWidget','blind', {duration: .2});
}

//cookie handling for contest
function contest_set_rating(cookie_name, story_id) {
	   jar = new CookieJar({
		   expires:31536000, // seconds (year)
		   path: '/'
	   });
	   incomingVal = jar.get(cookie_name);
	   fullCookieValue = story_id;
	   
	   alreadyWritten = false;
	   if (incomingVal != null) { 
		   fullCookieValue = incomingVal + "|" + story_id;
		   alreadyWritten = incomingVal.include(story_id);
	   }

	   //only write cookie once
	   if (!alreadyWritten) {
		   jar.put(cookie_name, fullCookieValue);
	   }
	  
}

function contest_get_rating(cookie_name, story_id) {
	jar = new CookieJar({
		   expires:31536000, // seconds (year)
		   path: '/'
	});
	incomingVal = jar.get(cookie_name);
	//check to make sure initial cookie set
	if(incomingVal != null) { 
		if (incomingVal.include(story_id)) {
		   // user already voted on story
			$("Review_Submit").actn.value = "";
			$("Review_Submit").action = $("Review_Submit").action += "?dsp=250510";
		}
	}
	
}

function lockRelativeURLs(content, externalHost) {
	var lockedContent = content.replace(/"\/([^\/][^"]*)"/g, "\"//" + externalHost + "/$1\"");
	return lockedContent;
}

function loadExternalFrame(externalDocument, frameId, externalHost) {
	$(frameId).innerHTML = lockRelativeURLs(externalDocument.getElementById(frameId).innerHTML, externalHost);
}

// Warning: This is not a robust IP address validator.  It simply checks to see if address is in a plausible IP address format (and not a domain name).
function isIPAddressFormat(address) {
	var ipAddressFormat = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
	if (address) {
		return address.match(ipAddressFormat);
	}
	return false;
}

function isThirdLevelDomain(domain) {
	if (domain) {
		var domainTokens = domain.split('.');
		if (domainTokens.length > 2 && !isIPAddressFormat(domain)) {
			return true;
		}
	}
	return false;
}

function extractBaseDomain(fullDomain) {
	if (fullDomain && isThirdLevelDomain(fullDomain)) {
		var domainTokens = fullDomain.split('.');
		domainTokens.shift();
		return domainTokens.join('.');
	}
	return fullDomain;
}

/* Referrer/Affiliate Tracking System */

var ComPaceEComRATS = Class.create();

ComPaceEComRATS.prototype = {
	cookieName: "comPaceEComReferrer",
	cookieDomain: '',
	cookieDuration: 5184000, // = 60 * 60 * 24 * 60; (60 days)
	noReferrerValue: "direct",
	emailQueryKey: "emr_mid",
	emailReferrerValue: "email",
	bridaluxeHost: "tracking.bridaluxe.com",

	initialize: function() {
		// init cookieDomain
		var myHost = window.location.host;
		if (isThirdLevelDomain(myHost)) {
			this.cookieDomain = '.' + extractBaseDomain(myHost);
		}
	},

	getReferrer: function() {
		jar = new CookieJar({expires: this.cookieDuration, domain: this.cookieDomain, path: '/'});

		return jar.get(this.cookieName) || this.noReferrerValue;
	},

	saveReferrer: function(refid, emailQueryKey, httpReferer) {
		jar = new CookieJar({expires: this.cookieDuration, domain: this.cookieDomain, path: '/'});

		if (refid) {
			// refid exists -> always use it
			jar.remove(this.cookieName);
			jar.put(this.cookieName, refid);
		} else if (emailQueryKey) {
			// emr_mid exists -> use this instead
			jar.remove(this.cookieName);
			jar.put(this.cookieName, this.emailReferrerValue);
		} else if (httpReferer) {
			// httpReferer exists -> process it
			var httpRefererHost = parseUri(httpReferer).host;

			if (httpRefererHost) {
				// httpRefererHost exists -> process it
				var selfDomain = extractBaseDomain(window.location.host);

				if (selfDomain) {
					 // successfully obtained our own website's domain -> compare it with httpRefererHost
					if (!httpRefererHost.include(selfDomain)) {
						// httpRefererHost does not contain our own website's domain -> save or overwrite the referrer cookie with it
						jar.remove(this.cookieName);
						jar.put(this.cookieName, httpRefererHost);
					} else {
						// httpRefererHost contains our own website's domain -> keep current cookie if exist or set as direct load
						if (!jar.get(this.cookieName)) {
							jar.put(this.cookieName, this.noReferrerValue);
						}
					}
				} else {
					// could not obtain our own website's domain -> something is wrong with the window, so don't do anything with the referrer cookie
				}
			} else {
				// no host could be extracted from httpReferer -> keep current cookie if exist or set as direct load
				if (!jar.get(this.cookieName)) {
					jar.put(this.cookieName, this.noReferrerValue);
				}
			}
		} else {
			// no refid, emr_mid, or httpReferer -> keep current cookie if exist or set as direct load
			if (!jar.get(this.cookieName)) {
				jar.put(this.cookieName, this.noReferrerValue);
			}
		}
	},

	isReferredBy: function(referrer) {
		return this.getReferrer().include(referrer);
	}
};

var myRATS = new ComPaceEComRATS();

/* External iframe integration with Thanx Media */

function setDomain() {
	if (window.location.host.include("search.exclusivelyweddings.com")) {
		document.domain = "exclusivelyweddings.com";
	}
	if (window.location.host.include("search.eweddings.com")) {
		document.domain = "eweddings.com";
	}
}

setDomain();


/* SessionCookieMgr handles external parameters passed in from vendors */
var SessionCookieMgr = Class.create();
SessionCookieMgr.prototype = {
		cookieNames: [ "nlid", "eid", "emr_mid" ],
		
		initialize: function() {
		},
		
		saveAllParams: function(myQueryKeys) {
			this.cookieNames.each(function(cname, index) {
				if(myQueryKeys[cname]) {
					this.setParam(cname, unescape(myQueryKeys[cname]));
				}
			}, this);
		},
		
		getParam: function(req) {
			if (this.cookieNames.include(req)) {
				jar = new CookieJar({ path: '/'});
				return jar.get(req);
			}
		},
		
		setParam: function(name, value) {
			jar = new CookieJar({ path: '/'});
			jar.put(name, value);
		}
};
var mySessionCookieMgr = new SessionCookieMgr();
mySessionCookieMgr.saveAllParams(parseUri(document.URL.replace('@','%40')).queryKey);
/* End SessionCookieMgr */ 
