﻿$(document).ready(function() {

    InitShowHide();

    /***************************
    Add autocomplete to form to 
    prevent Google autosuggestions
    ****************************/
    $('.mainForm').attr('autocomplete', 'off');

});

/********************************************************
 jQuery extension function to set jQuery selected objects to 
 same height
********************************************************/
jQuery.fn.setEqualHeight=function(o) {
        var maxHeight=0;
        var maxElement=null;
        jQuery(this).each(function(i) {
        if((jQuery(this).height()+parseInt(jQuery(this).css("padding-bottom"))+parseInt(jQuery(this).css("padding-top")))>maxHeight) {
        maxHeight=jQuery(this).height()+parseInt(jQuery(this).css("padding-top"))+parseInt(jQuery(this).css("padding-bottom"));
        maxElement=this;
    }
});
jQuery(this).not($(maxElement)).each(function() {$(this).height(maxHeight-parseInt(jQuery(this).css("padding-top"))-parseInt(jQuery(this).css("padding-bottom")))})
}

/********************************************************
 jQuery based function to hide / show all list items
 based on whether they meet the filter conditions.
********************************************************/
function refreshList() {
    var filterText = "";
    $('div.filterList ul li input:checked').each(function (i, val) {
        filterText+= $(this).parent().find('label').text()+",";
    });
    var filters = filterText.split(',');
    
    $('div.listing').each(function(i,val) {
        
        var blDisplay = (filters.length == 1) ? true : false;
        var filterIndex =0;
        var strTitle = $(this).attr('title');
        
        for (filterIndex=0;filterIndex<filters.length;filterIndex++) {
            if (filters[filterIndex].length > 0 && strTitle.indexOf(filters[filterIndex]) > -1) {
                blDisplay = true;
            }
        }
        if (blDisplay) {
            $(this).slideDown();
        }else{
            $(this).slideUp();
        }
    });
}

/********************************************************
 jQuery based function to auto suggest links based on
 search results
********************************************************/
function quickSearch(inputString, homePageId){
	if(inputString.length < 4) {
		$('#suggestions').slideUp();
	} else {
	    $('#searchInput').addClass('load');
        $.ajax({  
            type: "POST",  
            url: "/webservices/SiteSearch.asmx/Search",  
            contentType: "application/json; charset=utf-8",  
            data: "{HomeNodeId:'" + homePageId + "', Terms:'"+inputString+"', ResultsLimit:'5'}",  
            dataType: "json",  
            success: function(response) {
                $('#searchInput').removeClass('load');
                if ($('#searchInput').val().length > 3 && response.d.length > 0) {
			        $('#suggestionsList').html(response.d);
			        $('#suggestions').slideDown();
			    }else{
			        $('#suggestions').slideUp();
			    }
            },  
            error: function(msg) {
                $('#searchInput').removeClass('load');
                //alert('fail: '+msg.status + ', ' + msg.statusText +', ' + msg.responseText);   
            }
        });    
	}
}

function clearQuickSearch() {
    setTimeout(function() {
        $('#suggestions').slideUp();
    }, 100); // delay is for the blur event which causes this to execute. It executes before the click event on the LI does, so it's delayed by 100ms to compensate
}



/* Show/Hide*/
var mapHtml;
function InitShowHide() {
    if (mapHtml == undefined)
        mapHtml = $('#hidden-section').html();
    $('#hidden-section').html("");
    $('#expand-section').toggle(
		function() {
		    $('#hidden-section').html(mapHtml);
		    $('#expand-section').text("Hide map");
		    $('#expand-section').addClass('up');
		}, function() {
		$('#hidden-section').html("");
		$('#expand-section').text("Show map of Creative Clyde community");
		    $('#expand-section').removeClass('up');
		}
	)
}


function InitShowHide2() {
    $('#hidden-section').hide();
    $('#expand-section').toggle(
		function() {
		    $('#hidden-section').slideDown();
		    $('#expand-section').text("Hide map");
		    $('#expand-section').addClass('up');
		}, function() {
		    $('#hidden-section').slideUp();
		    $('#expand-section').text("Show map of Creative Clyde community");
		    $('#expand-section').removeClass('up');
		}
	)
}

function InitRotatingLogos() {
    /* Rotating items  */
    $('.rotating-logos').cycle({
        fx: 'fade',
        timeout: 5000,
        delay: 5000,
        pause: 'true'
    });
}

