// JavaScript Document
function djangoFileBrowser(field_name, url, type, win) {
    var url = "/admin/filebrowser/?pop=2&type=" + type;
    tinyMCE.activeEditor.windowManager.open(
        {
            'file': url,
            'width': 820,
            'height': 500,
            'resizable': "yes",
            'scrollbars': "yes",
            'inline': "no",
            'close_previous': "no"
        },
        {
            'window': win,
            'input': field_name,
            'editor_id': tinyMCE.selectedInstance.editorId
        }
    );
    return false;
}

$(function($) {
  // ga code to tack external links
  var ext_array = ["pdf", "doc", "xls", "zip"];
  $("a").click(function(){
    var link = $(this).attr('href');
    var myURL = parseURL(link);
    var file_name = myURL.file;
    var current_ext = '';
    
    try
      {
        var current_ext_raw = file_name.split("\.")[1];
        var current_ext = current_ext_raw.toLowerCase();
      }
    catch(err)
      {
        //do nothing
      }
    
    if((typeof(pageTracker) != 'undefined') && ($.inArray(current_ext, ext_array) > -1 || link.search("http") == 0)){
      var url = $(this).attr('href').replace( /^http\:\/\/(www\.)?/i , "" );
      pageTracker._trackPageview( '/external/' + url );	
    }
    
  })
  
  // vidoes on the homepage
  var videoPath = 'http://player.vimeo.com/video/';
  var queryString = '?title=0&amp;byline=0&amp;portrait=0&amp;color=bbd4f0';

  $('ul#videos li:first').addClass('first');
  $('ul#videos li:last').addClass('last');

  $('ul#videos li').click(function(event) {
    var $this = $(this);
    var id = $this.attr('id');
    var videoUrl = videoPath + id + queryString;

    $('div#main-video iframe').attr('src', videoUrl);

    event.preventDefault();
  });

  function scroll(){
    $("html:not(:animated),body:not(:animated)").animate({scrollTop: 400}, 1000);
    return false;
  }

  $('a.backToTop').click(function(event) {
    scroll();
    event.preventDefault();
  });
    
  $('div#home-events div.event ul li ul li').each(function(index) {
    var $this = $(this);
    var text = $this.text();

    if(text.length > 30) {
      $(this).text(text.substr(0 , 29) + '...');
    }
  });

  $("a#directions,a.photo").fancybox({
    'transitionIn'	:	'elastic',
    'transitionOut'	:	'elastic',
    'speedIn'		:	600, 
    'speedOut'		:	200, 
    'overlayShow'	:	true,
    'height'		:   525,
    'width'			: 	665 
  });
});

// This function creates a new anchor element and uses location
// properties (inherent) to get the desired URL data. Some String
// operations are used (to normalize results across browsers).
 
function parseURL(url) {
    var a =  document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':',''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function(){
            var ret = {},
                seg = a.search.replace(/^\?/,'').split('&'),
                len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
        hash: a.hash.replace('#',''),
        path: a.pathname.replace(/^([^\/])/,'/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
        segments: a.pathname.replace(/^\//,'').split('/')
    };
}

