$(document).ready(function(){
    var searchFormsQuery = $("#frm-search-keyword, #frm-cnt-search-number, #frm-hdr-search-number");
    var searchFormsQuicklookup = "#form-quick-lookup, #form-cnt-quick-lookup, #form-hdr-quick-lookup";    
    searchFormsQuery.children("input[name=query]").blur(function(){
        if ($(this).val() == '') {
            $(this).css("background", "#ffffff url('images/txt-search.gif') 5px 5px no-repeat");
        }
    }).focus(function(){
        $(this).css("background", "#ffffff none repeat scroll 0 0");
    }).blur();    
    $('#wrapper-home, #wrapper').find(searchFormsQuicklookup).submit(function(){
        if ($(this).attr('id') == 'form-quick-lookup'){
            $(this).mouseleave(function(){
                $(this).children('.error-search-message').delay(1000).hide();
            });
        }
        if ($(this).children('select[name=make]').val() == '' || $(this).children('select[name=model]').val() == '' || $(this).children('select[name=year]').val() == ''){
            if (!$(this).children('.error-search-message').attr('class')){
                $(this).append('<div class="error-search-message" style="display:none">Please fill all the fields</div>');
            }                        
            $(this).children('.error-search-message').fadeIn().delay(2500).fadeOut();
            return false;
        }        
    });
    
    $('#frm-search-category').submit(function(){
        if ($(this).children('#category').val() == ''){
            if (!$(this).children('.error-search-message').attr('class')){
                $(this).append('<div class="error-search-message" style="display:none">Please fill all the fields</div>');
            }
            $(this).children('.error-search-message').fadeIn().delay(2500).fadeOut();
            return false;
        }
    });    
    searchFormsQuery.submit(function(){
        if ($(this).children('input[name=query]').val() == '' || $(this).children('#query').val() == ' '){
            if (!$(this).children('.error-search-message').attr('class')){
                $(this).append('<div class="error-search-message" style="display:none">Please fill all the fields</div>');
            }
            $(this).children('.error-search-message').fadeIn().delay(2500).fadeOut();            
            return false;
        }
    });
    showQtip();
});

var selModel = '';
var selMake = '';

function showQtip()
{
    var categoriesPage = false;
    if ($(".product_bl").size() > 0){
        categoriesPage = true;
    }
    $('.qtip').each(function(){        
        if ($(this).next('input').val() != ''){
            var imageTag = ($(this).parent().children('img').size() > 0?true:false);
            $(this).qtip({
                hide: {
                    delay: 500
                },
                show:{
                    delay: 300,
                    effect: 'grow'
                },
                content: {
                    text: $(this).next('input').val()
                },
                position: {
                   corner: {
                      target: 'topMiddle',
                      //tooltip: 'bottomMiddle'
                      tooltip: (categoriesPage && imageTag?'bottomMiddle':'bottomLeft')
                   },
                   adjust: {
                      x: (categoriesPage && imageTag?0:-25)
                      //x: -25
                   }
                },                
                style: {
                    border: {
                        width: 1,
                        radius: 1,
						color: '#760d11'
                    },
                    /*width: {
                        min: 300,
                        max: 400
                    },*/
					width: 350,
                    color: '#000000',
                    padding: 10,
                    textAlign: 'left',
                    tip: {
                        //corner: 'bottomMiddle',
                        //corner: 'bottomLeft',
                        corner: (categoriesPage && imageTag?'bottomMiddle':'bottomLeft'),
						color: '#ffffff',
                        size: {
                            x: 24,
                            y: 12
                        }
                    }
                },
                wrapper: {
                    overflow: 'visible'
                }                
            });
      }
    });        
}

function _GET(param)
{
    var tmp = new Array();
    var tmp2 = new Array();
    var params = new Array();
    var get = location.search;
    if(get != '') {
        tmp = (get.substr(1)).split('&'); 
        for(var i=0; i < tmp.length; i++) {
            tmp2 = tmp[i].split('=');       
            params[tmp2[0]] = tmp2[1];      
        }
    }
    return params[param];
}

function initSearchData()
{
    selModel = _GET('model');    
    selMake = _GET('make');    
    if ($('#year').val() != ''){
        showMakes4Select(document.forms[0].year, true);
    }
}

function showMakes4Select(element, loadModels, adminTitle)
{
    if (adminTitle){
        var elementTitleModel = 'Select model';
        var elementTitleMake = 'Select make';
    } else {
        var elementTitleModel = 'Select your model';
        var elementTitleMake = 'Select your make';
    }
    var modelHtml = '<option value = "">'+elementTitleModel+'</option>';
    $(element.form.model).html(modelHtml).attr('disabled', 'disabled');
    if (element.form.year.value == '') {
        $(element.form.make).attr('disabled', 'disabled').children('option:first').attr('selected', 'yes');
        $(element.form.model).attr('disabled', 'disabled').children('option:first').attr('selected', 'yes');
    } else {
        $.ajax({
            url: BASE_URL + '/search/QuickLookup',
            dataType: 'json',
            type: 'POST',
            data: {
                action: 'getMakes4Select',                
                year: element.form.year.value
            },
            success: function (options, textStatus) {

                var html = '<option value = "">'+elementTitleMake+'</option>';
                var count = 0;
                for (var value in options) {
                    count++;
                }
                if (count == 0){
                    $(element.form.make).html(html).attr('disabled', 'disabled');
                    $(element.form.model).attr('disabled', 'disabled');
                    return false;
                }
                for (var value in options) {
                    html += '<option '
                         + ((selMake && selMake == options[value].idMake) ? 'selected = "selected" ' : '')
                         +'value = "' + options[value].idMake + '">' + options[value].make + '</option>'
                }
                selMake = '';
                $(element.form.make).html(html).removeAttr('disabled');
                if (loadModels){
                    showModels4Select(element.form.make);
                }
            }
        });
    }
}

function showModels4Select(element, adminTitle)
{
    if (adminTitle){
        var elementTitleModel = 'Select model';
    } else {
        var elementTitleModel = 'Select your model';
    }
    if (element.form.make.value == '') {
        $(element.form.model).attr('disabled', 'disabled').children('option:first').attr('selected', 'yes');
    } else {
        $.ajax({
            url: BASE_URL + '/search/QuickLookup',
            dataType: 'json',
            type: 'POST',
            data: {
                action: 'getModels4Select',
                make: element.form.make.value,
                year: element.form.year.value
            },
            success: function (options, textStatus) {
                var html = '<option value = "">'+elementTitleModel+'</option>';
                var count = 0;
                for (var value in options) {
                    count++;
                }                
                if (count == 0){
                    $(element.form.model).html(html).attr('disabled', 'disabled');
                    return false;
                }
                for (var value in options) {
                    html += '<option '
                         + ((selModel && selModel == options[value].idModel) ? 'selected = "selected" ' : '')
                         +'value = "' + options[value].idModel + '">' + options[value].model + '</option>'
                }                
                $(element.form.model).html(html).removeAttr('disabled');                
            }
        });
    }
}

