var pageurl = (location.pathname.lastIndexOf('/') === location.pathname.length - 1) ? location.pathname.toLowerCase() + 'default.aspx' : location.pathname.toLowerCase();
var sectionurl = location.pathname.substring(0, location.pathname.lastIndexOf('/') + 1).toLowerCase() + 'default.aspx';

function locmatch(c, l) {
    $(c).each(function() {
        var nlink = '';
        if ($('a:eq(0)', this).attr('href')) {
            var nlink = ($('a:eq(0)', this).attr('href').lastIndexOf('/') === $('a:eq(0)', this).attr('href').length - 1) ? $('a:eq(0)', this).attr('href').toLowerCase() + 'default.aspx' : $('a:eq(0)', this).attr('href').toLowerCase();
            console.log(nlink)
        }
        if (l === nlink) {
            $(this).addClass('selected');
            $(this).toggleClass('closed');
        }        
    });
    return;
}

function initTabs() {
    //$('.tab').hide().eq(0).show();
      
    $('#tabs li a').click(function() {
        $('.tab').hide().eq($('#tabs li a').removeClass('selected').index(this)).show();
        $(this).addClass('selected');
    }).eq(0).addClass('selected');
}

function trim(s) {
    return s.replace(/^\s+|\s+$/, '');
}

//print image
function PrintImg(imgSrc) {

    window.open('/Print.aspx?Id=' + imgSrc, '', 'width=100,height=100,left=0,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no,top=0');
    return false;
}

function Mid(str, start, len) {
    // Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
        iEnd = iLen;
    else
        iEnd = start + len;
    return String(str).substring(start, iEnd);
}

function printHousePage() {
    var photoBox = document.getElementById('bigPhotoImg');

    var photoBoxSrc = photoBox.src;
    var index = photoBoxSrc.indexOf('/Common/');
    if (index >= 0) {
        var str = Mid(photoBoxSrc, index, photoBoxSrc.length)
        
    }

    window.open('/Print.aspx?Id=' + str, '', 'width=100,height=100,left=0,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no,toolbar=no,top=0');
    return false;

}
    

