var videoMenu = new Object();  

/* 
Menu Panel Sizes

11px - Round top edge
20px - Each menu item
5px - Bottom gap
37px - Menu title

Total heights for .popupContent css
1 menu item = -73px;
2 menu items = -93px;
3 menu items = -113px;
4 menu items = -133px;
5 menu items = -153px;
6 menu items = -173px;
7 menu items = -193px;

*/

function stringBefore(needle, haystack)
{
    var index = haystack.indexOf(needle,0);
    if (index >= 0)
    {
        return haystack.substring(0, index);
    }
    else
    {
        return "";
    }
}

function stringAfter(needle, haystack)
{
    var index = haystack.indexOf(needle,0);
    if (index >= 0)
    {
        return haystack.substring(index+1, haystack.length-1);
    }
    else
    {
        return haystack;
    }
}


$(document).ready(function() {

    setTimeout('setupCarousel()', 100);

    var channels = $("#homeChannelSelect .menuPopupPanelWrapper");
    channels.each(function(i) {
        setupMenuRolloverV2(this);
    });

    try {
        $('.rating.enable-rating input').rating({
            callback: function(value, link) {
                var clipId = $(".clip-info-panel input.clip-id").val();
                //var channelId = $(".clip-info-panel input.channel-id").val();

                var url = window.location.toString();
                var testurl = url.indexOf("?");

                if (testurl > 0) {
                    jQuery.get(window.location + "&ClipRate=" + value);
                }
                else {
                    jQuery.get(window.location + "?ClipRate=" + value);
                }

                //jQuery.get(window.location + "?ClipRate=" + value);
                //                jQuery.ajax( {
                //                    type: "POST",
                //                    url: window.location + "/ClipRate",
                //                    data: "{"+value+"}",
                //                    contentType: "application/json; charset=utf-8",
                //                    dataType: "json",
                //                    success: function(msg) {
                //                        alert(msg.d);
                //                    }
                //                })
                //                
                $('.rating input').rating('readOnly');
                $('.clip-info-panel span.rating-count').html(parseInt($('.clip-info-panel span.rating-count').html()) + 1);
            }
        });
        $('.rating.disable-rating input').rating();
    }
    catch (err) {
        try {
            console.log("Err: " + err);
        }
        catch (err) { }
    }

    //    $("div.clip a.add-to-playlist").click(function() {
    //        var href = $(this).attr("href");
    //        var clips = stringBefore("|", stringAfter("#", href));
    //        var channel = stringAfter("|", href);
    //        addItem(clips, channel);
    //    });

    //Adding enter key event to the forgot password input field
    $(".loginForm input.inputBorder").keypress(function(event) {
        if (event.keyCode == '13') {
            //alert("Key: " + event.keyCode);
            $(".loginForm .loginFormButtons input").click();
            event.preventDefault();
        }
    });

    //$(".accordion .content").jScrollPane({ showArrows: true, scrollbarWidth: 16, reinitialiseOnImageLoad: true, scrollbarMargin: 4, arrowSize: 16, dragMinHeight: 16, dragMaxHeight: 80, scrollbarOnLeft: false });

    $(".accordion .content").each(function(i) {
        $(this).jScrollPane({ showArrows: true, scrollbarWidth: 16, reinitialiseOnImageLoad: true, scrollbarMargin: 4, arrowSize: 16, dragMinHeight: 16, dragMaxHeight: 80, scrollbarOnLeft: false });
    });

    $(".accordion .favouritesTitlePanel .panelcorner").attr("src", "/assets/images/favorites/panelcorner_sq.jpg");
    $(".accordion .favouritesTitlePanel.row0 .panelcorner").attr("src", "/assets/images/favorites/panelcorner.png");

    //setTimeout("initMenu()", 400);

    $(".defaultinputtext").each(function(index) {
        var default_value = $(this).attr("rel");
        if ($(this).val() == "") {
            $(this).val(default_value);
        }
    });

    $(".defaultinputtext").live("blur", function() {
        var default_value = $(this).attr("rel");
        if ($(this).val() == "") {
            $(this).val(default_value);
        }
    }).live("focus", function() {
        var default_value = $(this).attr("rel");
        if ($(this).val() == default_value) {
            $(this).val("");
        }
    });

});

