jQuery.noConflict();
jQuery(document).ready(function($) {

    /*------------  12345 extras  ---------------*/

    $("#extras ul a").click(function(e) {
        e.preventDefault();
        // Get the index of the clicked link
        var index = $("#extras ul a").index(this);

        // Update de class of the pic container.
        $("#picture").attr("class", "pic" + index);

        // Put the class on the correct LI
        $("#extras ul li").removeClass("highLight");
        $(this).parent().addClass("highLight");

        // Show the correct content
        $("#extras .content").hide();
        $("#extras .content").eq(index).show();
    });
    $("#extras .content").hide();
    $("#extras .content").eq(0).show();


    /*------------  1-2-3-4 Team Profile members ---------------*/

    $("#teamMemberProfile ul a").click(function(e) {
        e.preventDefault();
        // Get the index of the clicked link
        var index = $("#teamMemberProfile ul a").index(this);

        // Update de class of the pic container.
        //$("#picture").attr("class", "pic"+index);

        // Put the class on the correct LI
        $("#teamMemberProfile ul li").removeClass("highLight");
        $(this).parent().addClass("highLight");

        // Show the correct content
        $("#teamMemberProfile .onTheSpot").hide();
        $("#teamMemberProfile .onTheSpot").eq(index).show();
    });
    $("#teamMemberProfile .onTheSpot").hide();
    $("#teamMemberProfile .onTheSpot").eq(0).show();


    /* --- SlideShow Team member profile ---*/

    $('div.slideshow').each(function() {
        var $slides = $('div.onTheSpot', this);
        var $pagers = $('#teamMemberProfile li');
        var pos = 0;

        var slideshowTimer = window.setInterval(function() {
            // hide the old slice / remove highligth
            $slides.eq(pos).hide();
            $pagers.eq(pos).removeClass('highLight');

            // Update the index position
            pos = pos + 1;
            if (pos >= $slides.size()) {
                pos = 0;
            }

            // show the new slice / add highlight
            $slides.eq(pos).show();
            $pagers.eq(pos).addClass('highLight');
        }, 8000);

        $(this).parent().find('ul a').click(function() {
            window.clearInterval(slideshowTimer);
            var index = $pagers.find('a').index(this);

            // hide the old slice / remove highligth
            $slides.eq(pos).hide();
            $pagers.eq(pos).removeClass('highLight');

            // show the new slice / add highlight
            $slides.eq(index).show();
            $pagers.eq(index).addClass('highLight');
        });
    });


    /*--------------  select box stylish ------------------*/

    $('#my-dropdown, select.my-dropdown, #my-dropdown01, #my-dropdown1, #my-dropdown11, #my-dropdown5').sSelect();


    /*------------  Bio ---------------*/

    $("#my-dropdown2").sSelect().change(function(e) {
        // Get the index of the clicked link
        var index = $(this).val();
        var indexNumber = index.charAt(index.length - 1);
        if (index === '') { return; }

        // Show the correct content
        $("#teamMembers .bioMember").hide();
        $("#teamMembers .bioMember").eq(indexNumber).show();

        //Playlist.GetInstance().Play(Playlist.FeaturedVideos[index]);

        var clipTitle, clipId, clipThumb;
        switch (index) {
            case 'mgm_0':
                clipTitle = "Licence to Drill";
                clipId = "242453";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-savage.jpg";
                break;
            case 'mgm_1':
                clipTitle = "Licence to Drill";
                clipId = "242457";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-williams.jpg";
                break;
            case 'mgm_2':
                clipTitle = "Licence to Drill";
                clipId = "242456";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-bliston.jpg";
                break;
            case 'nabors_0':
                clipTitle = "Licence to Drill";
                clipId = "242454";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-robinson.jpg";
                break;
            case 'nabors_1':
                clipTitle = "Licence to Drill";
                clipId = "242455";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-millan.jpg";
                break;
            case 'nabors_2':
                clipTitle = "Licence to Drill";
                clipId = "242451";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-gange.jpg";
                break;
            case 'nabors_3':
                clipTitle = "Licence to Drill";
                clipId = "242450";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-vanderveen.jpg";
                break;
            case 'nabors_4':
                clipTitle = "Licence to Drill";
                clipId = "242448";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-long.jpg";
                break;
            case 'nabors_5':
                clipTitle = "Licence to Drill";
                clipId = "242449";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-mantie.jpg";
                break;
            case 'nabors_6':
                clipTitle = "Licence to Drill";
                clipId = "242445";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-forsyth.jpg";
                break;
            case 'nabors_7':
                clipTitle = "Licence to Drill";
                clipId = "242452";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-connolly.jpg";
                break;
            default:
                clipTitle = "Licence to Drill";
                clipId = "242453";
                clipThumb = "http://images.ctvdigital.com/images/pub2upload/2/2009_12_3/ltd-savage.jpg";
        }

        document.getElementById("OneClipViewer").innerHTML = "";

        Interface.GetInstance().PlayerViewer = document.getElementById("OneClipViewer");
        Playlist.FeaturedVideos = new Array();

        Playlist.FeaturedVideos.push(new Video({
            Title: clipTitle,
            Format: 'FLV',
            ClipId: clipId,
            Thumbnail: clipThumb,
            IsAd: false,
            BugUrl: 'http://watch.discoverychannel.ca/#clip' + clipId
        }));

        new Framework(Format.FlashVideo, 3, "chumtvdiscovery", "OneClip Player", "ctv.discoverychannelwatch.ca", "http://watch.discoverychannel.ca/themes/discovery/player/300x240_Blue_OneClip.aspx", clipThumb);
    });
    $("#teamMembers .bioMember").hide();
    $("#teamMembers .bioMember").eq(0).show();


    /*--------------  Scrollbar stylish -----------------*/

    $('.scroll-pane').jScrollPane({ showArrows: true, scrollbarWidth: 18 });


    /*------------  Albums - Clips  ---------------*/

    $("ul.largest a").hover(function(e) {

        // Get the index of the clicked link
        var txt = $(this).attr("title");
        var $descriptPh = $(this).closest("li.album").find("span");
        $descriptPh.text(txt);

        $descriptPh.addClass("showAlt");

    });

    $('li.album ul').hover(function() { }, function() {
        var $descriptPh = $(this).closest("li.album").find("span");
        $descriptPh.text('');
        $descriptPh.removeClass("showAlt");
    });


    /*------------  Albums - Clips  prettyPhoto ---------------*/

    $("a[rel^='prettyPhoto']").prettyPhoto();

    $("a[rel^='prettyPopin']").prettyPopin({ followScroll: false });
    
    
    

    /*------------   External links in blank target  ---------------*/

    $("a[rel^='external']").attr('target', '_blank');


    /*-------- Make Your Predictions -------*/
    $("a.predict").click(function() {
        $('#hiddenMakeYourPredictions').val(this.hash.replace('#', ''));
        $('#hiddenMakeYourPredictionsID').val(this.hash.replace('#', ''));
        $('#formMakeYourPreditions').submit();
        return false;
    });


    /*-------- Showtimes -------*/
    $('#ShowtimesHeaderSelect').hover(
        function() {
            $('#ShowtimesHeaderSelect').animate({ 'height': 60 }, 60);
        },
        function() {
            $('#ShowtimesHeaderSelect').animate({ 'height': 0 }, 60);
        }
    );

    $('.ShowtimesExpanded').live('click', function(e) {
        e.preventDefault();
        $(this).hide();
    });


    /*-------- Discovery Banner -------*/
    if ($.browser !== 'IE' || $.browser.version > 7) {
        $('#MainMenuShowsLink').hover(
            function() {
                $('#MainMenuShowsDropdown').show();
            },
            function() {
                $('#MainMenuShowsDropdown').hide();
            }
        );
    }


    /*--------- Showtimes update (last thing to do) */
    $.ajax({
        url: "/Ajax/updateShowtimes.aspx",
        type: 'get'
    });

    if (location.hash) {
        $(location.hash).click();
    }




    $('#pane').jScrollPane({
        showArrows: true
    });

    // Open featured tv clip in different window
    // Keep accessibility by warning for the new window popup
    $('#tvFeatures a[href^="http://watch.discoverychannel.ca"]')
        .click(function(e){
            return !window.open(this.href, 'clip');
        })
        .attr('title', 'open in new window')
        .attr('rel', 'external');
});

