/*  File: basic.js          */
/*  By:   Jeremy Tredway    */
/*  Ver:  2008-03-28        */

// Defined in global context so content loaded via ajax can set its own links.
var setExternalLinks = function() {
  $("a[href^=http]").not('.internal').each(function () {
    if(this.href.indexOf(location.hostname) == -1) {
      $(this).addClass('external').attr('target', '_blank');
    }
  });
  $("a img").parent().removeClass('external');
};

// Convenience function:
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// Indicates whether the page has loaded yet - useful to keep onclick links from doing things (such as launching Thickbox) before the browser is ready.
var pageLoaded = false;

$(function(){
  pageLoaded = true;

  // set active left-hand navigation
  $("#left_col .module ul").find("a").each(function(){
    if( location.href.indexOf(this.href) != -1) {
      $(this).parent().addClass("active");
    }
  });
  $("#left_col .module li:first[class='active']").css("margin-top", "1em");

  // set search parameters
  var DEF_VAL = "Search here";

  if ($('#search #query').val() == '')
    $('#search #query').val(DEF_VAL);  

  $("#search #query").focus(function() {
    $("#search #query").val("");
  }).blur(function() {
    if (this.value == "") $("#search #query").val(DEF_VAL);
  });

  // set external links
  setExternalLinks();

  // set advanced search toggle
  $("#adv_link").click(function() {
    $("#search_advanced").toggle();
    return false;
  });

  //slideshow start thumb
  $("a#slideshow_start_thumb").click(function() {
    $("a#slideshow_start").click();
    return false;
  });
  
  // Amplify demo video hookup:
  $('.amplify .demo_link').click(function() {
    launchFlashModal('/flash/amplify_demo.swf', 447, 335, '');
    return false;
  });
});
function launchFlashModal(theSrc, theWidth, theHeight, flashVars) {
  if (theSrc.indexOf(".swf")<0) {
    if (theSrc.indexOf("?")<0) {
      theSrc = theSrc + ".swf";
    } else {
      theSrc = theSrc.substr(0, theSrc.indexOf("?")) + ".swf" + theSrc.substr(theSrc.indexOf("?"), theSrc.length + 1);
    }
  }
  var rand_no =  Math.ceil(Math.random() * 10000);
  flashHTML = '<div style="border: 1px solid #8d8d8d; width: ' + theWidth + 'px;height: ' + theHeight + 'px; overflow: hidden">';
  flashHTML += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"  width="' + theWidth + '" height="' + theHeight + '" id="flashModal" align="middle">';
  flashHTML += '<param name="allowScriptAccess" value="sameDomain" />';
  flashHTML += '<param name="allowFullScreen" value="false" />';
  flashHTML += '<param name="movie" value="' + theSrc + '" />';
  flashHTML += '<param name="quality" value="high" />';
  flashHTML += '<param name="bgcolor" value="#ffffff" />';
  flashHTML += '<param name="wmode" value="window" />';
  if (flashVars.length > 0) {
    flashHTML += '<param name="flashvars" value="' + flashVars + '" />';
  }
  flashHTML += '<embed src="' + theSrc + '" quality="high" bgcolor="#ffffff" ';
  flashHTML += 'width="' + theWidth + '" height="' + theHeight + '" name="flashModal" wmode="window" ';
  if (flashVars.length > 0) {
    flashHTML += 'flashvars="' + flashVars + '" ';
  }
  flashHTML += 'align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
  flashHTML += '</object>';
  flashHTML += '</div>';
  $("#thickBoxContent").html(flashHTML);
  if (document.all) {
    theBoxHTML = document.getElementById("thickBoxContent").innerHTML;
    document.getElementById("thickBoxContent").innerHTML = theBoxHTML;
  }
  theWidth += 2;
  theHeight += 2;
  tb_show('', '#TB_inline?height=' + theHeight + '&width=' + theWidth + '&inlineId=thickBoxContent', '');
  
  //take focus away from Flash movie and onto the HTML frame, so that "esc" closes the thickbox
  $("#TB_closeWindowButton")[0].focus();
}
