﻿jQuery.fn.exists = function () {
    return jQuery(this).length > 0;
};

jQuery.noConflict();

jQuery(function ($) {
    /* Property Gallery colorbox */
    $('a[rel="gallery"]').colorbox({ opacity: 0.25, innerHeight: "515px", innerWidth: "668px", scalePhotos: true });

    $("#viewImages").click(function () {
        $('a[rel="gallery"]').eq(5).trigger("click");
    });


    /* Brochure print */
    $("#print").click(function () {
        window.print();
    });


    /* Contact Form validation */
    $("#contactForm").submit(function () {
        return validate();
    });

    function validate() {
        var valid = true;
        var value = $("#senderName").val();
        if (value.length > 1) {
            $("#senderName").removeClass("alertBorder");
        } else {
            $("#senderName").addClass("alertBorder");
            valid = false;
        }

        value = $("#senderEmail").val();
        if ((value.indexOf(".") > 2) && (value.indexOf("@") > 0)) {
            $("#senderEmail").removeClass("alertBorder");
        } else {
            $("#senderEmail").addClass("alertBorder");
            valid = false;
        }

        value = $("#senderPhone").val();
        if ((value - 0) == value && value.length > 0) {
            $("#senderPhone").removeClass("alertBorder");
        } else {
            $("#senderPhone").addClass("alertBorder");
            valid = false;
        }

        value = $("#message").val();
        if (value.length > 1) {
            $("#message").removeClass("alertBorder");
        } else {
            $("#message").addClass("alertBorder");
            valid = false;
        }

        return valid;
    }


    /* Brochure Map */
    if ($("#propertyMap").exists()) {
        var mapOptions = {
            zoom: 12,
            center: new google.maps.LatLng(parseFloat($("#propertyLat").val()), parseFloat($("#propertyLng").val())),
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            scrollwheel: false
        };

        var propertyMap = new google.maps.Map(document.getElementById("propertyMap"), mapOptions)

        var hostName = window.location.hostname;
        if (window.location.hostname.indexOf("www") > -1) {
            var components = hostName.split('.');
            hostName = [components[1], components[2]].join('.');
        };
        var propertyIcon = "/static/images/pin.png";

        var propertyLatLng = propertyMap.getCenter();
        var propertyMarker = new google.maps.Marker({
            position: propertyLatLng,
            map: propertyMap,
            icon: propertyIcon
        });
    }


    function activateSlider() {
        var mygallery = new simpleGallery({
            wrapperid: "agentPropertySlider",
            dimensions: [318, 197],
            imagearray: my_array,
            autoplay: [true, 5000, 2],
            persist: false,
            fadeduration: 400,
            oninit: function () {
                var next = "<img alt='previous' class='navLeft' src='/static/images/simpleGallery/arrow-left.png' title='Previous' />";
                var prev = "<img alt='next' class='navRight' src='/static/images/simpleGallery/arrow-right.png' title='Next' />";
                var address = "<span class='address'></span>";

                $(".desc-nav-bar").append(next + address + prev);
                $(".desc-nav-bar .navRight").click(function () { mygallery.navigate('next'); });
                $(".desc-nav-bar .navLeft").click(function () { mygallery.navigate('prev'); });
            },
            onslide: function (curslide, i) {
                $(".desc-nav-bar .address").text($(".gallerydesctext").text());
                $('.desc-nav-bar').css({ zIndex: '1001' });
            }
        });
    }

    function activateslideshow() {
        $('.slideShow').cycle({
            fx: 'fade'
        });
        $('#splash .slideShow').cycle({
            fx: 'shuffle'
        });
        $('.mule.slideShow').cycle({
            fx: 'shuffle',
            speed: 1500
        });
    }

    function callOnloadFunctions() {
        if ($(".brochure").exists())
            activateSlider();
        if ($(".slideShow").exists())
            activateslideshow();
    }

    window.onload = callOnloadFunctions;
});
