﻿
function enter(nextfield) {
    if (window.event && window.event.keyCode == 13) {
        var search = document.getElementById(nextfield);
        search.click();
        return false;
    }
}

function load(l, b) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(l, b), 12);
        map.addOverlay(new GMarker(new GLatLng(l, b)));
    }
}

function BookinBoxSearch() {
    jQuery(document).ready(
        function() {
            jQuery(".BookinBoxSearchUrl").trigger('click');
        }
    );
}

$(document).ready(function() {

    $(".randomize").shuffle();

    $("#globalMenuWrapper span.inner").click(function() {
        $(this).parent().click();
    });


    $("img").each(function() {
        $(this).attr("title", $(this).attr("alt"));
    });

    $(".facebook").attr('href', 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(document.location.toString().toLowerCase()) + '&t=' + encodeURIComponent(document.title));

    var script = "\n";
    script += "%3Cdiv id=\"first\"%3E\n";
    script += "%3Cinput type=\"hidden\" name=\"__EVENTTARGET\" id=\"__EVENTTARGET\" value=\"\" /%3E\n";
    script += "%3Cinput type=\"hidden\" name=\"__EVENTARGUMENT\" id=\"__EVENTARGUMENT\" value=\"\" /%3E\n";
    script += "%3C/div%3E\n";
    script += "%3Cscript type=\"text/javascript\"%3E\n";
    script += "//%3C![CDATA[\n";
    //script+="function test(){\n";
    //script+="alert('test');\n";
    //script+="}\n";
    script += "var theForm = document.forms['aspnetForm'];\n";
    script += "if (!theForm) {\n";
    script += "theForm = document.aspnetForm;\n";
    script += "}\n";
    script += "function __doPostBack(eventTarget, eventArgument) {\n";
    script += "if (!theForm.onsubmit || (theForm.onsubmit() != false)) {\n";
    script += "theForm.__EVENTTARGET.value = eventTarget;\n";
    script += "theForm.__EVENTARGUMENT.value = eventArgument;\n";
    script += "theForm.submit();\n";
    script += "}\n";
    script += "}\n";
    script += "//]]%3E\n";
    script += "%3C/script%3E%3Cscript src=\"/WebResource.axd?d=VPLMSNgpcTpv05tcSkDfAw2&amp;t=633703915560000000\" type=\"text/javascript\"%3E\n";
    script += "%3C/script%3E%3Cscript src=\"/ScriptResource.axd?d=LhapeBNkCVs2ugbwKLijhFUwDQrOLa5U4PoIXC7KmLyqudy-aBaHBJOjAo4cspEW00-sahdDKxfsAqEAA6CsH-_h5MWxXjMOXkPn01OiDRI1&amp;t=ffffffffa5342e99\" type=\"text/javascript\"%3E\n";
    script += "%3C/script%3E%3Cscript src=\"/ScriptResource.axd?d=LhapeBNkCVs2ugbwKLijhFUwDQrOLa5U4PoIXC7KmLyqudy-aBaHBJOjAo4cspEWxhnrAehPQtwrSgVS-0GjHIiHa_NDT2j81GHsw42ywgs1&amp;t=ffffffffa5342e99\" type=\"text/javascript\"%3E%3C/script%3E\n";
    script += "%3Cscript type=\"text/javascript\"%3E\n";
    script += "//%3C![CDATA[\n";
    script += "Sys.WebForms.PageRequestManager._initialize('ctl00$ScriptManager1', document.getElementById('aspnetForm'));\n";
    script += "Sys.WebForms.PageRequestManager.getInstance()._updateControls([], [], [], 90);\n";
    script += "//]]%3E\n";
    //script+="%3C/script%3E %3Ca href=\"#\" onclick=\"test()\"%3Etest%3C/a%3E\n";

    if (jQuery("#aspnetForm").html().indexOf("function __doPostBack") == -1) {
        jQuery("#aspnetForm").append(unescape(script));
    }

    $(".leftProductContent #thumbs").scrollable({ vertical: true, size: 4 });
    $("#gallery #thumbs").scrollable({ vertical: true, size: 8 });
    
    jQuery("#gallery div.items div img").each(function() {
        jQuery(this).click(function() {
            jQuery("#MainImage").attr('src', jQuery(this).attr('src'));
            jQuery("#MainImage").attr('alt', jQuery(this).attr('alt'));
        });
    });

    productPresentationImages();

    DynamicBookingDatePicker();
    try {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(DynamicBookingDatePicker);
    } catch (err) {
    }

    $(".btnSearchAvailability").append('<img src="/templates/fjordnorge/styles/fjordnorge/loading.gif" alt="" style="margin-left: 5px;display:none;" />');
    $(".btnSearchAvailability").click(function() {
        $(".btnSearchAvailability img").show();
        $(this).css('color', 'c0c0c0');
    });

    $(".DatePicker").datepicker($.datepicker.regional['no']);
    $('.DatePicker').datepicker('option', { dateFormat: 'dd.mm.yy' });
    $('.DatePicker').datepicker('option', 'minDate', new Date());

    //Links to open in new window
    shareOpenNewWindow();
});

function shareOpenNewWindow() {
    // Share open new window
    $('.share a').click(function(e) {
        e.preventDefault();
        window.open($(this).attr('href'), 'Del', 'height=400, width=600');
    });
}


