﻿	$(function(){
	$('.image-tooltip-content').hide();
	$('.image-tooltip').bt({
		trigger: 'click',
		contentSelector: "$('.image-tooltip-content').html()", /*get text of inner content of hidden div*/
		positions: 'top',
		width: 320,
		fill: '#434343',
		strokeWidth: 2, /*no stroke*/
		strokeStyle: '#727478',
		spikeLength: 11,
		spikeGirth: 18,
		padding: 20,
		cornerRadius: 0,
		centerPointX:     .5,                    // the spike extends from center of the target edge to this point
		centerPointY:     .5,                    // defined by percentage horizontal (x) and vertical (y)
		cssStyles: {
		fontSize: '13px',
		color: '#FFF'
	}
	})


	$(".scrollable").scrollable();

	$(".mmedia_video_list li").click(function() {
	   $(".multimedia_top_left").addClass("standby");
	   $(".multimedia_top_left").removeClass("multimedia_top_left");
	   var className = $(this).attr("class");
	   $("#" + className).removeClass("standby");
	   $("#" + className).addClass("multimedia_top_left");

	   $(".flash_target").attr("id", "");
	   $(".multimedia_top_left .flash_target").attr("id", "flash_target");
	   $("#flash_target").show();

	   $($(".multimedia_top_left .items img")[0]).click();
	});

     	$(document).ready(function() {
     	$(".items img").click(function() {
     		// see if same thumb is being clicked
     		if ($(this).hasClass("active")) { return; }

     		var caption = $(this).attr("caption");

     		$(".img-caption").text("").append(caption);

     		var imgCopyright = $(this).attr("copyright");
     		
     		if (imgCopyright == "") {
     		     $(".caption-copyright").text("\u00A0");  //&nbsp;
     		 } else {
     		     $(".caption-copyright").text("\u00A9 " + imgCopyright);
     		 }
            
     		

			var imgAlt = $(this).attr("alt");
			if (imgAlt == "") imgAlt = " ";
     		$(".image-tooltip-text").text(imgAlt);

     		var downloadLowLink = "/media/" + $(this).attr("imgId") + "/640x360/download";
     		$(".download-low-link").attr("href", downloadLowLink);
     		$(".download-low-bytes").text($(this).attr("lowBytes"));

     		var downloadHighLink = "/media/" + $(this).attr("imgId") + "/orig/download";
     		$(".download-high-link").attr("href", downloadHighLink);
     		$(".download-high-bytes").text($(this).attr("highBytes"));

     		var size = $(this).attr("size");
     		$(".download-low-image-size").text(size);

     		var download = $(this).attr("download")
     		if (download == "true") {
     			$('.image-tooltip').css("visibility", "visible");
     		} else {
     			$('.image-tooltip').css("visibility", "hidden");
     		}

     		var type = $(this).attr("type")
     		if (type == "image") {

     		   $("#flash_target").hide();
				$(".image-tooltip").show();

     			// calclulate large image's URL based on the thumbnail URL (flickr specific)
     			var url = "/media/" + $(this).attr("imgId") + "/640x360";
     			// get handle to element that wraps the image and make it semi-transparent
     			var wrap = $(".image_wrap");
     			/*if(!jQuery.browser.msie) {
     				wrap.fadeTo("medium", .7);
     			}*/
     			// the large image from www.flickr.com
     			var img = new Image();
     			// call this function after it's loaded
     			img.onload = function() {
     				// make wrapper fully visible
     				/*if(!jQuery.browser.msie) {
     					wrap.fadeTo("fast", 1);
     				}*/
     				// change the image
     				var targetImg = wrap.find("img");

     				if (targetImg == null) {
     					$(".image_wrap").empty();
     					targetImg = $('<img>');
     					$(".image_wrap").append(targetImg);
     				}

     				targetImg.attr("src", url);
     			};
     			// begin loading the image from www.flickr.com
     			img.src = url;
     			$(".image_wrap img").show();
     		}
     		else if (type == "video") {
     		   $(".caption-button").show();
     		   $(".image_wrap img").hide();
     		   $(".image_wrap img").attr("src", "");
			   var download = $(this).attr("download");
     		   var streamUrl = $(this).attr("url");
     		var prefix = "http://";

				$(".image-tooltip").hide();

     		if (streamUrl.indexOf(prefix) == 0) {
     		  streamUrl = streamUrl.substring(prefix.length, streamUrl.length);
     		}

			// append current full URL without http://
			var currentUrl = window.location.pathname;
			currentUrl = currentUrl.replace('http://', '');
			streamUrl += "|"+escape(currentUrl);
     		var url = "/video.xqy?videoURL=" + streamUrl;
			if(download == "true") {
				url += "|true";
			} else {
				url += "|false";
			}
     		swfobject.embedSWF("/assets/videos/uvp/ldsUniversalPlayer.swf", "flash_target", "620", "349", "9.0.0", "http://www.ldsavd.org/ldsUniversalPlayer/swf/expressInstall.swf", { xmlSource: url }, { menu: "false", allowScriptAccess: "always", scale: "noscale", allowFullScreen: "true", wmode: "transparent" });
     		var z=0;
     	  }
     		else if (type == "audio") {
     		   $(".caption-button").show();
     		   $(".image_wrap img").hide();
     		   $(".image_wrap img").attr("src", "");
     		   var streamUrl = $(this).attr("url");
     		swfobject.embedSWF("/assets/videos/uvp/newsroom-audio-player.swf", "flash_target", "620", "50", "9.0.0", "mediaplayerAudio", { media: streamUrl, type: "MP3" }, {  wmode: "opaque", allowScriptAccess: "always" });
     		}
     		else if (type == "brightcove") {
     		   var bcVideoId =  $(this).attr("vidid") 
     		   $(".image_wrap img").hide();
     		   $(".image_wrap img").attr("src", "");
     		   var brightcoveEmbedCode = '<object id="flash_target" class="BrightcoveExperience"><param name="bgcolor" value="#FFFFFF" /><param name="width" value="620" /><param name="height" value="360" /><param name="playerID" value="'+bcVideoId+'" /><param name="playerKey" value="AQ~~,AAAAjVbEnrk~,Ouiqo3EMYP54RW7wpA8Nsxdq7yIYBjLy" /><param name="isVid" value="true" /><param name="isUI" value="true" /><param name="dynamicStreaming" value="true" /><param name="@videoPlayer" value="'+bcVideoId+'" /></object><script type="text/javascript">brightcove.createExperiences();</script>'                    		   
     		   $("#flash_target").replaceWith(brightcoveEmbedCode);
     		   $("#flash_target").show();
     		   $(".caption-button").hide();
     		}

     		// activate item
     		$(".items img").removeClass("active");
     		$(this).addClass("active");
     	// when page loads simulate a "click" on the first image
     	})

     	// you'll probably want to modify this to get your video code to drop dynamically.
     	$('.video-thumb1').click(function() {
     		if ($(this).child().hasClass("active")) { return; }
     		var url = '';
     		$('video_wrap').html(url);
     		// activate item
     		$(".items img").removeClass("active");
     		$(this).child().addClass("active");
     	});
    });

	$(document).ready(function() {
	   var scroller = $(".mm-scroller");
	   if ($(".items").find("img").size() == 1) {
		  scroller.hide();
	   }
	   else if ($(".items").find("img").size() < 5) {
		  $("a.right").hide();
	   }

	   clickTargetedItem();
	})
	})

	function clickTargetedItem() {
	   var hash = window.location.hash;

	   var itemToClick = null;

	   if (hash == null || hash == "") {
	       itemToClick = $($(".items img")[0])
	   }
	   else {
    	   var mediaId = window.location.hash.substring(1, window.location.hash.length)
    	   var itemToClick = $("img[imgid*='" + mediaId + "']")

    	   if (itemToClick.length == 0) {
    	       itemToClick = $("img[vidid*='" + mediaId + "']")
    	   }

    	   if (itemToClick.length == 0) {
    	       itemToClick = $("img[audid*='" + mediaId + "']")
    	   }

    	   if (itemToClick.length == 0) {
    	       itemToClick = $($(".items img")[0])
    	   }
	   }

       itemToClick.click();
	};

	$(document).ready(function() {

    	//IF AGREEMENT CHECKED ALLOW DOWNLOAD
    	$(".download-low-link, .download-high-link").click(function(event){
    		if($(".license_agree").attr("checked")){
    			return;
    		}else{
    			event.preventDefault();
    		}
    	});
    	//END IF AGREEMENT CHECKED ALLOW DOWNLOAD

    	$(".license_agree").click(function(){

    	   if($(this).attr("checked")){
    	       $(".download-low-link").addClass("agreed");
    	       $(".download-high-link").addClass("agreed");
    	   }
    	   else {
    	       $(".download-low-link").removeClass("agreed");
    	       $(".download-high-link").removeClass("agreed");
    	   }

    	});

    	$(document).click(function(){
    		if($(".multimedia_top_left .download-image-info").is(":visible")){
    			$(".multimedia_top_left .download-image-info").toggle();
    		}
    		if($(".article .download-image-info").is(":visible")){
    			$(".article .download-image-info").toggle();
    		}
    		if($("#topic_body .download-image-info").is(":visible")){
    			$("#topic_body .download-image-info").toggle();
    		}
    	});

    	//MULTIMEDIA DOWNLOAD ICON CLICK
    	$(".image-tooltip img").click(function(event){
    		$(".download-image-info").toggle();
    		event.stopPropagation();
    	});
    	$(".download-image-info, .image-tooltip").click(function(event){
    		event.stopPropagation();
    	});

    	//MULTIMEDIA DOWNLOAD ICON HOVER
    	$(".use_tooltip .image-tooltip img").mouseenter(function(){
    	   var infoDiv = $(this).parent().find(".download-image-info");
    		if(infoDiv.not(":visible")){
    			infoDiv.delay(500).show(1);
    		}
    	});

	});

