﻿// Detect if Mobile then switch out ALL .flv with .m4v
var uagent = navigator.userAgent.toLowerCase();
function DetectIphoneOrIpodOrIpadOrAndroid() {
    if (uagent.search("iphone") > -1)
       return true;
    else if (uagent.search("ipod") > -1)
       return true;
	else if (uagent.search("android") > -1)
		return true;
	else if (uagent.search("ipad") > -1)
		return true;
    else
       return false;
}			
function convertToHTML5(jObject) {
	var href	= jObject.html();
	if(href.search('.flv')) {			
		var html5		= href.replace(/\.flv/g, '.m4v');
		html5	= html5.replace(/\/RecipeManager\/Uploads\/Videos\//g, '/RecipeManager/Uploads/Videos/HTML5/');
		html5	= html5.replace(/\/flash\/videos\//g, '/flash/videos/HTML5/');
		alert(html5);
		jObject.html(html5); 
	}
	/*
	jObject.click(function(){
		alert($(this).attr('href'));
	});
	*/
}
if(DetectIphoneOrIpodOrIpadOrAndroid()) {
	// Detect if Mobile then switch out ALL .flv with .m4v		
	$("#player").each(function(){ convertToHTML5($(this)); })
	//$("div.recipeVidContainer a").each(function(){ convertToHTML5($(this)); });
	//$("div.historyVidContainer a").each(function(){ convertToHTML5($(this)); });
	//$("div.commercialVidContainer a").each(function(){ convertToHTML5($(this)); });
	//$("div.plantainsVidContainer a").each(function(){ convertToHTML5($(this)); });
}

var countRecipeVideos = 0;
var countHistoryVideos = 0;
var countCommercialVideos = 0;

$(function() {
    //, onStart: adjustSizeOfScroll
    $("#recipesVideos").scrollable({ items: "div.recipeVidContainer", size: 4, clickable: true, activeClass: 'active', keyboard: false, nextPage: ".recipeNext", prevPage: ".recipePrev", onStart: adjustSizeOfScroll });

    $("#historyVideos").scrollable({ items: "div.historyVidContainer", size: 4, clickable: true, activeClass: 'active', keyboard: false, nextPage: ".historyNext", prevPage: ".historyPrev", onStart: historyAdjustScroll });

    $("#commercialsVideos").scrollable({ items: "div.commercialVidContainer", size: 4, clickable: true, activeClass: 'active', keyboard: false, nextPage: ".commercialNext", prevPage: ".commercialPrev", onStart: commercialAdjustScroll });

    //Start of FlowPlayer setup
	try
	{
		flowplayer("player", "../flash/flowplayer/flowplayer-3.1.5.swf", {
			clip: {
			//baseUrl: "/RecipeManager/Uploads/Videos"
			autoBuffering: true
			},
			plugins: {
				controls: {
					height: 17,
					autoHide: "always"
				}
			}
		});
	}
	catch(err)
	{
	}
//$f("player").ipad().playlist("div.historyVidContainer");
//$f("player").ipad().playlist("div.commercialVidContainer");
//$f("player").ipad().playlist("div.plantainsVidContainer");

/* This area adjusts */
function historyAdjustScroll() {
    var pageIndex = this.getPageIndex();
    var pageNumMax = Math.ceil(countHistoryVideos / 8);

    if (pageIndex == 0 && countHistoryVideos > 8) {
        $("#historyVideos .numberOfVideos").html("1 - 8 of " + countHistoryVideos);
        $("#historyVideos .historyPrev").hide();
        $("#historyVideos .historyNext").show();
    }
    else if (pageIndex < (pageNumMax - 1)) {
        $("#historyVideos .numberOfVideos").html(setIndexHTML(pageIndex, countHistoryVideos));
        $("#historyVideos .historyPrev").show();
        $("#historyVideos .historyNext").show();
    }
    else {
        $("#historyVideos .numberOfVideos").html(setIndexHTML(pageIndex, countHistoryVideos));
        $("#historyVideos .historyNext").hide();
        $("#historyVideos .historyPrev").show();
    }
}

function commercialAdjustScroll() {
    var pageIndex = this.getPageIndex();
    var pageNumMax = Math.ceil(countCommercialVideos / 8);

    if (pageIndex == 0 && countCommercialVideos > 8) {
        $("#commercialsVideos .numberOfVideos").html("1 - 8 of " + countCommercialVideos);
        $("#commercialsVideos .commercialPrev").hide();
        $("#commercialsVideos .commercialNext").show();
    }
    else if (pageIndex < (pageNumMax - 1)) {
        $("#commercialsVideos .numberOfVideos").html(setIndexHTML(pageIndex, countCommercialVideos));
        $("#commercialsVideos .commercialPrev").show();
        $("#commercialsVideos .commercialNext").show();
    }
    else {
        $("#commercialsVideos .numberOfVideos").html(setIndexHTML(pageIndex, countCommercialVideos));
        $("#commercialsVideos .commercialNext").hide();
        $("#commercialsVideos .commercialPrev").show();
    }
}

function adjustSizeOfScroll() {
    var pageIndex = this.getPageIndex();
    var pageNumMax = Math.ceil(countRecipeVideos / 8);

    if (pageIndex == 0 && countRecipeVideos > 8) {
        $("#recipesVideos .numberOfVideos").html("1 - 8 of " + countRecipeVideos);
        $("#recipesVideos .recipePrev").hide();
        $("#recipesVideos .recipeNext").show();
    }
    else if (pageIndex < (pageNumMax - 1)) {
        $("#recipesVideos .numberOfVideos").html(setIndexHTML(pageIndex, countRecipeVideos));
        $("#recipesVideos .recipePrev").show();
        $("#recipesVideos .recipeNext").show();
    }
    else {
        $("#recipesVideos .numberOfVideos").html(setIndexHTML(pageIndex, countRecipeVideos));
        $("#recipesVideos .recipeNext").hide();
        $("#recipesVideos .recipePrev").show();
    }
}


function setIndexHTML(indexNum, videoCount) {
    var indexNumStart = (indexNum * 8) + 1;
    var indexNumEnd = indexNumStart + 7;

    if (indexNumEnd > videoCount) {
        var html = indexNumStart + " - " + videoCount + " of " + videoCount;
    }
    else {
        var html = indexNumStart + " - " + indexNumEnd + " of " + videoCount;
    }
    return html;
}

function prepNumVideos() {
    $("#recipesVideos .recipePrev").hide();
    $("#historyVideos .historyPrev").hide();
    $("#commercialsVideos .commercialPrev").hide();

    //Count number of recipe videos in div and set paging values.
    countRecipeVideos = $(".recipeVidContainer > a").size();
    if (countRecipeVideos > 8) {
        $("#recipesVideos .numberOfVideos").html("1 - 8 of " + countRecipeVideos);
    }
    else {
        $("#recipesVideos .numberOfVideos").html("1 - " + countRecipeVideos + " of " + countRecipeVideos);
    }

    //Count number of history videos in div and set paging values.
    countHistoryVideos = $(".historyVidContainer > a").size();
    if (countHistoryVideos > 8) {
        $("#historyVideos .numberOfVideos").html("1 - 8 of " + countHistoryVideos);
    }
    else {
        $("#historyVideos .numberOfVideos").html("1 - " + countHistoryVideos + " of " + countHistoryVideos);
    }

    //Count number of history videos in div and set paging values.
    countCommercialVideos = $(".commercialVidContainer > a").size();
    if (countCommercialVideos > 8) {
        $("#commercialsVideos .numberOfVideos").html("1 - 8 of " + countCommercialVideos);
    }
    else {
        $("#commercialsVideos .numberOfVideos").html("1 - " + countCommercialVideos + " of " + countCommercialVideos);
    }
};
prepNumVideos();
});



//Tabs switch functionality for the 4 types of videos
$(function() {
    $("#videosHeader li a").each(function() {
        $(this).click(function() {
            removeAllActive();
            $(this).addClass("active");

            showVideoDiv($(this).attr("rel"));
            return false;
        });
    });
});

//Removes the background image that displays the active tab
function removeAllActive() {
    $("#videosHeader li a").each(function() {
        $(this).removeClass("active");
    });
};

function showVideoDiv(divToShow) {
    var divToShowVar = "#" + divToShow;
    $("#videosPlaylist div.TopLevelCatDivs").each(function() {
        $(this).hide();
    });
    $(divToShowVar).fadeIn(500);
};

function getQueryParams(qs) {
    qs = qs.split("+").join(" ");
    var params = {},
        tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
    }

    return params;
}

var $_GET = getQueryParams(document.location.search);
$(document).ready(function() {
	switch($_GET["tab"])
	{
		case "history":
            removeAllActive();
            $('#historyVideosTab').addClass("active");
			$("#videosPlaylist div.TopLevelCatDivs").each(function() {
				$(this).hide();
			});
			$('#historyVideos').show();

			switch($_GET["page"])
			{
				case "2":
					jQuery(".historyNext").click();
					break;
				case "3":
					jQuery(".historyNext").click().click();
					break;
				case "4":
					jQuery(".historyNext").click().click().click();
					break;
				case "5":
					jQuery(".historyNext").click().click().click().click();
					break;
			}
			break;
		case "commercials":
            removeAllActive();
            $('#commercialsVideosTab').addClass("active");
			$("#videosPlaylist div.TopLevelCatDivs").each(function() {
				$(this).hide();
			});
			$('#commercialsVideos').show();
			switch($_GET["page"])
			{
				case "2":
					jQuery(".commercialNext").click();
					break;
				case "3":
					jQuery(".commercialNext").click().click();
					break;
				case "4":
					jQuery(".commercialNext").click().click().click();
					break;
				case "5":
					jQuery(".commercialNext").click().click().click().click();
					break;
			}
			break;
		case "plantains":
            removeAllActive();
            $('#plantainsVideosTab').addClass("active");
			$("#videosPlaylist div.TopLevelCatDivs").each(function() {
				$(this).hide();
			});
			$('#plantainsVideos').show();
			switch($_GET["page"])
			{
				case "2":
					jQuery(".plantainsNext").click();
					break;
				case "3":
					jQuery(".plantainsNext").click().click();
					break;
				case "4":
					jQuery(".plantainsNext").click().click().click();
					break;
				case "5":
					jQuery(".plantainsNext").click().click().click().click();
					break;
			}
			break;
		default:
			switch($_GET["page"])
			{
				case "2":
					jQuery(".recipeNext").click();
					break;
				case "3":
					jQuery(".recipeNext").click().click();
					break;
				case "4":
					jQuery(".recipeNext").click().click().click();
					break;
				case "5":
					jQuery(".recipeNext").click().click().click().click();
					break;
			}
			break;
	}
});
