/*
 * SEM DEMO javascript v0.1
 * http://widetag.com/
 *
 * Copyright (c) 2009 Roberto Ostinelli for WideTag, Inc.
 *
 */

var BrowserDetect={init:function(){this.browser=this.searchString(this.dataBrowser)||"An unknown browser";this.version=this.searchVersion(navigator.userAgent)||this.searchVersion(navigator.appVersion)||"an unknown version";this.OS=this.searchString(this.dataOS)||"an unknown OS"},searchString:function(data){for(var i=0;i<data.length;i++){var dataString=data[i].string;var dataProp=data[i].prop;this.versionSearchString=data[i].versionSearch||data[i].identity;if(dataString){if(dataString.indexOf(data[i].subString)!=-1){return data[i].identity}}else if(dataProp){return data[i].identity}}},searchVersion:function(dataString){var index=dataString.indexOf(this.versionSearchString);if(index==-1){return}return parseFloat(dataString.substring(index+this.versionSearchString.length+1))},dataBrowser:[{string:navigator.userAgent,subString:"Chrome",identity:"Chrome"},{string:navigator.userAgent,subString:"OmniWeb",versionSearch:"OmniWeb/",identity:"OmniWeb"},{string:navigator.vendor,subString:"Apple",identity:"Safari",versionSearch:"Version"},{prop:window.opera,identity:"Opera"},{string:navigator.vendor,subString:"iCab",identity:"iCab"},{string:navigator.vendor,subString:"KDE",identity:"Konqueror"},{string:navigator.userAgent,subString:"Firefox",identity:"Firefox"},{string:navigator.vendor,subString:"Camino",identity:"Camino"},{string:navigator.userAgent,subString:"Netscape",identity:"Netscape"},{string:navigator.userAgent,subString:"MSIE",identity:"Explorer",versionSearch:"MSIE"},{string:navigator.userAgent,subString:"Gecko",identity:"Mozilla",versionSearch:"rv"},{string:navigator.userAgent,subString:"Mozilla",identity:"Netscape",versionSearch:"Mozilla"}],dataOS:[{string:navigator.platform,subString:"Win",identity:"Windows"},{string:navigator.platform,subString:"Mac",identity:"Mac"},{string:navigator.userAgent,subString:"iPhone",identity:"iPhone/iPod"},{string:navigator.platform,subString:"Linux",identity:"Linux"}]};function peopleReset(){var imgs=$("#ppl_list div").children().filter("img");imgs.removeClass('ppl_select_img_off');imgs.addClass('ppl_select_img_avail');var divs=$("#ppl_list div").children().filter("div");divs.removeClass('ppl_online');divs.addClass('ppl_offline');divs.html("")}function peopleDisable(id){var pplDiv=$("#sel_ppl_"+id);var pplImg=pplDiv.children().filter("img");pplImg.removeClass('ppl_select_img_on');pplImg.removeClass('ppl_select_img_avail');pplImg.addClass('ppl_select_img_off');var pplDesc=pplDiv.children().filter("div");pplDesc.removeClass('ppl_offline');pplDesc.addClass('ppl_online');pplDesc.html("ONLINE")}function setYourAvatarColorPicture(id){$("#ppl_"+id).attr("src","images/ppl_"+id+".jpg")}function setSocialLabel(num){$("#social_slider_content_title").html(num+" connected user(s)")}function resetSocialSliderValue(){$("#social_slider_content img").map(function(){$(this).removeClass($(this).attr('class'));$(this).css('opacity',0.2)})}function setSocialSliderValue(id,value){$("#ppl_"+id).css('opacity',1);$("#ppl_"+id).addClass("p"+value)}function showTopColor(value){$("#lamp_top div").hide();$("#lamp_t"+value).show()}function showBottomColor(value){$("#lamp_bottom div").hide();$("#lamp_b"+value).show()}var semobj={refreshInterval:3,consumptionValues:{'computer':0.03,'lamp':0.18,'iron':0.50,'hairdrier':1,'vacuum':1,'washer':1.90,'fan':2.28},dataUri:"/api",spimeId:null,pplInterval:null,spimesInterval:null,consumptionTot:0,init:function(spimeId){clearInterval(this.pplInterval);this.spimeId=spimeId;setYourAvatarColorPicture(this.spimeId);this.startGetDemoSpimes()},startGetActiveSpimes:function(){this.getActiveSpimes();this.pplInterval=setInterval("semobj.getActiveSpimes()",this.refreshInterval*1000)},getActiveSpimes:function(){var self=this;function activeSpimesLoaded(xmlDoc){var spimes=xmlDoc.getElementsByTagName('spime');peopleReset();for(var el=0;el<spimes.length;el++){peopleDisable(spimes[el].getAttribute("id").substr(5))}}$.ajax({type:"GET",url:self.dataUri+"/datareporting/",dataType:"xml",success:activeSpimesLoaded})},startGetDemoSpimes:function(){this.getDemoSpimes();this.pplInterval=setInterval("semobj.getDemoSpimes()",this.refreshInterval*1000)},getDemoSpimes:function(){var self=this;function demoSpimesLoaded(xmlDoc){var averageNode=xmlDoc.getElementsByTagName('average')[0].firstChild;if(averageNode===null){showTopColor('x')}else{showTopColor(averageNode.nodeValue)}var spimes=xmlDoc.getElementsByTagName('spime');var cSpimeId=null;var cValue=null;setSocialLabel(spimes.length);resetSocialSliderValue();for(var el=0;el<spimes.length;el++){cSpimeId=spimes[el].getAttribute("id").substr(5);cValue=spimes[el].firstChild.nodeValue;setSocialSliderValue(cSpimeId,cValue);if(cSpimeId==self.spimeId){showBottomColor(cValue)}}}$.ajax({type:"POST",url:self.dataUri+"/spime"+self.spimeId+"/datareporting/",data:"value="+self.consumptionTot,dataType:"xml",success:demoSpimesLoaded})},addConsumption:function(appl){this.consumptionTot=(1000*parseFloat(this.consumptionTot)+1000*parseFloat(this.consumptionValues[appl]))/1000},removeConsumption:function(appl){this.consumptionTot=(1000*parseFloat(this.consumptionTot)-1000*parseFloat(this.consumptionValues[appl]))/1000}};$(document).ready(function(){BrowserDetect.init();if(BrowserDetect.browser=="Explorer"&&BrowserDetect.version=="6"&&$("#demo").length>0){window.location="unsupported.html"}semobj.startGetActiveSpimes();$("#ppl_list img").css('opacity',0.5);$("#ppl_list div").hover(function(){var current=$(this).children().filter("img");var classes=current.attr('class');if(typeof(classes)!=='undefined'){classes=current.attr('class').split(" ");var cont=true;for(i=0;i<classes.length;i++){if(classes[i]=='ppl_select_img_on'||classes[i]=='ppl_select_img_off'){cont=false;break}}if(cont===true){current.fadeTo("fast",0.8)}}},function(){var current=$(this).children().filter("img");var classes=current.attr('class');if(typeof(classes)!=='undefined'){classes=current.attr('class').split(" ");var cont=true;for(i=0;i<classes.length;i++){if(classes[i]=='ppl_select_img_on'||classes[i]=='ppl_select_img_off'){cont=false;break}}if(cont===true){current.fadeTo("fast",0.5)}}});$("#ppl_list div").click(function(){var selected=$(this).children().filter("img");if(selected.attr('class')!='ppl_select_img_off'){var all=$("#ppl_list img");all.css('opacity',0.5);all.removeClass("ppl_select_img_on");selected.fadeTo("fast",1);selected.addClass("ppl_select_img_on")}});$("#enter_sem").css('opacity',0.8);$("#enter_sem").hover(function(){$(this).fadeTo("fast",1)},function(){$(this).fadeTo("fast",0.8)});$("#enter_sem").click(function(){var selected=$("#ppl_list img.ppl_select_img_on").parent().attr("id");if(typeof(selected)==='undefined'){alert("Please select your avatar to proceed.")}else{semobj.init(spimeId=selected.substr(8));$("#enter").fadeOut("fast",function(){$("#demo").fadeIn("fast")})}});$("#appliances img").css('opacity',0.3);$("#appliances img").hover(function(){if($(this).css('opacity')!=1){$(this).css('opacity',0.6)}},function(){if($(this).css('opacity')!=1){$(this).css('opacity',0.3)}});$("#appliances img").tooltip();$("#social_slider").click(function(){if($("#social_slider").css('right')=='-200px'){$("#social_slider").animate({right:"0px"},500)}else{$("#social_slider").animate({right:"-200px"},500)}});$("#social_slider_content img").css('opacity',0.2);$("#appliances img").click(function(){if($(this).css('opacity')==1){$(this).css('opacity',0.3);semobj.removeConsumption($(this).attr("id").substr(5))}else{$(this).css('opacity',1);semobj.addConsumption($(this).attr("id").substr(5))}})});
