/* INIT HTML5 ELEMENTS ON THE DOM **********/
var Elements=[
"header",
"hgroup",
"nav",
"section",
"article",
"aside",
"menu",
"footer"
]
function createElement(arr){
	for(elem in arr){
		document.createElement(arr[elem]);
	}
}
createElement(Elements);
/* *****************************************/

/* Searchbar autovalue ****************************************/
function searchInputSelect(elem){
	var srchval=$(elem).attr("value").toLowerCase();
	$(elem).focus(function(){
		if($(this).attr("value").toLowerCase()==srchval){
			$(this).attr("value","");
		}else if($(this).attr("value").toLowerCase()!=srchval && $(this).attr("value").toLowerCase()!=""){
			$(this).select();
		}
	});
	$(elem).blur(function(){
		if($(this).attr("value")==""){
			$(this).attr("value",srchval);
		}
	});
}
/* ******************************************************************/


$(document).ready(function() {
	
	searchInputSelect("#main-search-input");

    $('#main-search').submit(function() {
        if ($('#main-search-input').val()!='' || $('#main-search-input').val()!='search') {
            $('#main-search').attr('action',$('#main-search').attr('action')+$('#main-search-input').val());
            return true;
        }
        return false;
    });
	
	
    //HOUSE MENU AUTOHEIGHT *****************************************************************************/
    try{
            //You can make changes to the auto height by setting up the variable menu_height_fix
            //Example: if you set menu_height_fix=20 you will add 20px to the total height. if you set menu_height_fix=-50, you'll sustract 50px.
            //you can set menu_height_fix inside a <script> element, in the body of the edit mode of a page.
            $("#home-center").height($("#section-content").height()+$("#section-intro").height()+menu_height_fix);
    }catch(err){
            $("#home-center").height($("#section-content").height()+$("#section-intro").height());
    }
    //****************************************************************************************************/

    // BLOCK Find homes
    $("input[name='search']").click(function(){
            if($('#rank').val() == '' || $('#family_members').val() == ''){
                    alert('Please select both a Pay Grade and a number of Dependents.');
                    return false;	
            }	
    });

    // BLOCK We can help!
    $("input[name='send']").click(function(){
            if($("input[name='contactus-name']").val() == '' || $("input[name='contactus-email']").val() == '' || $("input[name='contactus-message']").val() == ''){
                    alert('Please complete all required fields.');
                    return false;	
            }	
    });
        
    // TABS webform
    if($('ul.tabs').children().eq(2).children().html() == 'Webform'){
        var node = $('ul.tabs').children().eq(2).children().attr('href');
        $('ul.tabs').append('<li><a href="'+node+'/emails">E-mails</a></li>');
        $('ul.tabs').append('<li><a href="'+node+'/configure">Form settings</a></li>');

        if($('#webform-emails-form').length){
                $('ul.tabs').children().removeClass('active');
                $('ul.tabs').children().eq($('ul.tabs').children().length-2).addClass('active');
                $('#home-center').css('height',800);
        }

        if($('#webform-configure-form').length){
                $('ul.tabs').children().removeClass('active');
                $('ul.tabs').children().eq($('ul.tabs').children().length-1).addClass('active');

                $('#webform-configure-form').prepend('<section id="section-content" class="semi-wrap">');
                $('#section-content').next().appendTo($('#section-content'));
                $('#home-center').css('height',1755);
        }
    }
    
    //fix height on images and left colum on house datails
    if($('#houseDetails').length){
        var d_height = 250;
        var c_images = $('#housePhotosTabContent .imageThumb').size();
        var c_lines = Math.round(c_images/2);
        $('#housePhotosTabContent').css('height',c_lines*d_height);
    }
    
});


