﻿/// <reference name="MicrosoftAjax.debug.js"/>
/// <reference path="jquery-1.3.2-vsdoc.js" />

var pageTitle;
var productFilter;
var lastProductState;
var lastReferenceState;
var lastNewsState;
var lastVideoState;
var sp = "normal";

var currentRefCountry = "";
var showPressDropDownListDiv = false;
var pressDropDownListID = "";
var currentPressYearText = "";
var hfPressYearID = "";
var currentPressYearValue = "";
var currentPressCountryText = "";
var hfPressCountryID = "";
var currentPressCountryValue = "";
var currentPressHeadingText = "";
var hfPressHeadingID = "";
var currentPressHeadingValue = "";

Sys.Application.add_init(pageInit);

function pageInit(sender, e)
{
    Sys.Application.add_navigate(onNavigate);

    pageTitle = document.title;
    productFilter = new ProductFilter();
    Resize();
    if (location.hash.length == 0)
        Navigate("Home");
    $(window).bind("resize", Resize);
    $.preload.gap = 13; // correct stack overflow problem in IE7
}

// Web Site navigation
function Navigate(menuID, filter, areaID, contentID, sort, parameter)
{
    if (!filter)
        filter = null;

    if (!areaID)
        areaID = null;

    if (!contentID)
        contentID = null;

    if (!sort)
        sort = null;

    if (!parameter)
        parameter = null;

    Sys.Application.addHistoryPoint({ m: menuID, f: filter, a: areaID, c: contentID, s: sort, p: parameter }, pageTitle);
}

function SetShinyStat(pageName)
{
    if (ssxl != undefined)
        ssxl("PAG=" + pageName);
}

function ChangeLanguage(url)
{
    location.href = "clear.aspx";
    location.href = url + location.hash;
}

