
var firstviews = null;
var secondviews = null;

$(document).ready(function () {

    var zIndex = 9;


    $(".big-banner").nervSlider();


    $(".print").click(function (e) {
        e.preventDefault();
        window.print();
    });


    $(".small_dark").click(function () {
        $("body").addClass("dark-template");
        $("body").removeClass("large-font");
    })

    $(".small_light").click(function () {
        $("body").removeClass("dark-template");
        $("body").removeClass("large-font");
    })

    $(".large_light").click(function () {
        $("body").removeClass("dark-template");
        $("body").addClass("large-font");
    })

    $(".large_dark").click(function () {
        $("body").addClass("dark-template");
        $("body").addClass("large-font");
    })

    sidebarReplace();

    $(".dateEntry").datepicker({ dateFormat: 'dd/mm/yy' });

    $(window).resize(function () {
        sidebarReplace();
        repositionViewMore();
        $(".fullwide table th").removeClass("last");
        $(".fullwide table th:visible").last().addClass("last");
    });

    $(".searchbar input").focus(function () {
        if ($(this).val() == "Search") {
            $(this).val("");
        }
    });


    $(".big-button-nav ul li").hover(function () {

        $(this).find("a").addClass("hovered");

    }, function () {

        $(this).find("a").removeClass("hovered");
    });

    if ($("#map").is(":visible")) {
        initialize_map();
    }

    $("#ctaxform").nerValid({ whileYouType: true, imageValidation: true, formFieldWrapper: ".inputwrapper" });


    $(".slider-trigger").live("click", function (e) {
        e.preventDefault();

        if ($(this).hasClass("active")) {
            $(this).removeClass("active")
        } else {
            $(this).addClass("active")
        }

        $(this).next().stop(true, true).slideToggle("fast");

    });


    $("#tabcontainer li a").live("click", function (e) {
        e.preventDefault();
        var id = $(this).attr("id").split("_");
        var container = $(this).parent();
        container.siblings().removeClass("active");
        container.addClass("active");
        if ($("#" + id[0]).not(":visible")) {
            $("#" + id[0]).siblings(".tabcontent").hide();
            $("#" + id[0]).fadeIn();
        }
    });

    if ($(".preston-popup").length > 0) {
        $(".preston-popup-overlay").css("width", $(window).width());
        $(".preston-popup-overlay").css("height", $(document).height());
        $(".preston-popup-overlay").css("opacity", "0.2");
    }

    $(".deny").live("click", function () {
        $(".preston-popup-overlay").hide();
        $(".preston-popup").hide();
    });

    imageReplaceSelects();

    firstviews = $(".box_viewmore").eq(0);
    secondviews = $(".box_viewmore").eq(1);
    repositionViewMore();
    parsebuilderForms();
});


var parsebuilderForms = function () {

    var formMasterForm = $(".fm_formmasterForm");
    var formMasterTxtIn = formMasterForm.find('input[type=text]');
    var formMasterTAreas = formMasterForm.find('textarea');
    var formlabels = formMasterForm.find("label");
    var formSelects = formMasterForm.find("select");
    var formSubmit = formMasterForm.find("input[type=submit]");
    var fileUploadField = formMasterForm.find("input[type=file]");

    $.each(fileUploadField, function () {
        //$(this).wrap('<div class="inputwrapper"/>')
        $(this).attr("size", "29")
        $(this).width(100)
    });

    $.each(formMasterTxtIn, function () {
        if ($(this).hasClass("date")) {
            $(this).wrap('<div class="inputwrapper datefield"/>');
            $(this).addClass('inputtext');
        } else {
            $(this).wrap('<div class="inputwrapper"/>')
            $(this).addClass('inputtext');
        }

    });


    $.each(formSelects, function () {
        $(this).wrap('<div class="inputwrapper"/>')
        $(this).addClass("inputselect");

    });

    $.each(formMasterTAreas, function () {
        $(this).wrap('<div class="textareawrapper" style="width:400px"/>')
    });

    $.each(formSubmit, function () {
        $(this).wrap('<div class="formcontent" style="padding:0"/>');
        $(this).addClass("submit");
        $(this).after('<div class="clear"/>')
    });




}




var initialize_map = function () {

    var latlng = new google.maps.LatLng(51.8979906, -2.0713084);

    var myOptions = {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: false,
        navigationControl: true,
        mapTypeControl: true,
        scaleControl: true,
        zoomControl: true,
        draggable: true
    }

    var map = new google.maps.Map(document.getElementById("map"), myOptions);
}


