﻿var MoviePlay=false;

$(document).ready(function() {
MovieManager.Init();

$(".PrintButton").click(function(e) {
    e.preventDefault();
    window.print();
});

});


var Misc = {
    ShowMask: function() {
        Misc.SetMaskSize();

        //transition effect		
        $('#Mask').fadeIn(200);
        $('#Mask').fadeTo("fast", 0.8);
    },
    HideMask: function() {
        $('#Mask').hide();
    },
    SetMaskSize: function() {
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#Mask').css({ 'width': maskWidth, 'height': maskHeight });
    },
    CenterDiv: function(id) {
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window position

        $(id).css('top', winH / 2 - $(id).height() / 2 + $(window).scrollTop());
        $(id).css('left', winW / 2 - $(id).width() / 2);
    }
};

/* Movies */

var MovieManager = {
    Movies: [],
    References: [],
    Init: function() {
        //        $('body').append($('#Movie'));
        $(".MovieList a, .ReferenceFacts a.MovieButton, .ReferenceSmallItems a.MovieButton, .MoviePlay").click(function(e) {

        var li = $(this).parents(".MovieListItem");
        var ul = li.parent();
        var index = ul.children(".MovieListItem").index(li);
        var movieFile = MovieManager.Movies[index];
            if (!movieFile) {
                return;
            }

            //Cancel the link behavior
            e.preventDefault();

            //Get the div
            var id = '#MovieWindow';

            Misc.ShowMask();
            Misc.CenterDiv(id);

            //transition effect
            $(id).fadeIn(100);

            jwplayer("MoviePlayer").setup({
                flashplayer: "/Templates2011/JWPlayer/player.swf",
                file: movieFile,
                height: 368,
                width: 640,
                events: { onComplete: function() { MovieManager.HideMovie() } }
            });

            jwplayer("MoviePlayer").play();
            if (typeof (pageTracker) !== "undefined") {
                pageTracker._trackPageview('/Film/' + MovieManager.References[index]);
            }

            //if close button is clicked
            $('.Window .Close').click(function(e) {
                //Cancel the link behavior
                e.preventDefault();
                MovieManager.HideMovie();

            });

            // if mask is clicked
            $('#Mask').click(function() { MovieManager.HideMovie(); });

            $(window).resize(function() {
                Misc.SetMaskSize();
                Misc.CenterDiv('#MovieWindow');
            });
        });
    },
    HideMovie: function() {
        Misc.HideMask();
        $('.Window').hide();
        jwplayer("MoviePlayer").remove();
    }
};

/* fileinputs */

var FileInputManager = {
};

FileInputManager.Init = function() {

    $(".UploadField").filestyle({
        image: "/Templates/Images/folder.png",
        imageheight: 22,
        imagewidth: 30,
        width: 128
    });

};

    $(document).ready(function() {


        /* Movie */


        $(".MovieLink").click(function(e) {

            //Cancel the link behavior
            e.preventDefault();
            pageTracker._trackPageview('/Startsidefilm');
            MoviePlay = true;
            $('#FlashWin').html(myFlash);

            //Get the div
            var id = '#MovieWindow';

            //Get the screen height and width
            var maskHeight = $(document).height();
            var maskWidth = $(window).width();

            //Set heigth and width to mask to fill up the whole screen
            $('#Mask').css({ 'width': maskWidth, 'height': maskHeight });

            //transition effect		
            $('#Mask').fadeIn(200);
            $('#Mask').fadeTo("fast", 0.5);

            //Get the window height and width
            var winH = $(window).height();
            var winW = $(window).width();

            //Set the popup window position
            //	$(id).css('top',  winH/2-$(id).height()/2);
            $(id).css('top', 120);
            $(id).css('left', winW / 2 - $(id).width() / 2);

            //transition effect
            $(id).fadeIn(100);


            //if close button is clicked
            $('.Window .Close').click(function(e) {
                //Cancel the link behavior
                e.preventDefault();

                $('#Mask').hide();
                $('.Window').hide();

                MoviePlay = false;
                $("#FlashWin").html('');
            });

            //if mask is clicked
            /*	$('#Mask').click(function () {
            $(this).hide();
            $('.Window').hide();
            });			
            */
        });

        //TooltipManager.Init();
    });

$(window).resize(function() { 

	var id = '#MovieWindow';

	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	//Set heigth and width to mask to fill up the whole screen
	$('#Mask').css({'width':maskWidth,'height':maskHeight});

	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
          
	//Set the popup window to center
	$(id).css('top',  120);
	$(id).css('left', winW/2-$(id).width()/2);

});

/* ToolTip */

var TooltipManager = {
    "ProductData": undefined,
    "ProductOrderListData":undefined
//    "ItemsLeft" : 6
};