var bookingWindow;
function DynamicBookingDatePicker() {
    jQuery(".DatePickerCheckIn").datepicker(
            {
                onSelect: function(date) {
                    DynamicBookingDatePickerCheckInSelected(date);
                }
            },
            $.datepicker.regional['no']

        );

    jQuery(".DatePickerCheckOut").datepicker(
            {
                onSelect: function(date) {
                    DynamicBookingDatePickerCheckOutSelected(date);
                }
            },
            $.datepicker.regional['no']

        );

    $('.DatePickerCheckIn, .DatePickerCheckOut').datepicker('option', { showAnim: '', dateFormat: 'dd.mm.yy' });
    $('.DatePickerCheckIn, .DatePickerCheckOut').datepicker('option', 'minDate', new Date());
    $('.DatePickerCheckIn, .DatePickerCheckOut').datepicker('option', { showAnim: 'slideDown' });
}

function DynamicBookingDatePickerCheckInSelected(date) {
    var input = $(".DatePickerCheckIn").datepicker("getDate");
    $('.DatePickerCheckOut').datepicker('option', 'minDate', input);
    var checkin = $(".DatePickerCheckIn").datepicker("getDate");
    checkin.setDate(checkin.getDate() + 1);
    $(".DatePickerCheckOut").attr('value', $.datepicker.formatDate('dd.mm.yy', checkin));
}

function DynamicBookingDatePickerCheckOutSelected(date) {
}

function share(type) {
    var url = location.href;
    var title = document.title;
    var description = '';
    var metas = document.getElementsByTagName('meta');
    for (var x = 0, y = metas.length; x < y; x++) {
        if (metas[x].name.toLowerCase() == "description") {
            description = metas[x];
        }
    }

    if (type == 1) {
        window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url) + '&amp;t=' + encodeURIComponent(title), 'facebookshare', 'toolbar=0,status=0,width=626,height=436'); return false;
    } else if (type == 2) {
        window.open('http://www.nettby.no/user/edit_link.php?name=' + escape(title) + '&url=' + escape(url) + '&amp;description=' + escape(description), 'nettbyshare', 'scrollbars=no,width=450,height=430');
    } else if (type == 3) {
        window.open('http://www.stumbleupon.com/submit?title=' + escape(title) + '&url=' + escape(url), 'stumbleuponshare', 'scrollbars=yes,width=790,height=430');
    } else if (type == 4) {
        window.open('http://digg.com/submit?title=' + escape(title) + '&url=' + escape(url), 'diggshare', 'scrollbars=yes,width=790,height=430');
    } else if (type == 5) {
        window.open('http://del.icio.us/post?title=' + escape(title) + '&url=' + escape(url), 'deliciousshare', 'scrollbars=yes,width=790,height=430');
    } else if (type == 6) {
        window.open('http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + encodeURIComponent(title) + '&c=' + '&u=' + encodeURIComponent(url) + '&l=' + 3, 'MySpaceShare', 'scrollbars=yes,width=790,height=430');
    }
    return false;
}

function productPresentationImages() {
    //make zoomicon trigger fancybox
    $(".zoom").click(function() {
        $(".mediaContainer .media a").click();
    });

    //make fancybox
    $("a[rel=imgGroup], a[rel=inlineMovie], a[rel=inlineMap], a[rel=inlinePPT]").fancybox();

    //click on thumb changes the main image
    $(".thumbs a").not(".embedMovie, .embedMap, .embedPPT").each(function() {
        $(this).click(function() {
            $(".zoom").hide();
            setDefaultImage(this);
            setTimeout('PlaceZoomIcon($(".mediaContainer .media a img").width(),$(".mediaContainer .media a img").height())', 800);
            return false;
        });
    });

    //click on the main image triggers the fancybox from the correct position in list
    $(".mediaContainer .media a").click(function() {
        var currentImg = $(this).attr("rel");
        $("." + currentImg).click();
        return false;
    });

    PlaceZoomIcon($(".mediaContainer .media a img").width(), $(".mediaContainer .media a img").height());
}

function setDefaultImage(lnk) {
    $(".thumbs a").removeClass("current");
    $(lnk).addClass("current");

    $(".thumbs a").children().attr("width", 84);
    $(".thumbs a").children().attr("height", 55);

    var child = $(lnk).children();
    child.attr("width", 80);
    child.attr("height", 51);

    var media = ".mediaContainer .media a";
    $(media).attr("href", $(lnk).attr("href"));
    $(media).children().attr("src", $(lnk).attr("href") + "&w=350&h=233");
    $(media).children("img").attr("alt", $(lnk).children("img").attr("alt"));
    $(media).children("img").attr("title", $(lnk).children("img").attr("alt"));

    //set rel attribute so we can find it in the list
    $(media).attr("rel", $(lnk).attr("rel"));
}

function PlaceZoomIcon(width, height) {
    var origWidth = width;
    width = ($(".media table").width() / 2 - width / 2);
    height = $(".media table").height() / 2 - height / 2;

    if ($.browser.msie) {
        $(".zoom").css("position", "absolute");
        $(".zoom").css("right", (width + 5) + "px");
        $(".zoom").css("bottom", (height + 5) + "px");
    } else {
        if (origWidth > 334) {
            $(".zoom").css("right", -(origWidth / 2 - 15) + "px");
            $(".zoom").css("bottom", "22px");
        } else {
            $(".zoom").css("right", "29px");
            $(".zoom").css("bottom", "5px");
        }
    }
    $(".zoom").fadeIn();
}

// Hack for å få IE Webpart Editor til å virke
$(document).ready(function() {
    if ($.browser.msis) {
        $('.epitoolbutton').mouseenter(function() {
            $('#ctl00_MainRegion_FullWidth_RightColumn_ctl00_ThemeEditorZone_PropertyDataEditorPart_ctl01_Editor_Editor').val(
            $("#ctl00_MainRegion_FullWidth_RightColumn_ctl00_ThemeEditorZone_PropertyDataEditorPart_ctl01_Editor_Editor_Editor").contents().find(".editmode").html()
        );
        });
    }
});

