jQuery(function ()
{
	
  jQuery(".page-template-contact-php .post h4").each(function ()
  {
    var header = jQuery(this);
    var paragraphs = jQuery.grep(
		    header.next('p'),
				     function (n)
				     {
				       return jQuery(n).prevAll('h4').text() == header.text();
				     });

    jQuery.merge(header, paragraphs).wrapAll(jQuery('<div class="column">'));
  });

  jQuery('.column').css({
    'float': 'left',
    'width': 860 / jQuery('.column').length + 'px'
  });

  //jQuery('#nav li:not(ul ul li):not(:last)').append('|');
  //jQuery('.disciplines li:not(:last), .clients li:not(:last)').css({'border-right-width': '1px', 'border-right-style': 'solid'});
  //jQuery('#nav li li:not(:last-child)').css({'border-right-width': '1px', 'border-right-style': 'solid'});

  jQuery('ul.galleries li img').each(function ()
  {
    if (!this.complete)
    {
      jQuery(this).load(function () {jQuery(this).fadeIn(300)});
    } else
    {
      jQuery(this).fadeIn(300);
    }
  });

  jQuery('#filter select').change(function ()
  {
    jQuery(this).parent('form').submit();
  }).next('button').css('display', 'none');

  jQuery('ul.galleries .gallery a').click(function ()
  {
    if (!tripline)
    {
      var clicked = jQuery(this).attr('href');
      tripline = true;
      jQuery.get(jQuery(this).attr('href'), function (data)
      {
        Shadowbox.open({
          content: jQuery(data).find('#gallery').html(),
          player: "html",
          width: 940,
          height: 560,
          title: jQuery(data).find('h2').html(),
          options: {onFinish: function () {killThumbLinks(clicked)}, onClose: resetOverflow, onOpen: temporaryHide}
        });

      }, 'html');
    }
    return false;
  });
  jQuery('#all-galleries h2').each(function ()
  {
    jQuery(this).css({
      'top': jQuery(this).next('ul').find('li:first-child').offset().top - jQuery('#all-galleries').offset().top,
      'left': jQuery(this).next('ul').find('li:first-child').offset().left - jQuery('#all-galleries').offset().left,
      'display': 'block'
    })
  });
  jQuery("#rotator").cycle({
    pager: '#pager',
    fx: 'scrollHorz',
    timeout: 8000,
    speed: 2000,
    pause: 1,
    fit: 1,
    easing: 'easeInOutSine',
    pagerClick: navPager,
    after: afterSlide,
    before: beforeSlide
  });

});


function navPager(index, element)
{

}

function temporaryHide()
{
  jQuery('#sb-body-inner').css('opacity', '0');
}

function beforeSlide(current, next, options, flag)
{
  for (i = 0; i < galleryText.length; i++)
  {
    if ('img-' + galleryText[i].index == jQuery(next).attr('class'))
    {
      var title = galleryText[i].title;
      var desc = galleryText[i].desc;
      var link = galleryText[i].link;
      var album = galleryText[i].album;
      //console.log(title);
    }
  }
  jQuery('#feature h3').fadeOut(500, function ()
  {
    jQuery(this).html(title + ': ');
    jQuery(this).fadeIn(500);
  });
  jQuery('#feature p').fadeOut(500, function ()
  {
    jQuery(this).html(desc);
    jQuery(this).fadeIn(500);
  });

  jQuery('#feature img').mouseover(function (){
	jQuery(this).css( 'cursor', 'pointer' );
  });

  jQuery('#feature img').click(function ()
  {
    if (!tripline)
    {
      var clicked = galleryText[jQuery(this).attr('id')].link;
      tripline = true;
      jQuery.get(galleryText[jQuery(this).attr('id')].link, function (data)
      {
        Shadowbox.open({
          content: jQuery(data).find('#gallery').html(),
          player: "html",
          width: 940,
          height: 560,
          title: jQuery(data).find('h2').html(),
          options: {onFinish: function () {killThumbLinks(clicked)}, onClose: resetOverflow, onOpen: temporaryHide}
        });

      }, 'html');
    }
    return false;
  });
  
  //jQuery('#feature img').click(function() 
  //{
    //jQuery(this).children('span').html(album);
	//window.location = link;
    //jQuery(this).fadeIn(500);
  //});

}

function afterSlide(current, next, options, flag)
{

}

function resetOverflow()
{
  jQuery('#sb-body').css('overflow', 'hidden');
  tripline = false;
}

var _clicked;

function fadeInNext(clicked)
{
  var c = _clicked.attr('class');
  jQuery('span.current').removeClass('current');

  var newFeatureSel = '.feature .' + c;


  jQuery('p.caption.' + c).fadeIn(200).addClass('current');
    jQuery(newFeatureSel).parent().addClass('current');
    _clicked.addClass('current');

  if (jQuery(newFeatureSel + ' img').length)
    jQuery(newFeatureSel).fadeIn(200);
  else
    jQuery(newFeatureSel).css('display', 'inline');

}


function swapThem()
{
  _clicked = jQuery(this);

  jQuery('a.current').removeClass('current');
  jQuery('p.caption.current').removeClass('current').hide();

  var oldThing = jQuery('.feature .current img');
  if (oldThing.length)
    oldThing.fadeOut(200, fadeInNext);

  else
  {
    oldThing = jQuery('.feature .current object');
    if (oldThing.length)
    {
      oldThing.css('display', 'none');
      fadeInNext(_clicked);
    }
  }

  return false;
}

function killThumbLinks(test)
{
  jQuery('.flviewer').each(function ()
  {
    permalink = permalink.replace('&', '%26');
    var flashvars = {
      file: jQuery(this).children('a').attr('href'),
      image: jQuery(this).children('img').attr('src'),
      fullscreen: true,
      controlbar: 'over',
      stretching: 'uniform',
      'viral.link': permalink,
      'viral.onpause': false,
      'viral.oncomplete': false,
      skin: videoSkin
    };
    var params = {
      allowfullscreen: true,
      allowscriptaccess: 'always',
      wmode: 'transparent'
    };
    var attributes = {
      styleclass: jQuery(this).children('img').attr('class')
    };
    swfobject.embedSWF(playerPath, jQuery(this).attr('id'), '580', '450', '9.0.0', 'expressInstall.swf', flashvars, params, attributes);

  });

  jQuery('.feature img').each(function ()
  {
    jQuery(this).css('margin-top', (460 - jQuery(this).height()) / 2 + "px");
  });

  jQuery('.feature img').load(function ()
  {
    jQuery(this).css('margin-top', (460 - jQuery(this).height()) / 2 + "px");
  });

  jQuery('#sb-body-inner').animate({
    'opacity': '1'
  });
  jQuery('p.caption.current').fadeIn(200);
  jQuery('.thumbs a').click(swapThem);

}
