// JavaScript Document // embed the watcher swf var __wtw_g_started = false; var WTW_Watcher = { whosOnline : {}, CTRL : false, SHIFT : false, ALT : false, selected_choice: false, current_poll_id: 0, current_choice: 0, rfxnum : /^([+-]=)?([\d+-.]+)(.*)$/, rdashAlpha : /-([a-z])/ig, fcamelCase : function( all, letter ) { return letter.toUpperCase(); }, lastRequestJID: "", lastRequestPhoto: "", lastRequestName: "", lastRequestUserName:"", isWatching : false, CHAR_CODE : -1, poll_visible : false, totalOnline : 0, registered_as_watcher : false, id_count:0, my_uid:0, audio_ring:null, audio_shut:null, jQuery:null, custom_name:"", get_available_poll: function(){ // only if allowable var stop_asking = WTW_Watcher.readCookie("__wtw_stop_asking"); if (stop_asking == null || WTW_Watcher.poll_visible) { var url = document.location.href; // get an available poll if there WTW_Watcher.getJSON('http://www.birthvillage.com/watcher/json.get_available_poll.php?uid=' + encodeURIComponent(WTW_Watcher.my_uid) + "&r=" + encodeURIComponent(WTW_Watcher.getRoomName()) + "&u=" + encodeURIComponent(url) + "&jsoncallback=WTW_Watcher.on_available_poll"); } }, isFunction: function( obj ) { return toString.call(obj) === "[object Function]"; }, getJSON: function( url ) { // setup json stuff var head = document.getElementsByTagName("head")[0] || document.documentElement; var script = document.createElement("script"); script.src = url; script.charset = 'utf-8'; // Use insertBefore instead of appendChild to circumvent an IE6 bug. // This arises when a base node is used (#2709 and #4378). head.insertBefore( script, head.firstChild ); }, on_available_poll: function(data){ if (data.hasPoll) { // got a new poll, so show it WTW_Watcher.prompt_poll(data.poll_info); } else { // keep checking //setTimeout("WTW_Watcher.get_available_poll()",10000); } }, nothing: function(){ // does nothing // for ajax calls that need it return; }, back_off: function(){ // stop asking questions // set cookie to stop asking and close it WTW_Watcher.createCookie("__wtw_stop_asking",1,1); // session cookie // close poll box WTW_Watcher.hide_poll_box(); // submit back off statistics WTW_Watcher.getJSON('http://www.birthvillage.com/watcher/json.back_off.php?uid=' + encodeURIComponent(WTW_Watcher.my_uid) + "&pid=" + encodeURIComponent(WTW_Watcher.current_poll_id ) + "&jsoncallback=WTW_Watcher.nothing"); }, chat_back_off: function(){ try { var jid = WTW_Watcher.lastRequestJID; var fl = WTW_Watcher.getFlashMovie("__wtw_fl_watcher"); fl.RejectChatRequest(jid, "ignore"); } catch (ex) { __wtw_debug("reject_chat_request: " + ex); } // stop asking chat // set cookie to stop asking and close it WTW_Watcher.createCookie("__wtw_stop_asking_chat",1,1); // session cookie // close poll box WTW_Watcher.hide_chat_box(); // submit back off statistics // WTW_Watcher.getJSON('http://www.birthvillage.com/watcher/json.back_off.php?uid=' + encodeURIComponent(WTW_Watcher.my_uid) + "&pid=" + encodeURIComponent(WTW_Watcher.current_poll_id ) + "&jsoncallback=WTW_Watcher.nothing"); }, getDiv: function(id) { return document.getElementById(id); }, create_poll_box: function(){ // if not already created, then create it if (WTW_Watcher.getDiv("__wtw_watcher_poll_box") == null) { // make it var poll_box = document.createElement("div"); var poll_box_inside = document.createElement("div"); // set id (css is set in style.css file) poll_box.id = "__wtw_watcher_poll_box"; poll_box_inside.id = "__wtw_watcher_poll_box_inside"; poll_box_inside.style.bottom = "-500px"; // setup placeholders poll_box_inside.innerHTML = '
Comments (optional)
stop asking me
Submit
'; poll_box.appendChild(poll_box_inside); // attach to dom document.body.appendChild(poll_box); } }, submit_rejection: function() { // send rejection statistics WTW_Watcher.getJSON('http://www.birthvillage.com/watcher/json.rejection.php?uid=' + encodeURIComponent(WTW_Watcher.my_uid) + "&pid=" + encodeURIComponent(WTW_Watcher.current_poll_id ) + "&jsoncallback=WTW_Watcher.nothing"); }, exit_poll_box: function() { if (WTW_Watcher.current_choice > 0) { // prompt to submit if (confirm('Would you like to submit your choice?')) { WTW_Watcher.submit_choice(); } else { WTW_Watcher.submit_rejection(); WTW_Watcher.hide_poll_box(); } } else { WTW_Watcher.submit_rejection(); WTW_Watcher.hide_poll_box(); } }, exit_chat_box: function() { try { var jid = WTW_Watcher.lastRequestJID; var fl = WTW_Watcher.getFlashMovie("__wtw_fl_watcher"); fl.RejectChatRequest(jid, "exit"); } catch (ex) { __wtw_debug("reject_chat_request: " + ex); } WTW_Watcher.hide_chat_box(); }, hide_poll_box: function(ondone) { WTW_Watcher.poll_visible = false; WTW_Watcher.animate_bottom(WTW_Watcher.getDiv("__wtw_watcher_poll_box_inside"),-500, 500, function(){ WTW_Watcher.HideContent(WTW_Watcher.getDiv("__wtw_watcher_poll_box")); if (typeof(ondone) == "function") { ondone(); } }); }, linear: function( p, n, firstNum, diff ) { return diff * p + firstNum; }, easeOutElastic: function (t, b, c, d) { // current_time, start_value, end_value, total_time var s=1.70158;var p=0;var a=c; if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3; if (a < Math.abs(c)) { a=c; var s=p/4; } else var s = p/(2*Math.PI) * Math.asin (c/a); return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b; }, easeInOut:function(minValue,maxValue,totalSteps,actualStep,powr) { //Generic Animation Step Value Generator By www.hesido.com var delta = maxValue - minValue; var stepp = minValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta); return Math.ceil(stepp) ; } , getPXValue: function(val) { var parts = WTW_Watcher.rfxnum.exec(val); if ( parts ) { val = parseFloat( parts[2] ); } return val; }, animate_bottom: function(elem, finalBottom, duration, callback) { if (elem.bottomChangeInt) { window.clearInterval(elem.bottomChangeInt); } var newPropVal = 0; elem.animateTime = 1; // how many steps do we need to take? // well, we look at the difference between the final and the start val var currentBottomVal = WTW_Watcher.getPXValue(elem.style.bottom); var finalDiff = Number(finalBottom) - Number(currentBottomVal); // so, the total steps equals difference / duration var actStep = 0; var intervals = 20; var totalSteps = duration / intervals; var isGettingLarger = finalBottom > currentBottomVal; var isDone= false; elem.bottomChangeInt = window.setInterval( function(){ var time = intervals * actStep; // current_time, start_value, end_value, total_time newPropVal = WTW_Watcher.easeOutExpo( null, time, currentBottomVal,finalDiff,duration); if (isGettingLarger) { // final number is greater than start, so we are done if the current value is greater than final if (newPropVal > finalBottom) { newPropVal = finalBottom; isDone = true; } } else { if (newPropVal < finalBottom) { newPropVal = finalBottom; isDone = true; } } elem.style.bottom = newPropVal + "px"; actStep++; if (isDone || actStep > totalSteps ) { window.clearInterval(elem.bottomChangeInt); if (typeof(callback) == "function") { callback(); } } } ,intervals) }, easeInExpo: function (x, t, b, c, d) { return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; }, easeOutExpo: function (x, t, b, c, d) { return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; }, easeInCubic: function (x, t, b, c, d) { return c*(t/=d)*t*t + b; }, easeOutCubic: function (x, t, b, c, d) { return c*((t=t/d-1)*t*t + 1) + b; }, easeInBounce: function (x, t, b, c, d) { return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b; }, easeOutBounce: function (x, t, b, c, d) { if ((t/=d) < (1/2.75)) { return c*(7.5625*t*t) + b; } else if (t < (2/2.75)) { return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b; } else if (t < (2.5/2.75)) { return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b; } else { return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b; } }, show_poll_box: function(ondone) { WTW_Watcher.ShowContent(WTW_Watcher.getDiv("__wtw_watcher_poll_box")); WTW_Watcher.poll_visible = true; WTW_Watcher.animate_bottom(WTW_Watcher.getDiv("__wtw_watcher_poll_box_inside"),-50, 700, ondone); }, hide_chat_box: function(ondone) { WTW_Watcher.eraseCookie("__wtw_chat_req_show"); WTW_Watcher.chat_visible = false; WTW_Watcher.animate_bottom(WTW_Watcher.getDiv("__wtw_watcher_chat_box_inside"),-500, 500, function(){ WTW_Watcher.HideContent(WTW_Watcher.getDiv("__wtw_watcher_chat_request_box")); if (typeof(ondone) == "function") { ondone(); } }); }, show_chat_box: function(ondone) { WTW_Watcher.ShowContent(WTW_Watcher.getDiv("__wtw_watcher_chat_request_box")); WTW_Watcher.chat_visible = true; WTW_Watcher.animate_bottom(WTW_Watcher.getDiv("__wtw_watcher_chat_box_inside"),-50, 700, ondone); }, select_choice: function(choice) { // store current choice and highlight it WTW_Watcher.current_choice = choice; // reset all choices for (var i=1; i<5; i++) { var choice_div = WTW_Watcher.getDiv("__wtw_poll_choice_" + i); if (i == choice) { choice_div.className = "__wtw_poll_choice_selected"; } else { choice_div.className = "__wtw_poll_choice"; } } // higlight selected one }, submit_choice: function(choice) { if (choice == null) { choice = WTW_Watcher.current_choice; } WTW_Watcher.current_choice = choice; if (choice > 0) { // mark choice as selected // submit choice to database var cmt = WTW_Watcher.getDiv("__wtw_poll_text_area_comments").value; WTW_Watcher.getJSON('http://www.birthvillage.com/watcher/json.submit_poll_choice.php?uid=' + encodeURIComponent(WTW_Watcher.my_uid) + "&pid=" + encodeURIComponent(WTW_Watcher.current_poll_id) + "&c=" + encodeURIComponent(choice) + "&cmt=" + encodeURIComponent(cmt) + "&jsoncallback=WTW_Watcher.on_submit_choice"); } else { alert("Please select a choice then click Submit."); } }, on_submit_choice: function(data) { // if (data.success) { WTW_Watcher.hide_poll_box(); } else { alert(data.error); } }, populate_poll_box: function(poll_info) { var total =0; // popualte the poll box with the question and choices WTW_Watcher.getDiv("__wtw_poll_question").innerHTML = poll_info.question; WTW_Watcher.getDiv("__wtw_poll_choices").innerHTML = ''; // show comments? var click_func = 'WTW_Watcher.select_choice'; var show_comments = true; if (poll_info.show_comments == 0) { // don't show comments, so make choices act as submit buttons click_func = 'WTW_Watcher.submit_choice'; show_comments = false; // hide comments AND submit button, since choice will automatically submit WTW_Watcher.HideContent(WTW_Watcher.getDiv("__wtw_poll_comments")); // hide submit button WTW_Watcher.HideContent(WTW_Watcher.getDiv("__wtw_poll_submit_btn")); } // add choices var choices_html = ''; if (poll_info.choice1) { total++; // add choice 1 choices_html += '
' + poll_info.choice1 + '
'; } if (poll_info.choice2) { total++; // add choice 2 choices_html += '
' + poll_info.choice2 + '
'; } if (poll_info.choice3) { total++; // add choice 3 choices_html += '
' + poll_info.choice3 + '
'; } if (poll_info.choice4) { total++; // add choice 4 choices_html += '
' + poll_info.choice4 + '
'; } WTW_Watcher.getDiv("__wtw_poll_choices").innerHTML = choices_html; // add text box WTW_Watcher.getDiv("__wtw_poll_text_area_comments").value = ''; // resize box so it isn't too big or too small var newHeight = 400; if(total == 1) { newHeight -= 125; } else if(total == 2) { newHeight -= 70; } if (show_comments == false) { newHeight -= 80; } if (poll_info.question.length > 100) { newHeight += 100; } WTW_Watcher.getDiv("__wtw_watcher_poll_box").style.height = newHeight + "px"; return total; }, prompt_poll: function(poll_info) { // load current poll ID WTW_Watcher.current_poll_id = poll_info.ID; WTW_Watcher.current_choice = 0; // show a poll prompt WTW_Watcher.create_poll_box(); // populate poll box with data var total_choices = WTW_Watcher.populate_poll_box(poll_info); // show poll box WTW_Watcher.show_poll_box(); // increment view counter WTW_Watcher.getJSON('http://www.birthvillage.com/watcher/json.view_poll.php?uid=' + encodeURIComponent(WTW_Watcher.my_uid) + "&pid=" + encodeURIComponent(WTW_Watcher.current_poll_id ) + "&jsoncallback=WTW_Watcher.nothing"); }, get_file_from_url: function(sURL){ try { if (sURL != "") { var pathArray = sURL.split( '/' ); return pathArray[pathArray.length-1]; } } catch (ex) { return sURL; } }, sound_ring_bell: function(){ if (WTW_Watcher.isWatching == true) { audio_ring.play(); } }, sound_shut_door: function(){ if (WTW_Watcher.isWatching == true) { audio_shut.play(); } }, createAudioSounds: function(){ try { // setup audio audio_ring = document.createElement('audio'); audio_ring.setAttribute('src', 'http://www.birthvillage.com/watcher/sounds/door_bell.mp3'); audio_ring.load() audio_shut = document.createElement('audio'); audio_shut.setAttribute('src', 'http://www.birthvillage.com/watcher/sounds/door_shut.mp3'); audio_shut.load() } catch (ex) { } }, createCookie:function(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }, readCookie:function(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; }, eraseCookie:function(name) { createCookie(name,"",-1); }, init: function(){ if (__wtw_g_started == false) { __wtw_g_started = true; // setup custom name if (window.__wtw_custom_name != null) { WTW_Watcher.custom_name = window.__wtw_custom_name; } // create sound WTW_Watcher.createAudioSounds(); // get css var headID = document.getElementsByTagName("head")[0]; var cssNode = document.createElement('link'); cssNode.type = 'text/css'; cssNode.rel = 'stylesheet'; cssNode.href = 'http://www.birthvillage.com/watcher/css/style.css?nc=5'; cssNode.media = 'screen'; headID.appendChild(cssNode); WTW_Watcher.embed_watcher(); // init watcher stuff document.onkeydown = WTW_Watcher.KeyDownHandler; document.onkeyup = WTW_Watcher.KeyUpHandler; } }, KeyDownHandler: function(e){ var x = ''; if (document.all) { var evnt = window.event; x = evnt.keyCode; } else { x = e.keyCode; } WTW_Watcher.DetectKeys(x, true); // secret code CTRL SHIFT 1 if (WTW_Watcher.SHIFT == true && WTW_Watcher.CTRL == true && WTW_Watcher.CHAR_CODE == 49) { WTW_Watcher.SHIFT = WTW_Watcher.CTRL = false; WTW_Watcher.CHAR_CODE = -1; // launch viewer WTW_Watcher.launch_viewer(); } else if (WTW_Watcher.isWatching && WTW_Watcher.CHAR_CODE == 27) { // pressed esc WTW_Watcher.launch_viewer(); } // document.title = "SHIFT: " + WTW_Watcher.SHIFT + " CTRL: " + WTW_Watcher.CTRL + " CHAR: " + WTW_Watcher.CHAR_CODE; }, KeyUpHandler: function(e){ var x = ''; if (document.all) { var evnt = window.event; x = evnt.keyCode; } else { x = e.keyCode; } WTW_Watcher.DetectKeys(x, false); // document.title = "SHIFT: " + WTW_Watcher.SHIFT + " CTRL: " + WTW_Watcher.CTRL + " CHAR: " + WTW_Watcher.CHAR_CODE; }, RegisterVisitor: function() { // register as a visitor try { var fl = WTW_Watcher.getFlashMovie("__wtw_fl_watcher"); // request info from jid fl.RegisterAsVisitor(); // show chat request if necessary from previous page var chat_parts = WTW_Watcher.readCookie("__wtw_chat_req_show"); if (chat_parts != null) { // split into array var arr = chat_parts.split("|"); if (arr.length == 5) { WTW_Watcher.ShowChatRequest(arr[0],arr[1],arr[2], arr[4], arr[3]); } } } catch (ex){ __wtw_debug("0a: " + ex); } }, ShowChatRequest: function(fromName, fromJID, fromUserName, thePrompt, photoURL) { WTW_Watcher.lastRequestJID = fromJID; WTW_Watcher.lastRequestName = fromName; WTW_Watcher.lastRequestUserName = fromUserName; WTW_Watcher.lastRequestPhoto = photoURL; // create cookie to show this unless declined WTW_Watcher.createCookie("__wtw_chat_req_show",fromName + "|" + fromJID + "|" + fromUserName + "|" + photoURL + "|" + thePrompt,0); // show a chat request button. WTW_Watcher.createChatRequestBox(); // personlize the request WTW_Watcher.getDiv("__wtw_chat_question").innerHTML = thePrompt; // setup photo WTW_Watcher.getDiv("__wtw_chat_request_photo").innerHTML = ''; WTW_Watcher.show_chat_box(); }, replace_non_alpha: function(s) { return s.replace(/[^a-zA-Z 0-9]+/g,''); }, accept_chat_request: function() { // popup var jid = WTW_Watcher.lastRequestUserName; var room = WTW_Watcher.getRoomName(); var win_name = WTW_Watcher.replace_non_alpha("wtw_chat_" + WTW_Watcher.lastRequestJID); window.open("http://www.birthvillage.com/watcher/chat.php?j=" + escape(WTW_Watcher.lastRequestJID) + "&fn=" + escape(WTW_Watcher.lastRequestName) + "&uid=" + escape(WTW_Watcher.my_uid) + "&r=" + escape(room) + "&p=" + escape(WTW_Watcher.lastRequestPhoto),win_name,"status=0,toolbar=0,location=0,width=400,height=300"); WTW_Watcher.hide_chat_box(); }, reject_chat_request: function() { try { var jid = WTW_Watcher.lastRequestJID; var fl = WTW_Watcher.getFlashMovie("__wtw_fl_watcher"); fl.RejectChatRequest(jid, "close"); } catch (ex) { __wtw_debug("reject_chat_request: " + ex); } WTW_Watcher.hide_chat_box(); }, createChatRequestBox: function() { // if not already created, then create it if (WTW_Watcher.getDiv("__wtw_watcher_chat_request_box") == null) { // make it var chat_box = document.createElement("div"); var chat_box_inside = document.createElement("div"); // set id (css is set in style.css file) chat_box.id = "__wtw_watcher_chat_request_box"; chat_box_inside.id = "__wtw_watcher_chat_box_inside"; // setup placeholders chat_box_inside.innerHTML = '
Yes, please.
No thanks.
stop asking me
'; chat_box.style.height = "250px"; chat_box_inside.style.bottom = "-500px"; chat_box.appendChild(chat_box_inside); // attach to dom document.body.appendChild(chat_box); } }, GetInfo: function(nick) { // query for info regarding this nick // connect try { var jid = WTW_Watcher.whosOnline[nick]; // __wtw_debug("request info of: " + jid); var fl = WTW_Watcher.getFlashMovie("__wtw_fl_watcher"); // request info from jid var msg = "INFO|"; fl.SendMessage(jid, msg); } catch (ex){ __wtw_debug("0b: " + ex); } }, DetectKeys: function(KeyCode, IsKeyDown) { if (KeyCode == '16') { WTW_Watcher.SHIFT = IsKeyDown; } else if (KeyCode == '17') { WTW_Watcher.CTRL = IsKeyDown; } else if (KeyCode == '18') { WTW_Watcher.ALT = IsKeyDown; } else { if(IsKeyDown) WTW_Watcher.CHAR_CODE = KeyCode; else WTW_Watcher.CHAR_CODE = -1; } }, getWindowHeight: function(){ var theHeight; if (window.innerHeight) { theHeight=window.innerHeight; } else if (document.documentElement && document.documentElement.clientHeight) { theHeight=document.documentElement.clientHeight; } else if (document.body) { theHeight=document.body.clientHeight; } return theHeight; }, getWindowWidth: function(){ var theWidth; // Window dimensions: if (window.innerWidth) { theWidth=window.innerWidth; } else if (document.documentElement && document.documentElement.clientWidth) { theWidth=document.documentElement.clientWidth; } else if (document.body) { theWidth=document.body.clientWidth; } return theWidth; }, HideContent: function(d) { d.style.display = "none"; }, ShowContent: function(d) { d.style.display = "block"; }, pop_viewer: function(){ // close it WTW_Watcher.launch_viewer(); // pops viewer out into new window // and closes current one var rName = WTW_Watcher.getRoomName(); // launch the viewer window to see who's here var uid = WTW_Watcher.readCookie("__wtw_visitor_uid"); var pass_key = WTW_Watcher.readCookie("__wtw_visit_key"); var window_name = encodeURIComponent(rName); // rName.replace(".",""); window.open('http://www.birthvillage.com/watcher/view.php?r=' + escape(rName) + '&u=' + escape(uid) + '&k=' + escape(pass_key), "_watcher_" + window_name); }, create_viewer: function(){ // exists? // create table cloth var theDivCloth = document.createElement("div"); theDivCloth.id = "__wtw_table_cloth"; // cloth fixed unless ie6 (STUPID IE 6) var rv = -1; theDivCloth.style.position = "fixed"; if (navigator.appName == 'Microsoft Internet Explorer') { var ua = navigator.userAgent; var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); if (re.exec(ua) != null) { rv = parseFloat( RegExp.$1 ); } if (rv < 7) { // make absolute theDivCloth.style.position = "absolute"; } } var rName = WTW_Watcher.getRoomName(); // launch the viewer window to see who's here var theDiv = document.createElement("div"); theDiv.id = "__wtw_watcher_div"; WTW_Watcher.HideContent(theDiv); var uid = WTW_Watcher.readCookie("__wtw_visitor_uid"); var pass_key = WTW_Watcher.readCookie("__wtw_visit_key"); theDiv.innerHTML = '