/*
  Author: Carlos G. 
*/

/* JQuery On ready */
$(document).ready(function() { 
  var loc = document.location.toString().replace(/\?.*/,'');
  loc = loc.match(/[a-zA-Z\-]*(?=\.[a-z]{3}$)/);
  loc = (loc) ? loc.toString() : 'index' ;
  
  
  
  TM.FlashPlayer = FlashPlayer;
  TM.videoList = videoList;
  
  TM.loadPage(loc);
  
   
}) ;
// Namespace for TrustMedia (TM)
var TM = {
  ENV : 'production',
  initialVideos: ["spaLaJolla","joeWillardsenCosmetic","dentalWallin","commercialCorn"],
  
  loadPage: function(location){
    var videoPlayer = false;
    TM.activateMainNavLink(location);
    switch(location){
     
        case "index":
 videoPlayer = TM.showTV('brent');
        TM.videoList.loadCategory("collision");
        $("#dave").parent("div").addClass("activePreview");
        TM.bindVideoNavClicks();
        break;
      case "service":
	   videoPlayer = TM.showTV('brent');
        TM.videoList.loadCategory("collision");
        $("#tour").parent("div").addClass("activePreview");
        TM.bindVideoNavClicks();
        break;	  
      case "aboutUs":
	   videoPlayer = TM.showTV('brent');
        TM.videoList.loadCategory("collision");
        $("#process").parent("div").addClass("activePreview");
        TM.bindVideoNavClicks();
        break;
      case "gallery":
	   videoPlayer = TM.showTV('brent');
        TM.videoList.loadCategory("collision");
        $("#Matt").parent("div").addClass("activePreview");
        TM.bindVideoNavClicks();
        break;
      case "estimate":
	   videoPlayer = TM.showTV('brent');
        TM.videoList.loadCategory("collision");
        $("#brent").parent("div").addClass("activePreview");
        TM.bindVideoNavClicks();
        break;
      case "contact":
 videoPlayer = TM.showTV('brent');
        TM.videoList.loadCategory("collision");
        $("#warranty").parent("div").addClass("activePreview");
        TM.bindVideoNavClicks();
        break;	  
       
        default:
        TM.updateContentHeight(videoPlayer);
    }
  },
 
  
  updateContentHeight: function(videoPlayer, hardValue){
//    alert($("#mainContent").height());

    var addMoreHeight = videoPlayer ? 384 : 15;
    var colMaskHeight = $(".colMask").height();
    var colRight = $("#rightColumn").height();
    var min = 0;
    min = (hardValue != null) ? hardValue : (colMaskHeight < colMaskHeight) ? colMaskHeight : colRight ;
//    if (hardValue != null){
//      min = $(".colMask").height(hardValue);
//    }
//    else{
//      min = (colMaskHeight < colMaskHeight) ? colMaskHeight : colRight ;
//    } 
    $(".colMask").height(min + addMoreHeight);
  },
  loadWebPortafolio: function(){
    var addPreviewCSS = function(obj, img){
      var cssObj = {
        "background-image"      : "url(images/previews/small/"+ img +")",
        "background-position"   : "0px 2px",
        "background-repeat"     : "no-repeat"
      }
      obj.css(cssObj);
      return true;
    };
    
    
    $("#videosPreview").html("");
    var row = getNewElement("div", null, "previewsRow");
    var col = null;
    var div = null;
    var anchor = null;
    for (var i in links){
      col = getNewElement("div", null,"previewColumn");
      anchor = getNewElement("a", {target: "_blank", href: "http://" + links[i].title },"smallPreview", links[i].title);
      div = getNewElement("div", {id: i },"smallPreview");
      div[0].appendChild(anchor[0]);
      addPreviewCSS(div, links[i].smallPreview);
        div.click(function(){
          $(".activePreview").removeClass("activePreview");
          $(this).parent("div").addClass("activePreview");
        });
//      }
      col[0].appendChild(div[0]);
      row[0].appendChild(col[0]);
    }
    $("#videosPreview")[0].appendChild(row[0]);
  },
  bindVideoNavClicks: function(){
    $(".videoBar li").each(function() {
      var liElement = $(this);
      var category = this.title;
      var executeFunction = null;
      if (this.title == 'webproductions'){
        executeFunction = function() {TM.loadWebPortafolio();}
      }
      else{
        executeFunction = function() {TM.videoList.loadCategory(category);}
      }
      liElement.click(function(){
        $("#videosPreview").slideUp(500, function(){
          executeFunction();
          var newHeight = $("#videoNavBar").height() + $("#videosPreview").height() + 30;
//          alert(newHeight );
          TM.updateContentHeight(true, newHeight); 
          $("#videosPreview").slideDown(500);
        });
        $("li.active").removeClass("active");
        liElement.addClass("active");
      });
    });
  },
  activateMainNavLink: function(location){
    if (location == 'index'){
      location = "newversion";
      $(".topBar a:first").parent('li').addClass("active");
    }
    else{
      $(".topBar a[href*='"+ location + "']").parent('li').addClass("active");
    }
  },
  bindLinks: function(){
  },
  showTV: function(video){
    $("#videoContent").hide();
    $("#tvHolder").slideDown(1000, function(){
        TM.loadVideo(video);
        TM.updateContentHeight();
    });
    return true;
  },
  loadVideo: function(video){
    var videoObj = TM.videoList.getVideo(video);
    // Display the requested video
    var videoPreview = "&image=images/previews/" + videoObj.preview;
    if (videoObj.file){
     /**
      * Flashvar for video player: http://developer.longtailvideo.com/trac/wiki/FlashVars
      */
      
       TM.FlashPlayer.loadVideo("videoContent","file=./flv/" +
                                       videoObj.file + "&usefullscreen=false&autostart=true" + 
                                       videoPreview);
      
     
    }
  }
 };