function onNavigate(sender, e)
{
    var state = e.get_state();
    var menuID = state.m;
    var areaID = state.a;
    var filter = state.f;
    var contentID = state.c;
    var sort = state.s;
    var parameter = state.p;

    if (!menuID)
        menuID = "Home";

    if (menuID == "Home")
        $("#vatNumber").show();
    else
        $("#vatNumber").hide();

    $("#menu .menuItem").each(function(i) { $(this).removeClass("selected"); });
    $("#menu" + menuID).addClass("selected");

    switch (menuID)
    {
        // Home   
        case "Home":
            $("#content1").hide()[0].innerHTML = "";
            $("#content2").hide();
            $("#images").hide();
            $("#background").show();
            $("#main").removeClass("white");
            $("#submenu").fadeOut(sp);
            GetContentList("/App_Views/Highlights.ascx", null, null, null, null, null, UpdateHighlights);
            lastReferenceState = state;
            lastProductState = state;
            SetShinyStat("Home");
            break;
        // References            
        case "References":
            $("#images").hide();
            if (filter)
            {
                if ($("#referenceList").length == 0)
                {
                    $("#content1").hide();
                    var instance = { loading: true };
                    GetContentList("/App_Views/LoadingReferenceList.ascx", areaID, null, null, null, null, Function.createDelegate(instance, UpdateReferenceList));
                }
                else
                    $("#referenceList")[0].innerHTML = "";

                $("#content2").hide();
                $("#images").hide();
                $("#background").show();
                $("#submenu").fadeIn(sp);
                $("#main").removeClass("white");
                $("body").css("cursor", "wait");

                lastReferenceState = state;

                var filters = filter.split(",");

                var referenceFilter = new Filter();
                referenceFilter.ItemID = "ReferenceType";
                referenceFilter.Operator = "References";
                referenceFilter.Value = filters[0];

                var countryFilter = new Filter();
                countryFilter.ItemID = "Country";
                countryFilter.Operator = "References";
                countryFilter.Value = filters[1];

                if (parameter == null)
                    GetSubMenu("/App_Views/SubMenuReferences.ascx", menuID, null, filter);
                GetContentList("/App_Views/ReferenceList.ascx", areaID, null, new Array(referenceFilter, countryFilter), sort, parameter, UpdateReferenceList);
            }
            else
            {
                lastProductState = state;

                if (areaID)
                {
                    $("#submenu").fadeOut(sp);
                    $("#content1").hide();
                    $("#background").hide();
                    $("#main").addClass("white");

                    if (contentID)
                        GetContent("/App_Views/Reference.ascx", areaID, contentID, UpdateReference);
                    else
                        GetContent("/App_Views/Reference.ascx", areaID, null, UpdateReference);
                }
                else
                {
                    $("#content1").hide();
                    $("#content2").hide();
                    $("#images").hide();
                    $("#background").show();
                    $("#main").removeClass("white");
                    GetSubMenu("/App_Views/SubMenuReferences.ascx", menuID);
                }
            }
            SetShinyStat("References");
            break;
        // Products              
        case "Products":
            if (filter)
            {
                $("#content2").hide();
                $("#images").hide();
                $("#background").show();
                $("#submenu").fadeIn(sp);
                $("#main").removeClass("white");

                lastProductState = state;
                productFilter.FromString(filter);

                var values = productFilter.GetValues();
                var value, title;
                for (var i = 0; i < values.length; i++)
                {
                    if (values[i])
                    {
                        value = values[i];
                        title = $("#submenu" + value).attr("title");
                    }
                }

                if ($("#productList").length > 0)
                {
                    $("#productList").jScrollPaneRemove();
                    $("#productList")[0].innerHTML = "";
                    $("#familyList")[0].innerHTML = "";
                }

                GetSubMenu("/App_Views/SubMenuProducts.ascx", menuID, null, value);
                var instance = { title: title };
                GetContentList("/App_Views/ProductList.ascx", areaID, null, productFilter.GetFilters(), null, null, Function.createDelegate(instance, UpdateProductList));
            }
            else
            {
                if (areaID)
                {
                    $("#submenu").fadeOut(sp);
                    $("#content1").hide();

                    lastReferenceState = state;

                    if (contentID)
                        GetContent("/App_Views/Product.ascx", areaID, contentID, UpdateProduct);
                    else
                        GetContent("/App_Views/Product.ascx", areaID, areaID, UpdateProduct);
                }
                else
                {
                    $("#content1").hide();
                    $("#content2").hide();
                    $("#images").hide();
                    $("#background").show();
                    $("#main").removeClass("white");
                    GetSubMenu("/App_Views/SubMenuProducts.ascx", menuID);
                }
            }
            SetShinyStat("Products");
            break;
        // Designers                            
        case "Designers":
            $("#content2").hide();
            $("#images").hide();
            $("#background").show();
            $("#main").removeClass("white");
            GetSubMenu("/App_Views/SubMenuDesigners.ascx", menuID, "Designer", contentID);
            if (contentID)
            {
                lastProductState = state;
                GetContent("/App_Views/Designer.ascx", areaID, contentID, UpdateDesigner);
            }
            else
            {
                GetContentList("/App_Views/DesignerList.ascx", areaID, "Designer", null, null, parameter);
            }
            SetShinyStat("Designers");
            break;
        // Company                          
        case "Company":
            $("#content2").hide();
            $("#images").hide();
            $("#background").show();
            $("#main").removeClass("white");
            GetSubMenu("/App_Views/SubMenu.ascx", menuID, "GenericContent", contentID);
            if (contentID)
            {
                $("#submenu .menuItem").each(function(i) { $(this).removeClass("selected"); });
                $("#submenu" + contentID).addClass("selected");

                GetContent("/App_Views/Company.ascx", areaID, contentID);
            }
            else
            {
                $("#content1").hide();
            }
            SetShinyStat("Company");
            break;
        // News                          
        case "News":
            $("#submenu").fadeOut(sp);
            $("#content2").hide();
            $("#content1").hide()[0].innerHTML = "";
            $("#images").hide();
            $("#background").show();
            $("#main").removeClass("white");
            if (contentID == null)
            {
                lastNewsState = state;
                GetContentList("/App_Views/News.ascx", areaID, null, null, null, parameter);
            }
            else
            {
                GetContent("/App_Views/NewsDetail.ascx", areaID, contentID);
            }
            SetShinyStat("News");
            break;
        // Press   
        case "Press":
            $("#content2").hide();
            if (!areaID)
                $("#content1").hide()[0].innerHTML = "";
            $("#images").hide();
            $("#background").show();
            $("#main").removeClass("white");
            GetSubMenu("/App_Views/SubMenuPress.ascx", menuID, "GenericContent", contentID);
            var pressFilter = new PressFilter();
            if (filter)
                pressFilter.FromString(filter);
            GetContentList("/App_Views/Press.ascx", areaID, null, pressFilter.GetFilters(), null, parameter, UpdatePress);
            SetShinyStat("Press");
            break;
        // Contacts                          
        case "ContactUs":
            $("#submenu").fadeOut(sp);
            $("#content1").hide();
            $("#images").hide();
            $("#background").hide();
            $("#main").addClass("white");
            GetContentList("/App_Views/ContactUs.ascx", areaID, null, null, null, parameter, UpdateContactUs);
            SetShinyStat("ContactUs");
            break;
        // Video                          
        case "Video":
            $("#submenu").fadeOut(sp);
            $("#content2").hide();
            $("#images").hide();
            $("#background").show();
            $("#main").removeClass("white");
            if (contentID == null)
            {
                lastVideoState = state;
                GetContentList("/App_Views/Video.ascx", areaID, null, null, null, parameter, UpdateVideo);
            }
            else
            {
                GetContent("/App_Views/VideoDetail.ascx", areaID, contentID, UpdateVideoDetail);
            }
            SetShinyStat("Video");
            break;
        // Materiali 
        case "Materials":
            $("#submenu").fadeOut(sp);
            $("#content2").hide();
            $("#images").hide();
            $("#background").show();
            $("#main").removeClass("white");
            GetContentList("/App_Views/MaterialList.ascx", areaID, null, null, null, null);
            SetShinyStat("Materials");
            break;
    }
}

