/*
 * search-filter-area support section.
 */
function toggleSearchFilterArea(show) {
    if (show) {
        jQuery('#filter-expand').hide();
		jQuery('#filter-collapse').show();
        jQuery('#search-filter-area').show('fast'); /* 'slow' */
		$('#search-result-info').addClass("filtered");
        $.cookie('filterAreaShow', 1, {path: '/'});
    } else {
		jQuery('#filter-collapse').hide();
        jQuery('#filter-expand').show();
        jQuery('#search-filter-area').hide('fast');
		$('#search-result-info').removeClass("filtered");
        $.cookie('filterAreaShow', null, {path: '/'});
    }
    return false;
};

jQuery(document).ready(function() {

    // shows the filter-area on clicking the filter-expand link
    jQuery('a#filter-expand').click(function() {
        toggleSearchFilterArea(true);
        return false;
    });

    // hides the filter-area on clicking the filter-expand link
    jQuery('a#filter-collapse').click(function() {
        toggleSearchFilterArea(false);
        return false;
    });
});

Dialog = newClass(null, {

     constructor: function(options){
         this.initChild(options);
     },

     handleResponse: function(json, validationData) {
	    var title = json.title;
	    var message = json.message;
	    var errors = json.errors;

	    if (errors && errors.length) {
	        for (var i = 0; i < errors.length; i++) {
	            var key = errors[i].substring("errors.".length);
	            var data = validationData[key];
	            if (data !== undefined) {
	                markValidationFailed(data.messageId, data.jqField);
	            }
	        }
	        return false;
	    } else {
	        showMessageParam(title, message);
	        return true;
	    }
	},
    
    postWithAjax: function(env, url, params, validationData, dialog, captchaLocator, frameLocator, iFrameLocator){
        // the ajax_get_cache is required, because of the AJAX request being cached.
        params["ajax_get_cache"] = encodeURIComponent((new Date()).getTime());

        enableLoadingBlock(true);
        $.ajax({
	        type: "POST",
	        dataType: "json",
	        url: url,
	        data: params,
	        success: function(data) {
	            enableLoadingBlock(false);
	            clearValidationResult(dialog);
	            if (env.handleResponse(data, validationData)) {
                    if (typeof env.publishToFB != "undefined" && data.result == "OK") {
                        env.publishToFB();
                    }
	                env.closeDialog();
	            } else {
	                reloadImg(captchaLocator);
	                updateFrameSize('#'+frameLocator, '#'+iFrameLocator);
	            }
	        },
	        error: function(XMLHttpRequest, textStatus, errorThrown) {
	            enableLoadingBlock(false);
	            env.closeDialog();
	            var response = typeof XMLHttpRequest.responseText == 'string' ? XMLHttpRequest.responseText.substring(0,150): "";
	            showMessageParam(null, textStatus + "<br/>" + response, true);
	        }
	    });
    }
});

function getScrollTop() {
	var top_pos = 0;
	if( document.documentElement && document.documentElement.scrollTop ) {
		top_pos = document.documentElement.scrollTop + 80 + 'px';
	}else if( typeof(window.pageYOffset ) == 'number' ) {
		top_pos = window.pageYOffset + 80 + 'px';
	}
	return top_pos;
}