
function windowWidth() {
    var myWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        myWidth = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}

function windowHeight() {
    var myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        myHeight = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}

$(function() {

    // Fix Firefox bug
    if (window.innerWidth % 2) {
        window.innerWidth -= 1;
    }

    if ($.browser.msie) {
        // Fix some IE issues
        $('#sidebar th:last-child').css('border-right', 'none');
        $('#sidebar td:last-child').css('border-right', 'none');
    }

    /*------------------------------------------------------------------------
     * Search bar
     */

    $('#ctl00_txtSearch').focusin(function() {
        $(this).css('background-image', 'none');
    });

    $('#ctl00_txtSearch').focusout(function() {
        if ($(this).val() == '') {
            $(this).css('background-image', 'url(/images/search.png)');
        }
    });

    /*-------------------------------------------------------------------------
     * Pages
    $('.pages ul li a').click(function() {
        $('.page ul').hide();
        var count = $(this).parent().prevAll().length + 1;
        $(this).parent().parent().next().next().children().hide();
        $(this).parent().parent().next().next().children(':nth-child(' + count + ')').fadeIn();
        return false;
    });
	*/

    /*-------------------------------------------------------------------------
     * Filters
     */

    $('.line1 a').click(function() {
        if ($(this).css('color') != 'rgb(211, 204, 199)') {
            $(this).css('color', '#d3ccc7');
            $(this).siblings().css('color', '#453c34');
            $('.filter .arrow-button-light span').html('reset');
            if ($('.line2').is(':visible')) {
                if ($(this).html() == 'by style') {
                    $('#by-city').fadeOut(300);
                    $('#by-style').delay(300).fadeIn();
                } else {
                    $('#by-style').fadeOut(300);
                    $('#by-city').delay(300).fadeIn();
                }
            } else {
                if ($(this).html() == 'by style') {
                    $('#by-city').hide();
                    $('#by-style').fadeIn();
                } else {
                    $('#by-style').hide();
                    $('#by-city').fadeIn();
                }
                $('.line2').slideDown();
            }
        }
        return false;
    });

    $('.line2 a').click(function() {
        if ($(this).css('color') == 'rgb(211, 204, 199)') {
            $(this).css('color', '#7b6d61');
        } else {
            $(this).css('color', '#d3ccc7');
            $(this).siblings().css('color', '#7b6d61');
        }
        return true;
    });

    $('.line3 a').click(function() {
        if ($(this).css('color') == 'rgb(211, 204, 199)') {
            $(this).css('color', '#7b6d61');
        } else {
            $(this).css('color', '#d3ccc7');
            $(this).siblings().css('color', '#7b6d61');
        }
        return true;
    });

    $('.filter>a').click(function() {
        if ($('.filter>a span').html() == 'reset') {
            $('.line2').slideUp();
            $('.filter>a span').html('filter');
            $('.line1 a').css('color', '#7b6d61');
        }
        return false;
    });

    /*-----------------------------------------------------------------------
     * Modal Box
     */

    $('.modal-open').click(function() {
        var modal;
		
        if ($(this).hasClass('lodging')) {
			
            //console.log(1);
            modal = $(this).parent().next();
        } else {		
			
            //console.log(2);
            modal = $(this).parent().parent().next();
        }
		
		
		
        modal.children().each(function() {

            // only 'body' works in Webkit, 'html' works in Firefox and IE
            var top = $('html').scrollTop();
            var left = $('html').scrollLeft();
            // if we get 0, try getting it from 'body'
            if (top == 0) {
                top = $('body').scrollTop();
            }
            if (left == 0) {
                left = $('body').scrollLeft();
            }
            var v = ((windowHeight() - $(this).height() - 600) / 2) + top + 'px';
            $(this).css('top', v)
                   .css('left', ((windowWidth() - 1046) / 2) + left + 'px');
        });

        modal.css('width', windowWidth())
             .css('height', $(document).height())
             .css('background-color', 'rgba(0, 0, 0, 0.8)');

         modal.fadeIn('slow');
         return false;
    });

    $('.modal-close').click(function() {
        $(this).parent().parent().fadeOut('slow');
        return false;
    });

    /*----------------------------------------------------------------
     * Remove the last row of horizonal rules from listings
     */

    $('.box:last').each(function() {
        $(this).css('background-image', 'none');
        if ($(this).prev().has('.box-left')) {
            $(this).prev().css('background-image', 'none');
        }
    });

    /*-----------------------------------------------------------------
     * Contact Form
     */

     // Clear out the input fields
    $('dd input[type=text]').val('');
    $('dd #message').val('');

    $('dd input[type=text]').focusin(function() {
        if ($(this).val() == '* required field') {
            $(this).val('')
                   .css('color', '#200e0f')
                   .css('background-color', '#82756b');
        } else {
            $(this).css('background-color', '#d3ccc7');
        }
    });

    $('dd input[type=text]').focusout(function() {
        if ($(this).val() == '') {
            $(this).val('* required field')
                 .css('color', '#9a190d')
                 .css('background-color', '#d3ccc7');
        } else {
            $(this).css('background-color', '#82756b');
        }
    });

    $('dd textarea').focusin(function() {
        if ($(this).val() == '* required field') {
            $(this).val('')
                   .css('color', '#200e0f')
                   .css('background-color', '#82756b');
        } else {
            $(this).css('background-color', '#d3ccc7');
        }
    });

    $('dd textarea').focusout(function() {
        if ($(this).val() == '') {
            $(this).val('* required field')
                   .css('color', '#9a190d')
                   .css('background-color', '#d3ccc7');
        } else {
            $(this).css('background-color', '#82756b');
        }
    });

    function errorCheck(id) {
        if ($(id).val() == '' || $(id).val() == '* required field') {
            $(id).val('* required field')
                 .css('color', '#9a190d')
                 .css('background-color', '#d3ccc7');
            return 1;
        }
        return 0;
    }

    $('#submit').click(function() {
        var errors = 0;
        errors += errorCheck('#first_name');
        errors += errorCheck('#last_name');
        errors += errorCheck('#organization');
        errors += errorCheck('#url');
        errors += errorCheck('#address');
        errors += errorCheck('#city');
        errors += errorCheck('#state');
        errors += errorCheck('#zip');
        errors += errorCheck('#country');
        errors += errorCheck('#phone');
        errors += errorCheck('#email');
        errors += errorCheck('#message');
        if (errors > 0) {
            return false;
        }
        return true;
    });

});