var oldScreenWidth, oldScreenHeight;
function Resize(e, img)
{
    var container = $("#container");
    if (container.width() != oldScreenWidth || container.height() != oldScreenHeight)
    {
        var background = $("#background");
        background.preload({ onFinish: function() { ResizeImage(background); background.css("visibility", "visible"); } });

        if (img == null)
        {
            img = $("#images img:visible")[0];
            img = $(img);
        }
        if (img)
            img.preload({ onFinish: function() { ResizeImage(img); } });

        oldScreenHeight = container.height();
        oldScreenWidth = container.width();
    }
}

function ResizeImage(img)
{
    var container = $("#container");

    var h = img.height() / img.width();
    var w = img.width() / img.height();

    if ((container.height() / container.width()) < h)
    {
        img.width(container.width());
        img.height(h * container.width());
    }
    else
    {
        img.height(container.height());
        img.width(w * container.height());
    }
}

function SetTextColor(img)
{
    if (img.attr("white") == "True")
        $("#main").removeClass("white");
    else
        $("#main").addClass("white");
}

function GoBackProducts()
{
    if (lastProductState)
    {
        if ($("#content1 #productList").length > 0)
        {
            $("#content2").hide();
            $("#images").hide();
            $("#background").show();
            $("#submenu").fadeIn(sp);
            $("#main").removeClass("white");
            $("#content1").show();

            ContentWebService.ClearProductSession();
        }
        else
            Navigate(lastProductState.m, lastProductState.f, lastProductState.a, lastProductState.c);
    }
    else
        Navigate("Products");
}

function GoBackReferences()
{
    if (lastReferenceState)
    {
        if ($("#content1 #referenceList").length > 0)
        {
            $("#content2").hide();
            $("#images").hide();
            $("#background").show();
            $("#submenu").fadeIn(sp);
            $("#main").removeClass("white");
            $("#content1").show();
        }
        else
            Navigate(lastReferenceState.m, lastReferenceState.f, lastReferenceState.a, lastReferenceState.c, lastReferenceState.s, lastReferenceState.p);
    }
    else
        Navigate("References");
}

function GoBackNews()
{
    if (lastNewsState)
        Navigate(lastNewsState.m, lastNewsState.f, lastNewsState.a, lastNewsState.c, lastNewsState.p);
    else
        Navigate("News");
}

function GoBackVideo()
{
    if (lastVideoState)
        Navigate(lastVideoState.m, lastVideoState.f, lastVideoState.a, lastVideoState.c, lastVideoState.p);
    else
        Navigate("Video");
}

function NewsDetailNextImage(hfCurrentImageIndexID, hfImagesCountID)
{
    var imageIndex = parseInt($get(hfCurrentImageIndexID).value);
    var imagesCount = parseInt($get(hfImagesCountID).value);
    if (imageIndex == 0)
        return;
    var id = "divNewsImage" + imageIndex;
    var div = $get(id);
    $("#divNewsImage" + imageIndex).fadeOut(sp);
    imageIndex = imageIndex + 1;
    if (imageIndex > imagesCount)
        imageIndex = 1;
    $("#divNewsImage" + imageIndex).fadeIn(sp);
    $get(hfCurrentImageIndexID).value = imageIndex;
}

