$(document).ready(function()
{

    $(function()
    {
        if (typeof GoogleMapShow == 'function')
        {
            GoogleMapShow();
        }

        $('#txtcbname, #txtcbemail, #txtcbphone').focus( function ()
        {
            var v = $(this).val();
            var t = $(this).attr('title');
            if (v == t)
            {
                $(this).val('');
            }
        });
        $('#txtcbname, #txtcbemail, #txtcbphone').blur( function ()
        {
            var v = $(this).val();
            var t = $(this).attr('title');
            if (v === '')
            {
                $(this).val(t);
            }
        });
        
        $('#callback').submit( function()
        {
            var boolValid = true;
            if (boolValid === true && ($('#txtcbname').val() == $('#txtcbname').attr('title') || $('#txtcbname').val() === ""))
            {
                boolValid = false;
                alert("Please enter your name");
                $('#txtcbname').focus();
            }
            if (boolValid === true && ($('#txtcbemail').val() == $('#txtcbemail').attr('title') || $('#txtcbemail').val() === ""))
            {
                boolValid = false;
                alert("Please enter your email address");
                $('#txtcbemail').focus();
            }
            if (boolValid === true && ($('#txtcbphone').val() == $('#txtcbphone').attr('title') || $('#txtcbphone').val() === ""))
            {
                boolValid = false;
                alert("Please enter your phone number");
                $('#txtcbphone').focus();
            }
            return boolValid;
        });
        
        $('#contactform').submit( function()
        {
            var boolValid = true;
            if (boolValid === true && ($('#txtfirstname').val() === ""))
            {
                boolValid = false;
                alert("Please enter your first name");
                $('#txtfirstname').focus();
            }
            if (boolValid === true && ($('#txtemail').val() === "" && $('#txttelephone').val() === ""))
            {
                boolValid = false;
                alert("Please enter your telephone number or email address");
                $('#txttelephone').focus();
            }
            if (boolValid === true && ($('#txtemail').val() !== ""))
            {
                var e = $('#txtemail').val();
                var regEmail = new RegExp(/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)+)@(([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)){2,}\.([A-Za-z]){2,4}$/gim);
                if (regEmail.test(e) === false)
                {
                    boolValid = false;
                    alert("Please enter a valid email address");
                    $('#txtemail').focus();
                }
            }
            
            
            if (boolValid === true && ($('#txtcomments').val() === '' || $('#txtcomments').val().length < 2))
            {
                boolValid = false;
                alert("Please enter your feedback");
                $('#txtcomments').focus();
            }
            
            return boolValid;
        });


        function PageHeight()
        {
            var intWindowHeight = parseInt($(window).height(), 10);
            var intBodyHeight = parseInt($('body').height(), 10);
            var intContainerHeight = parseInt($('.container').height(), 10);
            if (intWindowHeight > intBodyHeight)
            {
                $('.container, #background').css( { height: intWindowHeight + 'px' } );
            } else { 
                $('.container, #background').css( { height: intBodyHeight + 'px' } );
            }
        }
        $.ajax(
        {
            url: '/_header/background.asp',
            dataType: 'json',
            type: 'POST',
            cache: false,
            async: true,
            complete: function(XMLHttpRequest, textStatus) { },
            success: function(json, textStatus, XMLHttpRequest)
            {
                var strURL = "";
                var strPath = "";
                var strBackground = "";
                for (i=0;i<json.item.length;i++)
                {
                    strURL = json.item[i].url.replace(/default.asp/gi, '');
                    strPath = window.location.pathname.replace(/default.asp/gi, '');
                    var varPath = strPath.split('/');
                    if (varPath[1] === 'our-work')
                    {
                        strPath = "/our-work/";
                    }
                    if (strURL == strPath)
                    {
                        strBackground = json.item[i].background;
                        $('.container').prepend("<div id='background'></div>");
                        $(window).resize(function() { PageHeight(); });
                        PageHeight();
                        $('#background').css(
                        {
                            position: 'absolute',
                            top: '0',
                            left: '0',
                            width: '100%',
                            height: parseInt($('.container').height(), 10),
                            zIndex: '-1',
                            backgroundImage: 'url(' + strBackground + ')',
                            backgroundPosition: '50% 0',
                            backgroundRepeat: 'no-repeat'
                        });
                    }
                }
            }
        });


        var h = "";
        function GalleryLabel(strCombined)
        {
            if (strCombined !== undefined)
            {
                var lbl = strCombined.split("||");
                var h = "";
                //h = h + "<div id='close'><a href='#'>X</a></div>";
                if (lbl[0] !== "")
                {
                    h = h + "<h4>" + lbl[0] + "</h4>";
                }
                if (lbl[1] !== "")
                {
                    h = h + "<p>" + lbl[1] + "</p>";
                }
                return h;
            } else {
                return '';
            }
        }
        $('.projectimageslider img').each( function (i)
        {
            $(this).attr('title2', $(this).attr('title'));
        });
        $('.projectimageslider img').attr('title', '');
        $('.projectimagery').append("<span class='projectimageloading'>Loading...</span>");
        $('.projectimageloading').css(
        {
            width: '200px',
            height: '30px',
            top: ((parseInt($('#projectdetailimage').attr('height'), 10)-30)/2) + 'px',
            left: ((parseInt($('#projectdetailimage').attr('width'), 10)-200)/2) + 'px',
            opacity: 0.9
        });
        $('.projectimagery img:not(.projectimageslider img)').wrap("<div class='projectimageryshell'></div>");
        $('.projectimageryshell').append("<div class='imageryinfo'></div>");
        $('.projectimageryshell').append("<div class='imageryhover'></div>");
        $('#projectdetailimage').load(function()
        {
            $('#projectdetailimage').fadeTo(100, 1);
            $('.projectimageloading').hide();
        });
        $('.projectimageslider img:first-child').addClass('selected');
        $('.imageryinfo').html(GalleryLabel($('.projectimageslider img:first-child').attr('title2'))).hide();
        $('.projectimageslider img:not(:first-child)').fadeTo(100, 0.6);
        $('.projectimageslider img').mouseenter( function () { if ($(this).attr('class') !== 'selected') {  $(this).fadeTo(100, 1); } });
        $('.projectimageslider img').mouseleave( function () { if ($(this).attr('class') !== 'selected') {  $(this).fadeTo(100, 0.6); } });
        $('.projectimageslider img').click( function ()
        {
            //$('.imageryinfo').show();
            $('.projectimageloading').show();
            $('.projectimageslider img').removeClass('selected');
            $(this).addClass('selected');
            $('.projectimageslider img:not(.selected)').fadeTo(100, 0.6);
            strImage = $(this).attr('src');
            strImage = strImage.replace('/_project/thumb/', '/_project/large/');
            $('#projectdetailimage').fadeTo(100, 0.5);
            $('#projectdetailimage').attr('src', strImage);
            $('.imageryinfo').html(GalleryLabel($(this).attr('title2')));
        });
        $('.imageryinfo #close a').live('click', function() {
            $('.imageryinfo').fadeOut(250);
        });
        $('.imageryhover').hover(
            function () {
                $('.imageryinfo').fadeIn(250);
            },
            function () {
                $('.imageryinfo').fadeOut(250);
            }
        );
    });
});
