var search;

$(document).ready(function(){
   search = {
        script : '/controllers/ajax.php',
        init   : function(arr) {
                      var self = this;
                      ajax_request(self, 0, arr);
                  }
    }





    $('table.table tr:not(.hidden_row) td:not(.final_td)').hover(
		function () {
			$(this).parent().addClass('sel');
		},
		function () {
			$(this).parent().removeClass('sel');
		}
	);
});

function ajax_request(self, i, arr){
    if(typeof(arr[i]) != 'undefined'){
        $.ajax({
            data: 'action=get_prices&id='+arr[i],
            dataType: 'json',
            cache: true,
            url: self.script,
            success: function(res) {
                res = eval(res);
                for(x in res){
                    res[x][3] = (res[x][3]==0)?'':res[x][3]+'$';
                    res[x][4] = (res[x][4]==0)?'':res[x][4];
                    $('#product'+res[x][0]+' .site'+res[x][1]+' .name').text(res[x][2]);
                    $('#product'+res[x][0]+' .site'+res[x][1]+' .price').text(res[x][3]);
                    $('#product'+res[x][0]+' .site'+res[x][1]+' .qty').text(res[x][4]);
                    if(res[x][3]!=''||res[x][4]!='') {
                        $('#product'+res[x][0]+' .tabhead').show();
                        $('#product'+res[x][0]+' .site'+res[x][1]).show();
                        $('#product'+res[x][0]+' .site'+res[x][1]+' td').show();
                    }
                }
                ajax_request(self, ++i, arr);                            //alert(res);
            }
        });
    }
}


function load_cities(alias) {
    var str = '';
    $.ajax({
        data : 'action=get_cities&alias='+alias,
        url  : '/controllers/ajax.php',
        dataType: 'json',
        success: function(res) {
            for(var i in res) {
                str += '<option value="'+res[i].locId+'">'+res[i].city+'</option>';
            }
            $('[name=city_id]').html(str);
        }
    });
    return false;
}

var block = 1;
function get_distributors(product_id, manufacturer, obj) {
    $('.distributors').hide();
    var distr = obj.parent().find('.distributors');
    distr.show();

    if(block==0) {
       $(document).unbind('click',d);
       $('.distributors').unbind('click',b);
    }
    block = 1;
    $('.distributors').click(b=function(){block = 1});
    $(document).click(d=function(){
        if(block==0) $('.distributors').hide();
        else         block = 0;
    });

    if (typeof distr.find('img').attr('src') == "undefined") return false;
    var str = '';
    var i=0;
    $.ajax({
        data : 'action=get_distributors&product_id='+product_id,
        url  : '/controllers/ajax.php',
        //dataType: 'json',
        success: function(res) {
            if(res) res = eval(res);
            if(res)for(i in res) {
                str += '<p>'+lang_location+': '+res[i]['country']+', '+res[i]['city']+'<br />';
                str += lang_manufacturer+': '+manufacturer+'<br />';
                str += lang_distributor+': '+res[i]['name']+'<br />';
                str += lang_site+': '+res[i]['site']+'<br />';
                str += lang_email+': '+res[i]['email']+'<br />';
                str += lang_phone+': '+res[i]['phone']+'<br />';
                str += lang_address+': '+res[i]['address']+'<br /></p>';
            }
            str = (str!='')?str:'<p>'+lang_none+'</p>';
            distr.html(str);
        }
    });
    return false;
}

var blockParam = 1;
var visObj = false;

function show_all_params(obj)
{
	if (!visObj) {
		visObj = obj;
	}
	if (blockParam == 0) {
		$(document).unbind('click',cl_out);
		$('.more_attrs').unbind('click',cl);
	}
	
	$('.more_attrs').click(cl=function(){
		if (visObj == obj) {
//			alert('in');
			blockParam = 2;
			visObj = obj;
		}
	});
	
	$(document).click(cl_out=function(){
//		alert('out');
		if (visObj && blockParam != 2) {
			$(visObj).parent().parent().find('.hidden').hide();
			$(visObj).parent().parent().css('position', 'static');
	        $(visObj).parent().parent().parent().css('position', 'static');
	        $(visObj).parent().parent().css('padding', '0px 0px 0px 20px');
	        $(visObj).parent().parent().css('background', 'none');
	        $(visObj).text(lang_more);
	        $(visObj).addClass('more');
	        
	        visObj = obj;
		}

		blockParam = 0;
	});
	
    if($(obj).hasClass('more')) {
//    	alert('show');
        $(obj).parent().parent().find('.hidden').css('display', 'block');
        $(obj).parent().parent().parent().css('position', 'relative');
        $(obj).parent().parent().css('position', 'absolute');
        $(obj).parent().parent().css('top', '-13px');
        $(obj).parent().parent().css('left', '0px');
        $(obj).parent().parent().css('z-index', '10');
        $(obj).parent().parent().css('padding', '3px 10px 5px 20px');
        $(obj).parent().parent().css('background', '#DFE8F4');
        $(obj).text(lang_less);
        $(obj).removeClass('more');
    } else {
//    	alert('hide');
        $(obj).parent().parent().find('.hidden').hide();
        $(obj).parent().parent().css('position', 'static');
        $(obj).parent().parent().parent().css('position', 'static');
        $(obj).parent().parent().css('padding', '0px 0px 0px 20px');
        $(obj).parent().parent().css('background', 'none');
        $(obj).text(lang_more);
        $(obj).addClass('more');
    }
}

function show_all_rows(obj)
{
    if($(obj).hasClass('more_rows')) {
        $('.hide_row').show();
        $(obj).text(lang_less);
        $(obj).removeClass('more_rows');
    } else {
        $('.hide_row').hide();
        $(obj).text(lang_more);
        $(obj).addClass('more_rows');
    }
}

function show_all_attrs (obj) {
	if($(obj).hasClass('more_attrs')) {
        $(obj).parent().children('.hidden_attrs').show();
        //$('.hidden_attrs').show();
        $(obj).text('['+lang_less+']');
        $(obj).removeClass('more_attrs');
    } else {
        $(obj).parent().children('.hidden_attrs').hide();
        //$('.hidden_attrs').hide();
        $(obj).text('['+lang_more+']');
        $(obj).addClass('more_attrs');
    }
}

function show_all_firms (obj) {
	if($(obj).hasClass('more_attrs')) {
        $(obj).parents('.parse_info').children().children().children('.hidden').show();
        $(obj).text('['+lang_less+']');
        $(obj).removeClass('more_attrs');
    } else {
        $(obj).parents('.parse_info').children().children().children('.hidden').hide();
        $(obj).text('['+lang_more+' ('+$(obj).attr('title')+')]');
        $(obj).addClass('more_attrs');
    }
}