function NewsDetailPreviousImage(hfCurrentImageIndexID, hfImagesCountID)
{
    var imageIndex = parseInt($get(hfCurrentImageIndexID).value);
    var imagesCount = parseInt($get(hfImagesCountID).value);
    if (imageIndex == 0)
        return;
    $("#divNewsImage" + imageIndex).fadeOut(sp);
    imageIndex = imageIndex - 1;
    if (imageIndex == 0)
        imageIndex = imagesCount;
    $("#divNewsImage" + imageIndex).fadeIn(sp);
    $get(hfCurrentImageIndexID).value = imageIndex;
}

function GetSubMenu(viewURL, areaID, definitionID, selectedContentID)
{
    if (selectedContentID)
    {
        $("#submenu .menuItem").each(function(i) { $(this).removeClass("selected"); });
        $("#submenu" + selectedContentID).addClass("selected");
    }

    var ci = new ContextInfo();
    ci.AreaID.push(areaID);
    ci.DefinitionID.push(definitionID);
    var instance = { selectedContentID: selectedContentID };
    ContentWebService.GetHtml(viewURL, ci, null, Function.createDelegate(instance, UpdateSubMenu), OnFailed);
}

function GetContentList(viewURL, areaID, definitionID, filter, sort, parameters, onSucceeded)
{
    var ci = new ContextInfo();
    ci.AreaID.push(areaID);
    ci.DefinitionID.push(definitionID);
    ci.SortExpression.push(sort);
    ci.Parameters.push(parameters);
    if (!filter)
        filter = null;
    if (!onSucceeded)
        onSucceeded = UpdateContent;
    ContentWebService.GetHtml(viewURL, ci, filter, onSucceeded, OnFailed);
}

function GetContent(viewURL, areaID, contentID, onSucceeded)
{
    var ci = new ContextInfo();
    ci.AreaID.push(areaID);
    ci.ContentID.push(contentID);
    if (!onSucceeded)
        onSucceeded = UpdateContent;

    ContentWebService.GetHtml(viewURL, ci, null, onSucceeded, OnFailed);
}

function UpdateSubMenu(result)
{
    $("#submenu").html(result).fadeIn(sp);

    if (currentRefCountry)
        $("#dropDownCountriesSelection").text(currentRefCountry);

    if (this.selectedContentID)
    {
        $("#submenu .menuItem").each(function(i) { $(this).removeClass("selected"); });
        $("#submenu" + this.selectedContentID).addClass("selected");
    }

    var hfPressYear = $get(hfPressYearID);
    if (hfPressYear == null)
        return;
    hfPressYear.value = currentPressYearValue;
    $get("divPressDropDownYearSelectionText").innerHTML = currentPressYearText;
    $get(hfPressCountryID).value = currentPressCountryValue;
    $get("divPressDropDownCountrySelectionText").innerHTML = currentPressCountryText;
    $get(hfPressHeadingID).value = currentPressHeadingValue;
    $get("divPressDropDownHeadingSelectionText").innerHTML = currentPressHeadingText;
}

function UpdateContent(result)
{
    $("#content1").html(result).show();

    if ($(".divCompanyDescription").is(":visible"))
    {
        $(".divCompanyDescription").jScrollPane({ scrollbarWidth: 18 });
        $(".jScrollPaneTrack").css("left", "384px");
    }
    if ($(".divMaterialDetails").is(":visible"))
    {
        $(".divMaterialDetails").jScrollPane({ scrollbarWidth: 18, animateTo: true });
        $(".jScrollPaneTrack").css("left", "482px");
    }
}