TooltipManager.Init = function() {

    var xOffset = -60;
    var yOffset = -191;

    $(".ProdLink").mouseover(function() {

        this.Tilf = this.href.split("#");

        this.Count = parseInt(this.Tilf[1]);
        this.ProductName = TooltipManager.ProductData[this.Count][0];
        this.ProductText = TooltipManager.ProductData[this.Count][1];
        this.ProductImage = TooltipManager.ProductData[this.Count][2];
        this.ProductLink = TooltipManager.ProductData[this.Count][3];

        //xOffset = -60;
        this.Bg = "TooltipLeft";

        if (this.Count >= TooltipManager.ItemsLeft) {
            xOffset = -328;
            this.Bg = "TooltipRight";
        } else {
            xOffset = -60;
            this.Bg = "TooltipLeft";
        }


        var Content = '<img src="' + this.ProductImage + '" width="265" height="172" class="TooltipImage" /><div class="TooltipInfo"><p class="TooltipHeadline">' + this.ProductName + '</p><p class="TooltipText">' + this.ProductText + '</p></div>';
        var TooltipStr = "<div id='Tooltip' class='" + this.Bg + "'><div class='TooltipInner'>" + Content + "</div><a href='" + this.ProductLink + "' class='ToProductLink'></a></div>";

        $("body").append(TooltipStr);

        var posX, posY;

        var posX = $(this).offset().left;
        var posY = $(this).offset().top;

        $("#Tooltip")
        .css("top", ((posY + yOffset) + "px"))
        .css("left", ((posX + xOffset) + "px"))
        .show();

    });

    $(".ProdLink").mouseout(function() {
        $("#Tooltip").remove();
    });


};

/* TooltipContact */

var TooltipContactManager = {
};

TooltipContactManager.Init = function() {

    var xOffset = [-96, -96, -390, -390];
    var yOffset = 300;



    $(".ContactInfoPopupLink").mouseover(function() {

        this.Tilf = this.href.split("#");

        this.Count = parseInt(this.Tilf[1]);

        this.Type = (this.Count-1) % 4;

        var posX, posY;

        var posX = $(this).offset().left - $(".ContactList").offset().left;
        var posY = $(this).offset().top - $(".ContactList").offset().top; ;


        //alert(posX + ":" + posY + ":" + this.Type);

        $(".ContactInfoPopup" + this.Count)
        .css("top", ((posY + yOffset) + "px"))
        .css("left", ((posX + xOffset[this.Type]) + "px"))
        .show();

    });

    $(".ContactInfoPopupLink").mouseout(function() {
        $(".ContactInfoPopup" + this.Count).hide();
    });


};

/* TooltipProdCombinations */

var TooltipProdCombinationsManager = {
};

TooltipProdCombinationsManager.Init = function() {


    $(".Order .ProductCombinations a").mouseenter(function() {
        $(".ProductCombinationInfo").show();
        $('#Product' + $(this).attr('id')).show();
    })
    .mouseleave(function()
    {
        $(".Order .ProductCombinations a").mouseout(function() {
            $(".ProductCombinationInfo").hide();
            $('#Product' + $(this).attr('id')).hide();
    });

    $(".ProductCombinationInfo li").mouseenter(function()
    {
        $(".ProductCombinationInfo").show();
        $(this).show();
    })
    .mouseleave(function()
    {
        $(".ProductCombinationInfo").hide();
        $(this).hide();
    });
});





};

function writeEmail(email, href) {    
    site = '@kakservice.se';
    document.write('<a href="' + href + '">' + email + site + '</a>'  );
}

/* Logotypes */

var LogotypeManager = {
};

LogotypeManager.Init = function() {

    $(document).ready(function() {
        $(".LogotypeTextbox").autocomplete({
            minLength: 0,
            search: function(event, ui) {
                UpdateMatchingLogotypes();
                return false;
            }
        });
    });

}

function UpdateMatchingLogotypes() {
    var text = $(".LogotypeTextbox").val();
    if (text == "") {
        $("#LogoArchiveResult").hide();
    }
    else {
        $.post("/Lib/Products/Content/GetMatchingForeningar.ashx", { Text: text }, function(data) { UpdateMatchingLogotypesCallback(data); }, "json");
    }
}

function UpdateMatchingLogotypesCallback(data) {
    if (data.length == 0) {
        $result.hide();
        return;
    }

    var allCount = 0;
    var $result = $("#LogoArchiveResult");
    $result.html("");

    for (var i = 0; i < data.length; i++) {
        var item = data[i];
        if (item.Count == 0) {
            continue;
        }
        allCount += item.Count;
        var $option = $("<li><a href=\"#\"><span><img src=\"" + item.ImageUrl + "\" style=\"width:51px\" alt=\"" + item.Name + "\" /></span>" + item.Name + "</a></li>");

        $result.append($option);
    }
    if (allCount == 0)
        $result.hide();
    else
        $result.show();

    $("#LogoArchiveResult a").click(function(event) {
        $(".LogotypeTextbox").val($(this).children("span").children("img").attr("alt"));
        $("#LogoArchiveResult").hide();
        return false;
    });
}
