function showFullSizeView(url, linkUrl)
{
	var img = document.createElement('img');
	jQuery(img).attr('src', url);
	
	// create anchor to be used for img link
	var link = document.createElement('A');
	jQuery(link).attr('href', linkUrl);
	jQuery(link).attr('target', '_blank');
	jQuery(link).click(function() {
		pageTracker._trackPageview('/outbound/' + linkUrl);
	});
	link.appendChild(img);
	
	var overlay = document.createElement('div');
	overlay.setAttribute('id', 'overlay');
	jQuery(overlay).click(closeFullSizeView);
	jQuery(document.body).append(overlay);
	jQuery('#fullsize').html('');
	jQuery('#fullsize').append('<a href="#" class="close">X</a>');
	// jQuery('#fullsize').append(img);
	jQuery('#fullsize').append(link);
	jQuery('#fullsize').show();
	jQuery('#fullsize a.close').click(closeFullSizeView);
	// if (jQuery(document).height() <= 700)
		// jQuery('#fullsize').css('position', 'absolute');
}

function closeFullSizeView()
{
	jQuery('#fullsize').hide();
	jQuery('#overlay').remove();
	return false;
}

function showVideoPlayer()
{
	var overlay = document.createElement('div');
	overlay.setAttribute('id', 'overlay');
	jQuery(document.body).append(overlay);
	jQuery(overlay).click(closeVideoPlayer);
	jQuery('#videoPlayer').show();
	jQuery('#videoPlayer a.close').click(closeVideoPlayer);
	return false;
}

function closeVideoPlayer()
{
	jQuery('#videoPlayer').hide();
	jQuery('#overlay').remove();
	return false;
}

function onFullSizeLoaded(data)
{
	
}

function onShowFullSizeClick()
{
	var url = jQuery(this).attr('href');
	var link = jQuery(this).attr('rel');
	showFullSizeView(url, link);
	return false;
}

function submitVote()
{
	var itemId = jQuery(this).attr('rel');
	var hasVoted = readCookie('voted'+itemId);
	if (!hasVoted) {
		jQuery.get(jQuery(this).attr('href'), null, onVoteResponse);
		createCookie('voted'+itemId, 'true', 0.85);
	} else {
		alert('You may only vote once for each outfit.\n\nCome back tomorrow to vote again.');
	}
	jQuery(this).addClass('hasVoted');
	return false;
}

function onVoteResponse(data)
{
	if ( data == 0 ) {
		// alert('We\'re sorry, there was an error submitting your vote. Please try again later.');
	} else {
		pageTracker._trackPageview('/spring/vote/'+data);
		showFBStatusModal();
	}
}

function showFBStatusModal()
{
	jQuery('#fb-status-modal').show();
}

function closeFBStatusModal()
{
	jQuery('#fb-status-modal').hide();
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function initVoting()
{
	jQuery('.voting .thumb a.vote').click(submitVote);
	jQuery('a.embiggen').click(onShowFullSizeClick);
	jQuery('a.videoPlayer').click(showVideoPlayer);
	jQuery('#fb-status-modal a.close').click(closeFBStatusModal);
}


// Here there be Brightcove

var bcExp;
var modVP;
var modExp;
var modCon;
	 
// called when template loads, this function stores a reference to the player and modules.
function onTemplateLoaded(experienceID) {
 	bcExp = brightcove.getExperience(experienceID);
 
    modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
    modExp = bcExp.getModule(APIModules.EXPERIENCE);
    modCon = bcExp.getModule(APIModules.CONTENT);
	
	modExp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
    modCon.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);
	modVP.addEventListener(BCMediaEvent.PLAY,onVideoPlay);
}
function onTemplateReady(evt) {
    //alert("EVENT: TEMPLATE_READY"); 
}
function onContentLoad(evt) {
}
function onVideoPlay(evt) {
	pageTracker._trackPageview('/spring/video/66783856001');
}
function onVideoLoad(evt) {
    // Cue video that was just loaded
    modVP.cueVideo(evt.video.id);
}