var Omniture = Class.create({
  
  downloadsFileTypes: "pdf,exe,zip,wav,mp3,mpg,avi,doc,docx,xls,xlsx,ppt,pptx,swf",
  trackingClass: "omniture-download",
  initialize: function() { 
    this.initEvents();
  },
  
  initEvents: function(){
    $('email-page').observe('click',this.onEmailClick); 
    $$('.print-page')[0].observe('click',this.onPrintPageClick);
    $$('.zoom a').each(function(item){ 
      item.observe('click',this.onChnageFontSize.bind(this));
    }.bind(this));
    this.initDownload();
  },
  
  initDownload: function(){
    
    $$('a').each(function(a){

      var url = a.href;
      var params = {};
      
      if(url.include('?')){
        params = url.split("?")[1].toQueryParams();
        url = url.split("?")[0];
      };
      url = url.split("/").last();
      
      if(typeof params.omnitureFileName != "undefined"){
        
        a.observe('click',this.onDownload.bindAsEventListener(this,params.omnitureFileName));
        
      }else{
        
        if(!url.endsWith('.html')){
          var types = this.downloadsFileTypes.split(",");
          for(var i=0;i<types.length;i++){
            if(url.endsWith(types[i])){
              a.observe('click',this.onDownload.bindAsEventListener(this,url));
              break;
            };
          };
        };
        
      };
      
    }.bind(this));
  },
  
  onPrintPageClick: function(){
   // console.info('print-click');
    s.linkTrackVars='events';
    s.linkTrackEvents='event10';
    s.events='event10';
    s.tl(this,'o',s.pageName+'|printpage');
    s.events='';  
    s.linkTrackVars='prop35,prop36,prop37,prop38,eVar35,eVar36,eVar37,eVar38';
    s.linkTrackEvents='None';   
  },
  
  onEmailClick: function(){
    //console.info('email');
    s.linkTrackVars='events';
    s.linkTrackEvents='event11';
    s.events='event11';
    s.tl(this,'o',s.pageName+'|emailpage');
    s.events='';
    s.linkTrackVars='prop35,prop36,prop37,prop38,eVar35,eVar36,eVar37,eVar38';
    s.linkTrackEvents='None';
  },
  
  onChnageFontSize: function(ev){
    var size;
    switch(site.textZoom.getIndex.bind(site.textZoom)()){
      case 0:
        size = 'small';
      break;
      
      case 1:
        size = 'medium-small';
      break;
      
      case 2:
        size = 'medium-large';
      break;
      
      case 3:
        size = 'large';
      break;
    };

    s.linkTrackVars='events,eVar21';
    s.linkTrackEvents='event13';
    s.eVar21=size;
    s.events='event13'
    s.tl(this,'o',s.pageName+'|changefontsize');
    s.events='';
    s.linkTrackVars='prop35,prop36,prop37,prop38,eVar35,eVar36,eVar37,eVar38';
    s.linkTrackEvents='None';
  },
  
  onDownload: function(ev,fileName){ 
    s.linkTrackVars='events,prop11,eVar11,prop15,eVar15';
    s.linkTrackEvents='event7';
    s.prop15=s.eVar15=s.channel+':'+fileName;
    s.events='event7';
    s.tl(this,'d',s.pageName+'|'+fileName);
    s.events='';
    s.linkTrackVars='prop35,prop36,prop37,prop38,eVar35,eVar36,eVar37,eVar38'; 
    s.linkTrackEvents='None';
  },
  
  // TODO Determine if this needs to be updated
  omniLinkClick: function(u){
    var cutLength = (u.match(contextPath))? contextPath.length + 1 : 1;
    var url = u.substr(cutLength).replace(/[\/.]/g,"_");
    
    var config = {

      "patient_crc_index_html" : "colorectal_cancer",
      "patient_crc_mcrc_index_html" : "understanding_mcrc",
      "patient_crc_avastin_index_html" : "about_avastin",
      "patient_crc_sideeffects_index_html" : "side_effects_of_avastin",
      "patient_crc_resources_index_html" : "helpful_resources",
      "patient_crc_safety_index_html" : "important_safety_information",
      
      "patient_lung_index_html": "lung_cancer",
      "patient_lung_nsclc_index_html" : "understanding_nsclc",
      "patient_lung_avastin_index_html" : "about_avastin",
      "patient_lung_sideeffects_index_html" : "side_effects_of_avastin",
      "patient_lung_resources_index_html" : "helpful_resources",
      "patient_lung_safety_index_html" : "important_safety_information",
      
      "patient_gbm_index_html" : "glioblastoma",
      "patient_gbm_gbm_index_html" : "understanding_gbm",
      "patient_gbm_avastin_index_html" : "about_avastin",
      "patient_gbm_sideeffects_index_html" : "side_effects_of_avastin",
      "patient_gbm_resources_index_html" : "helpful_resources",
      "patient_gbm_safety_index_html" : "important_safety_information",
      
      "patient_kidney_index_html" : "kidney_cancer",
      "patient_kidney_kidney_index_html" : "understanding_kidney",
      "patient_kidney_avastin_index_html" : "about_avastin",
      "patient_kidney_sideeffects_index_html" : "side_effects_of_avastin",
      "patient_kidney_resources_index_html" : "helpful_resources",
      "patient_kidney_safety_index_html" : "important_safety_information",
      
      "hcp_index_html" : "are_you_hcp",
      "hcp_crc_index_html" : "colorectal_cancer",
      "hcp_lung_index_html" : "lung_cancer",
      "hcp_gbm_index_html" : "glioblastoma_cancer",
      "hcp_rcc_index_html" : "kideny_cancer"
      
    };
    
    if(config.hasOwnProperty(url)) window.omniLinkClick(config[url],"");
  }
  

});





