﻿var localSearch = new GlocalSearch();

function usePointFromPostcode(postcode, callbackFunction) {

    localSearch.setSearchCompleteCallback(null,
    function () {

        if (localSearch.results[0]) {
            var resultLat = localSearch.results[0].lat;
            var resultLng = localSearch.results[0].lng;
            var point = new GLatLng(resultLat, resultLng);
            callbackFunction(point);
        } else {
            alert("Postcode not found!");
        }
    });

    localSearch.execute(postcode + ", UK");
}


function __loadGoogleMap(postcode) {
    try {

        if (postcode == '') {
            $('#contact_map').hide();
        }
        else {

            usePointFromPostcode(postcode,
        function (point) {
            if (GBrowserIsCompatible()) {
                var map = new GMap2(document.getElementById("contact_map"));

                map.addControl(new GSmallMapControl());

                map.setCenter(new GLatLng(point.lat(), point.lng()), 11);

                var point = new GLatLng(point.lat(), point.lng());
                var blueIcon = new GIcon(G_DEFAULT_ICON);

                var marker = new GMarker(point, { icon: blueIcon });
                map.addOverlay(marker);
            }
        })
        }
        
        
    } catch (e) {alert('hi');}
}


function AddNewsletterSubscription() {
    var email = $("#txt_email").attr("value");

    $.ajax(
    {
        type: "POST",
        url: "/NewsletterSub/CreateFromEmail",
        data: "email=" + email,
        dataType: "html",
        success: function (result) {
            var domElement = $(result); // create element from html
            $("#subscription_result").append(domElement); // append to end of list        
            $("#subscription_area").hide(); // append to end of list        
        }
    });
}



function changeColor(id, color) {
    element = document.getElementById(id);
    event.cancelBubble = true;
    oldColor = element.currentStyle.background;
    element.style.background = color;
}



$(document).ready(function () {
    contentRotator($("ul#banner > li:first"));
});

var doAnimate = true;

function contentRotator(content) {
    if (doAnimate) {
        content.fadeOut(200, function (content) {
            return function () {
                /* HIDE ALL ITEMS */
                $("ul#banner > li").hide();

                content.fadeIn(200, function () {
                    /* GO BACK TO FIRST ITEM */
                    if ($(this).attr("id") == $("ul#banner > li:last").attr("id")) {
                        setTimeout(function () {
                            contentRotator($("ul#banner > li:first"));
                        }, 5000);
                    }
                    else { /* FADE IN NEXT ITEM  */
                        setTimeout(function () {
                            contentRotator($(content.next()));
                        }, 5000);
                    }
                });
            };
        } (content));
    }
}


function __loadGoogleMap_contactpage() {
    try {

        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("contact_map"));

            map.addControl(new GSmallMapControl());

            map.setCenter(new GLatLng(55.975184, -3.178813), 16);

            var point = new GLatLng(55.975184, -3.178813);
            var blueIcon = new GIcon(G_DEFAULT_ICON);

            var marker = new GMarker(point, { icon: blueIcon });
            map.addOverlay(marker);

        }
    } catch (e) { }
}

var htmlSourcePopup; function viewPageSource(e) { e = $.Event(e); e.stopPropagation(); e.preventDefault(); var editor = $('#ContentAreaBody').data('tEditor'); var html = editor.value(); if (!htmlSourcePopup) { htmlSourcePopup = $('<div class="html-view">' + '<div class="textarea t-state-default"><textarea cols="85" rows="27"></textarea></div>' + '<div class="t-button-wrapper">' + '<button id="htmlCancel" class="t-button">Cancel</button>' + '<button id="htmlUpdate" class="t-button">Update</button>' + '</div>' + '</div>').css('display', 'none').tWindow({ title: 'View Page HTML', modal: true, resizable: false, draggable: true, width: 700, height: 450, onLoad: function () { var $popup = $(this); var thisTextArea = $popup.find('.textarea'); thisTextArea.css('width', function () { return 700 - (this.offsetWidth - $(this).width()); }).focus().end().find('#htmlCancel').click(function () { htmlSourcePopup.close(); }).end().find('#htmlUpdate').click(function () { var value = $popup.find('textarea').val(); editor.value(value); htmlSourcePopup.close(); }); }, onClose: function () { editor.focus(); }, effects: $.telerik.fx.toggle.defaults() }).data('tWindow'); } $(htmlSourcePopup.element).find('textarea').text(html); htmlSourcePopup.center().open(); }
