﻿var animation = false;
function Move(direction, value) {
    animation = true;
    $('.newslist').stop().animate({
        left: direction + value
    }, 500, function () {
        // Animation complete.
        animation = false;
    });
 
}
$(document).ready(function () {

    if ($.address) {
        $.address.change(function (event) {
            var hash = event.value.replace("/", "#");
            if (hash.length > 1) {

                var currentposition = 0;

                var position = $('.newslist').position();
                currentposition = position.left;

                var page = hash.substring(hash.indexOf('page=') + 5);
                var value = (parseInt(page) - 1) * 611;

                if (parseInt(value) > parseInt(currentposition)) {
                    Move('-=', (parseInt(value) + parseInt(currentposition)));
                }
                else if (parseInt(value) < parseInt(currentposition)) {
                    Move('+=', (parseInt(value) + parseInt(currentposition)));
                }
            }
            else {
                Move('', 0);
            }
        });
    }

    $('.anleft').click(function (e) {
        //Check if element sholud move
        var value = $("#ctl00_cph_fieldHidden").attr("value");
        var r = (value - 1) * 611;
        var position = $('.newslist').position();
        if (position.left == -r || animation == true) return false;

        //Change page
        var page = getParameterByName('page');
        if (page == '') {
            window.location = '#page=2';
        }
        else {
            window.location = '#page=' + (parseInt(page) + 1);
        }
    });
    $('.anright').click(function (e) {
        //Check if element sholud move
        var position = $('.newslist').position();
        if (position.left == 0 || animation == true) return false;

        //Change page
        var page = getParameterByName('page');
        if (page != '' && page != undefined) {
            window.location = '#page=' + (parseInt(page) - 1);
        }
    });


    $('#accordion .head').click(function (e) {
        if ($(e.target).is(":input")) {
            return true;
        } else {
            $(this).next().slideToggle('fast');
            $(this).find(">:first-child").toggleClass('closed');
            return false;
        }
    }).next().hide();

    var current = null;
    $('#accordion .imgchannel').mousemove(
    function (e) {
        var eleOffset = $(this).offset();

        $(this).next().css('top', e.pageY - eleOffset.top + 23); //.offset().top = e.pageX;
        $(this).next().css('left', e.pageX - eleOffset.left + 10); //.offset().left = e.pageY;
    });
    $('#accordion .imgchannel').hover(
    function () {
        $(this).next().show();
    },
    function () {
        $(this).next().hide();
    }

    );

    if (window.location.pathname.indexOf('produkter/bredband/mobilt-bredband') > -1) {
        $('#accordion').find(".head").next().slideToggle('fast');
        $('#accordion').find(".head").find(">:first-child").toggleClass('closed');

    }
    else {
        $('#accordion').find(">:first-child").next().slideToggle('fast');
        $('#accordion').find(">:first-child").find(">:first-child").toggleClass('closed');
    }

    if (window.location.pathname.indexOf('bestilling1') > -1) {
        $('label').last().html('Jeg har lest og akseptert <a target="_blank" href="../upload/vilkar/generelle_vilkar.pdf">leveringsbetingelsene</a>');

    }



    // Add pdf icons to pdf links
    $(".attachments a").addClass("download"); //default icon, might be overridden later    
    $(".attachments a[href$='.pdf']").addClass("pdf");
    $(".attachments a[href$='.mp3'], .attachments a[href$='.wav']").addClass("audio");
    $(".attachments a[href$='.doc'], .attachments a[href$='.rtf'], .attachments a[href$='.docx']").addClass("word");
    $(".attachments a[href$='.xls'], .attachments a[href$='.xlsx'], .attachments a[href$='.csv']").addClass("excel");
    $(".attachments a[href$='.jpg'], .attachments a[href$='.png'], .attachments a[href$='.gif'], .attachments a[href$='.tif'], .attachments a[href$='.tiff'], .attachments a[href$='.bmp']").addClass("picture");
    $(".attachments a[href$='.avi'], .attachments a[href$='.flv'], .attachments a[href$='.wmv'], .attachments a[href$='.mov'], .attachments a[href$='.divx'], .attachments a[href$='.mpeg']").addClass("video");
    $(".attachments a[href$='.zip'], .attachments a[href$='.rar']").addClass("zip");


    /*
    // Add email icons to email links
    $("a[href^='mailto:']").addClass("email");

    //Add external link icon to external links - 
    $('a').filter(function() {
    //Compare the anchor tag's host name with location's host name
    return this.hostname && this.hostname !== location.hostname;
    }).addClass("external");
    */

    //You might also want to set the _target attribute to blank
    /*
    $('a').filter(function() {
    //Compare the anchor tag's host name with location's host name
    return this.hostname && this.hostname !== location.hostname;
    }).addClass("external").attr("target", "_blank");
    */
});
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\#]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function fillTextBox(place) {
    switch (place) {
        case 'name':
            $(".delName").val($(".newName").val());
            if ($(".invoName").length) {
                $(".invoName").val($(".newName").val());
            }
            break;
        case 'surname':
            $(".delSurname").val($(".newSurname").val());
            if ($(".invoSurname").length) {
                $(".invoSurname").val($(".newSurname").val());
            }
            break;
        default:
            break;
    }
}

