﻿    var oldbgLeft = null;
    var oldbgRight = null;

    function MouseDown(t, evt) {
        oldbgLeft = t.style.backgroundImage;
        oldbgRight = t.parentNode.style.backgroundImage;
        if (t && t.parentNode) {
            t.style.backgroundImage = 'url("/UI/Images/Buttons/button_left_pressed.gif")';
            t.parentNode.style.backgroundImage = 'url("/UI/Images/Buttons/button_right_pressed.gif")';
        }
    }

    function MouseUp(t) {    
        if (t && t.parentNode && oldbgLeft != null && oldbgRight != null) {
            t.style.backgroundImage = oldbgLeft;
            t.parentNode.style.backgroundImage = oldbgRight;
        }
        oldbgLeft = null;
        oldbgRight = null;
    }

    //E-post
    function ConvertEmail(account, domain, extra) {
        document.write(extra + "<a href='mai" + "lto:" + account + "&#64;" + domain + "'>" +
                account + "&#64;" + domain + "<\/a>");
    }

    // A TextualZoomControl is a GControl that displays textual "Zoom In"
    // and "Zoom Out" buttons (as opposed to the iconic buttons used in
    // Google Maps).
    function CloseButton() {
    }

    if(typeof GControl == 'function')
        CloseButton.prototype = new GControl();

    // Creates a one DIV for each of the buttons and places them in a container
    // DIV which is returned as our control element. We add the control to
    // to the map container and return the element for the map class to
    // position properly.
    CloseButton.prototype.initialize = function(map) {        
        var closeDiv = document.createElement("div");
        this.setDivStyle_(closeDiv);

        //container.appendChild(zoomInDiv);
        closeDiv.appendChild(document.createTextNode("Stäng"));

        GEvent.addDomListener(closeDiv, "click", function() {
            $('#googleMaps').fadeOut('fast');
        });

        map.getContainer().appendChild(closeDiv);
        return closeDiv;
    }

    // By default, the button will appear in the top left corner of the
    // map with 7 pixels of padding.
    CloseButton.prototype.getDefaultPosition = function() {    
        return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
    }

    // Sets the proper CSS for the given button element.
    CloseButton.prototype.setDivStyle_ = function(div) {

        div.style.textDecoration = "underline";
        div.style.color = "black";

        div.style.backgroundColor = "white";
        div.style.font = "small Arial";

        div.style.border = "1px solid black";
        div.style.padding = "2px";

        div.style.marginBottom = "3px";
        div.style.textAlign = "center";

        div.style.width = "4em";
        div.style.cursor = "pointer";
    }

    var map = null;
    function CreateMap() {//Create google map
        if (typeof GControl == 'function') {
            if (GBrowserIsCompatible()) {
                map = new GMap2(document.getElementById("googleMaps"));
                //map.setCenter(new GLatLng(59.322342, 18.073636), 15);
                map.addControl(new GSmallMapControl());

                var marker = new GMarker(new GLatLng(59.322369, 18.073575));
                map.addOverlay(marker);

                map.addControl(new CloseButton()); //Add close button
            }
        }
    }

    //Start script
    $(document).ready(function() {
        DisplayLogin();
        CreateMap();
        $("#googleMaps").resizable();        
        $('#containerFooter').find('a:first').click(function(event) { //First a tag            
            $('#googleMaps').fadeIn('fast');
            map.checkResize();
            map.setCenter(new GLatLng(59.322342, 18.073636), 15);
            event.preventDefault(); //Do not follow href link if user has javascript
            event.stopPropagation(); //Prevent click event on container class to happen first time.
            $('#googleMaps').click(function(event) {
                event.stopPropagation(); //No fade out when click on map.
            });
            $('#containerContent').click(function() {
                $('#googleMaps').fadeOut('fast');
            });
        });
        
        KeyListener.init();        
        $('a[href*="#"]').click(function() {
            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
                var $target = $(this.hash);
                $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
                if ($target.length) {
                    var targetOffset = $target.offset().top;
                    $('html,body').animate({ scrollTop: targetOffset }, 1000);
                    return false;
                }
            }
        });


        //For newspaper        
        $("#hrefShowMoreJS").css("display", "block");
        //        $("#hrefShowMoreJS").click(function() {


        //        });

    })

    //Get sites (AJAX)
    var xmlHttp = null;
    var nrOfNewsItems = 3;
    var activeRequest = false;
    function GetNewspapers(id) {
        xmlHttp = initXMLHTTPRequest();
        if (xmlHttp) {
            activeRequest = true;
            var url = "/GetNewspapersAjax.aspx?I=" + nrOfNewsItems + "&ID=" + id;
            xmlHttp.onreadystatechange = FinishedGettingSites;
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
        }
        return;
    }

    function FinishedGettingSites() {
        if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {            
            var result = xmlHttp.responseText;
            var scrollValue = $("#containerNews").offset().top + $("#containerNews").height();                        
            if (result == "&nbsp;")
                HideShowMoreButton();
            else
                $("#containerNews").append(result);
            nrOfNewsItems += 3;
            activeRequest = false;
            $('html,body').animate({ scrollTop: scrollValue }, 1000);
            return false;
        }
    }

    function HideShowMoreButton() {
        $("#hrefShowMoreJS").css("display", "none");
    }

    function initXMLHTTPRequest() {
        var request = null;
        if (window.XMLHttpRequest) request = new XMLHttpRequest();
        else if (window.ActiveXObject) request = new ActiveXObject("Microsoft.XMLHTTP");
        return request;
    }

    KeyListener = {
        init: function() {
            $("#containerContent").bind("keypress", function(e) {
                var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
                var target = e.target.tagName.toLowerCase();
                if (key === 13 && target === 'input') {
                    e.preventDefault();
                    TriggerPost();
                }
            });
            $("#menuSearch").bind("keypress", function(e) {
                var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
                var target = e.target.tagName.toLowerCase();
                if (key === 13 && target === 'input') {
                    e.preventDefault();
                    TriggerSearch();
                }
            });
//            $("#searchContainer").bind("keypress", function(e) {
//                var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
//                var target = e.target.tagName.toLowerCase();
//                if (key === 13 && target === 'input') {
//                    e.preventDefault();
//                    TriggerSearch();
//                }
//            });            
        }
    }   

    function TriggerPost() {
        if ($("#usernameBlob").val() == "") {
            $("#errorMessageLogin").text("Du måste ange ett användarnamn");
            $("#errorMessageLogin").show();
            return;
        }

        if ($("#passwordBlob").val() == "") {
            $("#errorMessageLogin").text("Du måste ange ett passord");
            $("#errorMessageLogin").show();
            return;
        }
        
        $("#errorMessageLogin").hide();
        $("#usernamePost").val($("#usernameBlob").val());
        $("#passwordPost").val($("#passwordBlob").val());
        document.getElementById("loginForm").submit();
    }

    function TriggerSearch() {        
        location.href = $("#hSearchTag").attr("href") + "?Query=" + encodeURIComponent($("#searchBox").val());
    }

    function DisplayLogin() {
        $("#contgainerNoJavascriptText").hide();
        $("#contgainerNoJavascriptLink").hide();
        $("#tableLogIn").show();
        $("#containerLogInPost").show();
    }