var imageReplaceSelects = function () {

    var iSelect = $("select.inputselect");
    var zIndex = 999;

    $(".inputwrapper").each(function () {
        if ($(this).find("input").hasClass("dateEntry")) {
            $(this).css({ zIndex: 999 });
        } else {
            $(this).css({ zIndex: zIndex });
            zIndex--;
        }
    });

    $(".selectreplace").remove();
    if (iSelect.length > 0) {
        $.each(iSelect, function (index, elem) {
            var parent = $(this).parent(); //selectbox container
            var menu = $(this).find("option:selected").text(); //option that is selected
            parent.css("z-index", "999");
            parent.append('<div class="selectreplace" id="select_' + index + '"><p>' + menu + '</p></div>'); //append image replacement box
            parent.append("<div class='subber'></div>"); //insert dropdown box
            //parent.find("#select_"+index).css("z-index",zIndex);
            //zIndex--;

            $(this).find("option").each(function (i, e) {
                $("#select_" + index).parent().find(".subber").append('<div class="sub" id="option_' + i + '"><p>' + $(this).text() + "</p></div>");
            });
            $(this).hide();
        });

    }

    $(".selectreplace p").bind("click", function () {
        var submenu = $(this).parent().parent().find(".subber");
        if (submenu.is(":visible")) {
            submenu.hide();
        } else {
            submenu.show();
        }

    });

    $(".subber .sub p").hover(function () { $(this).addClass("hovered") }, function () { $(this).removeClass("hovered") });

    $(".subber .sub p").bind("click", function () {
        var optionId = $(this).parent().attr("id").split("_")[1];
        var container = $(this).parent().parent().parent();

        container.find(".selectreplace p").html($(this).html());
        container.find(".subber").hide();
        switchSelect(container.find("select"), optionId);
    });

}

function switchSelect(select, option) {
    select.find("option").removeAttr("selected"); //remove all selected values there might be
    select.find("option").eq(option).attr("selected", "selected");
}


var repositionViewMore = function () {

    var firstelems = firstviews.find("li").clone();
    var secondelems = secondviews.find("li").clone();

    if ($(".wrapper").width() <= 480) {
        $(".box").each(function (i, e) {
            var list = $(this).find("ul");
            $(this).css("position", "relative")
            if (list.find("li.viewr").length > 0) {
                list.find("li.viewr").show();
            } else {
                if (i < 3) {
                    list.append(firstelems[i]);
                    list.find("li").last().addClass("viewr").css({ position: "absolute", bottom: "10px", left: "12px" });
                } else {
                    list.append(secondelems[i - 3]);
                    list.find("li").last().addClass("viewr").css({ position: "absolute", bottom: "10px", left: "12px" });
                }
            }
            $("li.viewr a").css("color", "#00B5E0")
        });

        $(".services-container").each(function (i, e) {
            var list = $(this).find("ul");
            $(this).css("position", "relative")
            if (list.find("li.viewr").length > 0) {
                list.find("li.viewr").show();
            } else {
                if (i < 3) {
                    list.append(firstelems[i]);
                    list.find("li").last().addClass("viewr").css({ position: "absolute", bottom: "10px", left: "0px", fontSize: "12px" });
                } else {
                    list.append(secondelems[i - 3]);
                    list.find("li").last().addClass("viewr").css({ position: "absolute", bottom: "10px", left: "0px", fontSize: "12px" });
                }
            }
            $("li.viewr a").css({ color: "#00B5E0", fontSize: "12px" })
        });
    } else {
        $(".box").each(function (i, e) {
            var list = $(this).find("ul");

            if (list.find("li.viewr").length > 0) {
                list.find("li.viewr").hide();
            }

        });
        $(".services-container").each(function (i, e) {
            var list = $(this).find("ul");

            if (list.find("li.viewr").length > 0) {
                list.find("li.viewr").hide();
            }

        });
    }
}



var sidebarReplace = function () {

    if ($(".wrapper").width() <= 480) {
        if ($(".content-sidebar").is(":visible")) {
            var sidebarCopy = $(".content-sidebar").clone();
            $(".content-sidebar").remove();
            sidebarCopy.insertAfter(".content-text-sidebar");
            $(".sidebar-button").hide();
            var sidebarbuttoncopy = null;
            if ($(".clonedbutton").length < 1) {
                sidebarbuttoncopy = $(".sidebar-button").clone().addClass("clonedbutton");
                sidebarbuttoncopy.insertAfter(".breadcrumb").css({ marginLeft: "auto", marginRight: "auto" });
            }
            $(".clonedbutton").show();

        }
    } else {
        if ($(".content-sidebar").is(":visible")) {
            var sidebarCopy = $(".content-sidebar").clone();
            $(".content-sidebar").remove();
            $(".clonedbutton").remove();
            sidebarCopy.insertBefore(".content-text-sidebar");
            $(".sidebar-button").css("display", "block");
        }
    }

}