/*-------- Showtimes related function -------*/

//A helper function to check the validity of a hidden field
//and it's value.
function isValidTimeLeftValue(element) {
    if (element === null) { return false; }
    if (element.value == '') { return false; }
    if (element.value != parseInt(element.value,10)) { return false; }
    return true;
}

//This function calculates the time difference between the show
//time and the current time. It displays the appropriate message
//based on that calculation.
function displayTimeLeft(expandedShowtimeElement) {
    //Check for all required values and their validity
    if (!(isValidTimeLeftValue(document.getElementById('ShowtimesHiddenDuration_' + expandedShowtimeElement)) &&
            isValidTimeLeftValue(document.getElementById('ShowtimesHiddenYear_' + expandedShowtimeElement)) &&
            isValidTimeLeftValue(document.getElementById('ShowtimesHiddenMonth_' + expandedShowtimeElement)) &&
            isValidTimeLeftValue(document.getElementById('ShowtimesHiddenDay_' + expandedShowtimeElement)) &&
            isValidTimeLeftValue(document.getElementById('ShowtimesHiddenHour_' + expandedShowtimeElement)) &&
            isValidTimeLeftValue(document.getElementById('ShowtimesHiddenMinute_' + expandedShowtimeElement)))) {
        return false;
    }

    var duration = document.getElementById('ShowtimesHiddenDuration_' + expandedShowtimeElement).value;
    var showtimeDate = new Date(document.getElementById('ShowtimesHiddenYear_' + expandedShowtimeElement).value,
                                document.getElementById('ShowtimesHiddenMonth_' + expandedShowtimeElement).value - 1,
                                document.getElementById('ShowtimesHiddenDay_' + expandedShowtimeElement).value,
                                document.getElementById('ShowtimesHiddenHour_' + expandedShowtimeElement).value,
                                document.getElementById('ShowtimesHiddenMinute_' + expandedShowtimeElement).value);
    var currentDate = new Date();
    var dateDifference = showtimeDate - currentDate;

    //Millisecond values
    var ONE_DAY = 1000 * 60 * 60 * 24;
    var ONE_HOUR = ONE_DAY / 24;
    var ONE_MINUTE = ONE_HOUR / 60;
    var ONE_MONTH = ONE_DAY * 30;

    //If the difference is a minute or less
    if (dateDifference <= 60000) {
        //If the time passed since show time is greater than
        //the duration of the show.
        if (Math.abs(dateDifference) > (duration * ONE_MINUTE)) {
            jQuery('#ShowtimesExpandedTimeLeft_' + expandedShowtimeElement).text('(missed)');
        } else {
            jQuery('#ShowtimesExpandedTimeLeft_' + expandedShowtimeElement).text('(on now)');
        }
    }
    else if (dateDifference > ONE_MONTH) {
        jQuery('#ShowtimesExpandedTimeLeft_' + expandedShowtimeElement).text('(more than a month from now)');
    }
    else {
        var days = Math.floor(dateDifference / ONE_DAY);
        dateDifference = dateDifference - days * ONE_DAY;
        var hours = Math.floor(dateDifference / ONE_HOUR);
        dateDifference = dateDifference - hours * ONE_HOUR;
        var minutes = Math.floor(dateDifference / ONE_MINUTE);

        var result = 'in ';
        if (days !== 0) {
            result += days;
            if (days == 1) {
                result += ' day, ';
            } else {
                result += ' days, ';
            }
        }
        if (hours !== 0) {
            result += hours;
            if (hours == 1) {
                result += ' hour, ';
            } else {
                result += ' hours, ';
            }
        }
        result += minutes;
        if (minutes == 1) {
            result += ' minute';
        } else {
            result += ' minutes';
        }

        jQuery('#ShowtimesExpandedTimeLeft_' + expandedShowtimeElement).text('(' + result + ')');
    }
}

