// Add extra div to blockquotes for styling purposes
// http://simonwillison.net/2003/May/21/stylingBlockquotes/

/* Probably do not need now.
window.onload = function(){ // (this is a secondary add for slow-loaders!)
	var iframeHt = $("#iframe").contents().find("#iframeWrapper").height();
	var actualIframe = $("#iframe").height();
	var iframeDiff = iframeHt - actualIframe;
	if (iframeDiff >= 1) {
		$("#iframe").animate({
	    height: iframeHt+16
	  }, 1500 );
	}	
}
*/

function muiFrame() {		
	var iframeHt = $('#iframe').contents().find('#iframeWrapper').height();
	$('.iframeSpinner').slideUp('slow');
	$('#iframe').animate({
    height: iframeHt+16
  }, 1500 );
  
  /* Placing this contents find digs too deep. the div#footer_bottom is right above. 
  $('#iframe').contents().find('#iframeWrapper').submit(function() {
	  $("div#footer_bottom").css('background-color','#000');
	  return true;
	});
	*/
  
}

function iframeLoad() { 
	$("#iframe").load(function(){ // this seems to work as long as the snippet is well built and timely on loading
		var pageBg = $('#iframe').parent().parent().parent().css('background-color'); // Grabs current page bg to match. // Could set loop to keep digging if background-color length or something not found.
		$('#iframe').contents().find('body').css({'display':'inline','background-color':pageBg}); // Display inline to thwart borders in IE & update background-color to the main area
		setTimeout("muiFrame();",1200);
	});
}

$(document).ready(function(){
	
	
	// Read a page's GET URL variables and return them as an associative array.
	function getUrlVars() {
	  var vars = [], hash;
	  var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	
	  for(var i = 0; i < hashes.length; i++)
	  {
	      hash = hashes[i].split('=');
	      vars.push(hash[0]);
	      vars[hash[0]] = hash[1];
	  }
	
	  return vars;
	}
	
	var hash = getUrlVars(); 
	
	if( hash == 'test' ) {
	  
	  // i am doing the thing - respect the thing.
	  $("body").addClass('test');
		setTimeout("$('#iframe').contents().find('body').attr('class','test');",1600);
	}

	
	
	// Would like to add an each on this to work some magic on multiple iframes on a page.
	/*
	$(document.body).click(function () {
    $("div").each(function (i) {
      if (this.style.color != "blue") {
        this.style.color = "blue";
      } else {
        this.style.color = "";
      }
    });
  });
  */

	// Adding internal/external content in a pseudo iframe.
  // looks for [style=iframe][link=xxx]link[/link][/style] ...
  var iframeUrl = $("span.iframe a").attr('href');
	// replaces, etc.
	$("span.iframe").wrap('<iframe id="iframe" name="iframe" frameborder="0" marginheight="0" marginwidth="0" scrolling="no" />').addClass('kill');
	$(".kill").remove();
	$("#iframe").attr({'src':iframeUrl,'scrolling':'no','height':'0','width':'628'}).css('overflow','hidden');
	$("#iframe").before('<p class="iframeSpinner" style="text-align: center; padding: 20px;"><img style="background-color: #fff; width: 66px; height: 66px; border: 3px solid #ccc; border-radius: 10px; moz-border-radius: 10px; webkit-border-radius: 10px; padding: 15px; opacity:0.5; filter:alpha(opacity=50)" src="/media/image/1/loading.gif" /></p>');
  iframeLoad();

  $("div#contentArea blockquote").wrapInner("<div></div>");

  // video popup calls
  $('.video a').click(function(){
    var vidUrl = $(this).attr('href');
    // if cloud front url then modify
    var vidSplit = vidUrl.split("cfx");
    if(vidSplit.length > 1){
      vidUrl = vidUrl.replace("http","rtmp");
      vidUrl = vidUrl + "&cf=true";
    }
    tb_show('','/page/1/video-player.jsp?flv='+vidUrl+'&height=300&width=490');
    return false;
  });

  // secure video popup calls
  $('.securevideo a').click(function(){
  	if ( vlt_Get_Cookie( 'vlt_videoOptIn' ) ) {
	    // show video
	    var vidUrl = $(this).attr('href');
	    // if cloud front url then modify
	    var vidSplit = vidUrl.split("cfx");
	    if(vidSplit.length > 1){
	      vidUrl = vidUrl.replace("http","rtmp");
	      vidUrl = vidUrl + "&cf=true";
	    }
    	tb_show('','/page/1/video-player.jsp?flv='+vidUrl+'&height=300&width=490');
    	return false;
  	} else {
		  // show form
		  var vidUrl = $(this).attr('href');
      // if cloud front url then modify
	    var vidSplit = vidUrl.split("cfx");
	    if(vidSplit.length > 1){
	      vidUrl = vidUrl.replace("http","rtmp");
	      vidUrl = vidUrl + "&cf=true";
	    }
      tb_show('','/page/1/video-opt-in.jsp?flv='+vidUrl+'&height=400&width=430');
	    return false;
  	}
  });

  // youTube video popup
  $('.youtube a').click(function(){
    var vidUrl = $(this).attr('href');
    tb_show('','/page/1/youtube-player.jsp?url='+vidUrl+'&height=300&width=490');
    return false;
  });

  // secure youTube video popup
  $('.secureyoutube a').click(function(){
  	if ( vlt_Get_Cookie( 'vlt_videoOptIn' ) ) {
	    var vidUrl = $(this).attr('href');
	    tb_show('','/page/1/youtube-player.jsp?url='+vidUrl+'&height=300&width=490');
	    return false;
  	} else {
  	    // show form
		var vidUrl = $(this).attr('href');
	    tb_show('','/page/1/video-opt-in.jsp?flv='+vidUrl+'&yt=true&height=400&width=430');
	    return false;
  	}
  });

});

function vlt_Get_Cookie( check_name ) {
   // first we'll split this cookie up into name/value pairs
   // note: document.cookie only returns name=value, not the other components
   var a_all_cookies = document.cookie.split( ';' );
   var a_temp_cookie = '';
   var cookie_name = '';
   var cookie_value = '';
   var b_cookie_found = false; // set boolean t/f default f

   for ( i = 0; i < a_all_cookies.length; i++ )
   {
     // now we'll split apart each name=value pair
     a_temp_cookie = a_all_cookies[i].split( '=' );


     // and trim left/right whitespace while we're at it
     cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

     // if the extracted name matches passed check_name
     if ( cookie_name == check_name )
     {
       b_cookie_found = true;
       // we need to handle case where cookie has no value but exists (no = sign, that is):
       if ( a_temp_cookie.length > 1 )
       {
         cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
       }
       // note that in cases where cookie is initialized but no value, null is returned
       return cookie_value;
       break;
     }
     a_temp_cookie = null;
     cookie_name = '';
   }
   if ( !b_cookie_found )
   {
     return null;
   }
 }
