//Enable Logging
var loggingEnabled = 0;

function doWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
  return myWidth;
};


function doPositionWrapper() {
    //Get Window Width
    var windowWidth = doWindowSize();
	
    //If InitalLoad has already been set to 1
	if(windowWidth <= 925 && initialLoad == 1){
	  logCJS(loggingEnabled,"Browser Width is " + windowWidth + "px, less than or equal to the 925px needed for #wrapper, bypassing doPositionWrapper()");
	} else {
      //Set Window Wdth to 925px if initialLoad is 0
      if(windowWidth <= 925 && initialLoad == 0){
        windowWidth = 925;
        logCJS(loggingEnabled,"Browser Width is " + windowWidth + "px, less than or equal to the 925px needed for #wrapper, because initialLoad is 0 set windowWidth to 925px");
      };
      
	  //Position Wrapper
	  var posLeft_R = (windowWidth - 925) / 2;
	  var posLeft_F = posLeft_R.toFixed(0);
    
	  //Update Wrapper Position
	  $('#wrapper').css("left",posLeft_F+"px");
    
	  if( initialLoad == 0 ){
	    initialLoad = 1;
	    $('#wrapper').css("display","block");
	  };
	  logCJS(loggingEnabled,"#wrapper css left repositoned to: " + posLeft_F + "px");
	};
};

function doWrapperInherit() {
  var wl = $("#bodycontent:last");
  var overhead = 26+23+30; //BC Padding, H1, Margin to add
  logCJS(loggingEnabled,"#bodycontent Last DIV Height = " + wl.height() + "px");
  $("#wrapper").css("height",((wl.height()+overhead)+"px"));
  logCJS(loggingEnabled,"wrapper Height = " +(wl.height()+overhead)+"px");
};


function showVimeo(assetID,assetTitle,assetWidth,assetHeight,gaIsOn,browserIs,pLength,mIMG){
  var listPadding = 21;
  var h1height = ($('#wrapper #list #section h1').height() + 2);
  var h2height = ($('#wrapper #list #section h2').height() + 25 + 2);
  var h3height = 0;
  var h4height = ($('#wrapper #list #section h4').height() + 25 + 2);
  var vidheight = 0;
  
  var assetIDHash = "#v" + assetID;
  var assetClose = "#vClose" + assetID;
  
  $(assetIDHash).css("width",assetWidth+"px");
  $(assetIDHash).css("height",assetHeight+"px");

  if(mIMG == 1){
    $(".imgcontrols").fadeOut("slow", function(){});
  };
    
    $("#IMGAssets").fadeOut("slow", function(){
        $(assetIDHash).fadeIn("slow", function(){
	    $(assetClose).fadeIn("fast", function(){
		//Change Sidebar Size Start
		if (!pLength == 0){
		//When <P> Tag is not Empty
		    h3height = 0;
		    vidheight = (assetHeight - h1height - h2height - h3height - h4height);
		} else {
		//When <P> Tag is Empty
		    h3height = 0;
		    vidheight = (assetHeight - h1height - h2height - h3height - h4height) - 8;
		}
		logCJS(loggingEnabled,"Change Sidebar Height, Video Height (" + assetHeight + "px) - H1 (" + h1height + "px) - H2 (" + h2height + "px) - H3 (" + h3height + "px) - H4 (" + h4height + "px) = P Height (" + vidheight + "px)");
		if (browserIs == 'ie'){
		    $('#content').css("min-height",(vidheight-listPadding)+"px");
		} else {
		    $('#content').css("height",(vidheight-listPadding)+"px");
		}
		//Change Sidebar Size End
	    });
	});
    });
    
    if (gaIsOn == 1){
	gaTrack(assetID,assetTitle,browserIs,"Triggered");
    };
    
    $('#wrapper #bodycontent div.VIDAssets').html(function() {
	var iframeLink = '<iframe id="v' + assetID + '" src="http://player.vimeo.com/video/' + assetID + '?title=0&byline=0&portrait=0" width="' + assetWidth + '" height="' + assetHeight + '" frameborder="0"></iframe>';
	return iframeLink;
    });
};

function vimeoClose(assetID,mIMG){
    if(mIMG == 1){
      $(".imgcontrols").fadeIn("slow", function(){});
    };
    
    $('#wrapper #bodycontent div.VIDAssets').html(function() {
	var iframeLink = '';
	return iframeLink;
    });
	
    var assetIDHash = "#v" + assetID;
    var assetClose = "#vClose" + assetID;
    
    doHeightCalc()
    $(assetClose).fadeOut("fast", function(){
        $(assetIDHash).fadeOut("slow", function(){
            $("#IMGAssets").fadeIn("slow", function(){});
        });        
    }); 
};








