﻿var horisontalchannelscroll = 3;
var horisontalexternalclipscroll = 3;
var horisontalclipscroll = 4;
var verticalclipscroll = 10;

$(document).ready(function() {
    $("li.heading").hide();
    $("ul.channels").hide();

    var addPadding = function() {
        return function() {
            var left = parseInt($("div", this).css("padding-left"));
            left = left + 16;
            var strleft = left.toString() + "px";
            $("div", this).css("padding-left", strleft);
            $("> ul > li", this).each(addPadding());
        }
    }

    $("#categoriesmenu > ul > li").each(addPadding());

    $("ul.categories li.category div").hover(overcategory(), outcategory());
    $("ul.categories li.category div").click(showcategory());

    $("ul.pages li.page div").hover(overcategory(), outcategory());
    $("ul.pages li.page div").click(showcategory());

    if (categoryid != -1) {
        var selectedCategory = $("ul.categories li#category_" + categoryid + " > div");
        if (selectedCategory.length == 0) selectedCategory = $("ul.pages li#page_" + categoryid + " > div");
        selectedCategory.trigger('click');
        return;
    }
    loadcategory("");
});


var showcategory = function() {
    return function() {
        if ($(this).parent().hasClass("channel")) {
            parent.document.location.href = 'channel.aspx?channelName=' + $(this).parent().attr("id");
            return;
        }
        if ($(this).parent().hasClass("categorylistinfo")) {
            return;
        }        
        
        categoryid = $(this).parent().attr("id").split("_")[1];

        var ul = $("> ul.channels", $(this).parent());

        var self = ul.parent();

        var clicked = this;

        $("#categoriesmenu li > div").each(function(i) {
            if (clicked == this) return;
            $(this).removeClass("selected");
            $(this).removeClass("over");
        });

        $(this).addClass("selected");

        //if (!($.browser.msie && parseInt(jQuery.browser.version) < 8)) {
        $("ul.channels").parent().each(function(i) {
            var self = this;
            var ul = $("ul.channels", this);
            if (ul.css("display") != "none") {
                ul.css("display", "none");
            }
        });
        if (ul.length > 0)
            ul.css("display", "block");
        loadcategory($(this).parent().attr("id"));
    };
}

function setuplists() {
    $("#content div.horisontal ul.channels").parent().each(
                function() {
                    $(this).jcarousel({
                        itemLoadCallback: mycarousel_itemLoadCallback,
                        size: parseInt($("h1:first", this).remove().html()),
                        scroll: horisontalchannelscroll
                    })
                }
            );
    $("#content div.horisontal ul.clips").parent().each(
                function() {
                    $(this).jcarousel({
                        itemLoadCallback: mycarousel_itemLoadCallback,
                        size: parseInt($("h1:first", this).remove().html()),
                        scroll: horisontalclipscroll
                    })
                }
            );

    $("#content div.vertical ul.clips").parent().each(
                function() {
                    $(this).jcarousel({
                        vertical: true,
                        itemLoadCallback: mycarousel_itemLoadCallback,
                        size: parseInt($("h1:first", this).remove().html()),
                        scroll: verticalclipscroll
                    })
                }
            );

    $("#content div.horisontal ul.channels li.jcarousel-item").hover(itemover(), itemout());
    $("#content div.horisontal ul.clips li.jcarousel-item").hover(itemover(), itemout());
    $("#content div.vertical ul.clips li.jcarousel-item").hover(itemover(), itemout());
}
var ardivs = new Array();
function loadcategory(id) {
    document.write = function(html) {
        var divid = ardivs.pop();
        //alert("document.write -> " + html);
        var div = $("#scripttag" + divid);
        var injhtml = html;
        if ($("div.mpsScrollableWrapper", html).html()) {
            injhtml = $("div.mpsScrollableWrapper", html).html();
        }
        div.append(injhtml);
        //$("ul.mpsContentlist",div).jcarousel({scroll:horisontalexternalclipscroll});
        $("ul", div).jcarousel({ scroll: horisontalexternalclipscroll });
        $("a", div).attr("target", "_new");
        $("li.jcarousel-item", div).hover(itemover(), itemout());
        $("li.jcarousel-item", div).click(function() { window.open($("a", this).attr("href")); });
    };

    //$("#search").html("");

    $.ajax({
         url: "showroom_ajax.aspx?id=" + id,
        dataType: 'json',
        cache: false,
        success: function(data) {
            html = data.html;
            $('#content').html("");
            $(html).each(function(i) {
                if ($(this).html().toLowerCase().indexOf("<script") != -1) {
                    $('#content').append('<div id="scripttag' + i + '" class="horisontal"></div>');
                    var mydiv = $('#content div:last');
                    ardivs.push(i);
                    mydiv.html($(this).html());
                }
                else {
                    if ($(this).hasClass("search")) {
                        $("#search").html("");
                        $("#search").html($(this).html());
                    }
                    else {
                        if ($.browser.msie) {
                            document.getElementById("content").innerHTML += this.outerHTML;
                        }
                        else {
                            $("#content").append($(this).outerHTML());
                        }
                    }
                }
            });
            setuplists();
			setPortalBranding(data.ads);

            $("input").keyup(function() {
               uri = "showroom_ajax.aspx?cmd=search&id=" + id;
        	   data = { search: $(this).val() };
              $.ajax(
                {    
                    dataType: 'json',
					data: data,
                    url: uri,
                    cache: false,
                    type: "POST",
                    success: function(data) 
                    {
                      $('#content').html("");
					  $("#content").html(data.html);
					  setuplists();
                    }
                    }
                ); 
               
               
               
               
               
               // $("#content").load("showroom_ajax.aspx?cmd=search&id=" + id, { search: $(this).val(), returnType:'html'},function(html) {setuplists();});
            }
                    );
        }
    });

}