function UpdateHighlights(result)
{
    $("#content1").html(result).show();
    $("#highlightReferences").cycle({ fx: 'scrollRight', timeout: 6000, cleartype: true, cleartypeNoBg: true });
    $("#highlightDesigners").cycle({ fx: 'scrollRight', timeout: 6000, cleartype: true, cleartypeNoBg: true });
    $("#highlightNews").cycle({ fx: 'scrollRight', timeout: 6000, cleartype: true, cleartypeNoBg: true });
    $("#highlightNews2").cycle({ fx: 'scrollRight', timeout: 6000, cleartype: true, cleartypeNoBg: true });
    $("#highlightPress").cycle({ fx: 'scrollRight', timeout: 6000, cleartype: true, cleartypeNoBg: true });
    $("#highlightVideo").cycle({ fx: 'scrollRight', timeout: 6000, cleartype: true, cleartypeNoBg: true });
    $("#highlightProducts").cycle({ fx: 'scrollRight', timeout: 6000, cleartype: true, cleartypeNoBg: true });
    $("#highlightAgenda").cycle({ fx: 'scrollRight', timeout: 6000, cleartype: true, cleartypeNoBg: true });
    $("#highlightNotices").cycle({ fx: 'scrollRight', timeout: 6000, cleartype: true, cleartypeNoBg: true });
}

function UpdateProductList(result)
{
    $("#content1").html(result).show();
    $("#productsTitle").html(this.title);
    $(".productItem").preload({ onFinish: function() { SetProductListScroll(); } });
}

function SetProductListScroll()
{
    var productList = $("#productList");
    if (productList[0].clientHeight < productList[0].scrollHeight)
    {
        productList.width(788).jScrollPane({ scrollbarWidth: 18 });
        $(".jScrollPaneTrack").css("left", "772px");
    }
}

function UpdateReferenceList(result)
{
    var c = $("#content1");
    c[0].innerHTML = result;
    c.show();
    if (!this.loading)
        $("body").css("cursor", "auto");

    var ddl = $("#dropDownCountries");
    var options = $("#countriesOptions");
    options.appendTo("#submenu");
}

function UpdateProduct(result)
{
    $("#content1").hide();
    $("#content2").html(result);
    var imgs = $("#productImages").html();
    $("#productImages").html("");

    $("#background").hide();
    $("#images").html(imgs).show(sp);
    var img = $("#images img:first");
    $("#main").addClass("white");
    $("#content2").show();

    img.preload({ onFinish: function() { ResizeImage(img); img.css("display", "inline") } });
    SetTextColor(img);
}

function UpdateDesigner(result)
{
    $("#content1").html(result).show();

    var designer = $get("designer");
    if (designer.clientHeight < designer.scrollHeight)
    {
        $(designer).width(788).jScrollPane({ scrollbarWidth: 18 });
        $(".jScrollPaneTrack").css("left", "772px");
    }
}

function UpdateReference(result)
{
    $("#content2").html(result).show();
    $("#referenceDetails .referenceDetail:first").css("display", "block");
}

function UpdatePress(result)
{
    $("#content1").html(result).show();

    $(".imgPress").preload({ onComplete: function(i) { DropShadow(i); } });
    $(document).bind("click", BodyClick);
}

function DropShadow(img)
{
    $(img.original).dropShadow({ top: 2, left: 1, blur: 1, opacity: 0.75, color: "#000" });
}

function UpdateVideo(result)
{
    $("#content1").html(result).show();

    var divVideo = $get("divDlVideo");
    if (divVideo.clientHeight < divVideo.scrollHeight)
    {
        $(divVideo).width(788).jScrollPane({ scrollbarWidth: 18 });
        $(".jScrollPaneTrack").css("left", "772px");
    }
}

function UpdateVideoDetail(result)
{
    $("#content1").html(result).show();
    var linkPlayer = $get("linkVideoDetailPlayer");
    if (linkPlayer.href != null && linkPlayer.href != "")
        flowplayer("linkVideoDetailPlayer", "FlashPlayer/flowplayer-3.0.7.swf");
}

function UpdateContactUs(result)
{
    var contacts = $("#contacts");
    if (contacts.length > 0)
    {
        contacts.html($(result).children("#contacts").html());
    }
    else
        $("#content2").html(result).show();
}

function Open(divID, sender)
{
    $("#" + divID).slideDown(sp);
    $(sender).parent().children("input:first").removeClass("hidden");
}

function Close(divID, sender)
{
    $("#" + divID).slideUp(sp);
    $(sender).addClass("hidden");
}

