var obj = null;

function checkHover() {
	if (obj) {
		obj.find('.submenu').fadeOut('fast');	
	} 
} 

$(document).ready(function() {
	$('div.menu-item').hover(function() {
		if (obj) {
			obj.find('.submenu').fadeOut('fast');
			obj = null;
		}				
		$(this).find('.submenu').fadeIn('fast');
		//$(this).css('backgroundImage','url(/static/img/button_back_over.jpg);');
	}, function() {
		obj = $(this);
		setTimeout("checkHover()",200);
	});	

	$('div.company').hover(function(){
		this.style.backgroundColor = '#6F6F6F';
		//$(this).css('backgroundColor','#6F6F6F;');
	},function(){
		this.style.backgroundColor = '#4F4F4F';
		//$(this).css('backgroundColor','#4F4F4F;');
	});	
	
	$('div.menu-item').hover(function(){
//		$(this).css('backgroundColor','white');
//		$(this).css('color','black');
	},function(){
//	$(this).css('backgroundColor','black');
//		$(this).css('color','white');
	});	
	
	$('a.lightbox').lightBox();
	$('a.lightbox-gal').lightBox();
    $('a.product-menu-item').click(function(){
        $('.prod-left-div').hide();
        $('#product-menu a').removeClass('product-menu-item-sel');
        var elemId = this.id;
        $('div.'+elemId).show();
        $(this).addClass('product-menu-item-sel');
    });
});


function searchProducts(searchForm){
    $("#product_list").hide();
    $("#searching").show();
	$.getJSON("/oferta_szukaj/szukaj/xhr",
			  { 'name' : searchForm.name.value,
			  	'price_from' : searchForm.price_from.value,
				'price_to' : searchForm.price_to.value,
				'type' : searchForm.type.value
			  }, function(data){
                $("#searching").hide();
				if(data.length < 1){
					$("#product_list").css('display','none');
					$("#no-results").css('display','block');
				} else {
					$("#product_list").css('display','block');
					$("#no-results").css('display','none');
					$("#products-tbody").html('');
					$.each(data, function(i,item){	
                        $("#products-tbody").append(
                            '<tr><td><a href="/produkt/'+item.id+'">'+
                            item.company+'</a></td>'+ '<td>'+item.name+'</td> <td>'+
                            item.type + '</td style="text-align:right;"><td>'+
                        item.price+'</td>');
					   });	
					$("#products-table").trigger("update"); 
					var sorting = [[0,0]]; 
					$("#products-table").trigger("sorton",[sorting]); 
				}
			  }
	);
}

function menuItemOver(divButton){
	divButton.style.backgroundColor = '#abb9f0';
}
function menuItemOut(divButton){
	divButton.style.backgroundColor = '#BDDCFD';
}

function showSubmenu(){
	var submenu = document.getElementById('oferta_sub');
	submenu.style.display = 'block';
	submenu.style.left = '306px';
	submenu.style.top = '138px';
}

function showDealers(sel){
    var val = sel.value;
    $("#dealers > div").hide();
    if(val==""){
        $("#dealers > div").show();
    } else {
        $("#dealers > #"+val).show();
    } 
}

function setRegion(region_code,region_name){
	$("#dealers-div").html('<h2> Wojew&oacute;dztwo '+region_name+'</h2>');
	$.getJSON("/dealerzy/"+region_code+"/xhr",
        function(data){
          $.each(data, function(i,item){	
			d = item.fields;
			dealerHtml = '<div class="dealer"';
			
			dealerHtml += i%3==0?'style="clear:left;"':'';
			dealerHtml +='><span class="name">'+d.name+'</span>' +
									'<br/><span class="city">'+d.city+' '+'</span> <br/>'+
									' ' + d.street;
			dealerHtml += d.phone==''?'<br/>':', '+d.post_code +'<br/>';
			dealerHtml += d.phone==''?'':'tel. ' + d.phone+'<br/>';
			dealerHtml += d.email==''?'':'<a href="mailto:'+d.email+'">'+d.email+'</a><br/>';
			dealerHtml += d.link==''?'':'<a href="'+d.link+'">'+d.link+'</a>';
			
           $("#dealers-div").append(dealerHtml);
          });
        });
} 

function showFullDescription(){
	if($("#product-desc-full").css("display")=="none"){
		 $("#product-desc-full").css("display","block");
		 $("#more-link").html("mniej");
	} else {
		$("#product-desc-full").css("display","none");
		$("#more-link").html("wi&#281;cej");
	}
}

function showFullCompanyDescription(){
	if($("#company-desc-full").css("display")=="none"){
		 $("#company-desc-full").css("display","block");
		 $("#more-link").html("mniej");
	} else {
		$("#company-desc-full").css("display","none");
		$("#more-link").html("wi&#281;cej");
	}
}

function showNews(elem){
        text = $(elem).prev("#news-full")
	link = $(elem)
	if(text.css("display")=="none"){
		 text.css("display","block");
		 link.html("mniej");
	} else {
		text.css("display","none");
		link.html("wi&#281;cej");
	}
}

function highlightCompanies(companies_ids){
    for(i=0; i<companies_ids.length; i++){
        $('#company'+companies_ids[i]).addClass('left-navi-over');
    }
}

function unhighlightCompanies(companies_ids){
    for(i=0; i<companies_ids.length; i++){
        $('#company'+companies_ids[i]).removeClass('left-navi-over');
    }
}

function highlightTypes(types_ids){
    for(i=0; i<types_ids.length; i++){
        $('#subtype'+types_ids[i]).addClass('subtype-over');
    }
}

function unhighlightTypes(types_ids){
    for(i=0; i<types_ids.length; i++){
        $('#subtype'+types_ids[i]).removeClass('subtype-over');
    }
}

function unhighlightAllTypes(){
    $('.subtype').removeClass('subtype-over');
}

function unhighlightAllCompanies(){
    $('.left-navi2').removeClass('left-navi-over');
}

function highComp(id){
    $.get("ajax/get_cmp/"+id, function(data){
    	highlightCompanies(data);
    });
}



