(function($) {
    $.fn.prepareTabs = function(options) {
        // ppv lower table accordion drawers
        var ppvAnchor = $('#PPVTable>tbody>tr>td>span>a');
        $(ppvAnchor).unbind("click");
        $(ppvAnchor).click(function() {
            var testRow2 = '<tr class="drawer_data"><td colspan="6"><div><img class=loader_img src=/midirectv/App_Themes/MyDirectv/images/loader.gif /></div></td></tr>';
            if ($(testRow2).is(':visible')) {
                // this means the drawer is open so we check if the same drawer is open
                // console.log('test row .drawer_data visible')
            }
            // if the drawer is already open then close it
            var justClicked = $(this).attr('id');
            //console.info($(this).parent().parent().parent().parent().next().html());
            var drawerIdVal = $('.drawer_data').attr('id');
            //console.log('check if drawer exists - ' + $('tr.drawer_data').length);
            var drawerCount = $('tr.drawer_data').length;

            if (drawerCount == 1) {
                //alert('drawer already open' + drawerCount);

                var drawerDataId = $('tr.drawer_data').attr('id');
                // var drawerDataId = '1';
                var drawerDataId2 = $(this).attr('id');

                if (drawerDataId == drawerDataId2) {
                    //	console.info('nothing');
                }
                else {
                    // console.info('open a new drawer');
                    var wrapRow = $(this).parent().parent().parent();
                    // console.log(wrapRow);
                    // add activity loader here
                    var testRow = '<tr class="drawer_data"><td colspan="6"><div>Loading</div></td></tr>';
                    $(wrapRow).addClass('selected');
                    $(testRow).insertAfter(wrapRow);
                    $('.selected').length;
                    $(this).parent().parent().parent();
                }
            }
            if (justClicked == drawerIdVal) {
                $('#PPVTable tr').removeClass('selected')
                $('tr.drawer_data').fadeOut('slow').remove();
                return false;
            }
            else {
                // alert('try	 again')
                //console.log('close up the prev and open the new')
                $('tr.drawer_data').fadeOut(function() {
                    //	console.log('not working when drawer is removed')
                    //	$('tr.drawer_data').remove();
                });
                $('tr').removeClass('selected');
                //alert('open the new drawer or no drawer open')
                //console.info('her eis : ' + $(this).attr('id'))
                var testRow = '<tr class="drawer_data"><td colspan="6"><div>Loading..</div></td></tr>';
                //console.log(testRow)
                var selectedRow = $(this).parents('tr');
                //console.info(selectedRow)
                $(selectedRow).addClass('selected');
                // console.info('now get drawer inserted')
            }
            $('tr.drawer_data').fadeOut('slow').remove();

            // grab unique id of film
            var filmIdVal = $(this).attr('id');
            var drawer = $('.drawer_data');
            //console.info('drawer status: ' + drawer)
            if (drawer.length == 1) {
                //console.log('here')


                // drawer open so, check the id vals match and do something else

                $('tr.drawer_data').fadeOut('slow', function() {
                    $('tr.drawer_data').fadeOut('slow');

                    // will echo each existing drawer
                    $('tr.drawer_data').length;
                    return false;
                });
                $('#PPVTable tr').removeClass('selected')


                if ($(testRow2).is(':visible')) {

                }
                else {
                    // console.log('is not vis')
                }

                return false;
            }

            else {
                // keep running - insert the drawer
            }
            // point to the row we need
            var selectedRow = $(this).parents('tr');


            // create testing row 	
            var testRow = '<tr class="drawer_data"><td colspan="6"><div>Loading..</div></td></tr>';

            // remove selected class from history items
            // add a class to track which is selected
            $(selectedRow).addClass('selected');

            // insert the test row after the selected row
            $('tr.drawer_data').length;
            $(testRow).insertAfter('#PPVTable_wrapper>#PPVTable .selected');
            $('.drawer_data').attr({
                id: filmIdVal
            });
            // load the data into the new drawer and fix styles declared in js from datagrid plugin
            var loadedInfo = "PPVTitleDetails.aspx?id=" + filmIdVal;
            $('.drawer_data>td').css({
                background: "#fff",
                margin: "0",
                padding: "0"
            });
            $('.drawer_data>td>div').load(loadedInfo).slideDown();

            return false;
        });
    }

    $.fn.cuenta = function(options) {

        //LANGUAGE.
        var domLocation = $('div#header>ul#topnav-right>li>a').eq(0);
        var languageChooser = domLocation.text().toLowerCase();

        var lblSports = "Deportes";
        var lblAdults = "Adultos";

      

        //Read Query String for options.
        var url = location.search;
        var defaultOpenedTab = $.query.get('section');




        // fix paginated nav on ppv browse
        $('.dataTables_paginate>span').eq(1).addClass('mid_nav');

        function generateNumberMovieTabs() {
            var tabHolder = $('div.tabs>ul');

            // check for existence of 2nd and 3rd rows
            var carouselRow1 = $('ul#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_first_carousel img');
            var carouselRow2 = $('ul#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_second_carousel img');
            var carouselRow3 = $('ul#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel img');
            var getCar2 = carouselRow2.length;
            var getCar3 = carouselRow3.length;
            //console.debug(getCar2);
            //console.debug(getCar3);

            if (getCar2 > 0) {
                $('div.tabs>ul>li').eq(0).show();
            } else if (getCar2 <= 0 && defaultOpenedTab == "adults") {
                defaultOpenedTab = "";
            }
            if (getCar3 > 0) {
                $('div.tabs>ul>li').eq(1).show();
            } else if (getCar3 <= 0 && defaultOpenedTab == "sports") {
                defaultOpenedTab = "";
            }


        };
        generateNumberMovieTabs();

        function dyniNav() {


            var item = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_first_carousel>li');
            var navContainer = $('div.jcarousel-control1');
            var itemLength = item.length;
            var itemDivided = itemLength / 4;


            if ((itemDivided == '.25') || (itemDivided == '1.25') || (itemDivided == '2.25') || (itemDivided == '3.25')) {
                itemDivided = itemDivided + .5
            }
            var findCarouselBtns = itemDivided + .10;
            var totalBtns = Math.round(itemDivided);

            $(navContainer).empty();
            navBtn = '<a class="nav1" href="#"><span></span>1</a>';
            i = 0;
            for (i = 0; i < totalBtns; i++) {
                $(navContainer).prepend(navBtn);
            }

            function insTextNode() {
                tempAnchorElem = $('div.jcarousel-control1 a').eq(0);
                anchorContents = '<span></span>1';
                $(tempAnchorElem).empty().prepend(anchorContents);

                tempAnchorElem1 = $('div.jcarousel-control1 a').eq(1);
                if (tempAnchorElem1) {
                    anchorContents1 = '<span></span>5';
                    $(tempAnchorElem1).empty().prepend(anchorContents1).removeClass('nav1').addClass('nav2');
                }
                tempAnchorElem2 = $('div.jcarousel-control1 a').eq(2);
                anchorContents2 = '<span></span>9';
                $(tempAnchorElem2).empty().prepend(anchorContents2).removeClass('nav1').addClass('nav3');

                tempAnchorElem3 = $('div.jcarousel-control1 a').eq(3);
                anchorContents3 = '<span></span>13';
                $(tempAnchorElem3).empty().prepend(anchorContents3).removeClass('nav1').addClass('nav4');

                tempAnchorElem4 = $('div.jcarousel-control1 a').eq(4);
                anchorContents4 = '<span></span>17';
                $(tempAnchorElem4).empty().prepend(anchorContents4).removeClass('nav1').addClass('nav5');

                tempAnchorElem5 = $('div.jcarousel-control1 a').eq(5);
                if (tempAnchorElem5) {
                    anchorContents5 = '<span></span>21';
                    $(tempAnchorElem5).empty().prepend(anchorContents5).removeClass('nav1').addClass('nav6');
                }

            }
            insTextNode();
        }
        dyniNav();


        function dyniNav2() {
            var item = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_second_carousel>li');
            //console.log(item.length)
            var navContainer = $('div.jcarousel-control2');
            var itemLength = item.length;
            var itemDivided = itemLength / 4;

            if ((itemDivided == '.25') || (itemDivided == '1.25') || (itemDivided == '2.25') || (itemDivided == '3.25')) {
                itemDivided = itemDivided + .5

            }

            var findCarouselBtns = itemDivided + .10;
            var totalBtns = Math.round(itemDivided);

            // console.log(totalBtns);
            $(navContainer).empty();
            navBtn = '<a class="nav1" href="#"><span></span>1</a>';
            i = 0;
            for (i = 0; i < totalBtns; i++) {
                $('div.jcarousel-control2').prepend(navBtn);
            }

            function insTextNode2() {
                tempAnchorElem = $('div.jcarousel-control2 a').eq(0);
                anchorContents = '<span></span>1';
                $(tempAnchorElem).empty().prepend(anchorContents);

                tempAnchorElem1 = $('div.jcarousel-control2 a').eq(1);
                if (tempAnchorElem1) {
                    anchorContents1 = '<span></span>5';
                    $(tempAnchorElem1).empty().prepend(anchorContents1).removeClass('nav1').addClass('nav2');

                }

                tempAnchorElem2 = $('div.jcarousel-control2 a').eq(2);
                anchorContents2 = '<span></span>9';
                $(tempAnchorElem2).empty().prepend(anchorContents2).removeClass('nav1').addClass('nav3');

                tempAnchorElem3 = $('div.jcarousel-control2 a').eq(3);
                anchorContents3 = '<span></span>13';
                $(tempAnchorElem3).empty().prepend(anchorContents3).removeClass('nav1').addClass('nav4');

                tempAnchorElem4 = $('div.jcarousel-control2 a').eq(4);
                anchorContents4 = '<span></span>17';
                $(tempAnchorElem4).empty().prepend(anchorContents4).removeClass('nav1').addClass('nav5');

                tempAnchorElem5 = $('div.jcarousel-control2 a').eq(5);
                if (tempAnchorElem5) {
                    anchorContents5 = '<span></span>21';
                    $(tempAnchorElem5).empty().prepend(anchorContents5).removeClass('nav1').addClass('nav6');
                }

                $(tempAnchorElem).click(function() {
                    //console.log('live function executed');
                });
            }
            insTextNode2();
        }
        dyniNav2();



        function dyniNav3() {
            var item = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel>li');
            //console.log(item.length)
            var navContainer = $('div.jcarousel-control3');
            var itemLength = item.length;
            var itemDivided = itemLength / 4;

            if ((itemDivided == '.25') || (itemDivided == '1.25') || (itemDivided == '2.25') || (itemDivided == '3.25')) {
                itemDivided = itemDivided + .5

            }

            var findCarouselBtns = itemDivided + .10;
            var totalBtns = Math.round(itemDivided);

            // console.log(totalBtns);
            $(navContainer).empty();
            navBtn = '<a class="nav1" href="#"><span></span>1</a>';
            i = 0;
            for (i = 0; i < totalBtns; i++) {
                $('div.jcarousel-control3').prepend(navBtn);
            }

            function insTextNode3() {
                tempAnchorElem = $('div.jcarousel-control3 a').eq(0);
                anchorContents = '<span></span>1';
                $(tempAnchorElem).empty().prepend(anchorContents);

                tempAnchorElem1 = $('div.jcarousel-control3 a').eq(1);
                if (tempAnchorElem1) {
                    anchorContents1 = '<span></span>5';
                    $(tempAnchorElem1).empty().prepend(anchorContents1).removeClass('nav1').addClass('nav2');

                }

                tempAnchorElem2 = $('div.jcarousel-control3 a').eq(2);
                anchorContents2 = '<span></span>9';
                $(tempAnchorElem2).empty().prepend(anchorContents2).removeClass('nav1').addClass('nav3');

                tempAnchorElem3 = $('div.jcarousel-control3 a').eq(3);
                anchorContents3 = '<span></span>13';
                $(tempAnchorElem3).empty().prepend(anchorContents3).removeClass('nav1').addClass('nav4');

                tempAnchorElem4 = $('div.jcarousel-control3 a').eq(4);
                anchorContents4 = '<span></span>17';
                $(tempAnchorElem4).empty().prepend(anchorContents4).removeClass('nav1').addClass('nav5');

                tempAnchorElem5 = $('div.jcarousel-control3 a').eq(5);
                if (tempAnchorElem5) {
                    anchorContents5 = '<span></span>21';
                    $(tempAnchorElem5).empty().prepend(anchorContents5).removeClass('nav1').addClass('nav6');
                }

                $(tempAnchorElem).click(function() {
                    //console.log('live function executed');
                });

            }
            insTextNode3();
        }
        dyniNav3();

        // activity loader added for upper ppv carousel
        $('#one_ppv_info').prepend('<style>.loader_img{margin:129px 0 0 305px}</style><img class=loader_img src=/midirectv/App_Themes/MyDirectv/images/loader.gif />');
        $('#wrap').css({
            padding: "0"
        });

        // fix width of genre column - ppv browse page
        /*$('.genre_col').css({
        width: "120px"
        });*/

        function removeSelectedNav() {
            $('.quick_nav a').removeClass('ppvClicked');
            $('.quick_nav a').removeClass('ppvClickedFirst');
        };

        function addBothBrowserTypes() {
            $('.jcarousel-control1>a').eq(0).addClass('ppvClicked');

            $('.jcarousel-control1>a').eq(0).addClass('ppvClickedFirst');
        };
        function addBothBrowserTypes2() {
            $('.jcarousel-control2>a').eq(0).addClass('ppvClicked');
            $('.jcarousel-control2>a').eq(0).addClass('ppvClickedFirst');
        };
        function addBothBrowserTypes3() {
            $('.jcarousel-control3>a').eq(0).addClass('ppvClicked');
            $('.jcarousel-control3>a').eq(0).addClass('ppvClickedFirst');
        };

        /********************************************************************************************************************************************************/
        //////////////////////////////////////////////////////// CAROUSEL EVENTS /////////////////////////////////////////////////////////////////////////////////
        /********************************************************************************************************************************************************/

        function mycarousel_itemFirstInCallback(carousel) {
            elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_first_carousel').css('left');
            //console.debug(elementPosition)
            rowLocation = $('.jcarousel-container').css('top');

            if (rowLocation == '0px' || rowLocation == "auto") {

                if (elementPosition == '0px') {
                    removeSelectedNav();
                    $('.jcarousel-control1>a').eq(0).addClass('ppvClicked');
                }
                if ((elementPosition == '-153px') || (elementPosition == '-306px') || (elementPosition == '-459px') || (elementPosition == '-612px')) {
                    removeSelectedNav();
                    $('.jcarousel-control1>a').eq(1).addClass('ppvClicked');
                }
                if ((elementPosition == '-765px') || (elementPosition == '-918px') || (elementPosition == '-1071px') || (elementPosition == '-1224px')) {
                    removeSelectedNav();
                    $('.jcarousel-control1>a').eq(2).addClass('ppvClicked');
                }
                if ((elementPosition == '-1377px') || (elementPosition == '-1530px') || (elementPosition == '-1683px') || (elementPosition == '-1836px')) {
                    removeSelectedNav();
                    $('.jcarousel-control1>a').eq(3).addClass('ppvClicked');
                }


                function fixButtonsInIE() {
                    button = $('#btn_nav1')
                    var buttonClasses = $(button).attr('class')

                    returnVal = buttonClasses.search(/ppv/);

                    if (returnVal == 11) {
                        $('#btn_nav1').css({
                            backgroundPosition: "0 0!important"
                        });
                    }

                }
                // script stops after this function is executed
                // fixButtonsInIE();

                // for IE 6 - carousel position is different
                if (elementPosition == '0') {
                    removeSelectedNav();
                    $('.jcarousel-control1>a#btn_nav1').addClass('ppvClickedFirst');
                }
                if (elementPosition == '-624px') {
                    removeSelectedNav();
                    $('.jcarousel-control1>a').eq(1).addClass('ppvClicked');
                }
                if (elementPosition == '-1248px') {
                    removeSelectedNav();
                    $('.jcarousel-control1>a').eq(2).addClass('ppvClicked');
                }
                if (elementPosition == '-1716px') {
                    removeSelectedNav();
                    $('.jcarousel-control1>a').eq(3).addClass('ppvClicked');
                }
                if (elementPosition == '-1872px') {
                    removeSelectedNav();
                    $('.jcarousel-control1>a').eq(3).addClass('ppvClicked');
                }
                if (elementPosition == '-2496px') {
                    removeSelectedNav();
                    $('.jcarousel-control1>a').eq(4).addClass('ppvClicked');
                }
            }
        }
        function mycarousel_itemFirstInCallback2(carousel) {
            elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_second_carousel').css('left');
            rowLocation = $('.jcarousel-container').css('top');
            if (rowLocation == '-225px') {
                if (elementPosition == '0px') {
                    removeSelectedNav();
                    addBothBrowserTypes2();
                }
                if ((elementPosition == '-153px') || (elementPosition == '-306px') || (elementPosition == '-459px') || (elementPosition == '-612px')) {
                    removeSelectedNav();
                    $('.jcarousel-control2>a').eq(1).addClass('ppvClicked');
                }
                if ((elementPosition == '-765px') || (elementPosition == '-918px') || (elementPosition == '-1071px') || (elementPosition == '-1224px')) {
                    removeSelectedNav();
                    $('.jcarousel-control2>a').eq(2).addClass('ppvClicked');
                }
                if ((elementPosition == '-1377px') || (elementPosition == '-1530px') || (elementPosition == '-1683px') || (elementPosition == '-1836px')) {
                    removeSelectedNav();
                    $('.jcarousel-control2>a').eq(3).addClass('ppvClicked');
                }
            }

            // for IE 6 - carousel position is different
            if (elementPosition == '0') {
                removeSelectedNav();
                addBothBrowserTypes();
            }
            if (elementPosition == '-624px') {
                removeSelectedNav();
                $('.jcarousel-control2>a').eq(1).addClass('ppvClicked');
            }
            if (elementPosition == '-1248px') {
                removeSelectedNav();
                $('.jcarousel-control2>a').eq(2).addClass('ppvClicked');
            }
            if (elementPosition == '-1716px') {
                removeSelectedNav();
                $('.jcarousel-control2>a').eq(3).addClass('ppvClicked');
            }
            if (elementPosition == '-1872px') {
                removeSelectedNav();
                $('.jcarousel-control2>a').eq(3).addClass('ppvClicked');
            }
            if (elementPosition == '-2496px') {
                removeSelectedNav();
                $('.jcarousel-control2>a').eq(4).addClass('ppvClicked');
            }

        }
        function mycarousel_itemFirstInCallback3(carousel) {
            elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel').css('left');
            if (elementPosition == '0px') {
                removeSelectedNav();
                $('.jcarousel-control3>a').eq(0).addClass('ppvClicked');
            }
            if ((elementPosition == '-153px') || (elementPosition == '-306px') || (elementPosition == '-459px') || (elementPosition == '-612px')) {
                removeSelectedNav();
                $('.jcarousel-control3>a').eq(1).addClass('ppvClicked');
            }
            if ((elementPosition == '-765px') || (elementPosition == '-918px') || (elementPosition == '-1071px') || (elementPosition == '-1224px')) {
                removeSelectedNav();
                $('.jcarousel-control3>a').eq(2).addClass('ppvClicked');
            }
            if ((elementPosition == '-1377px') || (elementPosition == '-1530px') || (elementPosition == '-1683px') || (elementPosition == '-1836px')) {
                removeSelectedNav();
                $('.jcarousel-control3>a').eq(3).addClass('ppvClicked');
            }
            // for IE 6 - carousel position is different
            if (elementPosition == '0') {
                removeSelectedNav();
                addBothBrowserTypes();
            }
            if (elementPosition == '-624px') {
                removeSelectedNav();
                $('.jcarousel-control3>a').eq(1).addClass('ppvClicked');
            }
            if (elementPosition == '-1248px') {
                removeSelectedNav();
                $('.jcarousel-control3>a').eq(2).addClass('ppvClicked');
            }
            if (elementPosition == '-1716px') {
                removeSelectedNav();
                $('.jcarousel-control3>a').eq(3).addClass('ppvClicked');
            }
            if (elementPosition == '-1872px') {
                removeSelectedNav();
                $('.jcarousel-control3>a').eq(3).addClass('ppvClicked');
            }
            if (elementPosition == '-2496px') {
                removeSelectedNav();
                $('.jcarousel-control3>a').eq(4).addClass('ppvClicked');
            }
        };

        /********************************************************************************************************************************************************/
        /////////////////////////////////////////////////////// ITEM CALL BACK ///////////////////////////////////////////////////////////////////////////////////
        /********************************************************************************************************************************************************/
        function mycarousel_initCallback1(carousel) {
            $('.jcarousel-control1 a').bind('click', function() {

                carousel.scroll($.jcarousel.intval($(this).text()));
                return false;
            });
        };
        function mycarousel_initCallback2(carousel) {

            $('.jcarousel-control2 a').bind('click', function() {

                carousel.scroll($.jcarousel.intval($(this).text()));
                return false;
            });
        };
        function mycarousel_initCallback3(carousel) {
            $('.jcarousel-control3 a').bind('click', function() {
                carousel.scroll($.jcarousel.intval($(this).text()));
                return false;
            });
        };

        /********************************************************************************************************************************************************/
        //////////////////////////////////////////////////////////// NEXT CALL BACK //////////////////////////////////////////////////////////////////////////////
        /********************************************************************************************************************************************************/

        function mycarousel_buttonNextCallback(carousel) {
            $('.jcarousel-control1 a').bind('click', function() {

                return false;
            });
        };

        /*****************************************************************************************************************************************************/
        ////////////////////////////////////////////////////// ITEM LOAD CALL BACK ////////////////////////////////////////////////////////////////////////////
        /*****************************************************************************************************************************************************/
        function mycarousel_itemLoadCallback(carousel) {
        }
        function mycarousel_itemLoadCallback2(carousel) {
            if (defaultOpenedTab.toLowerCase() == "adults") {
                callCarousel1();
            }
        }
        function mycarousel_itemLoadCallback3(carousel) {
            if (defaultOpenedTab.toLowerCase() == "sports") {
                callCarousel2();
            }
        }

        /***************************************************************************************************************************************************/
        ////////////////////////////////////// AUX FUNCTIONS - MOVE ACTIVE CAROUSEL  ////////////////////////////////////////////////////////////////////////
        /***************************************************************************************************************************************************/
        function callCarousel1() {

            var catTab = $('#one,#two,#three');
            $(catTab).parent().removeClass('selected');
            $('#two').parent().addClass('selected');

            elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel').css('left');
            if (elementPosition == '0px') {
                $('.quick_nav a').removeClass('ppvClicked');
                $('.quick_nav a').removeClass('ppvClickedFirst');
                $('.jcarousel-control3>a').eq(0).addClass('ppvClicked').addClass('ppvClickedFirst');
            }

            var ppvContainer = $('.jcarousel-container');
            var quickNav1 = $('.jcarousel-control1');
            var quickNav2 = $('.jcarousel-control2');
            var quickNav3 = $('.jcarousel-control3');


            $('.jcarousel-prev,.jcarousel-next').hide();
            setTimeout("$('.jcarousel-prev,.jcarousel-next').fadeIn('slow');", 1000);
            $(ppvContainer).animate({
                top: '0px'
            }, 1000);
            $(quickNav2, quickNav3).hide();
            $('.quick_nav').hide();
            $(quickNav1).show();
            $('.jcarousel-control1').css({ //top: "-10px"
            //top: "-10px"
        });
        $(quickNav1).show();

        elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_first_carousel').css('left');
        //console.debug(elementPosition)
        rowLocation = $('.jcarousel-container').css('top');
        //console.log('first row')

        if (elementPosition == '0px') {
            removeSelectedNav();
            $('.jcarousel-control1>a').eq(0).addClass('ppvClicked');
            //alert('found')
        }
        if ((elementPosition == '-153px') || (elementPosition == '-306px') || (elementPosition == '-459px') || (elementPosition == '-612px')) {
            removeSelectedNav();
            $('.jcarousel-control1>a').eq(1).addClass('ppvClicked');
        }
        if ((elementPosition == '-765px') || (elementPosition == '-918px') || (elementPosition == '-1071px') || (elementPosition == '-1224px')) {
            removeSelectedNav();
            $('.jcarousel-control1>a').eq(2).addClass('ppvClicked');
        }
        if ((elementPosition == '-1377px') || (elementPosition == '-1530px') || (elementPosition == '-1683px') || (elementPosition == '-1836px')) {
            removeSelectedNav();
            $('.jcarousel-control1>a').eq(3).addClass('ppvClicked');
        }

           $('.page_title_ppv').replaceWith("<div class=\"page_title_ppv page_title_ppv_adults\"><div><span><a href=\"LandingPage.aspx\">Inicio</a>&gt;</span><span class=\"current\">" + lblAdults + "</span></div><h2>" + lblAdults + "</h2></div>");


    }
    function callCarousel2() {
        var catTab = $('#one,#two,#three');
        $(catTab).parent().removeClass('selected');
        $('#three').parent().addClass('selected');

        elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel').css('left');
        if (elementPosition == '0px') {
            $('.quick_nav a').removeClass('ppvClicked');
            $('.quick_nav a').removeClass('ppvClickedFirst');
            $('.jcarousel-control3>a').eq(0).addClass('ppvClicked').addClass('ppvClickedFirst');
        }

        var ppvContainer = $('.jcarousel-container');
        var quickNav1 = $('.jcarousel-control1');
        var quickNav2 = $('.jcarousel-control2');
        var quickNav3 = $('.jcarousel-control3');


        $('.jcarousel-prev,.jcarousel-next').hide();
        setTimeout("$('.jcarousel-prev,.jcarousel-next').fadeIn('slow');", 1000);

        $(ppvContainer).animate({
            top: '-225px'
        }, 1000);
        $(quickNav1, quickNav3).hide();
        $('.quick_nav').hide();
        $(quickNav2).show();
        $('.jcarousel-control2').css({ //top: "-10px"
        //top: "-10px"
    });
    $(quickNav2).show();
    elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_second_carousel').css('left');
    rowLocation = $('.jcarousel-container').css('top');
    if (elementPosition == '0px') {
        removeSelectedNav();
        $('.jcarousel-control2>a').eq(0).addClass('ppvClicked');
        //alert('found')
    }
    if ((elementPosition == '-153px') || (elementPosition == '-306px') || (elementPosition == '-459px') || (elementPosition == '-612px')) {
        removeSelectedNav();
        $('.jcarousel-control2>a').eq(1).addClass('ppvClicked');
    }
    if ((elementPosition == '-765px') || (elementPosition == '-918px') || (elementPosition == '-1071px') || (elementPosition == '-1224px')) {
        removeSelectedNav();
        $('.jcarousel-control2>a').eq(2).addClass('ppvClicked');
    }
    if ((elementPosition == '-1377px') || (elementPosition == '-1530px') || (elementPosition == '-1683px') || (elementPosition == '-1836px')) {
        removeSelectedNav();
        $('.jcarousel-control2>a').eq(3).addClass('ppvClicked');
    }

       $('.page_title_ppv').replaceWith("<div class=\"page_title_ppv page_title_ppv_sports\"><div><span><a href=\"LandingPage.aspx\">Inicio</a>&gt;</span><span class=\"current\">" + lblSports + "</span></div><h2>" + lblSports + "</h2></div>");

}
function callCarousel3() {

    var catTab = $('#one,#two,#three');

    $(catTab).parent().removeClass('selected');
    $('#three').parent().addClass('selected');

    elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel').css('left');
    if (elementPosition == '0px') {
        $('.quick_nav a').removeClass('ppvClicked');
        $('.quick_nav a').removeClass('ppvClickedFirst');
        $('.jcarousel-control3>a').eq(0).addClass('ppvClicked').addClass('ppvClickedFirst');
    }

    var ppvContainer = $('.jcarousel-container');
    var quickNav1 = $('.jcarousel-control1');
    var quickNav2 = $('.jcarousel-control2');
    var quickNav3 = $('.jcarousel-control3');

    $('.jcarousel-prev,.jcarousel-next').hide();
    setTimeout("$('.jcarousel-prev,.jcarousel-next').show();", 1000);
    var value = $('#three').attr("rel");
    ///alert("value = " + value);
    $(ppvContainer).animate({
        top: value
    }, 1000);
    $(quickNav1, quickNav2).hide();
    $('.quick_nav').hide();
    $(quickNav3).show();


    elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel').css('left');
    //console.debug(elementPosition)
    rowLocation = $('.jcarousel-container').css('top');
    //console.log('first row')

    if (elementPosition == '0px') {
        $('.quick_nav a').removeClass('ppvClicked');
        $('.quick_nav a').removeClass('ppvClickedFirst');
        $('.jcarousel-control3>a').eq(0).addClass('ppvClicked');
        //alert('found')
    }
    if ((elementPosition == '-153px') || (elementPosition == '-306px') || (elementPosition == '-459px') || (elementPosition == '-612px')) {
        $('.quick_nav a').removeClass('ppvClicked');
        $('.quick_nav a').removeClass('ppvClickedFirst');
        $('.jcarousel-control3>a').eq(1).addClass('ppvClicked');
    }
    if ((elementPosition == '-765px') || (elementPosition == '-918px') || (elementPosition == '-1071px') || (elementPosition == '-1224px')) {
        $('.quick_nav a').removeClass('ppvClicked');
        $('.quick_nav a').removeClass('ppvClickedFirst');
        $('.jcarousel-control3>a').eq(2).addClass('ppvClicked');
    }
    if ((elementPosition == '-1377px') || (elementPosition == '-1530px') || (elementPosition == '-1683px') || (elementPosition == '-1836px')) {
        $('.quick_nav a').removeClass('ppvClicked');
        $('.quick_nav a').removeClass('ppvClickedFirst');
        $('.jcarousel-control3>a').eq(3).addClass('ppvClicked');
    }

    $('.page_title_ppv').replaceWith("<div class=\"page_title_ppv page_title_ppv_sports\"><div><span><a href=\"LandingPage.aspx\">Inicio</a>&gt;</span><span class=\"current\">" + lblSports + "</span></div><h2>" + lblSports + "</h2></div>");
}
/*******************************************************************************************************************************************/
/////////////////////////////////////////////// INSTANTIATE THE CAROUSEL FUNCIONALITY ///////////////////////////////////////////////////////
/*******************************************************************************************************************************************/
$('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_first_carousel').jcarousel({
    wrap: null,
    initCallback: mycarousel_initCallback1,
    itemFirstInCallback: mycarousel_itemFirstInCallback,
    buttonNextCallback: mycarousel_buttonNextCallback,
    itemLoadCallback: mycarousel_itemLoadCallback
});
$('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_second_carousel').jcarousel({
    wrap: null,
    initCallback: mycarousel_initCallback2,
    itemFirstInCallback: mycarousel_itemFirstInCallback2,
    itemLoadCallback: mycarousel_itemLoadCallback2
});
$('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel').jcarousel({
    wrap: null,
    initCallback: mycarousel_initCallback3,
    itemFirstInCallback: mycarousel_itemFirstInCallback3,
    itemLoadCallback: mycarousel_itemLoadCallback3
});

// add sliding function for movie cats
var ppvContainer = $('.jcarousel-container');
var quickNav1 = $('.jcarousel-control1');
var quickNav2 = $('.jcarousel-control2');
var quickNav3 = $('.jcarousel-control3');



if (defaultOpenedTab.toLowerCase() == "sports") {
    $(quickNav1).hide();
    $(quickNav2).hide();
    $(quickNav3).show();
} else if (defaultOpenedTab.toLowerCase() == "adults") {
    $(quickNav1).hide();
    $(quickNav2).show();
    $(quickNav3).hide();
} else {
    $(quickNav1).show();
    $(quickNav2).hide();
    $(quickNav3).hide();
}

/**************************************************************************************************************************************************************/
///////////////////////////////////////////////////// CAROUSEL TABS CLICK EVENT FUNCTIONS //////////////////////////////////////////////////////////////////////
/**************************************************************************************************************************************************************/
$('#one').click(function() {
    callCarousel1();

    //        $('.jcarousel-prev,.jcarousel-next').hide();
    //        setTimeout("$('.jcarousel-prev,.jcarousel-next').fadeIn('slow');", 1000);
    //        $(ppvContainer).animate({
    //            top: '0px'
    //        }, 1000);
    //        $(quickNav2, quickNav3).hide();
    //        $('.quick_nav').hide();
    //        $(quickNav1).show();
    //        $('.jcarousel-control1').css({ //top: "-10px"
    //        //top: "-10px"
    //    });
    //    $(quickNav1).show();

    //    elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_first_carousel').css('left');
    //    //console.debug(elementPosition)
    //    rowLocation = $('.jcarousel-container').css('top');
    //    //console.log('first row')

    //    if (elementPosition == '0px') {
    //        removeSelectedNav();
    //        $('.jcarousel-control1>a').eq(0).addClass('ppvClicked');
    //        //alert('found')
    //    }
    //    if ((elementPosition == '-153px') || (elementPosition == '-306px') || (elementPosition == '-459px') || (elementPosition == '-612px')) {
    //        removeSelectedNav();
    //        $('.jcarousel-control1>a').eq(1).addClass('ppvClicked');
    //    }
    //    if ((elementPosition == '-765px') || (elementPosition == '-918px') || (elementPosition == '-1071px') || (elementPosition == '-1224px')) {
    //        removeSelectedNav();
    //        $('.jcarousel-control1>a').eq(2).addClass('ppvClicked');
    //    }
    //    if ((elementPosition == '-1377px') || (elementPosition == '-1530px') || (elementPosition == '-1683px') || (elementPosition == '-1836px')) {
    //        removeSelectedNav();
    //        $('.jcarousel-control1>a').eq(3).addClass('ppvClicked');
    //    }

    //    $('.page_title_ppv').replaceWith("<div class=\"page_title_ppv\"><div><span><a href=\"LandingPage.aspx\">Inicio</a>&gt;</span><span class=\"current\">DIRECTV Cinema</span></div><h2>DIRECTV Cinema</h2></div>");

});


$('#two').click(function() {

    callCarousel1();


    //        $('.jcarousel-prev,.jcarousel-next').hide();
    //        setTimeout("$('.jcarousel-prev,.jcarousel-next').fadeIn('slow');", 1000);

    //        $(ppvContainer).animate({
    //            top: '-225px'
    //        }, 1000);
    //        $(quickNav1, quickNav3).hide();
    //        $('.quick_nav').hide();
    //        $(quickNav2).show();
    //        $('.jcarousel-control2').css({ //top: "-10px"
    //        //top: "-10px"
    //    });
    //    $(quickNav2).show();
    //    elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_second_carousel').css('left');
    //    //console.debug(elementPosition)
    //    rowLocation = $('.jcarousel-container').css('top');
    //    //console.log('first row')

    //    if (elementPosition == '0px') {
    //        removeSelectedNav();
    //        $('.jcarousel-control2>a').eq(0).addClass('ppvClicked');
    //        //alert('found')
    //    }
    //    if ((elementPosition == '-153px') || (elementPosition == '-306px') || (elementPosition == '-459px') || (elementPosition == '-612px')) {
    //        removeSelectedNav();
    //        $('.jcarousel-control2>a').eq(1).addClass('ppvClicked');
    //    }
    //    if ((elementPosition == '-765px') || (elementPosition == '-918px') || (elementPosition == '-1071px') || (elementPosition == '-1224px')) {
    //        removeSelectedNav();
    //        $('.jcarousel-control2>a').eq(2).addClass('ppvClicked');
    //    }
    //    if ((elementPosition == '-1377px') || (elementPosition == '-1530px') || (elementPosition == '-1683px') || (elementPosition == '-1836px')) {
    //        removeSelectedNav();
    //        $('.jcarousel-control2>a').eq(3).addClass('ppvClicked');
    //    }

    //    //$('.page_title_ppv').replaceWith("<div class=\"page_title_ppv\"><div><span><a href=\"LandingPage.aspx\">Inicio</a>&gt;</span><span class=\"current\">DIRECTV Pay Per VIew</span></div><h2>DIRECTV Pay Per VIew</h2></div>");
    //    $('.page_title_ppv').replaceWith("<div class=\"page_title_ppv page_title_ppv_adults\"><div><span><a href=\"LandingPage.aspx\">Inicio</a>&gt;</span><span class=\"current\">Adulto</span></div><h2>Adulto</h2></div>");


});

$('#three').click(function() {


    callCarousel2();

    //    $('.jcarousel-prev,.jcarousel-next').hide();
    //    setTimeout("$('.jcarousel-prev,.jcarousel-next').show();", 1000);
    //    var value = $('#three').attr("rel");
    //    ///alert("value = " + value);
    //    $(ppvContainer).animate({
    //        top: value
    //    }, 1000);
    //    $(quickNav1, quickNav2).hide();
    //    $('.quick_nav').hide();
    //    $(quickNav3).show();

    //    elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel').css('left');
    //    //console.debug(elementPosition)
    //    rowLocation = $('.jcarousel-container').css('top');
    //    //console.log('first row')

    //    if (elementPosition == '0px') {
    //        removeSelectedNav();
    //        $('.jcarousel-control3>a').eq(0).addClass('ppvClicked');
    //        //alert('found')
    //    }
    //    if ((elementPosition == '-153px') || (elementPosition == '-306px') || (elementPosition == '-459px') || (elementPosition == '-612px')) {
    //        removeSelectedNav();
    //        $('.jcarousel-control3>a').eq(1).addClass('ppvClicked');
    //    }
    //    if ((elementPosition == '-765px') || (elementPosition == '-918px') || (elementPosition == '-1071px') || (elementPosition == '-1224px')) {
    //        removeSelectedNav();
    //        $('.jcarousel-control3>a').eq(2).addClass('ppvClicked');
    //    }
    //    if ((elementPosition == '-1377px') || (elementPosition == '-1530px') || (elementPosition == '-1683px') || (elementPosition == '-1836px')) {
    //        removeSelectedNav();
    //        $('.jcarousel-control3>a').eq(3).addClass('ppvClicked');
    //    }
    //    $('.page_title_ppv').replaceWith("<div class=\"page_title_ppv page_title_ppv_sports\"><div><span><a href=\"LandingPage.aspx\">Inicio</a>&gt;</span><span class=\"current\">Deportes</span></div><h2>Deportes</h2></div>");


});

// selected movie cat function
var catTab = $('#one,#two,#three');
$(catTab).click(function() {
    $(catTab).parent().removeClass('selected');
    $(this).parent().addClass('selected');

    // find ppv movie location
    var clickedCat = $(this).attr('id');
    if (clickedCat == 'one') {
        elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_first_carousel').css('left');
        if (elementPosition == '0px') {
            removeSelectedNav();
            $('.jcarousel-control1>a').eq(0).addClass('ppvClicked').addClass('ppvClickedFirst');
        }
    }
    var clickedCat = $(this).attr('id');
    //console.log(clickedCat)
    if (clickedCat == 'two') {
        elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_second_carousel').css('left');
        if (elementPosition == '0px') {
            removeSelectedNav();
            $('.jcarousel-control2>a').eq(0).addClass('ppvClicked').addClass('ppvClickedFirst');
        }
    }
    var clickedCat = $(this).attr('id');
    if (clickedCat == 'three') {
        elementPosition = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_third_carousel').css('left');
        if (elementPosition == '0px') {
            removeSelectedNav();
            $('.jcarousel-control3>a').eq(0).addClass('ppvClicked').addClass('ppvClickedFirst');
        }
    }

});


// ppv carousel upper accordion drawer

$('li.jcarousel-item>div').click(function() {
    var carouselDrawer = $('div#one_ppv_info');
    if ($(carouselDrawer).is(':visible')) {
        // this means the drawer is open so we check if the same drawer is open
    }

    // dont bother checking if visible just check id val!
    var oldVal = $(this).attr('class');
    var newVal = $('#one_ppv_info').attr('class');
    if (newVal === oldVal) {
        $(carouselDrawer).slideUp().removeClass();
    }
    else {
        // grab the id val of the ppv item
        var filmIdVal = $(this).attr('class');
        var loadedInfo = "PPVTitleDetails.aspx?id=" + filmIdVal;

        // use the id val to slidedown with the drawer block
        // add activity loader here
        $('#one_ppv_info').removeClass().addClass(filmIdVal).load(loadedInfo).fadeIn().slideDown();
    }
});


function dynamicNavMenu() {
    var item = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_first_carousel>.jcarousel-item');
    var itemLength = item.length;
    var itemDivided = itemLength / 4;



    // console.log(itemDivided);
    navBtn = '<a class="nav1" href="#"><span></span>1</a>';
    navContainer = $('div.jcarousel-control1');
    $(navContainer).empty();
    i = 0;
    for (i = 0; i <= itemDivided; i++) {
        $('div.jcarousel-control1').prepend(navBtn);
    }

    function insTextNode() {
        tempAnchorElem = $('div.jcarousel-control1 a').eq(0);
        anchorContents = '<span></span>1';
        $(tempAnchorElem).empty().prepend(anchorContents);

        tempAnchorElem1 = $('div.jcarousel-control1 a').eq(1);
        if (tempAnchorElem1) {
            anchorContents1 = '<span></span>5';
            $(tempAnchorElem1).empty().prepend(anchorContents1).removeClass('nav1').addClass('nav2');

        }

        tempAnchorElem2 = $('div.jcarousel-control1 a').eq(2);
        anchorContents2 = '<span></span>9';
        $(tempAnchorElem2).empty().prepend(anchorContents2).removeClass('nav1').addClass('nav3');

        tempAnchorElem3 = $('div.jcarousel-control1 a').eq(3);
        anchorContents3 = '<span></span>13';
        $(tempAnchorElem3).empty().prepend(anchorContents3).removeClass('nav1').addClass('nav4');

        tempAnchorElem4 = $('div.jcarousel-control1 a').eq(4);
        anchorContents4 = '<span></span>17';
        $(tempAnchorElem4).empty().prepend(anchorContents4).removeClass('nav1').addClass('nav5');

        tempAnchorElem5 = $('div.jcarousel-control1 a').eq(5);
        if (tempAnchorElem5) {
            anchorContents5 = '<span></span>21';
            $(tempAnchorElem5).empty().prepend(anchorContents5).removeClass('nav1').addClass('nav6');
        }

        $(tempAnchorElem).click(function() {
            //console.log('live function executed');
        });

    }
    insTextNode();

}

// dynamicNavMenu();


// generate dynamic nav menu for upper carousel
var item = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPPVBrowse_first_carousel>.jcarousel-item');
itemLength = item.length;


// create object to generate nav buttons for top carousel

function createDynamicNav() {

    // count the number of films


    // generate a button for each multiple of four or less

    // insert the button into the DOM

}
createDynamicNav();



// fix credit card req, on add credit card page, by removing val attribute generated by control
var addCardType = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPaymentAccountAddCC_ddlCCType>option').eq(0).attr({
    value: ""
});
//console.log(addCardType);
// fix expiration date req'd on add credit card page

var cardExpDate = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlPaymentAccountAddCC_ddlCardExpMonth>option').eq(0).attr({
    value: ""
});