//Show the specified expanded info box
function DisplayExpandedShowtime(expandedShowtimeElement) {
    jQuery('#ShowtimeExpanded_' + expandedShowtimeElement).show();
    displayTimeLeft(expandedShowtimeElement);
}

function UpdateAjaxSchedule(timezone, body, timezoneSelected, timezoneOptions, sid) {
    jQuery.ajax({
        url: '/Ajax/ShowtimesTimezoneChange.aspx',
        data: {
            timezone: timezone,
            body: body,
            timezoneSelected: timezoneSelected,
            timezoneOptions: timezoneOptions,
            sid: sid
        },
        dataType: 'html',
        success: function (txt) {
            jQuery('#'+body).html(txt.split(":::::")[0]);
            jQuery('#'+timezoneSelected).html(txt.split(":::::")[1]);
            jQuery('#'+timezoneOptions).html(txt.split(":::::")[2]);
        }
    });
}

/*-------- Discovery Banner related function -------*/

function showShowsDropDown() {} // for compatibility

function hideShowsDropDown() {} // for compatibility

function submitSearch(e, value) {
    if (e.keyCode == 13 || e.which == 13) {
        //for IE
        e.returnValue = false;
        e.cancel = true;
        //for FF
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
        window.location = 'http://www.discoverychannel.ca/Search.aspx?q=' + encodeURIComponent(value);
    }
}


function closeFlashPopin() {
	jQuery("#b_close").click();
}