$(window).load(function () {
  // run code
  setTimeout("initMenu()", 400);
  var formcheck = $("form").attr("action").replace("?.aspx", ".aspx");
    
  $("form").attr("action",formcheck)
});

function initMenu() {
    //jQuery(document).ready(function() {
        $('.accordion .favouritesTitlePanel').click(function() {
            $(this).next().toggle('slow');
            return false;
        }).next().hide();
    //});

    $('.accordion .favouritesTitlePanel.row0').click();
}

window.onload = function () {
    // Setup tabs
    //$(".tab-container").tabs();
}

setupMenuRolloverV2 = function(menuPanelObj) {

    var menuElements = $(menuPanelObj).find(".popupContent .menuContent ul li");
    var count = menuElements.length * 20 + 53;
    var popupContent = $(menuPanelObj).find(".popupContent");
    popupContent.css("top", "-" + count + "px");

    var title = $(menuPanelObj).find(".titleHover");
    title.hover(function() {
        var wrapper = $(this).parent();
        videoMenu[wrapper.attr("id")] = true;
        popupContent.fadeIn("fast");
        wrapper.find(".menuTitle").attr("src", "/assets/images/menuPopup/" + wrapper.attr("id") + "_Hover_bg.png");
    },
	function() {
	    videoMenu[$(this).parent().attr("id")] = false;
	    setTimeout('callMenuCheckV2("' + menuPanelObj.id + '")', 50);
	});

	$(menuPanelObj).find("li").hover(function() {
    	var wrapper = $(this).parent().parent().parent().parent();
    	videoMenu[wrapper.attr("id")] = true;
    },
	function() {
	    var wrapper = $(this).parent().parent().parent().parent();
	    videoMenu[wrapper.attr("id")] = false;
	    setTimeout('callMenuCheckV2("' + wrapper.attr("id") + '")', 50);
	});
}

callMenuCheckV2 = function(panelName) {
    var panel = $("#" + panelName);

    if (videoMenu[panelName]) { return; }
    else {
        var popupContent = panel.find(".popupContent");
        popupContent.fadeOut("fast");
        panel.find(".menuTitle").attr("src", "/assets/images/menuPopup/blank_Hover_bg.png");
    }
}

setupCarousel = function() {
    // Setup carousels
    $(".carousel").each(function(i) {
        $(this).children(".jCarouselLite").jCarouselLite({
            btnNext: "#" + $(this).attr("id") + " .next",
            btnPrev: "#" + $(this).attr("id") + " .prev",
            circular: false,
            visible: 1,
            scroll: 1,
            onInit: initCarousel
        });
    });
}

var carouselsReady = 0;
initCarousel = function() {
    carouselsReady++;
    if (carouselsReady >= $(".jCarouselLite").length) {
        $(".tab-container").tabs();
    }
}

clipView = function() {
    var url = window.location.toString();
    var testurl = url.indexOf("?");

    if (testurl > 0) {
        jQuery.get(window.location + "&ClipView=1");
    }
    else {
        jQuery.get(window.location + "?ClipView=1");
    }

    //jQuery.get(window.location + "?ClipView=1");
//    jQuery.ajax( {
//        type: "POST",
//        url: window.location + "/ClipView",
//        data: "{}",
//        contentType: "application/json; charset=utf-8",
//        dataType: "json",
//        success: function(msg) {
//            alert(msg.d);
//        }
//    } )
}


var featuresListPage = 0;
var animating = false;

$(document).ready(function() {
    featutesListRollovers();
});