function gaTrack(assetID,assetTitle,playerType,eventType){
  pushMessage = "Vimeo ID: " + assetID + " - " + assetTitle;
  _gaq.push(['_trackEvent', playerType, eventType, pushMessage]);
};




function playvid(assetID,assetTitle,assetWidth,assetHeight,gaIsOn,mIMG,browserIs,pLength){
  var listPadding = 21;
  var h1height = ($('#wrapper #list #section h1').height() + 2);
  var h2height = ($('#wrapper #list #section h2').height() + 25 + 2);
  var h3height = 0;
  var h4height = ($('#wrapper #list #section h4').height() + 25 + 2);
  var vidheight = 0;
		
  $("#VIMEOContainer").css("width",assetWidth+"px");
  $("#VIMEOContainer").css("height",assetHeight+"px");
  if(isVideoPlaying == 0){
    isVideoPlaying = 1;
    currentAssetID = assetID;
    currentAssetTitle = assetTitle;
    if(mIMG == 1){
      $(".leftalign").fadeOut("slow", function(){});
    };
    $("#IMGAssets").fadeOut("slow", function(){
      $("#VIMEOContainer").fadeIn("slow", function(){
        $("#VIMEOExtClose").fadeIn("fast", function(){
	    //Change Sidebar Size Start
		  if (!pLength == 0){
            //When <P> Tag is not Empty
			h3height = 0;
			vidheight = (assetHeight - h1height - h2height - h3height - h4height);
		  } else {
            //When <P> Tag is Empty
			h3height = 0;
			vidheight = (assetHeight - h1height - h2height - h3height - h4height) - 8;
		  }
		  logCJS(loggingEnabled,"Change Sidebar Height, Video Height (" + assetHeight + "px) - H1 (" + h1height + "px) - H2 (" + h2height + "px) - H3 (" + h3height + "px) - H4 (" + h4height + "px) = P Height (" + vidheight + "px)");
		  if (browserIs == 'ie'){
			$('#content').css("min-height",(vidheight-listPadding)+"px");
		  } else {
			$('#content').css("height",(vidheight-listPadding)+"px");
		  }
		  //Change Sidebar Size End
		  flowplay(assetTitle,assetPath,gaIsOn,mIMG);
        });
      });
    }); 
  } else {
    if (gaIsOn == 1){
	gaTrack(currentAssetID,currentAssetTitle,browserIs,'Stop');
    };
    $("#VIMEOContainer").css("width",assetWidth+"px");
    $("#VIMEOContainer").css("height",assetHeight+"px");
    isVideoPlaying = 1;
    currentAssetID = assetID;
    currentAssetTitle = assetTitle;
	//Change Sidebar Size Start
	if (!pLength == 0){
      //When <P> Tag is not Empty
	  h3height = 0;
	  vidheight = (assetHeight - h1height - h2height - h3height - h4height);
	} else {
      //When <P> Tag is Empty
	  h3height = 0;
	  vidheight = (assetHeight - h1height - h2height - h3height - h4height) - 8;
	}
	logCJS(loggingEnabled,"Change Sidebar Height, Video Height (" + assetHeight + "px) - H1 (" + h1height + "px) - H2 (" + h2height + "px) - H3 (" + h3height + "px) - H4 (" + h4height + "px) = P Height (" + vidheight + "px)");
	if (browserIs == 'ie'){
	  $('#content').css("min-height",(vidheight-listPadding)+"px");
	} else {
	  $('#content').css("height",(vidheight-listPadding)+"px");
	}
	//Change Sidebar Size End
    flowplay(assetTitle,assetPath,gaIsOn,mIMG); 
  };
};


function resetIMGAssets(mIMG){  
    if(mIMG == 1){
     $(".leftalign").fadeIn("slow", function(){});
    };
    doHeightCalc()
    $("#VIMEOExtClose").fadeOut("fast", function(){
        $("#VIMEOContainer").fadeOut("slow", function(){
            $("#IMGAssets").fadeIn("slow", function(){
                isVideoPlaying = 0;
            });
        });        
    });
};

function extClose(mIMG){
  $f("VIMEOExtClose").stop();
  resetIMGAssets(mIMG);
};

function logCJS(logEnabled,eventText) {
  if( logEnabled != 0 ){
	if (window.console && window.console.log)
		window.console.log('[common.js] ' + eventText);
  };
};
