var content_sliders = [];
$(document).ready(function(){
  $('.content_slider').each(function() {
    content_sliders[content_sliders.length] = new ContentSlider(this);
  });
  
  $('.aside_photo_gallery').each(function(){
    var slider = new ItemSlider($(this).find('.wrap'), {});
    $(this).find('.right_arrow').click(function(){
      slider.next();
      return false;
    });
    $(this).find('.left_arrow').click(function(){
      slider.prev();
      return false;
    });
  });
  
  disableRightClick('#the_media img');
  disableRightClick('.spotlight_artifact img');
  
});

//disable right click on large images
function disableRightClick(sel) {
	$(sel).bind("contextmenu",function(e) {
		return false;
	});
}

//for WYSIWIG embedded flash
function embedVideo(p) {
	var params = {
		menu: "false",
		allowScriptAccess: "always",
		scale: "noscale",
		allowFullScreen: "true"
	};
	if(typeof(p.flashvars) != "undefined") {
    params['flashvars'] = decodeURIComponent(p.flashvars);
	}
	
	var attributes = {};

	swfobject.embedSWF(p.src, p.id, p.width, p.height, "9.0.0", "swf/expressInstall.swf", {}, params, attributes);
	
	//init the youTubeLoader javascript methods
	if(p.isvideo) {
	  SWFID = p.id;
  }
}

//the class that's actually responsible for moving stuff that slides
function ItemSlider(theEl, uparams) {
  var that = this;
  var el = theEl;
  var slideEl = $(el).children().eq(0);
  var animating = false;
  
  var defaults = {
    slideWidth: null
  }
  
  var options = {};
  if(uparams) {
    options = $.extend(defaults, uparams);
  }
  
  if( ! options.slideWidth) {
    options.slideWidth = slideEl.children().eq(0).outerWidth(true);
  }
  
  el.css({
    'position':'relative',
    'overflow':'hidden'
  });
  slideEl.css({
    'position':'absolute',
    'top': 0,
    'left': 0
  })
  slideEl.width(slideEl.children().size() * options.slideWidth);
  
  function move(direction) {
    if(animating){ return; }
    
    var pos = slideEl.position()['left'] + (direction * options.slideWidth);
    if(pos > 0) {
      pos = 0;
    }
    maxRight = $(el).width() - slideEl.width();
    if(pos < maxRight) {
      pos = maxRight;
    }
    if(slideEl.width() < el.width()) {
      pos = 0;
    }
    
    animating = true;
    slideEl.animate({'left': pos},150,'linear',function(){ animating = false; });
  }
  
  that.next = function() {
    move(-1);
  }
  
  that.prev = function() {
    move(1);
  }
  
  that.slideTo = function(i) {
    move(i);
  }
}

//the class that's responsible for controlling stuff like the Videos slider and the Photo Gallery Slider
function ContentSlider(theEl) {
  var that = this;
  var el = $(theEl);
  var id_prefix = el.get(0).id.replace('content_','');
  var sliders = [];
  
  
  function readSectionId(full_id) {
    return full_id.replace(id_prefix+'_', '');
  }
  
  function show(section_id) {
    el.find('.content_slider_contents').hide();
    el.find('#'+id_prefix+'_'+section_id).show();
    el.find('.slider_nav li a').removeClass('pill_white');
    el.find('.slider_nav li a').addClass('pill_grey');
    el.find('#'+id_prefix+'_nav_'+section_id).removeClass('pill_grey').addClass('pill_white');
  }
  
  function setup() {
    el.find('.content_slider_contents').each(function(){
      var slider = new ItemSlider($(this).find('.slider_wrapper'), {});
      $(this).find('.slider_next').click(function(){
        slider.next();
        return false;
      });
      $(this).find('.slider_prev').click(function(){
        slider.prev();
        return false;
      });
      sliders[sliders.length] = slider;
    });
    
    
    show(readSectionId(el.find('.content_slider_contents').get(0).id));
    el.find('.slider_nav li a').click(function(){
      id = readSectionId(this.href.split('#')[1]);
      show(id);
      return false;
    });
  }
  
  setup();
}

//tinymce embeds
function writeFlash(p) {
	writeEmbed(
		'D27CDB6E-AE6D-11cf-96B8-444553540000',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'application/x-shockwave-flash',
		p
	);
}

function writeShockWave(p) {
	writeEmbed(
	'166B1BCA-3F9C-11CF-8075-444553540000',
	'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
	'application/x-director',
		p
	);
}

function writeQuickTime(p) {
	writeEmbed(
		'02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
		'video/quicktime',
		p
	);
}

function writeRealMedia(p) {
	writeEmbed(
		'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'audio/x-pn-realaudio-plugin',
		p
	);
}

function writeWindowsMedia(p) {
	p.url = p.src;
	writeEmbed(
		'6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
		'application/x-mplayer2',
		p
	);
}

function writeEmbed(cls, cb, mt, p) {
	var h = '', n;

	h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
	h += typeof(p.id) != "undefined" ? 'id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? 'name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? 'width="' + p.width + '"' : '';
	h += typeof(p.height) != "undefined" ? 'height="' + p.height + '"' : '';
	h += typeof(p.align) != "undefined" ? 'align="' + p.align + '"' : '';
	h += '>';

	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';

	h += '<embed type="' + mt + '"';

	for (n in p)
		h += n + '="' + p[n] + '" ';

	h += '></embed></object>';

	document.write(h);
}
