
$(document).ready(
  function() {
	  
	//open external links in a new window
	externalLinksByHref();

	//video browsing widgets
    if ($('div#videoplayer-viewer ul li').length > 0) {
      //console.debug('loaded');
      $('div.videoplayer-viewer ul li').bind('mouseover mouseout', function () {
    	//$(this).parent().parent().parent().toggleClass('showcase-index-hover');
      });
      
      $('#videolisting ul li a')
        .bind('click', function () {
    	  //console.debug('click');
    	  key = $(this).parent().parent().attr('id');
    	  key = 'videoplayer-viewer' + key.substr(key.lastIndexOf('-'));
    	  //console.debug('key = ' + key);
    	  $('#videoplayer-viewer li').css('display', 'none');
    	  $('#videoplayer-viewer li#' + key).css('display', 'block');
    	  //$('#videoplayer-viewer li').fadeOut();
    	  //$('#videoplayer-viewer li#' + key).fadeIn();
    	  //console.debug('#videoplayer-viewer li#' + key);
    	  $(this).parent().parent().siblings().removeClass('selected').removeClass('highlight');
    	  $(this).parent().parent().addClass('selected');
    	  title = $(this).parent().text();
    	  //console.debug(title);
    	  $('#videoplayer-credit li.title').text(title);
    	  flowplayer('a.videoplayer', "/static/flowplayer-3.1.5.swf", {clip: { autoPlay: true }});
    	  return false;
        });
      
      $('#videolisting ul li')
        .bind("mouseenter", function () {
    	  //console.debug('over');
    	  $(this).addClass("highlight");
        })
        .bind("mouseleave", function () {
    	  //console.debug('out');
    	  if (!$(this).hasClass('selected')) {
    	    $(this).removeClass("highlight");
    	  }
        });
      
    }
	
	//audio browsing widgets
    if ($('div#audioplayer-viewer ul li').length > 0) {
      $('#audiolisting div.audiolisting-rows a')
        .bind('click', function () {
    	  key = $(this).parent().parent().attr('id');
    	  key = 'audioplayer-viewer' + key.substr(key.lastIndexOf('-'));
    	  $('#audioplayer-viewer li').css('display', 'none');
    	  $('#audioplayer-viewer li#' + key).css('display', 'block');
    	  //console.debug('#audioplayer-viewer li#' + key);
    	  $(this).parent().parent().siblings().removeClass('selected').removeClass('highlight');
    	  $(this).parent().parent().addClass('selected');
    	  title = $(this).parent().text();
    	  imgpath = $(this).parent().attr("title");
    	  imginit = (imgpath) ? "<img src=\""+imgpath+"\" alt=\""+title+"\"/>":"<img src=\"/static/bgthumb_audiospekear.jpg?w=253\" class=\"nothumb\" />";
    	  $('#audioplayer-credit li.title').html(title);
    	  $("#audioplayer-thumb").html(imginit);
    	  flowplayer('a.audioplayer', "/static/flowplayer-3.1.5.swf", {clip: { autoPlay: true }});
    	  return false;
        });
    }
  }
);





/**
 * vis: http://wiki.slideshowpro.net/SSPfl/C-DisplayCaptionsOutside
 * @param title
 * @param caption
 * @param year
 * @return
 */
function ssp_callback_updateCaption(title, location, country, caption, year) {
  //console.debug(title, location, country, caption, year);
  if (title == 'null') title='';
  if (location == 'null') location = '';
  if (country == 'null') country = '';
  if (caption == 'null') caption = ''; 
  if (year == 'null') year = '';
  
  string='';
  if (title != '') string += '<p class="title">' + title + '</p>';
  if (location != '') string += '<p class="location">' + location + '</p>';
  if (country != '') string += '<p class="country">' + country + '</p>';
  //if (caption != '') string += '<p class="caption">' + caption + '</p>';
  //if (year != '') string += '<p class="year">' + year + '</p>';
  $('#slideshow-credit').html(string);

  //$('#slideshow-credit .title').html(title);
  //$('#slideshow-credit .location').text(location); 
  //$('#slideshow-credit .caption').text(caption); 
 
  return null;
}


/**
 * vis: http://wiki.slideshowpro.net/SSPfl/C-Permalinks
 * track ssp image loads in stats: http://forums.slideshowpro.net/viewtopic.php?pid=69517
 * @param url
 * @return
 */
function ssp_callback_updateURL(url) {
  //console.debug(url);
  return 'status message';
}

function flashPutHref(href) {
  //location.href = href;
  //location.replace(href);
}


/**
 * slideshowpro methods
 * vis http://wiki.slideshowpro.net/SSPfl/C-ControlSSPHtmlJavascript
 */
function ssp_nextImage() { thisMovie("ssp").nextImage(); }
function ssp_previousImage() { thisMovie("ssp").previousImage(); }  
function ssp_setSize(w,h) { thisMovie("ssp").setSize(w,h); }  

/**
 * returns swf object
 * @param movieName
 * @return
 */
function thisMovie(movieName) {
  if (navigator.appName.indexOf("Microsoft") != -1)
    return window[movieName];
  else
    return document[movieName];
}


function revealPortfolioIndex() {
	$('h2.portfolio-index-layer').text('More Polar portfolios');
	$('div#content-portfolio-list').slideDown(200);
	$('div.portfolio-feature').fadeOut(400); 
	return false;
}

function externalLinksByHref() {
  var base_url = (document.URL ? document.URL : document.location.href).substr(0,(document.URL ? document.URL : document.location.href).indexOf('/',8));
  if (document.getElementsByTagName) {
    var anchors = document.getElementsByTagName('a');
    for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if ((anchor.getAttribute('href').indexOf('http:\/\/')==0
           || anchor.getAttribute('href').indexOf('https:\/\/')==0)
          && anchor.getAttribute('href').indexOf(base_url)<0) {
	anchor.target = '_blank';
      }
    }
  }
  else if (document.links) {
    var anchors = document.links;
    for (var i=anchors.length-1; i>=0; i--) {
      var anchor = anchors[i];
      if ((anchor.href.indexOf('http:\/\/')==0 || anchor.href.indexOf('https:\/\/')==0)
           && anchor.href.indexOf(base_url)<0) {
	anchor.target = '_blank';
      }
    } 
  }
}



/*
 * original concept follows:
 * http://www.sitepoint.com/article/1041/3http://www.sitepoint.com/article/1041/3
 *
 */

function externalLinksByRel() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external')
      anchor.target = '_blank';
  }
}

//window.onload = externalLinksByRel;

