﻿$(document).ready(function() {
    // ----- RIGHT COLUMN LAST AD MARGIN REMOVAL
    $('.content-right>div:last').css('margin-bottom', '0');
    // ----- RIGHT COLUMN CMS IMAGE CONTROL HIDE REMOVE IF NO IMAGE
    $('.content-right div.editableDisplay').click(function() {
        if($(this).children('img').attr('src') == undefined)
        {
            $('.content-right input[value="Remove Image"]').hide(10);
        }
    });
    // ----- VALIDATION FAIL BORDER APPLY
    function ValidationCheck() {
        $('.standard-form span label span').each(function() {
            if ($(this).css('visibility') != "hidden" && $(this).css('display') != "none") {
                $(this).parent('label').siblings('input').css('border', 'solid 1px #d90000');
            }
        });
    }
    $('.btn.bg-colour').click(function() {
        ValidationCheck();
    });
    // ----- CLICK ACTION FOR RIGHT COLUMN ADVERTS
    $('.content-right>div').click(function() {
        if($(this).children('div').children('p').children('a').length > 0) {
            var getDest = $(this).children('div').children('p').children('a').attr('href');
            window.location == getDest;
        }
    });
    // ----- ADMIN STYLING FOR HEADERS [applies class to edit panel]
    $('h1>span').each(function() {
        if ($(this).children("input").length > "0") {
            $(this).attr('class', 'editable_panel');
        }
    });
    // ----- RESET FORM FUNC
    $('.btn.reset').click(function() {
        var resetConfirm = confirm("Are you sure you want to reset the form?\n\r\n\rAll changes will be lost.")
        if (resetConfirm) {
            var formWrapObj = $('.standard-form span');
            $(formWrapObj).each(function() {
                var getFirstChildVal = $(this).children('select').children('option:eq(0)');
                if ($(this).children('input').attr('type') == "submit" || $(this).attr('class') == "field-flow") {
                    return;
                } else {
                    $(this).children('input').val("");
                    $(this).children('textarea').val("");
                    $(this).children('select').val(getFirstChildVal);
                }
            });
        } else {
            return;
        }
    });
    
    //contact form hide/show functionality:
    $('.field-flow select').change(function() {
        var getSelection = $(this).val();
        if (getSelection == "PleaseSelect") {
            return;
        } else {
            $('.standard-form div').slideUp('fast');
            $('.standard-form div#' + getSelection + 'Form').slideDown('slow');
        }
    });
    //property gallery functionality:
    $('.property-gallery ul li').click(function() {
        // style change
        $(this).siblings().removeClass('bg-colour');
        $(this).addClass('bg-colour');
        //image change
        var imageSrc = $(this).children('a').children('img').attr('src');
        $('.large-image').children('img').attr('src', imageSrc);
        //Image description change
        $("#PhotoDescription").html($(this).attr('title'));

    });
    //next & previous clicky
    $('.gallery-nav a').click(function() {
        var getCurrent = $('.gallery-nav ul li.on').attr('id');
        var getTotal = $('.gallery-nav ul li').length;
        newCurrent = getCurrent.replace("set", "");
        if ($(this).attr('class') == "next") {
            if (newCurrent == getTotal) {
                //do nothing
            } else {
                var nextObject = $('.gallery-nav ul li.on').next();
                $('.property-gallery>ul>li').hide();
                newCurrent++;
                $('.gallery-nav ul li.on').removeAttr('class');
                $(nextObject).attr('class', 'on');
                $('.property-gallery>ul>li.' + newCurrent).show();
            }
        } else if ($(this).attr('class') == "previous") {
            if (newCurrent == "1") {
                //do nothing
            } else {
                var prevObject = $('.gallery-nav ul li.on').prev();
                $('.property-gallery>ul>li').hide();
                newCurrent--;
                $('.gallery-nav ul li.on').removeAttr('class');
                $(prevObject).attr('class', 'on');
                $('.property-gallery>ul>li.' + newCurrent).show();
            }
        }
    });
    galleryCount();
    ValidationCheck();
    //indicator click
    $('.gallery-nav ul li').click(function() {
        var thisId = $(this).attr('id');
        newId = thisId.replace("set", "");
        $(this).siblings('li').removeAttr('class');
        $(this).attr('class', 'on');
        $('.property-gallery>ul>li').hide();
        $('.property-gallery>ul>li.' + newId).show();
    });
});


// ----- PROPERTY GALLERY FUNCTIONALITY :
function galleryCount() {
    var imageList = $('.property-gallery>ul>li');
    var setCount = 1;
    for (var i = 0; i <= imageList.length; i++) {
        $(imageList[i]).attr('class', setCount);
        if ((i + 1) % 6 == 0) {
            setCount++;
            if (i + 1 < imageList.length) {
                $('.gallery-nav ul').append('<li id="set' + setCount + '"></li>');
            }
        }
    }
}