function OpenDetail(n)
{
    var items = $("#productMenu .productMenuItem");
    if (!$(items[n]).hasClass("disabled"))
    {
        var details = $("#productDetails .productDetail").hide();
        var m;
        for (var i = 0; i < items.length; i++)
        {
            m = items[i];
            if (i == n)
            {
                $(m).children("span:first").addClass("selected");
                $(m).children("input:first").removeClass("hidden");
                $(details[n]).fadeIn(sp);
            }
            else
            {
                $(m).children("span:first").removeClass("selected");
                $(m).children("input:first").addClass("hidden");
            }
        }
    }
}

function CloseDetail(n)
{
    var m = $("#productMenu .productMenuItem")[n];
    $(m).children("span:first").removeClass("selected");
    $(m).children("input:first").addClass("hidden");
    var d = $("#productDetails .productDetail")[n];
    $(d).fadeOut(sp);
}

function NextImage()
{
    var imgs = $("#images img");
    var img = $("#images img:visible");
    var i = parseInt($("#imagesPosition").text());
    var nextImage;
    if (img[0] == imgs[imgs.length - 1])
    {
        nextImage = imgs[0];
        i = 1;
    }
    else
    {
        nextImage = img.next("img");
        i++;
    }
    $("#images img:visible").fadeOut(sp, function() { $(nextImage).preload({ onFinish: function() { ResizeImage($(nextImage)); } }); $(nextImage).fadeIn(sp); });
    $("#imagesPosition").text(i);
    SetTextColor($(nextImage));
}

function PrevImage()
{
    var imgs = $("#images img");
    var img = $("#images img:visible");
    var i = parseInt($("#imagesPosition").text());
    var prevImage;
    if (img[0] == imgs[0])
    {
        prevImage = imgs[imgs.length - 1];
        i = imgs.length;
    }
    else
    {
        prevImage = img.prev("img");
        i--;
    }
    $("#images img:visible").fadeOut(sp, function() { $(prevImage).preload({ onFinish: function() { ResizeImage($(prevImage)); } }); $(prevImage).fadeIn(sp); });
    $("#imagesPosition").text(i);
    SetTextColor($(prevImage));
}

function NextCompanyImage()
{
    var imgs = $("#companyImages img");
    var i = parseInt($("#imagesPosition").text());
    var nextImage;
    if (i == imgs.length)
    {
        nextImage = imgs[0];
        i = 1;
    }
    else
    {
        nextImage = imgs[i];
        i++;
    }
    $(".divCompanyImage").css('backgroundImage', 'url(' + nextImage.src + ')');
    $("#imagesPosition").text(i);
}

function PrevCompanyImage()
{
    var imgs = $("#companyImages img");
    var i = parseInt($("#imagesPosition").text());
    var prevImage;
    if (i == 1)
    {
        prevImage = imgs[imgs.length - 1];
        i = imgs.length;
    }
    else
    {
        prevImage = imgs[i - 2];
        i--;
    }
    $(".divCompanyImage").css('backgroundImage', 'url(' + prevImage.src + ')');
    $("#imagesPosition").text(i);
}

function NextRef()
{
    var divs = $("#referenceDetails .referenceDetail");
    var div = $("#referenceDetails .referenceDetail:visible");
    var i = parseInt($("#imagesPosition").text());
    var nextDiv;
    if (div[0] == divs[divs.length - 1])
    {
        nextDiv = divs[0];
        i = 1;
    }
    else
    {
        nextDiv = div.next(".referenceDetail");
        i++;
    }
    $("#referenceDetails .referenceDetail:visible").fadeOut(sp, function() { $(nextDiv).fadeIn(sp); });
    $("#imagesPosition").text(i);
}

function PrevRef()
{
    var divs = $("#referenceDetails .referenceDetail");
    var div = $("#referenceDetails .referenceDetail:visible");
    var i = parseInt($("#imagesPosition").text());
    var prevDiv;
    if (div[0] == divs[0])
    {
        prevDiv = divs[divs.length - 1];
        i = divs.length;
    }
    else
    {
        prevDiv = div.prev(".referenceDetail");
        i--;
    }
    $("#referenceDetails .referenceDetail:visible").fadeOut(sp, function() { $(prevDiv).fadeIn(sp); });
    $("#imagesPosition").text(i);
}

function GoToRef(img)
{
    var imgs = $("#referenceThumbs img");
    var n = imgs.index(img);
    var divs = $("#referenceDetails .referenceDetail");
    $("#referenceDetails .referenceDetail:visible").fadeOut(sp, function() { $(divs[n]).fadeIn(sp); });
    $("#imagesPosition").text(n + 1);
}

