var intBillboardSlide = 1;
function billboardTimer()
{
	var intBillboardCount = parseInt($('#billboard .more li').length, 10);
	intBillboardSlide = intBillboardSlide + 1;
	if (intBillboardSlide > intBillboardCount) {
		intBillboardSlide = 1;
	}
	$('.more ul li:eq(' + (intBillboardSlide-1) + ') a').trigger('click');
	//$('h1').text(intBillboardSlide);
}

if(jQuery)(
	function (jQuery) {
		jQuery.extend(jQuery.fn,{
			hint:function (options) {
				jQuery(this).each(function () {
				var settings = jQuery.extend({
					id              : jQuery(this).attr('id'),
					hint            : '',
					hintClass       : 'inactive'
				}, options);

				jQuery(this).addClass(settings.hintClass);
				if (settings.hint === '')
				{
					settings.hint = jQuery(this).attr('defaultValue');
				} else {
					jQuery(this).val(settings.hint);
					jQuery(this).attr('defaultValue', settings.hint);
				}
				jQuery(this).focus( function ()
				{
					if (jQuery(this).val() === "" || jQuery(this).val() === jQuery(this).attr('defaultValue'))
					{
						jQuery(this).val('').removeClass(settings.hintClass);
					}
				});
				jQuery(this).blur( function ()
				{
					if (jQuery(this).val() === "")
					{
						jQuery(this).val(jQuery(this).attr('defaultValue')).addClass(settings.hintClass);
					}
				});
			});
		}
	});
})(jQuery);

