// A Content Switching Object Literal
var Portfolio = {
  allThumbs:'',
  fileThumb:'',
  fileLarge:'',
  fileVideo:'',
  vidPlayer: new SWFObject('http://www.knoodleshop.com/wp-content/themes/kno/flash/player.swf','player','400','300','9'),
  // thumbLinks:'',
  firstLink: '',
  videoStill: '',
  caseStudyPRPDF: '',
  firstLinkInteractive: '',
  interactiveURL: '',

  init:function(){
    Portfolio.allThumbs = $$('a.thumbLinks');
    Portfolio.activeThumb();
    Portfolio.firstLink = Portfolio.allThumbs[0].href;
    Portfolio.firstLinkInteractive = Portfolio.allThumbs[0].rel;
    if($('tv')){    
      // If the page is TV, then load the first video
      //Portfolio.makeWay();
      Portfolio.setFileVideo(Portfolio.firstLink);
      Portfolio.setVideoStill(Portfolio.firstLink);
      Portfolio.loadTVPlayer();
    };
    if($('interactive')){
      Portfolio.setInteractiveLink(Portfolio.firstLinkInteractive);
    };
    if($('case-study') || $('public-relations')){
      Portfolio.setPDFlink(Portfolio.firstLink);
    }
  },
  
  setInteractiveLink:function(url){
    Portfolio.interactiveURL = url;
    $('placeholder').innerHTML = '<a href="'+Portfolio.interactiveURL+'" target="_blank">'+$('placeholder').innerHTML+'</a>';
  },
  
  setPDFlink:function(url){
    var temp = url;
    temp = temp.replace('jpg','pdf');
    temp = temp.replace('large','pdf');
    Portfolio.caseStudyPRPDF = temp;
    if($('case-study')){
      $('placeholder').innerHTML = '<a href="'+Portfolio.caseStudyPRPDF+'">'+$('placeholder').innerHTML+'</a><p><a href="'+Portfolio.caseStudyPRPDF+'">Click to download the full case study (PDF)</a></p>';
    };
    if($('public-relations')){
      $('placeholder').innerHTML = '<a href="'+Portfolio.caseStudyPRPDF+'">'+$('placeholder').innerHTML+'</a><p><a href="'+Portfolio.caseStudyPRPDF+'">Click to download the full story (PDF)</a></p>';
    }
  },
  
  makeWay:function(){    // Clear the content & show the loader
    $('placeholder').innerHTML = '';
    $('loader').show();
  },
  
  getImg:function(thumb){
    Portfolio.makeWay();    // create a new image node
    var loadImg = new Image();
    loadImg.src = thumb.href;
    // loadImg.style.maxWidth = '400px';
    // after the image loads, clear the placeholder, hide loader, and append new image node
    loadImg.onload = (function(){
      $('loader').hide();
      $('placeholder').appendChild(loadImg);
      if($('case-study') || $('public-relations')){
        Portfolio.setPDFlink(thumb.rel);
      }
      if($('interactive')){
        Portfolio.setInteractiveLink(thumb.rel);
      }
    });
    return false;
  },
  
  setFileVideo:function(thumb){
    Portfolio.fileVideo = thumb;
    return false;
  },
  
  setVideoStill:function(url){
    var temp = url;
    temp = temp.replace('videos','large');
    temp = temp.replace('mp4','jpg');
    Portfolio.videoStill = temp;
    return false;
  },
  
  getVid:function(thumb){
    // Portfolio.makeWay();
    Portfolio.setFileVideo(thumb);
    Portfolio.setVideoStill(thumb.href);
    Portfolio.loadTVPlayer();
    return false;
  },

  loadTVPlayer:function(url){
    Portfolio.vidPlayer.addParam('allowfullscreen','true');
    Portfolio.vidPlayer.addParam('allowscriptaccess','always'); 
    Portfolio.vidPlayer.addParam('flashvars','&file='+Portfolio.fileVideo+'&image='+Portfolio.videoStill+'&backcolor=#0C0704&frontcolor=#F7933F&lightcolor=#F7933F&screencolor=#0C0704&controlbar=over'); // &duration=34 &image= is the still
    $('loader').hide();
    Portfolio.vidPlayer.write('placeholder');
  },
    
  activeThumb:function(){
    for(i=0;i<Portfolio.allThumbs.length;i++){
      Portfolio.allThumbs[0].addClassName('active');
      Event.observe(Portfolio.allThumbs[i], 'click', function(event) {
        //remove the "active" class from the old anchor 
        var temp = $('thumbs-n-large').select('a.active').invoke('removeClassName', 'active');
        this.addClassName('active');
        return false;
      });
    }
  }
}
