var userLocation;

$(function () {

    $('#masterTabContent27153_0 .schedule li:gt(15)').remove();

    userLocation = getGeoLocation();

    clockUpdater();
    setInterval('clockUpdater()', 5000);

    if (window.IsLoggedIn && IsLoggedIn()) {
        $(".navigation .item a[href='/page/secure/MyAccount']").show();
        $('.navigation object, .navigation embed').hide();
    } else {  
         $('.subscription, .navigation object, .navigation embed').show();
        $(".subscribe").html('<div class="greenBtn"><div class="greenBtnLeft"><a href="/page/secure/loginOrCreateAccount">Subscribe now</a></div><div class="greenBtnRight"></div></div> or <a href="/page/loginOrBuy">sign in</a> to watch');
    }

    if (GLOBAL_PAGE_BASE_PATH == '/WatchLive') {

        var embedURL;

        if (window.IsLoggedIn && IsLoggedIn()) {
            // Static XML reader: FOXSOCCER_WatchLive_LoggedIn
            embedURL = "/page/elements/html/Home/0,," + GLOBAL_SITE_ID + "~226~10483,00.html";
        } else {
            // Static XML reader: FOXSOCCER_WatchLive_Guest
            embedURL = "/page/elements/html/Home/0,," + GLOBAL_SITE_ID + "~226~10478,00.html";
        }

        $.ajax({
            url: embedURL,
            async: false,
            dataType: "html",
            success: function (data) {
                $(".watchLivePage").html(data);
            }
        });
        
       $('div.liveEvent:nth-child(4n)').addClass('remMargin');

    } else if (GLOBAL_PAGE_BASE_PATH == '/Home') {
        $('.teaserArticle').css('margin-top', '20px');
    }
    
	if ( $('#eventCarousel li').length > 1 ) {
	    $('#eventCarousel').jcarousel({
            auto: 10,
            wrap: null,
            scroll: 1,
            initCallback: evcarousel_initCallback
        });
	}
	
	startCarousel();
   
});

function startCarousel() {
    $(".koTime .countdown").each(function() {
        $(this).countdown({
            type: "dates",
            endDate: $(this).html()
        });
     });
     //$(".koTime .countdown").show()
}

function clockUpdater() {
    var theDate = new Date();

    var htmlContent = userLocation.name + ' / ';
    htmlContent += theDate.format('h:MM TT');

    $('#userLocation').html(htmlContent);
}

function evcarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function () {
        carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('click', function () {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function () {
        carousel.stopAuto();
    }, function () {
        carousel.startAuto();
    });
}

function expandCollapse(num, logic) {
    var query = document.getElementById("q_" + num);
    if (!query) return true;
    if (logic || query.style.display == "none" || query.style.display == "") {
        $(query).show();
    } else {
        $(query).hide();
    }
    return false;
}

function printElement(element) {
    if ((element != "") && (element != null)) {
        w = window.open();
        w.document.write('<style>@media print{#close{display: none;}} #close{display:block;padding-bottom: 50px;}</style>');
        w.document.write('<a href="#" id="close" onclick="window.close()"> Close this window</a>');
        w.document.write($('#' + element).html());
        w.print();
        //w.close();
    }
}

function getPopOut() {
   /*var width = $('.articleVideo').width();
   var height = $('.articleVideo').height();
   var popOut = window.open('','','width='+width+',height='+height+'');*/
   var popOut = window.open();
   if(GLOBAL_PAGE_BASE_PATH =='/CatchUpVideo') {
       alert($('.videoSearch').html());
       popOut.document.write('<style>body{padding:0;margin:0;}form,#results,.videoInfo{display:none;}</style>');
       popOut.document.write($('.videoSearch').html());
   } else {
    alert($('.columnOne').html());
       popOut.document.write('<style>body{padding:0;margin:0;}.videoTitle{display:none;}</style>');
       popOut.document.write($('.columnOne').html());
   }
}

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

/*
    VOD Search Functionality
    Same function is used for search, pagination and listing videos.
*/

function loadVOD(form, page, pageSize) {

    var pageSize = pageSize || 20;
    var searchTerm;
    var gXsltId = 1402;
	

    $(".results").css("display", "none");
    $(".loader").css("display", "block");

    if (GLOBAL_PAGE_BASE_PATH == "/Home") {
		pageSize = 6;
	}
	else if (GLOBAL_PAGE_BASE_PATH.indexOf("/CatchUpVideo") != -1) {
			pageSize = 8;
		}
		
		
    
    // TODO: FIX IF USING PAGINATION TO KEEP SEARCH TERM
    if (!form) {
        searchTerm = "";
    } else {
        searchTerm = $.trim(form.freetext.value) + "*";
    }
	
	//var activeTabId = $('VideoTab:visible')[0].attr('id');
	
	
	
	

    $.ajax({
        url: "/siteSearch/siteSearch/0,," + GLOBAL_SITE_ID + ",00.html?iTypeIds=1,36&1filterIn_pageCategoryId=12016513&36filterIn_videoTypeId=714,738,807&36filterIn_clipCategoryId=18235,18238,18234&sortf=publishedDate&pageSize=" + pageSize + "&pageNumber=" + page + "&gXsltId=" + gXsltId + "&freetext=" + searchTerm,
        async: false,
        cache: false,
        dataType: "html",
        success: vodSuccess,
        error: vodError
    });

    return false;

}

function vodSuccess(data) {

    if (data.indexOf('Invalid request') != -1) {
        data = '<div class="empty">Sorry, no results found</div>';
    }

    $(".loader").css("display", "none");
    $(".results").css("display", "block");

    if (GLOBAL_PAGE_BASE_PATH.indexOf("/CatchUpVideo") != -1) {
        // Scroll down page to show results
        $('html,body').animate({
            scrollTop: $("#results").offset().top
        }, 'slow');
    }

    $("#results").html(data);

}

function vodError() {
    $(".loader").css("display", "none");
    $(".results").css("display", "block");
    $("#results").html('<div class="empty">Error loading content, please refresh the page.</div>');
}


