/**
 *  Frontend javascript functions
 *  @author Peter Britka
 */

/**
 * Performs the search action and returns results
 *
 * @param string url
 */
function ajaxSearch(url)
{
    is_ajax = typeof(is_ajax) != 'undefined' ? is_ajax : true;
	var url = url.replace(/&amp;/ig, '&');
        var params = getSearchParameters();
        if ( params == '' )
        {
            return;
        }

	if(startLoadNavigationData()){

	var request = new Ajax.Request(url,
	  {
	    method:'get',
	    parameters:'ajax=1&'+params,
	    onSuccess: function(transport){

	    	var response = eval('('+(transport.responseText || false)+')');

	    	replaceNavigationBlock(response.navigation);
			replaceProductsBlock(response.product_list);
			replaceLeftRightNavigationBlocks('gan-left-nav-main-container', response.navigation_left);
			replaceLeftRightNavigationBlocks('gan-right-nav-main-container', response.navigation_right);

			if(response.eval_js){
				eval(response.eval_js);
				navigation_eval_js = response.eval_js;
			}

			stopLoadNavigationData();

	    },
	    onFailure: function(){
	    	setLocation(url); //trying redirect to url
	    }
	  });

	}
}

/**
 * Return get parameters for advanced search
 */
function getSearchParameters()
{
    var params = '';

    $j('.leftsidebar-select ul li.selected').each(function() {
        if ( $j(this).attr('id') != undefined )
        {
            params += params == '' ? $j(this).attr('id') : '&' + $j(this).attr('id');
        }
    });

    return params;
}

/**
 * Returns number of products for the current search criteria
 *
 * @param string url
 */
function getProductCount(url)
{
    var params = getSearchParameters();
    if ( params == '' )
    {
        $j('#result_count').html(productCount);
        return;
    }

    var request = new Ajax.Request(url,
	  {
	    method:'get',
	    parameters: params,
	    onSuccess: function(transport){
                var response = transport.responseText;
                $j('#result_count').html(response);
	    }
	  });
}

//indicates whether some of the filters is open
var filterOpen = false;
//indicates whether filter function has already been called
var wasFilterCalled = false;

/**
 * Triggers after click on some of the filters
 * @param div filter element
 */
function filterClick(el)
{
    if ( !wasFilterCalled && $j(el).children('ul').hasClass('open') )
    {
        closeFilters();
    }
    wasFilterCalled = false;
}

/**
 * Triggers after user clicks on filter option
 * @param li filter option element
 */
function filterSelect(el)
{
    wasFilterCalled = true;

    //no filter is open, so we show options
    if ( !filterOpen )
    {
        $j('.bgEmpty').show();
        openFilter(el);
    }
    else
    {
        //the same filter was clicked
        if ( $j(el).parent().hasClass('open') )
        {
            if ( $j(el).attr('id') != undefined )
            {
                $j(el).parent().prev().val($j(el).attr('id'));
            }
            $j(el).parent().children('li').removeClass('selected');
            $j(el).addClass('selected');
            closeFilters();
        }
        //some other filter was clicked and another is still open
        else
        {
            //close old filter
            $j('ul.open').children('li').hide();
            $j('ul.open').children('li.selected').show();
            $j('ul.open').removeClass('open');
            $j('.leftsidebar-select').css('z-index', '101');
            //show new filter
            openFilter(el);
        }
    }
}

/**
 * Triggers when user click on filter arrow. Calls filterSelect function
 * @param span arrow element
 */
function arrowFilterClick(el)
{
    filterSelect($j(el).siblings('ul').children('.selected'));
}

$j(document).ready(function(){
    $j('.bgEmpty').click(closeFilters);
});

/**
 * Open selected filter
 * @param li filter option element
 */
function openFilter(el)
{
    $j(el).parent().children('li').show();
    if ( $j(el).parent().children('li').length < 9 )
    {
        $j(el).parent().css({'height': 'auto', 'overflow-y': 'auto'});
    }
    $j(el).parent().parent().css('z-index', '102');
    $j(el).parent().addClass('open');
    filterOpen = true;
}

/**
 * Close all open filters
 */
function closeFilters()
{
    $j('ul.open').children('li').hide();
    $j('ul.open').children('li.selected').show();
    $j('ul.open').removeClass('open');
    $j('.leftsidebar-select').css('z-index', '101');
    $j('.bgEmpty').hide();
    filterOpen = false;
    getProductCount($j('#productCountUrl').val());
}

/**
 * Checks whether user has entered some search query
 */
function quickSearch(id, defaultText)
{
    if ( $j('#' + id).val() == defaultText )
    {
        $j('#' + id).addClass('notext');
    }
}

/**
 * Changes content of tabs in detail page and sets the right tab picture
 * @param tab header div
 */
function changeTabs(el)
{
    if ( $j('.htab').length < 2)
    {
        return;
    }
    var id = $j(el).attr('id');
    var num = id.substring(4);
    //change selected class for tabs
    $j('.htab').removeClass('selected');
    $j(el).addClass('selected');
    //change selected class for content
    $j('.tab').removeClass('selected');
    $j('#tab' + num).addClass('selected');
    //make all tabs passive
    $j('.htab .active').hide();
    $j('.htab .passive').show();
    //show right tab and content
    $j('.selected .passive').hide();
    $j('.selected .active').show();
    $j('.tab').hide();
    $j('.selected').show();
}
