// Here we attach functions to resize text to the anchors on story detail pages.
$(document).ready(function(){
	$("a#txtResizeMinus").click(function () { 
		txtResizeSmaller();
	});
	$("a#txtResizePlus").click(function () { 
		txtResizeLarger();
	});

	$("#featured_assets").hover(
		function () {
			highlightMorePhotos( 1 );
      		}, 
        	function () {
			highlightMorePhotos( 0 );
      		}
    	);
	 $("#more_photos").hover( 
                function () {
                        highlightMorePhotos( 1 );
                }, 
                function () {
                        highlightMorePhotos( 0 );
                }
        );
});

// This function simply fades the backgound color of the More Photos div
//   in and out depending on the action passed, 1 == fade color in, 0 == out
function highlightMorePhotos( act ){
	if( act == 1 ){
		$("#morePhotos").css("background-color","#FFFFCC");
		$("#morePhotos").css("border","1px dashed #E0E0E0");
	}		
	if( act == 0 ){
		$("#morePhotos").css("background-color","white");
		$("#morePhotos").css("border","none");
	}
}

function txtResizeSmaller() {
	var currentTxtSize = $("#story_text_top > p").css("font-size");
	var newTxtSize = currentTxtSize.split("px");

	var currentTxtLineHeight = $("#story_text_top > p").css("line-height");
	var newTxtLineHeight = currentTxtLineHeight.split("px");

	if( parseInt( newTxtSize[0] ) <= 8 ){
		$("#txtResizeTool img#txtResizeMinus").css( "display", "none" );
		$("img#txtResizeMinusFade").css( "display", "inline" );
	} else {
		currentTxtSize = parseInt( newTxtSize[0] ) - 2;
		currentTxtLineHeight = parseFloat( newTxtLineHeight[0] ) - 2;
		if( parseInt( currentTxtSize ) <= 8 ){
			$("#txtResizeTool img#txtResizeMinus").css( "display", "none" );
			$("img#txtResizeMinusFade").css( "display", "inline" );
		}

		currentTxtSize = currentTxtSize + "px";
		currentTxtLineHeight = currentTxtLineHeight + "px";
	}
	
	$("#txtResizeTool img#txtResizePlus").css("display", "inline");
	$("img#txtResizePlusFade").css("display", "none");

	$("#story_text_top > p").css("font-size", currentTxtSize);
	$("#story_text_top > p").css("line-height", currentTxtLineHeight);
	$("#story_text_remaining > p").css("font-size", currentTxtSize);
	$("#story_text_remaining > p").css("line-height", currentTxtLineHeight);
}

function txtResizeLarger() {
	var currentTxtSize = $("#story_text_top > p").css("font-size");
	var newTxtSize = currentTxtSize.split("px");

	var currentTxtLineHeight = $("#story_text_top > p").css("line-height");
	var newTxtLineHeight = currentTxtLineHeight.split("px");

	if( parseInt( newTxtSize[0] ) >= 24 ){
		$("#txtResizeTool img#txtResizePlus").css( "display", "none" );
		$("img#txtResizePlusFade").css( "display", "inline" );
	} else {
		currentTxtSize = parseInt( newTxtSize[0] ) + 2;
		currentTxtLineHeight = parseFloat( newTxtLineHeight[0] ) + 2;
		if( parseInt( currentTxtSize ) >= 24 ){
			$("#txtResizeTool img#txtResizePlus").css( "display", "none" );
			$("img#txtResizePlusFade").css( "display", "inline" );
		}

		currentTxtSize = currentTxtSize + "px";
		currentTxtLineHeight = currentTxtLineHeight + "px";
	}
	
	$("#txtResizeTool img#txtResizeMinus").css("display", "inline");
	$("img#txtResizeMinusFade").css("display", "none");

	$("#story_text_top > p").css("font-size", currentTxtSize);
	$("#story_text_top > p").css("line-height", currentTxtLineHeight);
	$("#story_text_remaining > p").css("font-size", currentTxtSize);
	$("#story_text_remaining > p").css("line-height", currentTxtLineHeight);
}




// This function sets up the way images cycle on the story detail pages.
function setupCycleSlideShow() {
	$("a#morePhotos").toggle(function(){
        	$("a#morePhotos").html("CLICK TO HIDE PHOTOS");
        	$("ul#cycleNav").slideDown();
      	},function(){
        	$("a#morePhotos").html("CLICK FOR MORE PHOTOS");
        	$("ul#cycleNav").slideUp();
      	});

      $('#cycleSlideShow').hover(
          function() { $('#cycleControls').fadeIn(); },
          function() { $('#cycleControls').fadeOut(); }
      );
	
      $('#cycleSlides').cycle({ 
        fx:      'fade', 
        speed:    500, 
        timeout:  10000,
  //    before:   switchCycleImageCaption,
      height: 'auto',
        next:     "#cycleNext",
        prev:     "#cyclePrev",
        pager:    "#cycleNav",
        pagerAnchorBuilder: function( idx, slide ){
          return '#cycleNav li:eq(' + (idx) + ') a';
        }
      });
  // $("#cycleSlides > div.photo-entry").css({"position" : "static"});
}

// This function is called everytime an image switches via cycle, and 
//   swaps out the images caption.
// function switchCycleImageCaption() {
// 
// $('#cycleImageCaption').html( this.title );
// var imgID = this.id;
// imgID += "-buyNow";
 /* This will break the widget in IE.
	only use for testing.
 console.debug(imgID);
 */
// $("#buyPhoto .Buy-It-Now").css('display','none');
// $("#" + imgID).css('display','block');
// } 

/*
// This function is called everytime an image switches via cycle, and 
//   swaps out the images caption. -OLD
function switchCycleImageCaption() { 
	// Change out the current image caption
	$('#cycleImageCaption').html( this.title );
	
	$('#pictopiaLink div').css('display','none');
	var linkID = $(this).attr('id');
	var pictopiaID = "pictopia" + linkID;
	$("#" + pictopiaID).css('display','block');
	// Now we have to change the pictopia link. 
	//var pictopiaLinkStart = "http://pictopia.com/perl/ptp/bradenton?photo_name=";
        //var pictopiaLinkMid   = "&embedded=y&t_url="
	//var currentPictureURL = this.pathname.split("/smedia")[1];
	//var pictopiaLink = pictopiaLinkStart + currentPictureURL + pictopiaLinkMid + currentPictureURL;
	// Once URL is built set new anchor href with 
	//$('div#pictopiaLink a').attr({ 
	//          href: pictopiaLink
        //});
} 

*/

// This function converts all our anchors with the .media class to be embedded instead of 
//   just links.  Currently this is not really needed, but I abstracted it thinking that 
//   down the road affiliates may want to customize this functionality.
function setupInlineMedia() {
	$('.media').media();
}
