/* Copyright (c), 2004-2010 SustainableCircles Corp. All rights reserved. */
(function() {
SL = {};
SL.util = {
addListener: function() {
if (window.addEventListener) {
return function(el, type, fn) {
el.addEventListener(type, fn, false);
};
} else if (window.attachEvent) {
return function(el, type, fn) {
el.attachEvent('on'+type, this._IE6Listener_(fn));
};
} else {
return function(el, type, fn) {
el['on'+type] = fn;
}
}
}(),
_IE6Listener_: function(fn){
return function(e){
e.target = e.srcElement;
fn.call(e.target, e);
}
},
removeListener: function(element, type, listener)
{
if(!element)
return false;
if(window.removeEventListener)	{ // Standard
element.removeEventListener(type, listener, false);
return true;
} else if(window.detachEvent) { // IE
element.detachEvent('on' + type, listener);
return true;
} else return false;
},
stopEvent: function(evt) {
if (!evt) evt = window.event;
(evt.stopPropagation) ? evt.stopPropagation() : evt.cancelBubble = true;
(evt.preventDefault) ? evt.preventDefault() : evt.returnValue = false;
return false;
},
hasClass: function(el, className) {
return el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'));
},
addClass: function(el, className) {
el = this.get(el);
if (!this.hasClass(el, className))
el.className = [el.className, className].join(' ');
},
removeClass: function(el, cls) {
el = this.get(el);
var re = new RegExp('(\\s|^)' + cls + '(\\s|$)');
if (el.className.match(re)) {
el.className=el.className.replace(re, ' ');
}
},
popupNewWindow: function(url, width, height) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open('"
+ url + "', '" + id
+ "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,width=" + width
+ ",height=" + height
+ ",left = 100,top = 200, resizable=yes');"
);
return false;
},
move: function(element, anchorEl, xOffset, yOffset) {
var pos = this.findPos(anchorEl);
if (xOffset)
pos[0] += xOffset;
if(yOffset)
pos[1] += yOffset;
else
pos[1] += anchorEl.offsetHeight;
element.style['top'] = pos[1] + 'px';
element.style['left'] = pos[0] + 'px';
},
findPos: function(obj) {
var l = t = 0;
do {
l += obj.offsetLeft;
t += obj.offsetTop;
} while (obj = obj.offsetParent);
return [l,t];
},
openReusablePopup: function(popup, anchorEl, xOffset, yOffset) {
popup = this.get(popup);
this.closeReusablePopup();
this.move(popup, anchorEl, xOffset, yOffset);
this.openPopup = popup;
this.addClass(popup, 'show_popup');
return false;
},
closeReusablePopup: function(){
if (this.openPopup){
this.removeClass(this.openPopup, 'show_popup');
this.openPopup = null;
}
return false;
},
openPopupAligned: function(popup, anchorEl, yOffset) {
popup = this.get(popup);
yOffset = yOffset || anchorEl.offsetHeight+1;
this.openReusablePopup(popup, anchorEl, 0, yOffset);
var x = popup.offsetLeft - (popup.offsetWidth - anchorEl.offsetWidth)/2;
popup.style['left'] = x + 'px';
return false;
},
get: function(el, doc){
if (typeof el == 'string'){
doc = doc || document;
return doc.getElementById(el);
}
return el;
},
addParams: function(el){
el = this.get(el);
var re = /param\[([^\]]+)\]/;
var links = this.get(el).getElementsByTagName('a');
for (var i=0; i<links.length; i++){
var a = links[i];
if (a.href && a.rel && re.test(a.rel)){
if (a.href.indexOf('?') <= 0)
a.href = a.href + '?' + RegExp.lastParen;
else
a.href = a.href + '&' + RegExp.lastParen;
}
}
}
}
})();
SL.text = {
addClearCopyListeners: function (textField, originalCopy, copyClass){
var u = SL.util;
if (!originalCopy)
originalCopy = textField.defaultValue;
if (!copyClass)
copyClass = 'instructional_copy';
textField.copyClass = copyClass;
textField.originalCopy = originalCopy;
if (textField.value == ''){
u.addClass(textField, copyClass);
textField.value = originalCopy;
u.addListener(textField, "focus", this._focus_);
u.addListener(textField.form, "submit", this._submit_);
}
else if (textField.value == originalCopy){
u.addClass(textField, copyClass);
u.addListener(textField, "focus", this._focus_);
u.addListener(textField.form, "submit", this._submit_);
textField.form.manualSubmit = this._submit_;
}else{
u.removeClass(textField, copyClass);
}
},
_focus_: function(){
var e = this;//evt.target;
SL.util.removeClass(e, e.copyClass);
if (e.value == e.originalCopy){
e.value  = '';
}
},
_submit_ : function(){
var elem = this.elements; //evt.target.elements;
for (var i=0; i<elem.length; i++){
var inp = elem[i];
if (inp.originalCopy && inp.value == inp.originalCopy)
inp.value = '';
}
},
getParent:function(element, parentType){
parentType = parentType.toLowerCase();
var oParent = element.parentNode;
while(oParent){
if (oParent.tagName.toLowerCase() == parentType)
return oParent;
oParent = oParent.parentNode;
}
},
selectText:function(el){
el = SL.util.get(el);
if (document.selection){
var div = document.body.createTextRange();
div.moveToElementText(el);
div.select();
}else{
var div = document.createRange();
div.setStartBefore(el);
div.setEndAfter(el) ;
window.getSelection().addRange(div);
}
return false;
},
addSubmitOnceBlocker: function (evt){
if (this.sl_blocked)
SL.util.stopEvent(evt);
this.sl_blocked='b';
var newDiv=document.createElement('div');
newDiv.id='blocker';
this.parentNode.appendChild(newDiv);
newDiv = document.createElement('div');
newDiv.id='blocker_message';
newDiv.innerHTML = '<span>Sending, please wait...</span>';
this.parentNode.appendChild(newDiv);
},
addCharCounter: function (el, maxLen){
el.maxLen = maxLen;
el.characters_left = document.getElementById('characters_left');
el.characters_over = document.getElementById('characters_over');
SL.util.addListener(el, 'keyup', this._charCount_);
},
_charCount_ : function(){
var txt = this;
var diff = txt.maxLen - txt.value.length;
if (diff > 20){
txt.characters_over.innerHTML = '';
txt.characters_left.innerHTML = diff + ' characters left';
}else if (diff >= 0){
txt.characters_left.innerHTML = '';
txt.characters_over.innerHTML = diff + ' characters left';
}else{
txt.characters_left.innerHTML = '';
txt.characters_over.innerHTML = '<b>'+ (-diff) + ' characters too long </b>';
}
}
}
SL.pop = {
isIE6: function() {
return (typeof document.body.style.maxHeight) == "undefined";
},
init: function(){
var u = SL.util; t = this;
var blocker = t.append('div','p_overlay');
u.addListener(blocker, 'click', t.close);
if (!this.isIE6()){
u.addListener(window, 'resize', function(){SL.pop.center(null)});
}
this.init = null;
},
openModal: function(el){
var u = SL.util; t = this;
el = u.get(el);
if (!el)
return;
if(t.init) t.init();
u.addClass(document.documentElement, 'popup_on');
u.addClass(el, 'popup_visible');
t.last = el;
if (!t.isIE6())
t.center(el);
document.onkeyup = function(e){
if (e == null) { // ie
keycode = event.keyCode;
} else { // mozilla
keycode = e.which;
}
if(keycode == 27){ // close
SL.pop.close();
}
};
return false;
},
openFrame: function(url){
if (url.indexOf('?') < 0)
url += "?popup=1"
else
url += "&amp;popup=1"
var pop = this.append('div','p_pop');
pop.className='popup_window';
pop.innerHTML = "<iframe id='i" + new Date().getTime()
+ "' frameborder='0' allowtransparency='true' scrolling='no' src='"+ url+"'/>";
return this.openModal(pop);
},
iframeLoaded:function(){
var frame = u.get('p_pop').getElementsByTagName('iframe')[0];
var doc = frame.contentDocument || frame.contentWindow.document;
var popupDoc = doc.getElementById('popup-doc');
frame.style.width = (popupDoc.scrollWidth) + 'px';
frame.style.height = (popupDoc.scrollHeight + 4) + 'px';
if (!this.isIE6()) this.center();
frame.setAttribute('scrolling','true');
},
close:function(){
var u = SL.util, p = SL.pop, last = p.last;
document.onkeyup = '';
if (window.jQuery && last){
$(last).fadeTo(500,0.4, function(){
u.removeClass(document.documentElement, 'popup_on');
u.removeClass(this, 'popup_visible');
$(this).css('opacity', '');
});
}else{
u.removeClass(document.documentElement, 'popup_on');
u.removeClass(last, 'popup_visible');
}
if (p.onPopupClose) p.onPopupClose();
p.onPopupClose = null;
p.last = null;
p.lastUrl = null;
return false;
},
center:function(el){
el = el || this.last;
if (el){
el.style['marginTop'] = (el.offsetHeight/-2)+'px';
el.style['marginLeft'] = (el.offsetWidth/-2)+'px';
}
},
append:function(tag, id){
var el = SL.util.get(id);
if (el)
return el;
el = document.createElement(tag);
el.setAttribute('id',id);
document.body.appendChild(el);
return SL.util.get(id);
},
attachSignup:function(base){
var links = document.getElementsByTagName('a');
var pref = base + '/user/';
for (var i=0; i<links.length; i++)
{
var l = links[i];
if(l.href && l.href.indexOf(pref) == 0){
if (l.onclick) continue;
SL.util.addListener(l, 'click', this.signupListener);
}
}
},
signupListener:function(evt){
var t = SL.pop;
t.lastUrl = this.href;
var url = "/b2c/user/redirect_popup.jsp?parent=" + escape(this.href);
t.openFrame(url);
SL.util.stopEvent(evt);
}
};