var overcategory = function() {
    return function() {
        if ($(this).parent().hasClass("categorylistinfo")) return;
        if ($(this).hasClass("selected")) return;

        $(this).addClass("over");
    }
}

var outcategory = function() {
    return function() {
        if ($(this).parent().hasClass("categorylistinfo")) return;
        if ($(this).hasClass("selected")) return;

        $(this).removeClass("over");
    }
}

var itemover = function() {
    return function() {
        $(this).addClass("over");
    }
}
var itemout = function() {
    return function() {
        $(this).removeClass("over");
    }
}

var ar = new Array();

function mycarousel_initCallback(carousel, state) {
    ar.push(carousel);
}

function setCarouselEventHandlers(item) {

    item.click(
                    function() {
                        if ($(this).hasClass("channel")) parent.document.location.href = 'channel.aspx?channelName=' + $(this).attr("id");
                        if ($(this).hasClass("clip")) parent.document.location.href = 'portal.aspx?contentId=' + $(this).attr("id") + "&categoryid=" + categoryid;
                        return false;
                    }
                );
    $("div.channelinfo a", item).click
                (
                    function() {
                        parent.document.location.href = $(this).attr("href");
                        return false;
                    }
                )
    $("div.headinginfo a", item).click
                (
                    function() {
                        parent.document.location.href = $(this).attr("href");
                        return false;
                    }
                )

}


function mycarousel_itemLoadCallback(carousel, state) {
    for (var i = carousel.first; i <= carousel.last; i++) {
        var item = carousel.get(i); 
        if (item.attr("id")) { // Prevent eventhandlers from being set twice
            setCarouselEventHandlers(item);
        } 
    }

    if (carousel.size() > 0) {
        if ($("span.page", carousel.list.parent().parent())[0]) {
            $("span.page", carousel.list.parent().parent()).html("(" + carousel.first + "-" + carousel.last + ")/" + carousel.size());
        } else {
            $("span.page", carousel.list.parent().parent().parent()).html("(" + carousel.first + "-" + carousel.last + ")/" + carousel.size());
        }
    }

    // Check if the requested items already exist
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }
    var id = carousel.list.parent().parent().attr("id");
    if (id == null || id == undefined || id == "") {
        id = carousel.list.parent().parent().parent().attr("id");
    }

    var uri = "showroom_ajax.aspx?id=" + id + "&first=" + (carousel.first - 1) + "&last=" + (carousel.last - 1)
    var data = {};
    
    if (id.indexOf("_search") != -1) {
        uri += "&cmd=search";
        data = { search: $("#search input").val() };
    }

    $.ajax({    
        dataType: 'json',
        data: data,
        url: uri,
        cache: false,
        type: "POST",
        success: function(data) {
            html = data.html;
            $('li', html).each(function(i) {
                carousel.add(carousel.first + i, $(this).html());
                var item = carousel.get(carousel.first + i);
                item.attr("id", $(this).attr("id"));
                if ($(this).hasClass("channel")) item.addClass("channel");
                if ($(this).hasClass("clip")) item.addClass("clip");
                
                setCarouselEventHandlers(item);
            });
            $('li.jcarousel-item').hover(itemover(), itemout());
        }
    }
            );
};


function setPortalBranding(ads) {

    if (ads != null && parent.$("bannerAreaRight"))
        parent.$("bannerAreaRight").innerHTML = ads;
        
    if ($("#portal_baseColor").html() && parent.$("top")) {
        parent.$("top").style.backgroundColor = $("#portal_baseColor").html();
        if (parent.$("menuBar")) parent.$("menuBar").style.backgroundColor = $("#portal_baseColor").html();
        $("#portal_baseColor").remove();
    }
    if ($("#portal_topImg").html() && parent.$("top")) {
        parent.$("top").style.backgroundImage = "url(" + $("#portal_topImg").html() + ")";
        $("#portal_topImg").remove();
    }
    if ($("#portal_baseColor").html() && parent.$("menuBar")) {
        parent.$("menuBar").style.backgroundColor = $("#portal_baseColor").html();
        $("#portal_baseColor").remove();
    }
    if ($("#portal_menuBarImg").html() && parent.$("menuBar")) {
        parent.$("menuBar").style.backgroundImage = "url(" + $("#portal_menuBarImg").html() + ")";
        $("#portal_menuBarImg").remove();
    }
    if ($("#portal_logoImg").html() && parent.$("logo")) {
        parent.$("logo").style.backgroundImage = "url(" + $("#portal_logoImg").html() + ")";
        $("#portal_logoImg").remove();
    }

    if (parent.$("navIframe")) {
        var height = document.getElementById("content").clientHeight;
        var catheight = document.getElementById("categoriesmenu").clientHeight;
        if (catheight > height) height = catheight;
        height += 200;
        parent.$("navIframe").style.height = height + "px";
    }
}