function OnFailed(error)
{
    var stackTrace = error.get_stackTrace();
    var message = error.get_message();
    var statusCode = error.get_statusCode();
    var exceptionType = error.get_exceptionType();
    var timedout = error.get_timedOut();

    // Display the error.    
    var display =
    //            "Stack Trace: " +  stackTrace + "<br/>" +
        "Service Error: " + message + "\n" +
        "Status Code: " + statusCode + "\n" +
        "Exception Type: " + exceptionType + "\n" +
        "Timedout: " + timedout;

    alert(display);
}

// ContextInfo class
ContextInfo = function()
{
    this.AreaID = new Array();
    this.DefinitionID = new Array();
    this.ContentID = new Array();
    this.SortExpression = new Array();
    this.Parameters = new Array();
}
ContextInfo.registerClass("ContextInfo", null, Sys.IDisposable);

// Filter class
Filter = function()
{
    this.ItemID;
    this.Operator;
    this.Value = "";
}
Filter.registerClass("Filter", null, Sys.IDisposable);

// ProductFilter class
ProductFilter = function()
{
    this.Collection = new Filter();
    this.Collection.ItemID = "Collection";
    this.Collection.Operator = "References";

    this.Type = new Filter();
    this.Type.ItemID = "ProductType";
    this.Type.Operator = "References";

    this.Usage = new Filter();
    this.Usage.ItemID = "ProductUsages";
    this.Usage.Operator = "References";
}
ProductFilter.registerClass("ProductFilter", null, Sys.IDisposable);

ProductFilter.prototype = {

    ToString: function()
    {
        var s = this.Collection.Value + "," + this.Type.Value + "," + this.Usage.Value;
    },

    FromString: function(s)
    {
        var values = s.split(",");
        if (values[0])
            this.Collection.Value = values[0];
        else
            this.Collection.Value = "";
        if (values[1])
            this.Type.Value = values[1];
        else
            this.Type.Value = "";
        if (values[2])
            this.Usage.Value = values[2];
        else
            this.Usage.Value = "";
    },

    GetFilters: function()
    {
        var a = new Array();
        a.push(this.Collection);
        a.push(this.Type);
        a.push(this.Usage);
        return a;
    },

    GetValues: function()
    {
        var a = new Array();
        a.push(this.Collection.Value);
        a.push(this.Type.Value);
        a.push(this.Usage.Value);
        return a;
    }
}

// PressFilter class
PressFilter = function()
{
    this.Year = new Filter();
    this.Year.ItemID = "Year";
    this.Year.Operator = "Equal";

    this.Country = new Filter();
    this.Country.ItemID = "Country";
    this.Country.Operator = "References";

    this.Heading = new Filter();
    this.Heading.ItemID = "Heading";
    this.Heading.Operator = "Equal";
}
PressFilter.registerClass("PressFilter", null, Sys.IDisposable);

PressFilter.prototype = {

    ToString: function()
    {
        var s = this.Year.Value + "," + this.Country.Value + "," + this.Heading.Value;
    },

    FromString: function(s)
    {
        var values = s.split(",");
        if (values[0])
            this.Year.Value = values[0];
        else
            this.Year.Value = "";
        if (values[1])
            this.Country.Value = values[1];
        else
            this.Country.Value = "";
        if (values[2])
            this.Heading.Value = values[2];
        else
            this.Heading.Value = "";
    },

    GetFilters: function()
    {
        var a = new Array();
        a.push(this.Year);
        a.push(this.Country);
        a.push(this.Heading);
        return a;
    },

    GetValues: function()
    {
        var a = new Array();
        a.push(this.Year.Value);
        a.push(this.Country.Value);
        a.push(this.Heading.Value);
        return a;
    }
}

function BodyClick()
{
    if (showPressDropDownListDiv)
        showPressDropDownListDiv = false;
    else
        ClosePressDropDown();
    $("#countriesOptions").slideUp(sp);
}

function DropDownListClick(ddlID)
{
    var ddl = $("#" + ddlID);
    if (ddl.is(":visible"))
        ddl.slideUp(sp);
    else
        ddl.slideDown(sp);
}

function ClosePressDropDown()
{
    if (pressDropDownListID != "")
    {
        $(".divPressDropDownOptions:visible").jScrollPaneRemove();
        showPressDropDownListDiv = false;
        $("#" + pressDropDownListID).slideUp(sp);
        pressDropDownListID = "";
    }
}