function validateRegisterForm() {
    var prefix = 'ctl00_holderBody_';
    var firstName = document.getElementById(prefix + 'firstName');
    var firstNameBox = document.getElementById('firstNameBox');
    var errorMsgFirstName = document.getElementById('errorMsgFirstName');
    var lastName = document.getElementById(prefix + 'lastName');
    var lastNameBox = document.getElementById('lastNameBox');
    var errorMsgLastName = document.getElementById('errorMsgLastName');
    var suburb = document.getElementById(prefix + 'suburb');
    var suburbBox = document.getElementById('suburbBox');
    var errorMsgSuburb = document.getElementById('errorMsgSuburb');
    var state = document.getElementById(prefix + 'state');
    var stateBox = document.getElementById('stateBox');
    var errorMsgState = document.getElementById('errorMsgState');
    var phone = document.getElementById(prefix + 'phone');
    var phoneBox = document.getElementById('phoneBox');
    var errorMsgPhone = document.getElementById('errorMsgPhone');
    var email = document.getElementById(prefix + 'email');
    var emailBox = document.getElementById('emailBox');
    var errorMsgEmail = document.getElementById('errorMsgEmail');
    var contact = document.getElementById(prefix + 'contact');
    //var contactBox = document.getElementById('contactBox');
    var errorMsgContact = document.getElementById('errorMsgContact');
    var policy = document.getElementById(prefix + 'declaration');
    var errorMsgPolicy = document.getElementById('errorMsgPolicy');
    var isValidEmail = false;
    var isValidPhone = false;
    var isValidfirstName = false;
    var isValidlastName = false;
    var isValidState = false;
    var isValidSuburb = false;
    var isValidPolicy = false;
    //var isValidContact = false;
    var isValid = false;

    if (trim(firstName.value) == '') {
       // firstNameBox.style.border = '1px solid red';
        firstName.style.border = '1px solid red';
        errorMsgFirstName.innerHTML = 'Please enter a first name';
    } else {
        //firstNameBox.style.border = '0px';
        firstName.style.border = '1px solid #CCCCCC';
        errorMsgFirstName.innerHTML = '';
        isValidfirstName = true;
    }
    
    if (trim(lastName.value) == '') {
        //lastNameBox.style.border = '1px solid red';
        lastName.style.border = '1px solid red';
        errorMsgLastName.innerHTML = 'Please enter a last name';
    } else {
        //lastNameBox.style.border = '0px';
        lastName.style.border = '1px solid #CCCCCC';
        errorMsgLastName.innerHTML = '';
        isValidlastName = true;
    }
    
    if (trim(suburb.value) == '') {
        //suburbBox.style.border = '1px solid red';
        suburb.style.border = '1px solid red';
        errorMsgSuburb.innerHTML = 'Please enter a suburb';
    } else {
        //suburbBox.style.border = '0px';
        suburb.style.border = '1px solid #CCCCCC';
        errorMsgSuburb.innerHTML = '';
        isValidSuburb = true;
    }
    
    if (trim(state.value) == '') {
        stateBox.style.border = '1px solid red';
        state.style.border = '0px';
        errorMsgState.innerHTML = 'Please enter a state';

    } else {
        stateBox.style.border = '0px';
        state.style.border = '1px solid #CCCCCC';
        errorMsgState.innerHTML = '';
        isValidState = true;
    }

    if (trim(phone.value) == '') {
        //phoneBox.style.border = '1px solid red';
        phone.style.border = '1px solid red';
        errorMsgPhone.innerHTML = 'Please enter a phone no';
    } else {
        //phoneBox.style.border = '0px';
        phone.style.border = '1px solid #CCCCCC';
        errorMsgPhone.innerHTML = '';
        isValidPhone = true;
    }

    if (trim(email.value) == '') {
        //emailBox.style.border = '1px solid red';
        email.style.border = '1px solid red';
        errorMsgEmail.innerHTML = 'Please enter an email address';
    } else {
       // emailBox.style.border = '0px';
        email.style.border = '1px solid #CCCCCC';
        errorMsgEmail.innerHTML = '';
        isValidEmail = true;
    }

	if (!policy.checked) {
		errorMsgPolicy.innerHTML = 'Please accept the privacy policy';
	}
	else {
		errorMsgPolicy.innerHTML = '';
		isValidPolicy = true;
	}

	return isValid = isValidEmail && isValidPhone && isValidfirstName && isValidlastName && isValidState && isValidSuburb && isValidPolicy;

    }

    function validateEmailFriendForm() {
       
        var prefix = 'ctl00_holderBody_';
        var fullName = document.getElementById(prefix + 'fullName');
        var fullNameBox = document.getElementById('fullNameBox');
        var errorMsgFullName = document.getElementById('errorMsgFullName');
        var email = document.getElementById(prefix + 'email');
        var emailBox = document.getElementById('emailBox');
        var errorMsgEmail = document.getElementById('errorMsgEmail');
        var friendsName = document.getElementById(prefix + 'friendsName');
        var friendsNameBox = document.getElementById('friendsNameBox');
        var errorMsgFriendsName = document.getElementById('errorMsgFriendsName');
        var friendsEmail = document.getElementById(prefix + 'friendsEmail');
        var friendsEmailBox = document.getElementById('friendsEmailBox');
        var errorMsgFriendsEmail = document.getElementById('errorMsgFriendsEmail');
        
        var isValidFullName = false;
        var isValidEmail = false;
        var isValidFriendsName = false;
        var isValidFriendsEmail = false;
        var isValid = false;


        if (trim(fullName.value) == '') {
            fullNameBox.style.border = '1px solid red';
            fullName.style.border = '0px';
            errorMsgFullName.innerHTML = 'Please enter your name';

        }
        else {
            fullNameBox.style.border = '0px';
            fullName.style.border = '1px solid #CCCCCC';
            errorMsgFullName.innerHTML = '';
            isValidFullName = true;
        }
        if (trim(email.value) == '') {
            emailBox.style.border = '1px solid red';
            email.style.border = '0px';
            errorMsgEmail.innerHTML = 'Please enter your email';

        }
        else {
            emailBox.style.border = '0px';
            email.style.border = '1px solid #CCCCCC';
            errorMsgEmail.innerHTML = '';
            isValidEmail = true;

        }
        if (trim(friendsName.value) == '') {
            friendsNameBox.style.border = '1px solid red';
            friendsName.style.border = '0px';
            errorMsgFriendsName.innerHTML = 'Please enter your friend\'s name';

        }
        else {
            friendsNameBox.style.border = '0px';
            friendsName.style.border = '1px solid #CCCCCC';
            errorMsgFriendsName.innerHTML = '';
            isValidFriendsName = true;
        }
        if (trim(friendsEmail.value) == '') {
            friendsEmailBox.style.border = '1px solid red';
            friendsEmail.style.border = '0px';
            errorMsgFriendsEmail.innerHTML = 'Please enter your friend\'s email';

        }

        else {
            friendsEmailBox.style.border = '0px';
            friendsEmail.style.border = '1px solid #CCCCCC';
            errorMsgFriendsEmail.innerHTML = '';
            isValidFriendsEmail = true;
        }



        return isValid = isValidFullName && isValidEmail && isValidFriendsName && isValidFriendsEmail;

    }

    $(document).ready(function() {

        navInit();
        if ($("#mnPhotos").length > 0) {
            if ($("#mnPhotos #box > li").length < 7) {
                $("#nextBtn").hide();
            }
        }

        $('#sidenav a:has(strong)').click(function() {
            var i = $('#sidenav li').index($(this).parent()[0]);
            $('#sidenav li').not(':eq(' + i + ')').addClass('closed');
            $(this).parent().toggleClass('closed');
        });
        function navInit() {
            $('#sidenav > li').addClass('closed');
            locmatch('#sidenav li', pageurl);
            if ($('#sidenav .selected').length === 0) {
                pageurl = location.pathname.replace(location.pathname.slice(location.pathname.lastIndexOf('/') + 1, location.pathname.lastIndexOf('.aspx')), 'default').toLowerCase();
                locmatch('#sidenav li', pageurl);
            }
            var ro = $('#sidenav li.selected').parent().parent();
            if (ro.hasClass('closed')) {
                $(ro).toggleClass('closed');
            }

            $('#sidenav li.selected ul').parent().addClass('multiple');

        }


        //initTabs();
        // new tabs functionality for flash
        //$('.tab').hide().eq(0).show();

        $('#tabs li a').click(function() {
            var index = $('#tabs li a').removeClass('selected').index(this);
            $('.tab')
            .hide()
            .eq(index)
            .show();

            $(this).addClass('selected');
            //var swf = index == 0 ? "masterplan.swf" : "current_release.swf";

        }).eq(0).addClass('selected');
        //end tabs


        var i = 0;
        var max = $('#mnPhotos #box > li').length;
        var prevHouseID = '';
        initPackages();

        $('#mnPhotos li a').click(function() {
            displayPackage($('#mnPhotos li a').index(this));
        });

        function initPackages() {
            if (window.location.href.indexOf('item=') > 0)
                displayPackage(window.location.href.split('item=')[1]);
            else displayPackage(0);
            $('#mnPhotos #previous').hide();
        }

        function displayPackage(index) {
            $('#bigPhotos li').hide().eq(index).show();
            $('#houseFloorPlan .floorplan').hide().eq(index).show();
            emailFriend();
        }

        /*function emailFriend() {
        var type,
        item;
        if ($('#houseFloorPlan').hasClass('pkg')) type = 'pkg';
        else type = 'house';
        item = $('#houseFloorPlan .floorplan').index($('#houseFloorPlan .floorplan:visible')[0]);
        $('#emailLink').attr('href', '/EmailFriend.aspx?type=' + type + '&item=' + item);
        }*/
        function emailFriend() {
            var type,
                item;
            if ($('#houseFloorPlan').hasClass('pkg')) type = 'pkg';
            else type = 'house';
            item = $('#houseFloorPlan .floorplan').index($('#houseFloorPlan .floorplan:visible')[0]);
            $('.actions .email:eq(' + item + ')').attr('href', '/EmailFriend.aspx?type=' + type + '&item=' + item);
        }

        $('#mnPhotos li').hover(function() {
            $(this).find('.onHoverBox').removeClass('hidden');
            $(this).find('img').addClass('yellow');
        }, function() {
            $(this).find('.onHoverBox').addClass('hidden');
            $(this).find('img').removeClass('yellow');
        });

        var left = $('#mnPhotos #box').css('margin-left');                  // 
        var width = $('#mnPhotos li:eq(0)').outerWidth({ margin: true });   // to calculate -margin to shift ul by
        var visibleImages = 6;                                              // number of houses visible

        function display(which) {


            if (which == 1) {
                left = (parseInt(left.substring(0, left.length - 2)) - width) + 'px';
            } else {
                left = (parseInt(left.substring(0, left.length - 2)) + width) + 'px';
            }
            $('#mnPhotos #box').animate({ marginLeft: left }, 'fast');

            if (parseInt(left.split('px')[0]) >= -20) $('#mnPhotos #previous').hide();
            else $('#mnPhotos #previous').show();

            var length = $("#box > li").length;
            var marginLeft = $("#box > li").length > visibleImages ? -(($("#box > li").length - visibleImages) * width) : 0;
            if (parseInt(left.split('px')[0]) <= marginLeft) $('#mnPhotos #next').hide();
            else $('#mnPhotos #next').show();
        }

        $('#mnPhotos #next').click(function() {
            if ($('#mnPhotos #box:animated').size() == 0) {
                $('#mnPhotos #box').removeClass('resetBox');
                var left = $('#mnPhotos #box').css('margin-left');
                left = parseInt(left.split('px')[0]);
                if (left > -(($("#box > li").length - visibleImages) * width))
                    display(1);
            }
            return false;
        });

        $('#mnPhotos #previous').click(function() {
            if ($('#mnPhotos #box:animated').size() == 0) {
                $('#mnPhotos #box').removeClass('resetBox');
                var left = $('#mnPhotos #box').css('margin-left');
                left = parseInt(left.split('px')[0]);
                if (left == -20)
                    left = 0;
                if (left < 0) {
                    display(0);
                }
                if (left > 0) {
                    $('#mnPhotos #box').addClass('resetBox');
                }
            }
            return false;
        });
    });