var OTSWidgetObj;
function makeOTSWidget(template, restemplate, showStates,AffXe){
if(!AffXe){AffXe='Missing';}
  OTSWidgetObj=new OTSWidget('','',AffXe);
  OTSWidgetObj.s_tmpl=template;
  OTSWidgetObj.r_tmpl=restemplate;
  OTSWidgetObj.theseStates=showStates;
  document.getElementById('OTSWidget').innerHTML='Loading Ski Report';
  return OTSWidgetObj;
}


function OTSWidget(template,theseStates,AffXE){
  this.AffXE=AffXE;
  this.theseStates=theseStates;
  this.stopNow=false;
  this.s_tmpl='';
  this.r_tmpl='';
  this.statesLink='http://www.onthesnow.com/json/json_state2.js';
  this.resortsLink='http://www.onthesnow.com/json/resort/';
  this.snowLink='http://www.onthesnow.com/json/snow/';
  this.StateObj='';
  this.ResortObj='';
  this.SnowObj='';	
  this.defaultNull=new Array();
  
}
OTSWidget.prototype.makeStatesPage=function()
{
  var html='';
  for(var s=0;s<this.theseStates.length;s++){
  	html+=this.makeStatePage(this.theseStates[s]);
  }
  document.getElementById('OTSWidget').innerHTML=html;
}

OTSWidget.prototype.loadfailMsg=function(){
  document.getElementById('OTSWidget').innerHTML='<h3>Oops!</h3><p>There was a problem opening the page.  If you are using Internet Explorer click the link to refresh the page.  <a href="'+window.location.href+'">Click Here to refresh the page.</a></p>';
}
OTSWidget.prototype.makeStatePage=function(state)
{
  var html='';
  var sobj='';
  if(!this.StateObj[state] || typeof(this.StateObj[state].resorts)=='string'){
    this.loadfailMsg();this.stopNow=true; return;
  }
  var cnt=-1;
  var tmpl=this.s_tmpl;
  
  for (var i in this.s_tags){
  
    if(i.match(/^put.resorts$/)){
      var res=this.makeResortsPage(state);
      if(this.stopNow){return;}
      var rpl=this.s_tags[i];
      tmpl=tmpl.replace(eval('/'+rpl+'/g'),res) ;	
    }
    else{
      var rpl=this.s_tags[i];
      if(this.StateObj[state][i]){
      	tmpl=tmpl.replace(eval('/'+rpl+'/g'),this.StateObj[state][i]) ;
      }
    }
  }
  if(this.stopNow){
    this.loadfailMsg();
    this.stopNow=true; 
    return '';
  }
  html+=tmpl;
  return html;	
}

OTSWidget.prototype.findDefault=function(element){
  if(this.defaultNull && this.defaultNull[element]){
    return this.defaultNull[element];
  }
  else{
    return '';
  }
}
OTSWidget.prototype.makeCustomElement=function(resort, element){
  var tmpl=this[element+'_tmpl'];
  if(this[element+'_tags']){
    for (var j in this[element+'_tags']){
      var rpl=this[element+'_tags'][j];
      tmpl=tmpl.replace(eval('/'+rpl+'/g'), resort[j] != null ? resort[j] : this.findDefault(j) );
    }
  }
  return tmpl;
}
OTSWidget.prototype.makeResortsPage=function(state){
  var resorts=this.StateObj[state].resorts;
  var html='';
  if(typeof(resorts) == 'string'){
    this.loadfailMsg();
    this.stopNow=true; 
    return;
  }
  for(var i=0;i<resorts.order.length;i++){
    var tmpl=this.r_tmpl;
    var tmpl_xc=this.xc_tmpl;
    var tmpl_ski=this.ski_tmpl;
    var tmpl_sbd=this.sbd_tmpl;
    var tmpl_lift=this.lift_tmpl;
    
    if(tmpl){
      var rid=resorts.order[i];
      var resort=resorts.data[rid];
      if(rid && resort){
  resort.url_iw= resort.url_iw+'&XE_AFF='+escape(this.AffXE); 
  resort.url_lf_tkts= resort.url_lf_tkts+'&XE_AFF='+escape(this.AffXE); 
  resort.url_cams= resort.url_cams+'&XE_AFF='+escape(this.AffXE); 
  resort.url_tr= resort.url_tr+'&XE_AFF='+escape(this.AffXE); 
     for (var j in this.r_tags){
         var rpl=this.r_tags[j];
         if(j.match(/^put.resorts.xc$/)){        
          if(tmpl_xc && resort['rp_xc']=='yes'){
           var _HTML=this.makeCustomElement(resort, 'xc');
           tmpl=tmpl.replace(eval('/'+rpl+'/g'), _HTML );
          }
          else{
           tmpl=tmpl.replace(eval('/'+rpl+'/g'), '');
          }	
          }
          else if(j.match(/^put.resorts.ski$/)){
            if(tmpl_ski && resort['rp_ski']=='yes'){
              var _HTML=this.makeCustomElement(resort, 'ski');
              tmpl=tmpl.replace(eval('/'+rpl+'/g'), _HTML );
            }
            else{
              tmpl=tmpl.replace(eval('/'+rpl+'/g'), '');
            }	
          }
          else if(j.match(/^put.resorts.sbd$/)){
            if(tmpl_ski && resort['rp_snbd']=='yes'){
              var _HTML=this.makeCustomElement(resort, 'sbd');
              tmpl=tmpl.replace(eval('/'+rpl+'/g'), _HTML );
            }
            else{
              tmpl=tmpl.replace(eval('/'+rpl+'/g'), '');
            }	
          }
          else if(j.match(/^put.resorts.lift$/)){
            if(tmpl_lift && resort['rp_ski']=='yes'){
              var _HTML=this.makeCustomElement(resort, 'lift');
              tmpl=tmpl.replace(eval('/'+rpl+'/g'), _HTML );
            }
            else{
              tmpl=tmpl.replace(eval('/'+rpl+'/g'), '');
            }						
          }
          else{
            tmpl=tmpl.replace(eval('/'+rpl+'/g'), resort[j] != null ? resort[j] : '' );
          }
        }
      }
    }	
  html+= tmpl;
  }
  return html;
}

OTSWidget.prototype.getTag=function(string,attr){
  var matches=string.match(/<%(ots.+?)%>/g);
  if(matches){
    for(var i=0;i<matches.length;i++){
      var tag=matches[i].match(/(?:<%ots\.)(.+)(?:%>)/);
      var tag=matches[i];
      tag=tag.replace(/<%ots\.|%>/g, "");
      this[attr][tag]=matches[i];
    }		
  }
}

OTSWidget.prototype.prepPrinter=function(){
  this.s_tags=new Array();
  this.r_tags=new Array();
  this.xc_tags=new Array();
  this.ski_tags=new Array();
  this.lift_tags=new Array();
  this.sbd_tags=new Array();

  var reTags = new RegExp("<%(ots.+?)%>",'g');
  if(this.s_tmpl){
    this.getTag(this.s_tmpl, 's_tags');
  }
  if(this.r_tmpl){
    this.getTag(this.r_tmpl, 'r_tags');	
  }
  if(this.xc_tmpl){
    this.getTag(this.xc_tmpl, 'xc_tags');	
  }
  if(this.ski_tmpl){
    this.getTag(this.ski_tmpl, 'ski_tags');	
  }
  if(this.lift_tmpl){
    this.getTag(this.lift_tmpl, 'lift_tags');	
  }
  if(this.sbd_tmpl){
    this.getTag(this.sbd_tmpl, 'sbd_tags');	
  //	alert(this.sbd_tags.length);
  }

}