//console.info('cuenta.js')
$('.faqs .accordion .header').click(function() {
    $(this).toggleClass('faq_open');
});




// forgot password two options
var topOption = $('div#forgotPassword>div.labelInputHolder>div.inputHolder').eq(0);
var btmOption = $('div#forgotPassword>div.labelInputHolder>div.inputHolder').eq(1);
var topLink = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlForgotPassword_rdoValidateSeq');
var btmLink = $('#ctl00_ctl00_ContentPlaceHolderView_MainColumn_ctlForgotPassword_rdoEmailReset');

$(btmOption).hide();
$(btmLink).click(function() {
    $(topOption).slideUp();
    $(btmOption).slideDown();
});

$(topLink).click(function() {
    $(btmOption).slideUp();
    $(topOption).slideDown();
});

// check which option open first
var checkOptions = $(topOption).attr('checked');
//console.log(checkOptions)

var checkedStatus = $(topLink).attr('checked');
//console.log(checkedStatus)			
if (checkedStatus !== true) {
    $(btmLink).click();
}
else {
    $(topLink).click();
}


// video page 
/*$('ul#video_thumbs li h4 a').click(function() {
//console.log($(this))
$('ul#video_thumbs li').removeClass('thumb_selected');
$(this).parent().parent().addClass('thumb_selected');
return false;
});
*/



$('ul#video_thumbs li div').click(function() {
    //console.log($(this))
    $('ul#video_thumbs li').removeClass('thumb_selected');
    $(this).parent().addClass('thumb_selected');
    return false;
});


// point thumbs to load movie on page
$('#video_thumbs li a').click(function() {
    var playerLink = $(this).attr('href');
    var iframeSource = $('iframe').attr('src');
    $('iframe').attr({ src: playerLink });
});
$('.jcarousel-control1>a').eq(0).addClass('ppvClicked');



};
})(jQuery);