function FilterReferences(filterType, filterCountry, countryText)
{
    if (!filterType)
        filterType = $("#hfRefType").val();
    if (!filterCountry)
        filterCountry = $("#hfRefCountry").val();

    if (countryText)
        currentRefCountry = countryText;
    $("#dropDownCountriesSelection").text(currentRefCountry);

    var filter = filterType + "," + filterCountry;
    var sort = $("#hfRefSort").val();
    Navigate("References", filter, "References", null, sort);
}

function PressDropDownListClick(ddlID)
{
    if (pressDropDownListID != "" && $("#" + pressDropDownListID).is(":visible"))
        ClosePressDropDown();
    else
    {
        showPressDropDownListDiv = true;
        pressDropDownListID = ddlID;
        var dropDown = $("#" + pressDropDownListID);
        if (dropDown.children("div").length < 20)
            dropDown.height("auto");
        dropDown.slideDown(sp, SetPressDropDownScroll);
    }
}

function SetPressDropDownScroll()
{
    var div = $(".divPressDropDownOptions:visible");
    if (div[0].clientHeight < div[0].scrollHeight)
    {
        div.jScrollPane({ scrollbarWidth: 18 });
        $(".jScrollPaneTrack").css("left", "173px");
    }
    else
        div.css("height", "auto");
}

function SetPressFilterYear(text, value, hfID)
{
    $get("divPressDropDownYearSelectionText").innerHTML = text;
    $get(hfID).value = value;
    ClosePressDropDown();
}

function SetPressFilterCountry(text, value, hfID)
{
    $get("divPressDropDownCountrySelectionText").innerHTML = text;
    $get(hfID).value = value;
    ClosePressDropDown();
}

function SetPressFilterHeading(text, value, hfID)
{
    $get("divPressDropDownHeadingSelectionText").innerHTML = text;
    $get(hfID).value = value;
    ClosePressDropDown();
}

function FilterPress(hfYearID, hfCountryID, hfHeadingID)
{
    var filterExpression = $get(hfYearID).value + "," + $get(hfCountryID).value + "," + $get(hfHeadingID).value;

    currentPressYearText = $get("divPressDropDownYearSelectionText").innerHTML;
    hfPressYearID = hfYearID;
    currentPressYearValue = $get(hfYearID).value;
    currentPressCountryText = $get("divPressDropDownCountrySelectionText").innerHTML;
    hfPressCountryID = hfCountryID;
    currentPressCountryValue = $get(hfCountryID).value;
    currentPressHeadingText = $get("divPressDropDownHeadingSelectionText").innerHTML;
    hfPressHeadingID = hfHeadingID;
    currentPressHeadingValue = $get(hfHeadingID).value;

    Navigate("Press", filterExpression, "Press", null, null, null);
}

function TogglePressOffice()
{
    var divPressOffice = $("#divPressOffice");
    if (divPressOffice.is(":visible"))
        divPressOffice.fadeOut(sp);
    else
        divPressOffice.fadeIn(sp);
}

function ClosePressOffice()
{
    $("#divPressOffice").fadeOut(sp);
}

function ShowContactUsMap()
{
    $get("divContactUsMap").style.visibility = "visible";
}

function HideContactUsMap()
{
    $get("divContactUsMap").style.visibility = "hidden";
}

function ShowNewsDetailVideo()
{
    $("#divNewsDetailVideo").show();

    var linkPlayer = $get("linkNewsDetailPlayer");
    if (linkPlayer.href != null && linkPlayer.href != "")
        flowplayer("linkNewsDetailPlayer", "FlashPlayer/flowplayer-3.0.7.swf");
}

function HideNewsDetailVideo()
{
    $("#divNewsDetailVideo").hide();
}

function ShowDialog(obj)
{
    var parent = $(obj).parent();
    var html = parent.children("div .dialog").html();
    var dialog = $("#dialog").html(html);
    dialog.css("top", parent.offset().top);
    dialog.css("left", parent.offset().left);
    dialog.fadeIn(sp);
}

function HideDialog(obj)
{
    $("#dialog").fadeOut(sp);
}

function ScrollTo(targetID)
{
    $(".divMaterialDetails")[0].scrollTo("#" + targetID);
}

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