featutesListRollovers = function() {
    //Rollovers for the features list.
    $(".rc_hitbox").each(function(i) {
        $(this).hover(function() {
            //On hover
            $(".rc_featuresThumbnail:eq(" + i + ")").css("border-color", "#f1f0ee");

            $(".rc_featuresButton img:eq(" + i + ")").attr("src", "/assets/images/buttons/featuresButton_over.png");
            $(".rc_featuresListItems:eq(" + i + ")").css("background-color", "#9b2b43");
            var fileName = $("a.rc_featureLink:eq(" + i + ")").attr("rel");
            var featureUrl = $("a.rc_featureLink:eq(" + i + ")").attr("href");
            $(".rc_featuresImage img").attr("src", fileName);
            $(".rc_featuresImage a").attr("href", featureUrl);
        }, function() {
            //Off hover
            $(".rc_featuresThumbnail:eq(" + i + ")").css("border-color", "#c13653");
            $(".rc_featuresButton img:eq(" + i + ")").attr("src", "/assets/images/buttons/featuresButton.png");
            $(".rc_featuresListItems:eq(" + i + ")").css("background-color", "#c13653");
        });
    });

    //Back button
    $(".rc_backButton img").hover(function() {
        //On hover
        $(this).attr("src", "/assets/images/buttons/previousButton_over.png");
    }, function() {
        //Off hover
        $(this).attr("src", "/assets/images/buttons/previousButton.png");
    });

    //forwards Button
    $(".rc_forwardButton img").hover(function() {
        //On hover
        $(this).attr("src", "/assets/images/buttons/nextButton_over.png");
    }, function() {
        //Off hover
        $(this).attr("src", "/assets/images/buttons/nextButton.png");
    });

    //Features Button
    $(".rc_MoreFeaturedButton img").hover(function() {
        //On hover
        $(this).attr("src", "/assets/images/buttons/featuredVideosButton_over.png");
    }, function() {
        //Off hover
        $(this).attr("src", "/assets/images/buttons/featuredVideosButton.png");
    });

    //Setup scrolling for features panel
    featuresListScrollUp();
    featuresListScrollDown();
}

featuresListScrollUp = function() {
    //Rollover for scroll toggles
    $(".rc_backButton").click(function() {
        if (animating) return;

        //Scroll Up
        if (featuresListPage > 0) {
            featuresListPage--;
            animating = true;

            checkButtonFades(featuresListPage);

            var topPos = parseInt($('.rc_featuresListContent ul').css("top").replace("px", ""));
            topPos += 256;

            $('.rc_featuresListContent ul').animate({ top: topPos + "px" }, 1000, function() {
                animating = false;
            });
        }
    });
}

featuresListScrollDown = function() {
    $(".rc_forwardButton").click(function() {
        if (animating) return;
        //Scroll Down
        if (featuresListPage < 2) {
            featuresListPage++;
            animating = true;

            checkButtonFades(featuresListPage);

            var topPos = parseInt($('.rc_featuresListContent ul').css("top").replace("px", ""));
            topPos -= 256;

            $('.rc_featuresListContent ul').animate({ top: topPos + "px" }, 1000, function() {
                animating = false;
            });
        }
    });

    $(".rc_MoreFeaturedButton").click(function() {
        if (animating) return;
        //Scroll Down				
        if (featuresListPage < 2) {
            featuresListPage++;
            animating = true;

            checkButtonFades(featuresListPage);

            var topPos = parseInt($('.rc_featuresListContent ul').css("top").replace("px", ""));
            topPos -= 256;

            $('.rc_featuresListContent ul').animate({ top: topPos + "px" }, 1000, function() {
                animating = false;
            });
        }
    });
}

checkButtonFades = function(step) {
    switch (step) {
        case 0:
            $(".rc_forwardButton").fadeOut();
            $(".rc_backButton").fadeOut(function() {
                $(".rc_MoreFeaturedButton").fadeIn();
            });
            break;
        case 1:
            $(".rc_MoreFeaturedButton").fadeOut(function() {
                $(".rc_forwardButton").fadeIn();
                $(".rc_backButton").fadeIn();
            });
            break;
        case 2:
            $(".rc_forwardButton").fadeOut();
            break;
        default:
            break;
    }
}

function showHide(objID,viewState) {
	if(objID == "featuredVideo") {
		document.getElementById('tab01').className = 'roundHeader roundHeaderRed first';
		document.getElementById('tab02').className = 'roundHeader roundHeaderBlue second';
		document.getElementById('homeChannelSelect').style.display = "none";
		document.getElementById(objID).style.display = "block";
	}
	if(objID == "homeChannelSelect") {
		document.getElementById('tab01').className = 'roundHeader roundHeaderBlue first';
		document.getElementById('tab02').className = 'roundHeader roundHeaderRed second';
		document.getElementById('featuredVideo').style.display = "none";
		document.getElementById(objID).style.display = "block";
	}
}


