<?xml version="1.0" encoding="UTF-8" ?>
<Module>
 
  <ModulePrefs 
        title="doodleMe Board" 
        title_url="http://www.iwiddit.com"
        description="A drawing canvas for your page! Allow your friends to make Tags and publish them for all to see. Absolutely no plugins required. Brought to you by your friends at iWiddit.com."
        author="Dustin Jorge"
        author_email="dustinjorge.feedback+doodleMe@gmail.com"
        author_affiliation="iWiddit.com"
        author_location="Seattle, Washington. USA"
        thumbnail="http://www.iwiddit.com/xml_support/doodleMe/doodleMe-ss.png"
        scrolling="true"
        height='320' 
        width='335'
        
  >
        <Require feature="opensocial-0.5" /> 
        <Require feature="settitle" /> 

  </ModulePrefs>

  
  <Content type="html">
  
  <![CDATA[

<style type='text/css'>
#cv,#board{background:URL('http://www.iwiddit.com/xml_support/doodleMe/img/invis.gif');cursor:crosshair;left:10px;top:25px}#cv,#board,div#container,p#widdit,div#messages,p#title,div#alert .qConts #q{display:block}#cv,#board,p#widdit{z-index:100}#cv,#board,div#inkCont,div#inkLevel,p#inkLab,div#colors,div#strokeCont,div#menuShow,div.strokeChange,p#title,img#iwiddit,div#alert{position:absolute}#board{width:430px;height:200px}#board,div#inkCont,p#inkLab,p#widdit,div#messages,div#colors,div#alert .qConts #q{margin:0}#board,div#inkCont,p#inkLab,div#messages,div#colors,div#alert .qConts h1{padding:0}#cv{background:__UP_boardBG__ none;z-index:1;border:1px dashed #CCC}div#container{background:#FFF no-repeat;width:450px;height:270px;margin-left:-225px}div#container,div#alert .qConts{position:relative;left:50%}div#inkCont{width:380px;top:250px}div#inkCont,div#inkLevel,.swatch{height:10px}div#inkCont,div#strokeCont,.swatch{border:1px solid #CCC}div#inkCont,p#inkLab{left:56px}div#inkCont,div#inkLevel,div#strokeCont,div#menuShow,div.strokeChange,.swatch{line-height:1px;font-size:1px}div#inkLevel{background:#0F0;left:0;filter:alpha(opacity = 60);opacity:0.6}div#inkLevel,div#messages,div#alert{width:100%}p#widdit{width:400px;margin-left:-200px;position:absolute;left:50%;}div#inkLevel,p#title{top:0}p#inkLab,p#widdit,div#messages{font:bold 11px Arial}p#inkLab,p#widdit a,p#widdit a:hover,div#messages,p#title,div#alert .qConts h1,div#alert  .qConts a{color:#333}p#inkLab{top:235px}p#widdit{color:#283523;height:15px;padding:5px 0 0 0}p#widdit,div#messages,div#alert .qConts img,div#alert .qConts h1,.swatch{float:left}p#widdit,div#messages,div#alert .qConts,div#alert .qConts #q{text-align:center}p#widdit a{position:static;color:#000;padding:0 10px}p#widdit a,div#alert  .qConts a{text-decoration:none}div#messages,div#menuShow,div.strokeChange{background:transparent}div#messages,div#alert .qConts #q{clear:both}div#colors,div#strokeCont,div#alert .qConts{background:#FFF}div#colors{width:200px;border:0;left:253px;top:233px;line-height:0;font-size:0}div#colors,div#alert .qConts{height:auto}div#strokeCont{left:18px;top:240px;width:18px;height:18px}div#strokeCont,div#menuShow,div.strokeChange,.swatch{cursor:pointer}div#menuShow,div.strokeChange{width:7px}div#menuShow,div.strokeChange,p#title{height:12px}div#strokeUp{background:URL("http://www.iwiddit.com/xml_support/doodleMe/is.gif");left:41px}div#strokeUp,div#strokeDown{top:244px}div#strokeDown{background:URL("http://www.iwiddit.com/xml_support/doodleMe/ds.gif");left:8px}p#title{visibility:hidden;text-align:left;left:15px;padding-right:5px;font:10px arial;line-height:10px}p#title strong{color:#069}p#title a{color:#F60}img#iwiddit{background:#F00;left:330px;width:100px;height:19px}img#iwiddit,div#alert .qConts{top:30px}div#alert{background:URL("http://www.iwiddit.com/xml_support/doodleMe/img/blur.gif");height:100%;left:-1000px;top:-1000px;z-index:2000}div#alert .qConts{width:300px;border:1px solid #333;margin-left:-150px;-moz-border-radius:5px;padding:20px}div#alert .qConts img{height:40px;width:40px;margin:5px}div#alert .qConts #q{float:none;padding:10px;padding-bottom:20px}div#alert .qConts h1{font:bold 21px Arial;margin:10px}div#alert  .qConts a{font-weight:bold;margin:10px 5px 20px 5px}.swatch{width:10px;margin-right:5px}
</style>

<div id='container'>
    <p id='title'></p>
    <canvas id='cv' height=200 width=430></canvas>
    <div id='board'></div>
    <div id='strokeUp' class='strokeChange' title='Larger Chalk'></div>
    <div id='strokeDown' class='strokeChange' title='Smaller Chalk'></div>
    <div id='strokeCont' title='Current Chalk Width'></div><div id='colors'></div>
    <p id='inkLab'>Chalk Length: <span>100</span>%</p>
    <div id='inkCont'>
        <div id='inkLevel'></div>
    </div>
    <img id='iwiddit' alt='iWiddit.com' src='http://www.iwiddit.com/xml_support/doodleMe/iwiddit_logo.png' />
</div>
<p id='widdit'></p>
<div id='messages'></div>
<div id='alert'>
    <div class='qConts'>
        <img id='queryImg' src='' alt='query' />
        <h1 id='qText'></h1>
        <p id='q'>This is query text.</p>
        <a href='javascript:void(0)' onclick='' class='confBut' id='okBut'>Ok</a>
        <a href='javascript:void(0)' onclick='Alerts.hideQuery()' class='confBut'>Cancel</a>
    </div>
</div>

<script type='text/javascript'>

function _uc( str ){
        return str.substring(0,1).toUpperCase() + str.substring(1);
    }
/* 
   With the exception of ExplorerCanvas ( (c) Google, inc. ), this code is the property of Dustin Jorge and iWiddit.com
   iWiddit.com reserves all rights with respect to intellectual property and grants no permissions to re-use all
   or portions of this code without prior written permission.
   (c) 2007, Dustin Jorge / iWiddit.com -- All Rights Reserved.
*/

TagBoard = {
undoPt:null, Alerts:null, cMessage:null, mini:null, menu:null, offset:null, penSize:null, maxInk:null, curInk:null, curColor:null, mList:null, mDown:null, aColors:null, ar_pixels:null, mY:null, mX:null, lastX:null, lastY:null, drawSize:null, lineStep:null, pixOffset:null, msgMax:null, maxPenSize:null, minPenSize:null, canvas:null, container:null, ink:null, inkLabel:null, colorSpot:null, msg:null, strokeCont:null, draw:null, title:null, boardCleared:null, prefs:null, driver:null 
    ,create:function(driver){
            this.driver = driver;
            this.undoPt = 0;
            //this.prefs = new _IG_Prefs();
            this.penSize = 4;
            this.maxInk = 1500;
            this.curInk = 0;
            this.curColor = 'black';
            this.mY = 0;
            this.mX = 0;
            this.lastX = 0;
            this.lastY = 0;
            this.drawSize = 2;
            this.msgMax = 350;
            this.maxPenSize = 15;
            this.minPenSize = 1;
            this.canvas = _gel('board');
            this.title = _gel('title');
            this.container = _gel('container');
            this.ink = _gel('inkLevel');
            this.inkLabel = _gel('inkLab');
            this.colorSpot = _gel('colors');
            this.msg = _gel('messages');
            this.strokeCont = _gel('strokeCont');
            this.draw = _gel('cv').getContext('2d');
            this.offset = this.findPos(this.canvas);
            this.draw.lineCap = 'round';
            this.draw.lineJoin = 'miter';
            this.draw.miterLimit = 150.00;
            this.draw.strokeStyle = this.curColor;
            this.draw.shadowBlur = 0;
            this.Alerts = Alerts;
            this.Alerts.init();
            this.ds(this.canvas);
            this.ds(this.ink);
            this.ds(this.inkLabel);
            this.ds(this.msg);
            this.mDown = false;
            this.ar_pixels = [];
            // available colors
            this.aColors = ["black", "white", "gray", "red", "green", "blue", "orange", "yellow", "purple", "pink", "brown"];
            this.hColors = ["#000", "#FFF", "#999", "#C1222D", "#66FF66", "#3366FF", "#FF9C00", "#EAED01", "#663399", "#FF33FF", "#996600"];
            // menu items
            var _self = this;
            var mLabels = [ 
                    ["Clear",function(){_self.clearBoard();},"Clear the board and re-up on chalk!"],
                    ["Publish", function(){_self.publish();},"Save the board for everyone to see."],
                    ["Optimize Lines", function(){_self.optimizeLines();},"Optimize the lines in my tag."],
                    ["Undo",function(){_self.doUndo();},"Undo the last move."],                 
                    ["Reload",function(){_self.redrawImg();},"Reload the initial tag on this board."],                       
                    ["iWiddit.com", function(){_self.getPage("http://www.iWiddit.com");},"Visit the creator." ]            
                  ];
            var iWiddit = _gel('widdit');
            for(var i = 0; i < mLabels.length; i++){
                    var itm2 = document.createElement("a");    
                    var txt2 = document.createTextNode(mLabels[i][0]);
                    itm2.onclick = mLabels[i][1];
                    itm2.title = mLabels[i][2]; 
                    itm2.href = "javascript:void(0)"; 
                    itm2.appendChild(txt2);
                    iWiddit.appendChild(itm2);     
            }
            // mouseDown boolean setters
            document.body.onmousedown = function(){ _self.mDown = true; };
            document.body.onmouseup = function(){ _self.mDown = false; };
            // ie box model hacks
            if(document.all){
                _gel("inkCont").style.height="12px";
                this.strokeCont.style.left = "19px";
                this.strokeCont.style.top = "241px";
            }
            // stroke change buttons
            _gel('strokeUp').onclick = function(){ _self.incrementStroke(); };
            _gel('strokeDown').onclick = function(){ _self.decrementStroke(); };
            this.strokeCont.onclick = function(){ _self.incrementStroke(); };
            this.addPaintListener(function(){_self.paintCall();});
            this.drawColors();
            this.drawStroke();
    }
    ,clearTitle:function(){
            this.title.style.visibility = "hidden";
    }
    ,setMouseCoords:function(NSeV){
        this.mX = document.all ? event.clientX + document.body.scrollLeft - this.offset[0] : NSeV.clientX - this.offset[0];
        this.mY = document.all ? event.clientY + document.body.scrollTop - this.offset[1] : NSeV.clientY - this.offset[1];   
    }
    ,addPaintListener:function(callBack){
        var _self = this;
        this.canvas.onmousemove = function(e){
            _self.setMouseCoords(e);
            callBack();
        };
        this.canvas.onmousedown = function(){ _self.undoPt = _self.ar_pixels.length; _self.lastX = _self.mX; _self.lastY = _self.mY; _self.drawStandard(_self.mX,_self.mY,_self.mX+1,_self.mY);};
        this.canvas.onmouseout = function(){ _self.mDown = false; };
    }
    ,removePaintListener:function(){
        this.canvas.onmousemove = null;    
    }
    ,paintCall:function(){
        var gX = this.lastX > this.mX ? this.lastX : this.mX;
        var sX = this.lastX < this.mX ? this.lastX : this.mX;
        var gY = this.lastY > this.mY ? this.lastY : this.mY;
        var sY = this.lastY < this.mY ? this.lastY : this.mY;
        if(this.mDown && ( ( gX - sX > this.drawSize ) || ( gY - sY > this.drawSize ) )){
             this.drawStandard(this.lastX,this.lastY,this.mX,this.mY);
        }
    }    
    ,drawColors:function(){
        for( var i = 0; i < this.aColors.length; i++ ){
            var swatch = document.createElement("div");
            swatch.className = "swatch";
            swatch.style.backgroundColor = this.hColors[i];
            swatch.title = "Change to "+_uc(this.aColors[i])+".";
            swatch.col = this.aColors[i];
            var _self = this;
            swatch.onclick = function(){ 
                _self.changeColor(this.col);
            };
            // ie strikes again
            if(document.all){
                swatch.style.width = "12px";
                swatch.style.height = "12px";   
            }        
            this.colorSpot.appendChild(swatch);
        }    
    }
    ,changeColor:function(color){
        this.curColor = color;   
        this.drawStroke();
    }
    ,drawStroke:function(){
        var stroke = document.createElement( 'div' );
        stroke.style.backgroundColor = this.hColors[this.aColors.indexOf(this.curColor)];
        stroke.style.width = this.penSize + 1 + "px";
        stroke.style.height = this.penSize + 1 + "px";
        stroke.style.position = "absolute";
        if(!document.all){
            stroke.style.top = ((17-this.penSize)/2)>>0; //rnd
            stroke.style.left = ((17-this.penSize)/2)>>0;
        }else{
            stroke.style.top = ((15-this.penSize)/2)>>0;
            stroke.style.left = ((15-this.penSize)/2)>>0;
            stroke.style.MozBorderRadius = Math.floor(this.penSize/2);
        }
        while( this.strokeCont.firstChild ) this.strokeCont.removeChild( this.strokeCont.firstChild );
        this.strokeCont.appendChild( stroke );
        if(this.curColor == "white") this.strokeCont.style.backgroundColor = "#999";
        else this.strokeCont.style.backgroundColor = "#FFF";
    }
    ,incrementStroke:function(){
        if( this.penSize < this.maxPenSize-1 ) this.penSize+=2;
        this.drawStroke();
    }
    ,decrementStroke:function(){
        if( this.penSize > this.minPenSize+1 ) this.penSize-=2;
        this.drawStroke();   
    }
    ,drawStandard:function(x0,y0,x1,y1){
        if( ! this.updateInk() ) return;
        var aInd = this.aColors.indexOf( this.curColor );
        this.draw.strokeStyle = this.hColors[ aInd ];
        this.draw.lineWidth = this.penSize;
        this.draw.beginPath();
        this.draw.moveTo( x0, y0);
        this.draw.lineTo( x1, y1);
        this.draw.stroke();       
        this.ar_pixels[this.ar_pixels.length] = [ aInd , x0 , y0 , x1 , y1, this.penSize ];
        this.lastX = x1;
        this.lastY = y1;   
    }
    ,drawLineStandard:function(x0,y0,x1,y1,color,width){
        if( this.updateInk() ){
            this.draw.moveTo( x0, y0);
            this.draw.lineTo( x1, y1);      
            this.ar_pixels[this.ar_pixels.length] = [ color , x0 , y0 , x1 , y1, width ];   
        }
    }
    ,updateInk:function(){
        this.curInk++;
        if(this.curInk<=this.maxInk){
              var inkPerc = 100-((this.curInk/this.maxInk * 100)>>0);  // bitwise round should be a bit faster
              this.ink.style.width = inkPerc + "%";
              this.inkLabel.childNodes[1].childNodes[0].nodeValue = inkPerc;
              return true;
        }
        return false;
    }
    ,getDoodleData:function(){
        var dataList = "";
        this.ar_pixels = this.optimizePointsArray(this.ar_pixels);
        for(var i = 0; i < this.ar_pixels.length; i++){
               dataList += this.ar_pixels[i].join(",")+",";
        } 
        dataList = dataList.substring(0,dataList.length-1);
        return dataList;
    }
    ,drawFromString:function(str){
        if( typeof( str ) == "undefined" ) return;
        var datAr = str.split(",");
        this.drawFromSplitArray(datAr);
    }
    ,drawFromSplitArray:function(datAr){
        var tmpCol = this.curColor;
        var tmpSize = this.penSize;
        var i;
        for( i = 0; i <= datAr.length-6 ;i+=6 ){
            var curCol = datAr[i];
            var curStroke = datAr[i+5];
            if(document.all){    // firefox poor quality if not rendered for each line... IE (explorerCanvas) poor performance if rendered for each line
                this.draw.beginPath();
                this.draw.lineWidth = curStroke;
                this.draw.strokeStyle = this.hColors[datAr[i]];    
                while(datAr[i]==curCol && datAr[i+5]==curStroke){ 
                        this.drawLineStandard(datAr[i+1],datAr[i+2],datAr[i+3],datAr[i+4],datAr[i+0],datAr[i+5]);                
                        i+=6;
                }
                this.draw.stroke();
            }else{
                this.draw.beginPath();
                this.draw.lineWidth = curStroke;
                this.draw.strokeStyle = this.hColors[datAr[i+0]];
                this.drawLineStandard(datAr[i+1],datAr[i+2],datAr[i+3],datAr[i+4],datAr[i+0],datAr[i+5]);   
                this.draw.stroke();   
            }
        }
        i-=6;
        this.lastX = datAr[i+3];
        this.lastY = datAr[i+4];
        this.curColor = tmpCol;
        this.penSize = tmpSize;
    }
    ,drawFromArray:function(datAr){
        var tmpCol = this.curColor;
        var tmpSize = this.penSize;
        var dl = datAr.length;
        var i;
        for( i = 0; i < dl ;i++ ){
            if(document.all){    // firefox poor quality if not rendered for each line... IE (explorerCanvas) poor performance if rendered for each line
                this.draw.beginPath();
                this.draw.lineWidth = datAr[i][5];
                this.draw.strokeStyle = this.hColors[datAr[i][0]];    
                while(i < dl && this.hColors[datAr[i][0]]==this.draw.strokeStyle && datAr[i][5]==this.draw.lineWidth){ 
                        this.drawLineStandard(datAr[i][1],datAr[i][2],datAr[i][3],datAr[i][4],datAr[i][0],datAr[i][5]);                
                        i++;
                }
                this.draw.stroke();
            }else{
                this.draw.beginPath();
                this.draw.lineWidth = datAr[i][5];
                this.draw.strokeStyle = this.hColors[datAr[i][0]];
                this.drawLineStandard(datAr[i][1],datAr[i][2],datAr[i][3],datAr[i][4],datAr[i][0],datAr[i][5]);   
                this.draw.stroke();  
            }
        }
        this.curColor = tmpCol;
        this.penSize = tmpSize;
    }
    ,optimizePointsArray:function(ar){
        var ret = [];
        var sX, sY, eX, eY, cC, sS; // line drawing points
        var ceX, ceY; // keep track of last values line stretches
        var xOpt = false;
        var yOpt = false; // keep track of optimized line sets per axis
        function drawStub(){
            ret[ret.length] = [cC,sX,sY,eX,eY,sS];
            eX = null;
            eY = null;
            sX = null;
            sY = null;
            cC = null;
            sS = null;        
        }
        var i;
        for( i = 0; i < ar.length; i++ ){
            var cCol = ar[i][0];
            var cStr = ar[i][5];
            while( i < ar.length ){
                    if( ar[i][0]==cCol && ar[i][5]==cStr ){
                                var item = ar[i];
                                var dx = item[1] - item[3];
                                var dy = item[2] - item[4];
                                if( dy == 0 && ( ceX == item[1] ) ){
                                    // draw any previous line stretches
                                    if( yOpt){
                                        drawStub();
                                        yOpt = false;                        
                                     }                    
                                    sX = (!sX) ? item[1] : sX;
                                    sY = (!sY) ? item[2] : sY;
                                    eY = (!eY) ? item[4] : eY;
                                    cC = (!cC) ? item[0] : cC;
                                    sS = (!sS) ? item[5] : sS;
                                    eX = item[3];
                                    xOpt = true; 
                                }else if ( dx == 0 && ( ceY == item[2] )  ){
                                    // draw any previous line stretches
                                    if( xOpt){
                                        drawStub();
                                        xOpt = false;                        
                                     }                 
                                    sX = (!sX) ? item[1] : sX;
                                    sY = (!sY) ? item[2] : sY;
                                    eY = item[4];
                                    eX = (!eX) ? item[3] : eX; 
                                    cC = (!cC) ? item[0] : cC;
                                    sS = (!sS) ? item[5] : sS;                    
                                    yOpt = true;
                                }else{
                                    // draw any previous line stretches
                                    if( sX !== null && sX !== undefined){
                                        drawStub();
                                        xOpt = false;
                                        yOpt = false;                       
                                     }
                                    // now draw this line
                                    ret.push(item);
                                }
                                ceX = item[3];
                                ceY = item[4];    
                                i++;    
                    }else{
                        // draw any previous line stretches
                        if( sX !== null && sX !== undefined){
                            drawStub(); 
                            xOpt = false;
                            yOpt = false;                       
                        }
                        i--;
                        break;
                    }
            }
            // draw any previous line stretches
            if( sX !== null && sX !== undefined){
                drawStub(); 
                xOpt = false;
                yOpt = false;                       
            }
        }
        return ret;
    }
    ,clearBoard:function(){
        this.draw.clearRect(0,0,600,600);
        this.clearTitle();
        this.curInk = -1;
        this.updateInk();
        this.ar_pixels = [];
        this.boardCleared = true;
    }
    ,optimizeLines:function(){
        var tmp_pixels = this.optimizePointsArray(this.ar_pixels);
        var bC = this.boardCleared;
        this.clearBoard();
        this.undoPt = tmp_pixels.length;
        this.boardCleared = bC;
        this.drawFromArray(tmp_pixels);       
    }
    ,drawTitle:function(name,author,url){
        if( typeof( name ) == "undefined" || typeof( author ) == "undefined" ) return;
        this.title.innerHTML = "This Doodle: <strong>" + name + "</strong>" + " by " + "<a href='"+this.driver.getProfURL(url)+"'>"+ author +"</a>";
        this.title.style.visibility = "visible";
        this.boardCleared = false;
    }
    ,redrawImg:function(){
        this.clearBoard();
        this.drawFromString(this.driver.getDoodle());
        this.boardCleared = false;  
        this.driver.makeTitle(); 
    }
    ,ds:function(element) {
        element.onselectstart = function() {
            return false;
        };
        element.oncontextmenu = function() {
            return false;   
        };
        element.unselectable = "on";
        element.style.MozUserSelect = "none";
    }
    ,doUndo:function(){
        if( this.undoPt > this.ar_pixels.length ) this.undoPt = this.ar_pixels.length;
        var tmp_pixels = this.ar_pixels.slice(0,this.undoPt);   
        this.clearBoard();
        this.drawFromArray( tmp_pixels );
    }
    ,getPage:function(p){
        this.driver.getPage(p);
    }
    ,getPrefs:function(){
        return this.prefs;    
    }
    ,publish:function(){
        /*if(this.boardCleared)*/this.driver.publish(this.getDoodleData());   
       // else Alerts.showWarningAlert("You can't publish over someone else's tag. Please start from scratch (clear) to create a tag that can be published."); 
    }
    ,findPos:function(obj){ // courtesy of quirksmode.org
    	var curleft = 0;
    	var curtop = 0;
    	if (obj.offsetParent) {
    		curleft = obj.offsetLeft;
    		curtop = obj.offsetTop;
    		while (obj = obj.offsetParent) {
    			curleft += obj.offsetLeft;
    			curtop += obj.offsetTop;
    		}
    	}
    	return [curleft,curtop];
    }
}

Social = {
    reqObj:null,viewer:null,owner:null,viewerFriends:null,ownerFriends:null,cDoodle:null,cDoodleName:null,cAuthorName:null,cAuthorURL:null,
    init:function(BoardObj, callBack, altCallBack){
        this.boolChecks.init(BoardObj.getPrefs());
        this.reqObj = opensocial.newDataRequest();
        this.initPopulateData(callBack, altCallBack);
    }
    ,initPopulateData:function(callBack, altCallBack){
        this.reqObj.add(this.reqObj.newFetchPersonRequest("VIEWER"),"v");
        this.reqObj.add(this.reqObj.newFetchPersonRequest("OWNER"),"o");
        this.reqObj.add(this.reqObj.newFetchPeopleRequest("VIEWER_FRIENDS"),"vf");
        this.reqObj.add(this.reqObj.newFetchPeopleRequest("OWNER_FRIENDS"),"of");   
        this.reqObj.add(this.reqObj.newFetchInstanceAppDataRequest(["cDoodle","cDoodleName","cAuthorName","cAuthorURL"]),"cDoodle"); 
        var _self = this;
        this.reqObj.send( function(ret){ _self.initSetData(ret, callBack, altCallBack); });
    }
    ,initSetData:function(ret, callBack, altCallBack){
        if(ret.hadError() || ret.get("v").hadError() || ret.get("o").hadError()){
             this.viewer.hadError = true;
             this.owner.hadError = true;
             return;
        }
        this.viewer = ret.get("v").getData();
        this.viewer.hadError = false;
        this.owner = ret.get("o").getData();
        this.owner.hadError = false;
        this.viewerFriends = typeof( ret.get("vf") ) != "undefined" ? ret.get("vf").getData() : {};
        this.ownerFriends = typeof( ret.get("of") ) != "undefined" ? ret.get("of").getData() : {};
        var d = ret.get("cDoodle").getData();
        this.cDoodle = (!d) ? null:ret.get("cDoodle").getData()["cDoodle"];
        this.cDoodleName = (!d) ? null:ret.get("cDoodle").getData()["cDoodleName"];
        this.cAuthorName = (!d) ? null:ret.get("cDoodle").getData()["cAuthorName"];
        this.cAuthorURL = (!d) ? null:ret.get("cDoodle").getData()["cAuthorURL"];
        if(d) callBack();
        else altCallBack();
    }
    ,dataGets:{
        getName:function(person){
            return person.getField(opensocial.Person.Field.NAME);
        }
        ,getThumb:function(person){
            return person.getField(opensocial.Person.Field.THUMBNAIL_URL);
        }
        ,getProfURL:function(person){
            return person.getField(opensocial.Person.Field.PROFILE_URL);
        }    
    }
    ,boolChecks:{
        publishPref:null
        ,init:function(prefs){
            //this.publishPref = prefs.getInt("publish_settings");
        }
        ,isFriend:function(friends, person){
            friends.each( function(friend){
                if(friend.getId() == person.getId()) return true;
            });
            return false;
        }
        ,isMatch:function(owner,person){
            return (person.getId() == owner.getId());
        }
        ,canPublish:function(owner,ownerFriends,person){
            switch(this.publishPref){
                case 0:
                    return true;
                case 1:
                    // how do we deal with users who arent logged in?
                    return (this.isMatch(owner,person) || !person.hadError);
                case 2:
                    return (this.isMatch(owner,person) || this.isFriend(ownerFriends,person));
                case 3:
                    return owner.getId() == person.getId();
                default:
                    return false;    
            }
        }
    }
    ,getDoodle:function(){
        return this.cDoodle;    
    }
    ,getDoodleName:function(){
        return this.cDoodleName;    
    }
    ,getAuthorName:function(){
        return this.cAuthorName;    
    }
    ,getAuthorURL:function(){
        return this.cAuthorURL;    
    }
    ,getProfURL:function(urlDat){
        return urlDat.substring(0,7) == 'http://' ? urlDat : _args()["parent"]+urlDat;
    }
    ,publish:function(dDat, callBack){ 
         var canPub = (this.boolChecks.canPublish(this.owner,this.ownerFriends,this.viewer));
         if(!canPub) Alerts.showWarningAlert("You do not have permission to post to this page.");
         var _self = this;
         Alerts.showInputAlert("Please enter a name for your doodle:",function(){ 
                Alerts.hideQuery();
                _self.publishGetName(dDat, callBack);
         }); 
    }   
    ,publishGetName:function(dDat, callBack){
        var dName = _gel("userInpt").value;
        this.doPublish(dName,dDat,callBack);    
    }
    ,doPublish:function(dname,newDoodle,callBack){
        var vId = this.viewer.getId();  
        // update local data
        this.cDoodle = newDoodle;
        this.cDoodleName = dname;
        this.cAuthorName = !this.viewer.hadError ? this.viewer.getField(opensocial.Person.Field.NAME) : "";
        this.cAuthorURL = !this.viewer.hadError ?this.viewer.getField(opensocial.Person.Field.PROFILE_URL) : "";
        this.reqObj.add(this.reqObj.newUpdateInstanceAppDataRequest("cDoodle",newDoodle),"dat");
        this.reqObj.add(this.reqObj.newUpdateInstanceAppDataRequest("cDoodleName",dname),"dat");
        this.reqObj.add(this.reqObj.newUpdateInstanceAppDataRequest("cAuthorName",this.cAuthorName),"dat"); 
        this.reqObj.add(this.reqObj.newUpdateInstanceAppDataRequest("cAuthorURL",this.cAuthorURL),"dat");
        var _self = this;
        this.reqObj.send( function(ret){ _self.finishPublish(ret,callBack); } );            
    }
    ,finishPublish:function( ret, callBack ){
            if(!ret.hadError()){
                callBack();
                return;
            }
            Alerts.showWarningAlert("An error occured while sending data to the server, please try again.");
            return;
    }
}

Driver = {
    init:function(){
          Core.init();
          TagBoard.create(this);
          var _self = this; 
          Social.init(TagBoard,function(){
            TagBoard.drawFromString( Social.getDoodle() );
            _self.makeTitle();  
          }, function(){
            TagBoard.boardCleared = true;
          });  
    }
    ,getPage:function(nPage){
          window.open(nPage);   
    }
    ,getProfURL:function(url){
        return Social.getProfURL(url);    
    }
    ,makeTitle:function(){
        TagBoard.drawTitle( Social.getDoodleName() , Social.getAuthorName() , Social.getAuthorURL() );     
    }
    ,getDoodle:function(){
        return Social.getDoodle();    
    }
    ,publish:function(dDat){
        var _self = this;
        Social.publish(dDat, function(){
            _self.makeTitle();           
        });    
    }
}


// core utility functions **************************************************

Core = {
    init:function(){
        Array.prototype.indexOf = function( obj ){
            for( var i = 0; i < this.length; i++ ){
                if( this[i] == obj ){
                    return i;    
                }    
            }
            return -1;
        }
        
        Array.prototype.forEach = function(callBack){
            var ar = this;
            for( var i = 0; i < ar.length; i++){
                ar[i] = callBack(ar[i]);    
            }
            return ar;
        }
    }
}



// query objects

Alerts = {
  alrt:null,query:null,okBut:null,queryImg:null,qText:null,imgs:null
  ,init:function(){
        this.alrt = _gel('alert');
        this.query = _gel('q');
        this.okBut = _gel('okBut');
        this.queryImg = _gel('queryImg');    
        this.qText = _gel('qText');
        this.imgs = [
                    ["http://www.iwiddit.com/xml_support/doodleMe/img/exclaim.jpg",40,40],
                    ["http://www.iwiddit.com/xml_support/doodleMe/img/question.jpg",40,40]
               ];
        this.imgLoads = [];
        for( var i = 0; i < this.imgs.length; i++){
                this.imgLoads.push(new Image(this.imgs[i][1],this.imgs[i][2]));
                this.imgLoads[this.imgLoads.length-1].src = this.imgs[i][0];
        }
  }
  ,showQuery:function(){
    this.alrt.style.left = "0px";
    this.alrt.style.top = "0px";    
  }
  ,hideQuery:function(){
    this.alrt.style.left = "-1000px";
    this.alrt.style.top = "-1000px";
  }
  ,showConfirmAlert:function(str,callBack){
    this.qText.innerHTML = "Query";
    this.query.innerHTML = str;
    this.queryImg.src = this.imgs[1][0];
    this.okBut.onclick = callBack;
    this.showQuery();
  }
  ,showWarningAlert:function(str,callBack){
    this.qText.innerHTML = "Warning";
    this.query.innerHTML = str;
    this.queryImg.src = this.imgs[0][0];
    var _self = this;
    this.okBut.onclick = typeof(callBack) == "function" ? callBack : function(){ _self.hideQuery() };
    this.showQuery();
  }
  ,showInputAlert:function(str,callBack){
    this.qText.innerHTML = "Query";
    this.query.innerHTML = str;
    this.query.innerHTML += "<br /><input type='text' id='userInpt' value='' />";
    this.queryImg.src = this.imgs[1][0];
    var _self = this;
    this.okBut.onclick = typeof(callBack) == "function" ? callBack : function(){ _self.hideQuery() };
    this.showQuery();    
    _gel("userInpt").focus();
   }
}




window.onload = function(){
        Driver.init();
    }

</script>

<!--[if IE]>
<script type='text/javascript'>
// ExplorerCanvas -- (c) 2006 Google, Inc.
if(!window.CanvasRenderingContext2D){(function(){var I=Math,i=I.round,L=I.sin,M=I.cos,m=10,A=m/2,Q={init:function(a){var b=a||document;if(/MSIE/.test(navigator.userAgent)&&!window.opera){var c=this;b.attachEvent("onreadystatechange",function(){c.r(b)})}},r:function(a){if(a.readyState=="complete"){if(!a.namespaces["s"]){a.namespaces.add("g_vml_","urn:schemas-microsoft-com:vml")}var b=a.createStyleSheet();b.cssText="canvas{display:inline-block;overflow:hidden;text-align:left;width:300px;height:150px}g_vml_\\:*{behavior:url(#default#VML)}";
var c=a.getElementsByTagName("canvas");for(var d=0;d<c.length;d++){if(!c[d].getContext){this.initElement(c[d])}}}},q:function(a){var b=a.outerHTML,c=a.ownerDocument.createElement(b);if(b.slice(-2)!="/>"){var d="/"+a.tagName,e;while((e=a.nextSibling)&&e.tagName!=d){e.removeNode()}if(e){e.removeNode()}}a.parentNode.replaceChild(c,a);return c},initElement:function(a){a=this.q(a);a.getContext=function(){if(this.l){return this.l}return this.l=new K(this)};a.attachEvent("onpropertychange",V);a.attachEvent("onresize",
W);var b=a.attributes;if(b.width&&b.width.specified){a.style.width=b.width.nodeValue+"px"}else{a.width=a.clientWidth}if(b.height&&b.height.specified){a.style.height=b.height.nodeValue+"px"}else{a.height=a.clientHeight}return a}};function V(a){var b=a.srcElement;switch(a.propertyName){case "width":b.style.width=b.attributes.width.nodeValue+"px";b.getContext().clearRect();break;case "height":b.style.height=b.attributes.height.nodeValue+"px";b.getContext().clearRect();break}}function W(a){var b=a.srcElement;
if(b.firstChild){b.firstChild.style.width=b.clientWidth+"px";b.firstChild.style.height=b.clientHeight+"px"}}Q.init();var R=[];for(var E=0;E<16;E++){for(var F=0;F<16;F++){R[E*16+F]=E.toString(16)+F.toString(16)}}function J(){return[[1,0,0],[0,1,0],[0,0,1]]}function G(a,b){var c=J();for(var d=0;d<3;d++){for(var e=0;e<3;e++){var g=0;for(var h=0;h<3;h++){g+=a[d][h]*b[h][e]}c[d][e]=g}}return c}function N(a,b){b.fillStyle=a.fillStyle;b.lineCap=a.lineCap;b.lineJoin=a.lineJoin;b.lineWidth=a.lineWidth;b.miterLimit=
a.miterLimit;b.shadowBlur=a.shadowBlur;b.shadowColor=a.shadowColor;b.shadowOffsetX=a.shadowOffsetX;b.shadowOffsetY=a.shadowOffsetY;b.strokeStyle=a.strokeStyle;b.d=a.d;b.e=a.e}function O(a){var b,c=1;a=String(a);if(a.substring(0,3)=="rgb"){var d=a.indexOf("(",3),e=a.indexOf(")",d+1),g=a.substring(d+1,e).split(",");b="#";for(var h=0;h<3;h++){b+=R[Number(g[h])]}if(g.length==4&&a.substr(3,1)=="a"){c=g[3]}}else{b=a}return[b,c]}function S(a){switch(a){case "butt":return"flat";case "round":return"round";
case "square":default:return"square"}}function K(a){this.a=J();this.m=[];this.k=[];this.c=[];this.strokeStyle="#000";this.fillStyle="#000";this.lineWidth=1;this.lineJoin="miter";this.lineCap="butt";this.miterLimit=m*1;this.globalAlpha=1;this.canvas=a;var b=a.ownerDocument.createElement("div");b.style.width=a.clientWidth+"px";b.style.height=a.clientHeight+"px";b.style.overflow="hidden";b.style.position="absolute";a.appendChild(b);this.j=b;this.d=1;this.e=1}var j=K.prototype;j.clearRect=function(){this.j.innerHTML=
"";this.c=[]};j.beginPath=function(){this.c=[]};j.moveTo=function(a,b){this.c.push({type:"moveTo",x:a,y:b});this.f=a;this.g=b};j.lineTo=function(a,b){this.c.push({type:"lineTo",x:a,y:b});this.f=a;this.g=b};j.bezierCurveTo=function(a,b,c,d,e,g){this.c.push({type:"bezierCurveTo",cp1x:a,cp1y:b,cp2x:c,cp2y:d,x:e,y:g});this.f=e;this.g=g};j.quadraticCurveTo=function(a,b,c,d){var e=this.f+0.6666666666666666*(a-this.f),g=this.g+0.6666666666666666*(b-this.g),h=e+(c-this.f)/3,l=g+(d-this.g)/3;this.bezierCurveTo(e,
g,h,l,c,d)};j.arc=function(a,b,c,d,e,g){c*=m;var h=g?"at":"wa",l=a+M(d)*c-A,n=b+L(d)*c-A,o=a+M(e)*c-A,f=b+L(e)*c-A;if(l==o&&!g){l+=0.125}this.c.push({type:h,x:a,y:b,radius:c,xStart:l,yStart:n,xEnd:o,yEnd:f})};j.rect=function(a,b,c,d){this.moveTo(a,b);this.lineTo(a+c,b);this.lineTo(a+c,b+d);this.lineTo(a,b+d);this.closePath()};j.strokeRect=function(a,b,c,d){this.beginPath();this.moveTo(a,b);this.lineTo(a+c,b);this.lineTo(a+c,b+d);this.lineTo(a,b+d);this.closePath();this.stroke()};j.fillRect=function(a,
b,c,d){this.beginPath();this.moveTo(a,b);this.lineTo(a+c,b);this.lineTo(a+c,b+d);this.lineTo(a,b+d);this.closePath();this.fill()};j.createLinearGradient=function(a,b,c,d){var e=new H("gradient");return e};j.createRadialGradient=function(a,b,c,d,e,g){var h=new H("gradientradial");h.n=c;h.o=g;h.i.x=a;h.i.y=b;return h};j.drawImage=function(a,b){var c,d,e,g,h,l,n,o,f=a.runtimeStyle.width,k=a.runtimeStyle.height;a.runtimeStyle.width="auto";a.runtimeStyle.height="auto";var q=a.width,r=a.height;a.runtimeStyle.width=
f;a.runtimeStyle.height=k;if(arguments.length==3){c=arguments[1];d=arguments[2];h=(l=0);n=(e=q);o=(g=r)}else if(arguments.length==5){c=arguments[1];d=arguments[2];e=arguments[3];g=arguments[4];h=(l=0);n=q;o=r}else if(arguments.length==9){h=arguments[1];l=arguments[2];n=arguments[3];o=arguments[4];c=arguments[5];d=arguments[6];e=arguments[7];g=arguments[8]}else{throw"Invalid number of arguments";}var s=this.b(c,d),t=[],v=10,w=10;t.push(" <g_vml_:group",' coordsize="',m*v,",",m*w,'"',' coordorigin="0,0"',
' style="width:',v,";height:",w,";position:absolute;");if(this.a[0][0]!=1||this.a[0][1]){var x=[];x.push("M11='",this.a[0][0],"',","M12='",this.a[1][0],"',","M21='",this.a[0][1],"',","M22='",this.a[1][1],"',","Dx='",i(s.x/m),"',","Dy='",i(s.y/m),"'");var p=s,y=this.b(c+e,d),z=this.b(c,d+g),B=this.b(c+e,d+g);p.x=Math.max(p.x,y.x,z.x,B.x);p.y=Math.max(p.y,y.y,z.y,B.y);t.push("padding:0 ",i(p.x/m),"px ",i(p.y/m),"px 0;filter:progid:DXImageTransform.Microsoft.Matrix(",x.join(""),", sizingmethod='clip');")}else{t.push("top:",
i(s.y/m),"px;left:",i(s.x/m),"px;")}t.push(' ">','<g_vml_:image src="',a.src,'"',' style="width:',m*e,";"," height:",m*g,';"',' cropleft="',h/q,'"',' croptop="',l/r,'"',' cropright="',(q-h-n)/q,'"',' cropbottom="',(r-l-o)/r,'"'," />","</g_vml_:group>");this.j.insertAdjacentHTML("BeforeEnd",t.join(""))};j.stroke=function(a){var b=[],c=O(a?this.fillStyle:this.strokeStyle),d=c[0],e=c[1]*this.globalAlpha,g=10,h=10;b.push("<g_vml_:shape",' fillcolor="',d,'"',' filled="',Boolean(a),'"',' style="position:absolute;width:',
g,";height:",h,';"',' coordorigin="0 0" coordsize="',m*g," ",m*h,'"',' stroked="',!a,'"',' strokeweight="',this.lineWidth,'"',' strokecolor="',d,'"',' path="');var l={x:null,y:null},n={x:null,y:null};for(var o=0;o<this.c.length;o++){var f=this.c[o];if(f.type=="moveTo"){b.push(" m ");var k=this.b(f.x,f.y);b.push(i(k.x),",",i(k.y))}else if(f.type=="lineTo"){b.push(" l ");var k=this.b(f.x,f.y);b.push(i(k.x),",",i(k.y))}else if(f.type=="close"){b.push(" x ")}else if(f.type=="bezierCurveTo"){b.push(" c ");
var k=this.b(f.x,f.y),q=this.b(f.cp1x,f.cp1y),r=this.b(f.cp2x,f.cp2y);b.push(i(q.x),",",i(q.y),",",i(r.x),",",i(r.y),",",i(k.x),",",i(k.y))}else if(f.type=="at"||f.type=="wa"){b.push(" ",f.type," ");var k=this.b(f.x,f.y),s=this.b(f.xStart,f.yStart),t=this.b(f.xEnd,f.yEnd);b.push(i(k.x-this.d*f.radius),",",i(k.y-this.e*f.radius)," ",i(k.x+this.d*f.radius),",",i(k.y+this.e*f.radius)," ",i(s.x),",",i(s.y)," ",i(t.x),",",i(t.y))}if(k){if(l.x==null||k.x<l.x){l.x=k.x}if(n.x==null||k.x>n.x){n.x=k.x}if(l.y==
null||k.y<l.y){l.y=k.y}if(n.y==null||k.y>n.y){n.y=k.y}}}b.push(' ">');if(typeof this.fillStyle=="object"){var v={x:"50%",y:"50%"},w=n.x-l.x,x=n.y-l.y,p=w>x?w:x;v.x=i(this.fillStyle.i.x/w*100+50)+"%";v.y=i(this.fillStyle.i.y/x*100+50)+"%";var y=[];if(this.fillStyle.p=="gradientradial"){var z=this.fillStyle.n/p*100,B=this.fillStyle.o/p*100-z}else{var z=0,B=100}var C={offset:null,color:null},D={offset:null,color:null};this.fillStyle.h.sort(function(T,U){return T.offset-U.offset});for(var o=0;o<this.fillStyle.h.length;o++){var u=
this.fillStyle.h[o];y.push(u.offset*B+z,"% ",u.color,",");if(u.offset>C.offset||C.offset==null){C.offset=u.offset;C.color=u.color}if(u.offset<D.offset||D.offset==null){D.offset=u.offset;D.color=u.color}}y.pop();b.push("<g_vml_:fill",' color="',D.color,'"',' color2="',C.color,'"',' type="',this.fillStyle.p,'"',' focusposition="',v.x,", ",v.y,'"',' colors="',y.join(""),'"',' opacity="',e,'" />')}else if(a){b.push('<g_vml_:fill color="',d,'" opacity="',e,'" />')}else{b.push("<g_vml_:stroke",' opacity="',
e,'"',' joinstyle="',this.lineJoin,'"',' miterlimit="',this.miterLimit,'"',' endcap="',S(this.lineCap),'"',' weight="',this.lineWidth,'px"',' color="',d,'" />')}b.push("</g_vml_:shape>");this.j.insertAdjacentHTML("beforeEnd",b.join(""));this.c=[]};j.fill=function(){this.stroke(true)};j.closePath=function(){this.c.push({type:"close"})};j.b=function(a,b){return{x:m*(a*this.a[0][0]+b*this.a[1][0]+this.a[2][0])-A,y:m*(a*this.a[0][1]+b*this.a[1][1]+this.a[2][1])-A}};j.save=function(){var a={};N(this,a);
this.k.push(a);this.m.push(this.a);this.a=G(J(),this.a)};j.restore=function(){N(this.k.pop(),this);this.a=this.m.pop()};j.translate=function(a,b){var c=[[1,0,0],[0,1,0],[a,b,1]];this.a=G(c,this.a)};j.rotate=function(a){var b=M(a),c=L(a),d=[[b,c,0],[-c,b,0],[0,0,1]];this.a=G(d,this.a)};j.scale=function(a,b){this.d*=a;this.e*=b;var c=[[a,0,0],[0,b,0],[0,0,1]];this.a=G(c,this.a)};j.clip=function(){};j.arcTo=function(){};j.createPattern=function(){return new P};function H(a){this.p=a;this.n=0;this.o=
0;this.h=[];this.i={x:0,y:0}}H.prototype.addColorStop=function(a,b){b=O(b);this.h.push({offset:1-a,color:b})};function P(){}G_vmlCanvasManager=Q;CanvasRenderingContext2D=K;CanvasGradient=H;CanvasPattern=P})()};
</script>
<![endif]-->
  ]]>
  
  </Content>

</Module>