$(document).ready(function ()
{
    $(function ()
    {
        if (typeof GoogleMapShow == 'function')
        {
            GoogleMapShow();
        }
        
        function ResizeBackground()
        {
            $('#background img').width('100%').height('100%');
            
            var intWindowWidth = parseInt($(window).width(), 10);
            var intWindowHeight = parseInt($(window).height(), 10);
			var intBodyWidth = parseInt($('body').width(), 10);
            var intBodyHeight = parseInt($('body').height(), 10);
            var intBackgroundWidth = parseInt($('#background img').attr('width'), 10);
            var intBackgroundHeight = parseInt($('#background img').attr('height'), 10);
            var strBackgroundLongDesc = $('#bgimg').attr('longdesc');
            var varBackgroundLongDesc = strBackgroundLongDesc.split(',');
            intBackgroundWidth = varBackgroundLongDesc[0];
            intBackgroundHeight = varBackgroundLongDesc[1];
            var intResizedWidth = 0;
            var intResizedHeight = 0;
            var intMarginTop = 0;
            var intMarginLeft = 0;
            
            $('#container').height(intWindowHeight);

            var boolRatioWidth = parseFloat((intWindowWidth / intBackgroundWidth), 10);
            var boolRatioHeight = parseFloat((intWindowHeight / intBackgroundHeight), 10);
            //landscape
            boolRatio = intWindowWidth / intBackgroundWidth;
            intResizedHeight = parseInt(boolRatio * intBackgroundHeight, 10);
            intResizedWidth = intWindowWidth;
            intMarginTop = 0 - parseInt((intResizedHeight - intWindowHeight)/2, 10);
            intMarginLeft = 0;
            if (intMarginTop > 0)
            {
                //portrait
                boolRatio = intWindowHeight / intBackgroundHeight;
                intResizedWidth = parseInt(boolRatio * intBackgroundWidth, 10);
                intResizedHeight = intWindowHeight;
                intMarginLeft = 0 - parseInt((intResizedWidth - intWindowWidth)/2, 10);
                intMarginTop = 0;
            }
            $('#background').css( { marginTop: intMarginTop + 'px', marginLeft: intMarginLeft + 'px', width: (intResizedWidth) + 'px', height: (intResizedHeight) + 'px' });

        }
        function ValidTelephoneNumber(sValue)
        {
            var booValid = false;
            var strTelephoneNumber = $.trim(sValue);
            
            // Remove any spacers
            strTelephoneNumber = strTelephoneNumber.replace("+", "");
            strTelephoneNumber = strTelephoneNumber.replace("(", "");
            strTelephoneNumber = strTelephoneNumber.replace(")", "");
            
            if (strTelephoneNumber.length > 6)
            {
                booValid = true;
            }
            
            return booValid;
        }
        function ValidEmailAddress(pEmailAddress)
        {
            var booValid = false;
            var strEmailAddress = $.trim(pEmailAddress);
            
            if (strEmailAddress.length > 6)
            {
                if (strEmailAddress.indexOf("@") > 1)
                {
                    booValid = true;
                }
            }
            
            return booValid;
        }
        
		var boolQuicklinkSliding = false;
		var boolQuicklinkShouldClose = false;
		$('.quicklinks .tab a').click( function () { return false; } );
        $('.quicklinks').hover( 
			function ()
			{
				if (boolQuicklinkSliding === false)
				{
					boolQuicklinkSliding = true;
					var h = $(this).find('>.wrapper').outerHeight(true) + $(this).find('.links .wrapper').innerHeight() -1;
					$(this).stop(true, true).animate( { height: h + 'px' }, function ()
					{
						boolQuicklinkSliding = false;
						if (boolQuicklinkShouldClose === true)
						{
							boolQuicklinkShouldClose = false;
							var h = $(this).find('>.wrapper').outerHeight(true);
							$(this).animate( { height: (h+5) + 'px' } );
						}
					});
				}
			},
			function ()
			{
				if (boolQuicklinkSliding === false)
				{
					boolQuicklinkSliding = true;
					var h = $(this).find('>.wrapper').outerHeight(true);
					$(this).stop(true, true).animate( { height: (h+5) + 'px' }, function () { boolQuicklinkSliding = false; } );
				} else {
					boolQuicklinkShouldClose = true;
				}
				
			}
		);
		$('#header').hover( function () { $('.homepagefeatures').fadeIn(); } );
		$('#background').mouseenter( function () { $('.homepagefeatures').fadeOut(); } );
        $(window).resize(function () { ResizeBackground(); });
		if ($('#background').length > 0)
		{
			$('body').css( {overflow: 'hidden' } );
            ResizeBackground();
		}
        
        if ($('#billboard').not('.list').length > 0)
        {
            var strStyle = $('#billboard').attr('class');
            strStyle = strStyle.replace(/billboard/gi, '');
            strStyle = strStyle.replace(/ /gi, '');
            var intBillboards = $('#billboard img').length;
            var intBillboardWidth = $('#billboard img:first-child').width();
            var intBillboardHeight = $('#billboard img:first-child').height();
            $('#billboard').css( { position: 'relative' } );
            $('#billboard>img').css( { position: 'absolute', left: 0, top: 0 } );
            $('#billboard>img').removeClass('hide');
            $('#billboard>img:first-child').addClass('selected');
            $('#billboard .more li:first-child a').addClass('selected');
            $('#billboard .more .btn').each( function (i)
            {
                var intStartLeft = '0';
                var intStartTop = '0';
                switch (strStyle)
                {
                    case 'slideleft':  intStartLeft = intBillboardWidth + 'px'  ; break;
                    case 'slideright': intStartLeft = -intBillboardWidth + 'px' ; break;
                    case 'slideup':    intStartTop  = intBillboardHeight + 'px' ; break;
                    case 'slidedown':  intStartTop  = -intBillboardHeight + 'px'; break;
                }
                $(this).click( function ()
                {
                    var b = $('#billboard img:nth-child(' + (i+1) + ')');
                    if ($(b).hasClass('selected') === false)
                    {
                        $(b).css( { left: intStartLeft, top: intStartTop } );
                        if (strStyle === 'fade') { $(b).hide(); }
                        $(b).addClass('animating');
                        $('#billboard .more .btn').removeClass('selected');
                        $('#billboard .more li:nth-child(' + (i+1) + ') a').addClass('selected');
                        if (strStyle === 'fade')
                        {
                            $(b).fadeIn( 500, function ()
                            {
                                $('#billboard>img').removeClass('selected');
                                $(b).addClass('selected');
                                $(b).removeClass('animating');
                            });
                        } else {
                            $(b).animate( { left: '0', top: '0' }, 500, function ()
                            {
                                $('#billboard>img').removeClass('selected');
                                $(b).addClass('selected');
                                $(b).removeClass('animating');
                            });
                        }
                    }
                    return false;
                });
            });
			setInterval("billboardTimer()", 5000);
			
        }
        if ($('#projects .project').length > 0)
        {
            $('#projects .project a').css( { position: 'relative' } );
            $('#projects .project a').each( function (i)
            {
                var slidePanel = $(this).find('.detail');
                var slidePanelHeight = $(slidePanel).height();
                $(slidePanel).css( { position: 'absolute', left: 0, top: slidePanelHeight + 'px', zIndex: 2000000 } );
                $(this).hover( function () { $(slidePanel).animate( { top: 0 }, 500 ); } , function () { $(slidePanel).animate( { top: '140px' }, 500 ); } );
            });
        }
        if ($('#projects .filters').length > 0)
        {
            $('#projects .filters ul>li.filter').each( function (i)
            {
                //console.log($(this).find('>a').length);
                //$(this).find('>a').hover( function () { }, function () { } );
                $(this).find('ul').slideUp(0);
                var subMenu = $(this).find('ul');
                $(this).hover( function ()
                {
                    //$('h1').text('In');
                    //$(this).find('ul').animate( { height: '200px' }, 500);
                    $(subMenu).slideDown(250, 'swing');
                }, function ()
                {
                    //$('h1').text('Out');
                    $(subMenu).slideUp(250, 'swing');
                });
                /*
                var filterPanel = $(this).parent().find('>ul');
                var filterPanelHeight = $(filterPanel).height();
                $(this).parent().find("ul.subnav").slideDown('fast').show();
                $(this).hover( function () { $(filterPanel).slideDown(500); console.log('in'); } , function () { $(filterPanel).slideUp(500); console.log('out'); } );

                //$(filterPanel).css( { position: 'absolute', left: 0, marginTop: '-10px' , zIndex: 2000000 } );
                /*
                console.log(i);
                $(this).slideDown('fast').show();
                */
            });
        }
		$('form.newsletter input').hint( { hint: 'enter email address', hintClass: 'something' } );
		$('#bktdji-bktdji').hint( { hint: 'enter email address', hintClass: 'something' } );
		//$('form.newsletter input').val('Malcolm');
		
        $('#footer .features').css({ position: 'relative' });
        $('#footer .features').append("<div class='fadeout'></div>")
        $('#footer .features .fadeout').mouseenter( function () { $(this).fadeTo('500', 0, function () { $('#footer .features .fadeout').hide(); } ); } );
        $('#footer .features').mouseleave( function () { $(this).find('.fadeout').show(); $(this).find('.fadeout').fadeTo('500', 0.75); } );
        
        $('#btnContactFormSubmit').click(function() {
            var strErrorMessage = 'Please provide the following information:\n';
            var boolError = false;
            
            if ($('#txtusersname').val() === '')
            {
                boolError = true;
                strErrorMessage = strErrorMessage + '* Your name\n';
            }
            if (ValidTelephoneNumber($('#txttelephone').val()) === false && ValidEmailAddress($('#txtemail').val()) === false)
            {
                boolError = true;
                strErrorMessage = strErrorMessage + '* A valid email address or telephone number\n';
            }
            if ($('#txtcomments').val() === '')
            {
                boolError = true;
                strErrorMessage = strErrorMessage + '* Enquiry / message information\n';
            }
            
            if (boolError === true)
            {
                alert(strErrorMessage);
                return false;
            }
            else
            {
                return true;
            }
        });
		
    });
    if ($('#projects .filters .filter ul').length > 0)
    {
        var intMax = 0;
        var intHeight = 0;
        var intFilterHeight = parseInt($('#projects .filters').height(), 10);
        var intProjectHeight = $('#projects').height();
        $('#projects .filters .filter ul').each (function (i)
        {
            intHeight = parseInt($(this).height(), 10);
            if (intHeight > intMax)
            {
                intMax = intHeight;
            }
        });
        var intMinHeight = (intFilterHeight + intMax);
        if (intProjectHeight < intMinHeight)
        {
            $('#projects').height(intMinHeight);
        }
    }
    
    $('.homepagefeatures').click( function ()
    {
        //console.log('homepagefeatures: clicked');
    });
	
	$('a[href^="http://"]').attr("target", "_blank");
});
