﻿/// <reference path="jquery-ui-1.8.4.custom.min.js"/>

$(function () {
	$('html, body').animate({scrollTop: '0px'}, 300);
    if ($("#videoPlayer .transcriptButton").attr("href") == "")
    {
    	$("#videoPlayer .transcriptButton").hide();
    }
    else
    {
    	$("#videoPlayer .transcriptButton").show();
    	//$("#videoPlayer .transcriptButton").attr("href", transcriptUrl); David Magerl commented out line - 6/29/11 10:25am
    }

	var autoStart = "false";

	var currentUrl = window.document.URL.toString();
	var queryString = currentUrl.split("?");
	if (queryString[1]) {// David Magerl Removed .lenght > 0 - 6/29/11 10:25am
		autoStart = "true";
	}
	else {
		autoStart = "false";
	}
	

	var videoHolder = $("#videoHolder");
	var videoTitle = $("#videoTitle");
	var videoDescription = $("#videoDescription");
	//var pageTracker = _gat._getTracker("UA-2223196-23");  
    var mp4Url = $("#mp4url").attr("href");  
    var poster = $("#videoHolder").attr("data-image");  

    if (Modernizr.video && Modernizr.video.h264) {
    	$("#videoHolder").html('<video src="' + mp4Url + '" controls poster="' + poster + '" height="' + 298 + '" width="' + 530 + '"></video>');
    }
    else {
	    jwplayer("videoHolder").setup({
	        file: $("#videoHolder").attr("data-file"),
	        image: $("#videoHolder").attr("data-image"),
	        flashplayer: "/Video/Scripts/js/player.swf",
	        streamer: "rtmp://videoserver.upmc.com/flash/",
	        height: 298,
	        width: 530,
	        autostart: autoStart,
	        controlbar: "bottom",
	        stretching: "fill",
	        allowscriptaccess: "always"
	    });    
    }

    //$(".thumbnail").children("div").hide();

    $(".listLayers").accordion({ collapsible: true, header: 'h5', active: 0 });
    
    $(".videoLayerHeader").live('click', function () {
		var parentCollectionType = $(this).attr("id");
		parentCollectionType = parentCollectionType.slice(0, -6);
		var parentCollectionLabel = parentCollectionType;
		var parentCollectionID = parentCollectionLabel;
    	var trackingInfo = parentCollectionID + " videos";

		if (parentCollectionType.match("_") == null) {
		}
		else {
			parentCollectionLabel = parentCollectionLabel.replace(/_/g, " ");
		}
		var cloneCategory = $("#" + parentCollectionID).parent().clone();
		$("#selectedCategoryTitle").html("Selected Category: " + parentCollectionLabel);
		$("#selectedCategoryContent").html($(cloneCategory).children());
		$("#selectedCategoryContent div").removeClass('ui-widget-content');
		pageTracker._trackEvent('VideoLibrary', 'ClickVidCategory', trackingInfo);
	});
	
    $(".thumbnail").live('click', function(){
        var mp4Url = $(this).children(".mp4url").attr("href");
        if ($(this).children(".transcriptButton").attr("href") == null) {
        	var transcriptUrl = $(this).children(".transcripturl").attr("href");
        }
        else {
        	var transcriptUrl = $(this).children(".transcriptButton").attr("href");
        }
        var poster = $(this).attr("data-image"); 
		var videoTitle = $(this).children("h6").text();
		var videoDescription = $(this).children(".videoDescription").html();
		var trackingType = $(this).attr("data-trackingtype");
		var trackingLabel = $(this).attr("data-trackinglabel");
		
		autoStart = "true";
		
	    // Populate the video holder with the specified item in the list
	    if (Modernizr.video && Modernizr.video.h264) {
		    $("#videoHolder").html('<video src="' + mp4Url + '" controls autoplay="autoplay" poster="' + poster + '" height="' + 298 + '" width="' + 530 + '"></video>');
	    }
	    else {
	    	
		    jwplayer("videoHolder").setup({
		        file: $(this).attr("data-file"),
		        image: $(this).attr("data-image"),
		        flashplayer: "/Video/Scripts/js/player.swf",
		        streamer: "rtmp://videoserver.upmc.com/flash/",
		        height: 298,
		        width: 530,
		        autostart: autoStart,
		        controlbar: "bottom",
		        stretching: "fill",
		        allowscriptaccess: "always"
		    });    
	    }

	    var parentCollectionType = $(this).parent().attr("id");
	    var parentCollectionLabel = parentCollectionType;
	
	    if (parentCollectionType.match("_") == null) {
	    }
	    else {
	        parentCollectionLabel = parentCollectionLabel.replace(/_/g, " ");
	    }
	
	    $(".videoTitle").html(videoTitle);
	    $("#videoDescription").html(videoDescription);
	    $("#videoPlayer .transcriptButton").attr("href", transcriptUrl);
	    
	    if (transcriptUrl == "")
	    {
	    	$("#videoPlayer .transcriptButton").hide();
	    }
	    else
	    {
	    	$("#videoPlayer .transcriptButton").show();
	    	$("#videoPlayer .transcriptButton").attr("href", transcriptUrl);
	    }
	    
	    $("#videoDescription").children().show();
	    $("#selectedCategoryTitle").html("Selected Category: " + parentCollectionLabel);
	    $("#selectedCategoryContent").html($(this).parent().parent().children().clone());
	    $("#selectedCategoryContent div").removeClass('ui-widget-content');
        
        // Register Google Analytics load event when thumbnail is clicked...
        var ttype = "";
        var label = "";
        // Check if tracking type was set in the list, if not set to 'none'
        if (trackingType == "undefined") {
            ttype = "none"
        }
        else {
            ttype = trackingType;
        }
        // Check if tracking label was set in the list, if not set it to 'none'
        if (trackingLabel == "undefined") {
            label = "none"
        }
        else {
            label = trackingLabel;
        }

        // Google analytics tag to track click on thumbnail....
        pageTracker._trackEvent(ttype, 'view', label);

	    $('html, body').animate({scrollTop: '0px'}, 300);
	    
 	});
	
    if ($(".videoList").children().size() > 5)
    {
    	$(".videoList").children('li').css('height', '190px');
    }
});

