<?xml version="1.0" encoding="UTF-8" ?> <Module>  
	<ModulePrefs title="My eBay - AuctionMonitor.net"
	title_url = "http://www.auctionmonitor.net/myEbay.php" 
	description = "A small iGoogle gadget to show you things saved to My eBay.  It is automatically updates to reflect your most recent watching, selling, and buying lists from My eBay."
	author="Christopher Wong"
	author_email="admin@auctionmonitor.net"
	author_affiliation = "AuctionMonitor.net"
	author_link = "http://www.auctionmonitor.net"
	author_quote = "K.I.S.S."
	screenshot = "http://www.auctionmonitor.net/images/igooglescreenshot_280.png"
	thumbnail = "http://www.auctionmonitor.net/images/igooglethumb.gif"
	scrolling="true"
	height="200"
	>
	<Require feature="setprefs" /> 
	<Require feature="tabs" />
	<Require feature="minimessage"/>
	<Require feature="dynamic-height"/> 
	<Require feature="settitle"/> 
	
	</ModulePrefs>      
	<UserPref name="token" datatype="hidden" default_value=""/>
	<UserPref name="login" datatype="hidden" default_value=""/>
	<UserPref name="selectedTab" datatype="hidden" default_value="0"/>
	<UserPref name="AutoResize" datatype="bool" default_value="true"/>
	<UserPref name="height" display_name="Height: (only used if autoResize is not checked)" datatype="string" default_value="200"/>
	
	<Content type="html">
	<![CDATA[
	<style type="text/css">
        .message{
            text-align: center;
            padding: 10px 0px;
        }
        .ebayItem a{
            font-size:11px;
            text-align:center;
        
        }
        .ebayItem div{
            font-size:11px;
            color:#6f6f6f;
        
        }
        .logout {
            float:right;
            cursor:pointer;
        }
        .loggedin {
            float:left;
        }
        .pleaseLogin{
            display:block;
            text-align:center;
            margin-top:20px;
            cursor:pointer;
            text-decoration:underline;
        
        }
        .loadingBox {
            background-image:url(http://www.auctionmonitor.net/images/loading.gif);
            background-repeat: no-repeat;
            display:block;
            height:16px;
            width:16px;
            margin:20px 50%;
        }
        .refresh {
            float:left;
            margin-left:100px;
            padding-left:13px;
            background-image:url(http://www.auctionmonitor.net/images/refresh.png);
            background-repeat: no-repeat;
            display:block;
            height:10px;
            width:10px;
            cursor:pointer;
        }
        .ebayItemL div{
            color: red;
        }
        .ebayItemW div{
            color: green;
        }
    </style>
	<script type="text/javascript">
	    var WATCHING = 2;
		var SELLING = 1;
		var BUYING = 0;
		
		var prefs = new _IG_Prefs();
		var msg = new _IG_MiniMessage(__MODULE_ID__);
		
		
		var safari = (document.childNodes) && (!document.all) && (!navigator.taintEnabled) && (!navigator.accentColorName);
		
		var loginObj;
		
    	//{{{ init function	
    		function init(){
    		    loginObj = new login();
    		    	
    			tabs = new _IG_Tabs(__MODULE_ID__); 
    			tabs[BUYING] = tabs.addTab("Buying",{callback:function(m){ return function(){loginObj.getData(m)}}(BUYING)});
    			tabs[SELLING] = tabs.addTab("Selling",{callback:function(m){ return function(){loginObj.getData(m)}}(SELLING)});
    			tabs[WATCHING] = tabs.addTab("Watching",{callback:function(m){ return function(){loginObj.getData(m)}}(WATCHING)});

    			if(loginObj.token && loginObj.token != ""&&loginObj.token != "null"){
    			    clearTabs();
    			    loadingIcon();
    			    loginObj.showLoginBar();    			    
    			    loginObj.getData(prefs.getInt("selectedTab"));
    			}else {
                    loginObj.pleaseLogin();
                    
                    //Even if auto resize is turned on go to 200px
        			if(prefs.getBool("AutoResize")){
            		    _IG_AdjustIFrameHeight(200);
            		}else {
            		    var h = prefs.getInt("height");
        		        if(h > 0){
        		            _IG_AdjustIFrameHeight(h);
        		        }
            		}
    			}
    		}
    		


    		function makeMessage(str, link){
    			var newDiv = document.createElement("div"); 
    			newDiv.innerHTML = str;
    			if(link){
    				newDiv.onclick = link;
    			}
    			return newDiv;
    		}
    		function clearTabs(){
    			while (_gel(tabs[BUYING]).firstChild){
    				_gel(tabs[BUYING]).removeChild(_gel(tabs[BUYING]).firstChild);
    			}
    			while (_gel(tabs[SELLING]).firstChild){
    				_gel(tabs[SELLING]).removeChild(_gel(tabs[SELLING]).firstChild);
    			}
    			while (_gel(tabs[WATCHING]).firstChild){
    				_gel(tabs[WATCHING]).removeChild(_gel(tabs[WATCHING]).firstChild);
    			}
    		}
    		function loadingIcon(m){
    		    if(m > -1){
    		        _gel(tabs[m]).appendChild(makeMessage("<span class=\"loadingBox\"></span>",loginObj.showLogin()));
    		    }else {
  
    		        _gel(tabs[BUYING]).appendChild(makeMessage("<span class=\"loadingBox\"></span>",loginObj.showLogin()));
		            _gel(tabs[SELLING]).appendChild(makeMessage("<span class=\"loadingBox\"></span>",loginObj.showLogin()));
		            _gel(tabs[WATCHING]).appendChild(makeMessage("<span class=\"loadingBox\"></span>",loginObj.showLogin()));
    		    }
    		}
    		function randomString(iLen) {
    			var sChrs = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    			var sRnd = '';
    			for (var i=0; i < iLen; i++){
    				var randomPoz = Math.floor(Math.random() * sChrs.length);
    				sRnd += sChrs.substring(randomPoz,randomPoz+1);
    			}
    			return sRnd;
    		}
    		function clearTab(mode){
    			while (_gel(tabs[mode]).firstChild){
    				_gel(tabs[mode]).removeChild(_gel(tabs[mode]).firstChild);
    			}
    		}
    	//}}}
        
    	//{{{ login object 
		function login (){
        	//Initialize all variables
        	//this.timer = t;
        	this.login = prefs.getString("login");
			this.token = prefs.getString("token");
        	this.randString = randomString(15);
        	this.window = null;
        	

        }

        login.prototype.showLogin = function(){
        	var rand = this.randString;
        	return function(){
        	clearTabs();
        	_gel(tabs[BUYING]).appendChild(makeMessage("<span class=\"pleaseLogin\">Please login in the popup window<br>Click here when you are finished.</span>",loginObj.getLogin));
        	_gel(tabs[SELLING]).appendChild(makeMessage("<span class=\"pleaseLogin\">Please login in the popup window<br>Click here when you are finished.</span>",loginObj.getLogin));
        	_gel(tabs[WATCHING]).appendChild(makeMessage("<span class=\"pleaseLogin\">Please login in the popup window<br>Click here when you are finished.</span>",loginObj.getLogin));
            var url = 'http://www.auctionmonitor.net/igooglelogin.php?r=';
         	this.window = window.open (url+rand,"ebayWindow");
         	} 
        }



        login.prototype.getLogin = function (){
            var url = "http://www.auctionmonitor.net/getToken.php?r="+ loginObj.randString;
                        
            _IG_FetchContent(url, function (o){
        	    var items = eval("(" + o + ")");
        		
        	    if (items.token == ""){
        			var answer = confirm("We could not detect that you have logged in.  Would you like to try again?");
                    if (answer){
                        var rand = loginObj.randString;
                     	this.window = window.open ('http://www.auctionmonitor.net/igooglelogin.php?r='+rand,"ebayWindow");
                    }else {
                        loginObj.pleaseLogin();
                    }
        		}else {
        			loginObj.token = items.token;
        			loginObj.login = items.ebayname;
        			if(this.token != ""){
        			    prefs.set("token",loginObj.token);
        			}
        			if(this.login != ""){
        			    prefs.set("login",loginObj.login);
        			}
        			clearTabs();
        			loadingIcon();
        			loginObj.showLoginBar();
        			loginObj.getData(BUYING);
        		}
            },{refreshInterval:10 });
        }
        
        login.prototype.getData = function (mode){
            if(loginObj.token && loginObj.token != "" && loginObj.token != "null"){
            var m;
            switch(mode){
                case SELLING:
                m = 's';
                break;
                case BUYING:
                m = 'b';
                break;
                case WATCHING:
                m = 'w';
                break;
            }
            var url = 'http://www.auctionmonitor.net/members/myEbayRss.php?' +  't=' + loginObj.token + '&s=j&mode='+m+'&c=1&u='+loginObj.login;
            _IG_FetchContent(url, function(mode){ return function (o){
        	    var items = eval("(" + o + ")");
        	    clearTab(mode);
        		_gel(tabs[mode]).appendChild(formatItems(items.items));
        		if(prefs.getBool("AutoResize") && !safari){    
        		    _IG_AdjustIFrameHeight();
        		}else {
        		    var h = prefs.getInt("height");
    		        if(h > 0){
    		            _IG_AdjustIFrameHeight(h);
    		        }
        		}
            };}(mode),{refreshInterval: 60} );
            }
        }
        
        login.prototype.logout = function (){
            
            prefs.set("login",null);
            prefs.set("token",null);
            loginObj.token = null;
            loginObj.login = null;
            loginObj.clearLogin();
            
            loginObj.pleaseLogin();
        }
        login.prototype.showLoginBar = function(){
		    var div = document.createElement("div");
            div.innerHTML = "<span class=\"loggedin\">Logged in as: "+ loginObj.login + "</span><span class=\"refresh\" onclick=\"loginObj.refresh();\">refresh</span><span class=\"logout\" onclick=\"loginObj.logout();\">Logout</span>";
            loginObj.loginMsg = msg.createStaticMessage(div);
            _IG_SetTitle("My eBay for " + loginObj.login );
            loginObj.setTimer();
		}
		login.prototype.clearLogin = function (){
		    msg.dismissMessage(loginObj.loginMsg);
		
		}
		login.prototype.pleaseLogin = function (){
		    clearTabs();
            
            _gel(tabs[BUYING]).appendChild(makeMessage("<span class=\"pleaseLogin\">Click here to login</span>",loginObj.showLogin()));
		    _gel(tabs[SELLING]).appendChild(makeMessage("<span class=\"pleaseLogin\">Click here to login</span>",loginObj.showLogin()));
		    _gel(tabs[WATCHING]).appendChild(makeMessage("<span class=\"pleaseLogin\">Click here to login</span>",loginObj.showLogin()));
		}
		login.prototype.refresh = function (){
		    var m = prefs.getInt("selectedTab");
		    clearTab(m);
		    loadingIcon(m);
		    loginObj.getData(m);
		}
		login.prototype.setTimer = function (){
		    //check every 30 mins for updates
		    if(loginObj.token && loginObj.token != "" && loginObj.token != "null"){
		        clearTimeout(loginObj.timer);
		        loginObj.timer = setTimeout(loginObj.setTimer,1000*60*30);
		        loginObj.refresh();
		    }
		}
        //}}} login object 
        
        function formatItems (items) {
             var mainDiv = document.createElement('div');
             var innerDiv;
             if(!items[0]){
        	    mainDiv.innerHTML = "- no items -";
        	    mainDiv.setAttribute("class","message");
        	    mainDiv.className = "message";
        	    return mainDiv;
             }
             for (var i in items){
        	    var item = items[i];
        	    
        	    var linkDiv = document.createElement('a');
        	    linkDiv.setAttribute("target","ebayItem");
         	    innerDiv = document.createElement('div');
        	    linkDiv.setAttribute('href',item.link + '&s=ig');

        	    if(item.end == "ended"){
        	        linkDiv.innerHTML = "<strike>" + item.title + "</strike>";
        	    } else {
        	        linkDiv.innerHTML =item.title;
        	    }
        	    innerDiv.appendChild(linkDiv);
        	    var descDiv = document.createElement('div');
        	    descDiv.innerHTML = item.description;
        	    innerDiv.appendChild(descDiv);
        	    innerDiv.setAttribute("class", "ebayItem");
        	    innerDiv.className = "ebayItem";
        	    innerDiv.className = innerDiv.className + " ebayItem" + item.status;
        	    mainDiv.appendChild(innerDiv);
             }
             return mainDiv;
        }
        
	    _IG_RegisterOnloadHandler(init);
	</script>
	
	]]>
      </Content>

</Module>
