﻿/// <reference path="../../../Default/Scripts/jquery-1.4.1-vsdoc.js" />
/// <reference path="../../../Default/Scripts/jquery.bnet-vsdoc.js" />
/// <reference path="../../../Default/Scripts/MediaCart-vsdoc.js" />

// PNG fix for IE
$(document).ready(function () {
    $(document).pngFix();

    $("#CommentsWrapper").hide();

    $("#ShowRating").click(function () {
        $("#CommentsWrapper").animate({
            height: 'toggle'
        });
        if ($("#ShowRating img").attr("src") == "/Customers/Vtro/VisitTrondheim/Skin/Images/arrow_down.png") {
            $("#ShowRating img").attr("src", "/Customers/Vtro/VisitTrondheim/Skin/Images/arrow_up.png");
        }
        else {
            $("#ShowRating img").attr("src", "/Customers/Vtro/VisitTrondheim/Skin/Images/arrow_down.png");
        }
    });
    $("#HiddenRating").hide();
    $("#VisibleRating div img").mouseover(function () {
        var value = $(this).attr("data-value");
        var type = $(this).attr("data-type");
        $("#VisibleRating div img").each(function () {
            if ($(this).attr("data-type") == type && $(this).attr("data-value") <= value && !$(this).hasClass("locked")) {
                $(this).attr("src", "/Customers/Vtro/VisitTrondheim/Skin/Images/rating_heart.png");
            }
            else {
                if (!$(this).hasClass("locked")) {
                    $(this).attr("src", "/Customers/Vtro/VisitTrondheim/Skin/Images/rating_heart_empty.png");
                }
            }
        });
    });
    $("#VisibleRating div img").mouseout(function () {
        $("#VisibleRating div img").each(function () {
            if (!$(this).hasClass("locked")) {
                $(this).attr("src", "/Customers/Vtro/VisitTrondheim/Skin/Images/rating_heart_empty.png");
            }
        });
    });
    $("#VisibleRating div img").click(function () {
        var value = $(this).attr("data-value");
        var type = $(this).attr("data-type");
        $("#ctl00_ContentAreaPlaceHolder_ctl00_Comments_" + $(this).attr("data-type") + "_" + ($(this).attr("data-value") - 1)).attr("checked", true);
        $("#VisibleRating div img").each(function () {
            if ($(this).attr("data-type") == type && $(this).attr("data-value") <= value) {
                $(this).attr("src", "/Customers/Vtro/VisitTrondheim/Skin/Images/rating_heart.png");
                $(this).addClass("locked");
            }
            else if ($(this).attr("data-type") == type && $(this).attr("data-value") > value) {
                $(this).attr("src", "/Customers/Vtro/VisitTrondheim/Skin/Images/rating_heart_empty.png");
                $(this).removeClass("locked");

            }
        });
    });
    $(".addToMediaCart").click(function () {
        var $this = $(this);
        var fileId = $(this).attr("data-fileid");
        var itemId = $(this).attr("data-itemid");
        var languageId = $(this).attr("data-languageid");
        $this.html("<img src='/Customers/Vtro/VisitTrondheim/Skin/Images/ajax-loading-1.gif' /> Lagrer");
        MediaCart.AddFileToCart(itemId, languageId, fileId, function (data) {
            $this.html("Lagret!");
        }, function (error) {
            $this.html("Oops! Noe gikk galt");
            bNet.log(error);
        });
    });

    $(".removeFileInCart").click(function () {
        var $this = $(this);
        var fileId = $(this).attr("data-fileid");
        var languageId = $(this).attr("data-languageid");
        $this.html("<img src='/Customers/Vtro/VisitTrondheim/Skin/Images/ajax-loading-1.gif' /> Fjerner");
        MediaCart.RemoveFileInCart(fileId, languageId, function (data) {
            $this.html("Fjernet!");
            $this.parents("tr").fadeOut("slow");
        }, function (error) {
            $this.html("Oops! Noe gikk galt");
            bNet.log(error);
        });
    });

    $("#BackgroundImage").maxImage({
        isBackground: true,
        overflow: 'auto'
    });

    if ($("#FocusedPeople li").length > 3) {
        $("#Teasers").slideshow({
            carousel: true,
            autoScroll: false,
            navigation: true,
            transition: 'slide'
        });
    }
    if ($("#FocusedEvents ul li").length > 3) {
        $("#FocusedEvents ul").slideshow({
            carousel: true,
            autoScroll: false,
            navigation: true,
            transition: 'slide'
        });
    }
    if ($(".guides li").length > 3) {
        $(".guides ul").slideshow({
            carousel: true,
            autoScroll: false,
            navigation: true,
            transition: 'slide'
        });
    }
    if ($(".advert li").length > 3) {
        $(".advert").slideshow({
                carousel: true,
                autoScroll: false,
                navigation: true,
                transition: 'slide'
            });
    }
    $("#Teasers li .rollover").hover(function () {
        var rolloverUrl = $(this).attr("data-rollover");
        bNet.log("over", rolloverUrl);
        $(this)
        .find("img.rolloverImage")
        .attr("src", rolloverUrl)
        .end()
        .find("div.rolloverText")
        .show();
    }, function () {
        var orgUrl = $(this).attr("data-imageUrl");
        console.log("out", orgUrl);
        $(this)
        .find("img.rolloverImage")
        .attr("src", orgUrl)
        .end()
        .find("div.rolloverText")
        .hide();
    });
    //replace My trondheim in menu with image
    if ($("#Menu li.last a").length > 0) {
        if ($("#Menu li.last a").html().toLowerCase() == "my trondheim") {
            var mytrondheimCount = 0;
            if ($.cookie("mytrondheim") != null) {
                var cv = $.cookie("mytrondheim");
                var stored = cv.split(";");
                mytrondheimCount = stored.length - 1;
            }
            $("#Menu li.last a").html("<span>" + mytrondheimCount + "</span>");
            $("#Menu li.last").addClass("myTrondheimMenu");
        }
    }
    if ($(".orderLink").length > 0) {
        var url = location.href;
        $(".orderLink").each(function () {
            if (url.indexOf($(this).attr("data-order")) != -1) {
                $(".orderLink").removeClass("active");
                $(this).addClass("active");
            }
        });
    }

    $(".myTrondheimButton").click(function () {

        var category = $(this).attr("data-category");
        var id = $(this).attr("data-id");
        var valueToSet;
        if (category != null)
            valueToSet = category + "," + id + ";";
        else
            valueToSet = id + ";";

        var cookieValue = $.cookie("mytrondheim");
        var added;
        var startPosition = $(this).offset();

        if ($.cookie("mytrondheim") == null) {
            $.cookie('mytrondheim', valueToSet, { path: '/' });
            added = true;
        }
        else if (cookieValue.indexOf(valueToSet) == -1) {
            valueToSet += $.cookie('mytrondheim');
            $.cookie('mytrondheim', valueToSet, { path: '/' });
            added = true;
        }
        else if (cookieValue.indexOf(valueToSet) != -1) {
            bNet.log('Item exists in cookie!');
        }
        if (added) {
            $(".confirmBox").remove();
            $("body").append("<div class=\"confirmBox\"><img src=\"/Customers/Vtro/VisitTrondheim/Skin/Images/bubble-saved.png\" /></div>");
            $(".confirmBox").css({
                position: 'absolute',
                width: '200px',
                height: '200px',
                left: startPosition.left - 163,
                top: startPosition.top - 156
            });
            setTimeout('$(".confirmBox").remove()', 5000);
            $("#Menu li.last span").html(mytrondheimCount += 1);
        }
    });
    $(".removeMyTrondheim").click(function () {
        var category = $(this).attr("data-category");
        var id = $(this).attr("data-id");
        var cookieValue = $.cookie('mytrondheim');
        var valueToSet = "";
        var itemsList = cookieValue.split(";");
        if (itemsList.length < 3) {
            $(".productListWrapper").hide();
        }
        for (var i = 0; i <= (itemsList.length - 2); i++) {
            if (category != null) {
                if (itemsList[i] != category + "," + id) {
                    valueToSet = valueToSet + itemsList[i] + ";";
                    $(this).closest(".productListWrapper").hide();
                }
            }
            else {
                if (itemsList[i] != id) {
                    valueToSet = valueToSet + itemsList[i] + ";";
                    $(this).closest(".productListWrapper").hide();
                }
            }
        }
        $("#Menu li.last span").html(mytrondheimCount -= 1);
        removeCookie('mytrondheim');
        $.cookie('mytrondheim', valueToSet, { path: '/' });
    });

    if ($("#OrderByWrapper ul").length > 0) {
        $("#OrderByWrapper ul li").each(function () {
            if ($(this).find("a:first").hasClass("active")) {
                $(this).addClass("active");
            }
        });
    }
    $(function () {
        $('.googleSearchField').keypress(function (event) {
            var keycode = (event.keyCode ? event.keyCode : event.which);
            var currentLanguage = $(".googleSearchField").attr('data-language');
            if (keycode == '13') {
                event.preventDefault();
                if (currentLanguage == "no")
                    var redirectLocation = "/soek/?q=" + $('.googleSearchField').val();
                else
                    var redirectLocation = "/en/search/?q=" + $('.googleSearchField').val();

                window.location = redirectLocation;
            }
        });
    });

    //CALENDAR
    $(".calendar table td:first-child").addClass("first nav");
    $(".calendar table td:last-child").addClass("last nav");

    if ($("#GeoLatitude").val() != undefined) {
        var geoLatitude = $("#GeoLatitude").val();
    }

    if ($("#GeoLongtitude").val() != undefined) {
        var geoLongtitude = $("#GeoLongtitude").val();
    }
    if ($("#GeoName").val() != undefined) {
        var geoName = $("#GeoName").val();
    }

    if (geoLatitude != undefined && geoLongtitude != undefined && geoName != undefined) {
        getMap(geoLatitude, geoLongtitude, geoName);
    }

});

function removeCookie(name) {
    $.cookie(name, null);
}

function getMap(geoLatitude, geoLongtitude, geoName) {
    bNet.log("");
    var latlng = new google.maps.LatLng(geoLatitude, geoLongtitude, geoName);
    var myOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("LeftContentMap"), myOptions);
    
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: geoName
    });
    var mapBig = new google.maps.Map(document.getElementById("CentralContentMap"), myOptions);

    var marker2 = new google.maps.Marker({
        position: latlng,
        map: mapBig,
        title: geoName
    });

}

